texture_progress_bar.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /**************************************************************************/
  2. /* texture_progress_bar.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "texture_progress_bar.h"
  31. #include "core/config/engine.h"
  32. void TextureProgressBar::set_under_texture(const Ref<Texture2D> &p_texture) {
  33. _set_texture(&under, p_texture);
  34. }
  35. Ref<Texture2D> TextureProgressBar::get_under_texture() const {
  36. return under;
  37. }
  38. void TextureProgressBar::set_over_texture(const Ref<Texture2D> &p_texture) {
  39. _set_texture(&over, p_texture);
  40. }
  41. Ref<Texture2D> TextureProgressBar::get_over_texture() const {
  42. return over;
  43. }
  44. void TextureProgressBar::set_stretch_margin(Side p_side, int p_size) {
  45. ERR_FAIL_INDEX((int)p_side, 4);
  46. if (stretch_margin[p_side] == p_size) {
  47. return;
  48. }
  49. stretch_margin[p_side] = p_size;
  50. queue_redraw();
  51. update_minimum_size();
  52. }
  53. int TextureProgressBar::get_stretch_margin(Side p_side) const {
  54. ERR_FAIL_INDEX_V((int)p_side, 4, 0);
  55. return stretch_margin[p_side];
  56. }
  57. void TextureProgressBar::set_nine_patch_stretch(bool p_stretch) {
  58. if (nine_patch_stretch == p_stretch) {
  59. return;
  60. }
  61. nine_patch_stretch = p_stretch;
  62. queue_redraw();
  63. update_minimum_size();
  64. notify_property_list_changed();
  65. }
  66. bool TextureProgressBar::get_nine_patch_stretch() const {
  67. return nine_patch_stretch;
  68. }
  69. Size2 TextureProgressBar::get_minimum_size() const {
  70. if (nine_patch_stretch) {
  71. return Size2(stretch_margin[SIDE_LEFT] + stretch_margin[SIDE_RIGHT], stretch_margin[SIDE_TOP] + stretch_margin[SIDE_BOTTOM]);
  72. } else if (under.is_valid()) {
  73. return under->get_size();
  74. } else if (over.is_valid()) {
  75. return over->get_size();
  76. } else if (progress.is_valid()) {
  77. return progress->get_size();
  78. }
  79. return Size2(1, 1);
  80. }
  81. void TextureProgressBar::set_progress_texture(const Ref<Texture2D> &p_texture) {
  82. _set_texture(&progress, p_texture);
  83. }
  84. Ref<Texture2D> TextureProgressBar::get_progress_texture() const {
  85. return progress;
  86. }
  87. void TextureProgressBar::set_progress_offset(Point2 p_offset) {
  88. if (progress_offset == p_offset) {
  89. return;
  90. }
  91. progress_offset = p_offset;
  92. queue_redraw();
  93. }
  94. Point2 TextureProgressBar::get_progress_offset() const {
  95. return progress_offset;
  96. }
  97. void TextureProgressBar::set_tint_under(const Color &p_tint) {
  98. if (tint_under == p_tint) {
  99. return;
  100. }
  101. tint_under = p_tint;
  102. queue_redraw();
  103. }
  104. Color TextureProgressBar::get_tint_under() const {
  105. return tint_under;
  106. }
  107. void TextureProgressBar::set_tint_progress(const Color &p_tint) {
  108. if (tint_progress == p_tint) {
  109. return;
  110. }
  111. tint_progress = p_tint;
  112. queue_redraw();
  113. }
  114. Color TextureProgressBar::get_tint_progress() const {
  115. return tint_progress;
  116. }
  117. void TextureProgressBar::set_tint_over(const Color &p_tint) {
  118. if (tint_over == p_tint) {
  119. return;
  120. }
  121. tint_over = p_tint;
  122. queue_redraw();
  123. }
  124. Color TextureProgressBar::get_tint_over() const {
  125. return tint_over;
  126. }
  127. void TextureProgressBar::_set_texture(Ref<Texture2D> *p_destination, const Ref<Texture2D> &p_texture) {
  128. DEV_ASSERT(p_destination);
  129. Ref<Texture2D> &destination = *p_destination;
  130. if (destination == p_texture) {
  131. return;
  132. }
  133. if (destination.is_valid()) {
  134. destination->disconnect_changed(callable_mp(this, &TextureProgressBar::_texture_changed));
  135. }
  136. destination = p_texture;
  137. if (destination.is_valid()) {
  138. // Pass `CONNECT_REFERENCE_COUNTED` to avoid early disconnect in case the same texture is assigned to different "slots".
  139. destination->connect_changed(callable_mp(this, &TextureProgressBar::_texture_changed), CONNECT_REFERENCE_COUNTED);
  140. }
  141. _texture_changed();
  142. }
  143. void TextureProgressBar::_texture_changed() {
  144. update_minimum_size();
  145. queue_redraw();
  146. }
  147. Point2 TextureProgressBar::unit_val_to_uv(float val) {
  148. if (progress.is_null()) {
  149. return Point2();
  150. }
  151. if (val < 0) {
  152. val += 1;
  153. }
  154. if (val > 1) {
  155. val -= 1;
  156. }
  157. Point2 p = get_relative_center();
  158. // Minimal version of Liang-Barsky clipping algorithm
  159. float angle = (val * Math_TAU) - Math_PI * 0.5;
  160. Point2 dir = Vector2(Math::cos(angle), Math::sin(angle));
  161. float t1 = 1.0;
  162. float cp = 0.0;
  163. float cq = 0.0;
  164. float cr = 0.0;
  165. float edgeLeft = 0.0;
  166. float edgeRight = 1.0;
  167. float edgeBottom = 0.0;
  168. float edgeTop = 1.0;
  169. for (int edge = 0; edge < 4; edge++) {
  170. if (edge == 0) {
  171. if (dir.x > 0) {
  172. continue;
  173. }
  174. cq = -(edgeLeft - p.x);
  175. dir.x *= 2.0 * cq;
  176. cp = -dir.x;
  177. } else if (edge == 1) {
  178. if (dir.x < 0) {
  179. continue;
  180. }
  181. cq = (edgeRight - p.x);
  182. dir.x *= 2.0 * cq;
  183. cp = dir.x;
  184. } else if (edge == 2) {
  185. if (dir.y > 0) {
  186. continue;
  187. }
  188. cq = -(edgeBottom - p.y);
  189. dir.y *= 2.0 * cq;
  190. cp = -dir.y;
  191. } else if (edge == 3) {
  192. if (dir.y < 0) {
  193. continue;
  194. }
  195. cq = (edgeTop - p.y);
  196. dir.y *= 2.0 * cq;
  197. cp = dir.y;
  198. }
  199. cr = cq / cp;
  200. if (cr >= 0 && cr < t1) {
  201. t1 = cr;
  202. }
  203. }
  204. return (p + t1 * dir);
  205. }
  206. Point2 TextureProgressBar::get_relative_center() {
  207. if (progress.is_null()) {
  208. return Point2();
  209. }
  210. Point2 p = progress->get_size() / 2;
  211. p += rad_center_off;
  212. p.x /= progress->get_width();
  213. p.y /= progress->get_height();
  214. p = p.clampf(0, 1);
  215. return p;
  216. }
  217. void TextureProgressBar::draw_nine_patch_stretched(const Ref<Texture2D> &p_texture, FillMode p_mode, double p_ratio, const Color &p_modulate) {
  218. Vector2 texture_size = p_texture->get_size();
  219. Vector2 topleft = Vector2(stretch_margin[SIDE_LEFT], stretch_margin[SIDE_TOP]);
  220. Vector2 bottomright = Vector2(stretch_margin[SIDE_RIGHT], stretch_margin[SIDE_BOTTOM]);
  221. Rect2 src_rect = Rect2(Point2(), texture_size);
  222. Rect2 dst_rect = Rect2(Point2(), get_size());
  223. if (p_ratio < 1.0) {
  224. // Drawing a partially-filled 9-patch is a little tricky -
  225. // texture is divided by 3 sections toward fill direction,
  226. // then middle section is stretching while the other two aren't.
  227. double width_total = 0.0;
  228. double width_texture = 0.0;
  229. double first_section_size = 0.0;
  230. double last_section_size = 0.0;
  231. switch (p_mode) {
  232. case FILL_LEFT_TO_RIGHT: {
  233. width_total = dst_rect.size.x;
  234. width_texture = texture_size.x;
  235. first_section_size = topleft.x;
  236. last_section_size = bottomright.x;
  237. } break;
  238. case FILL_RIGHT_TO_LEFT: {
  239. width_total = dst_rect.size.x;
  240. width_texture = texture_size.x;
  241. // In contrast to `FILL_LEFT_TO_RIGHT`, `first_section_size` and `last_section_size` should switch value.
  242. first_section_size = bottomright.x;
  243. last_section_size = topleft.x;
  244. } break;
  245. case FILL_TOP_TO_BOTTOM: {
  246. width_total = dst_rect.size.y;
  247. width_texture = texture_size.y;
  248. first_section_size = topleft.y;
  249. last_section_size = bottomright.y;
  250. } break;
  251. case FILL_BOTTOM_TO_TOP: {
  252. width_total = dst_rect.size.y;
  253. width_texture = texture_size.y;
  254. // Similar to `FILL_RIGHT_TO_LEFT`.
  255. first_section_size = bottomright.y;
  256. last_section_size = topleft.y;
  257. } break;
  258. case FILL_BILINEAR_LEFT_AND_RIGHT: {
  259. width_total = dst_rect.size.x;
  260. width_texture = texture_size.x;
  261. first_section_size = topleft.x;
  262. last_section_size = bottomright.x;
  263. } break;
  264. case FILL_BILINEAR_TOP_AND_BOTTOM: {
  265. width_total = dst_rect.size.y;
  266. width_texture = texture_size.y;
  267. first_section_size = topleft.y;
  268. last_section_size = bottomright.y;
  269. } break;
  270. case FILL_CLOCKWISE:
  271. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE:
  272. case FILL_COUNTER_CLOCKWISE: {
  273. // Those modes are circular, not relevant for nine patch.
  274. } break;
  275. case FILL_MODE_MAX:
  276. break;
  277. }
  278. double width_filled = width_total * p_ratio;
  279. double middle_section_size = MAX(0.0, width_texture - first_section_size - last_section_size);
  280. // Maximum middle texture size.
  281. double max_middle_texture_size = middle_section_size;
  282. // Maximum real middle texture size.
  283. double max_middle_real_size = MAX(0.0, width_total - (first_section_size + last_section_size));
  284. switch (p_mode) {
  285. case FILL_BILINEAR_LEFT_AND_RIGHT:
  286. case FILL_BILINEAR_TOP_AND_BOTTOM: {
  287. last_section_size = MAX(0.0, last_section_size - (width_total - width_filled) * 0.5);
  288. first_section_size = MAX(0.0, first_section_size - (width_total - width_filled) * 0.5);
  289. // When `width_filled` increases, `middle_section_size` only increases when either of `first_section_size` and `last_section_size` is zero.
  290. // Also, it should always be smaller than or equal to `(width_total - (first_section_size + last_section_size))`.
  291. double real_middle_size = width_filled - first_section_size - last_section_size;
  292. middle_section_size *= MIN(max_middle_real_size, real_middle_size) / max_middle_real_size;
  293. width_texture = MIN(width_texture, first_section_size + middle_section_size + last_section_size);
  294. } break;
  295. case FILL_MODE_MAX:
  296. break;
  297. default: {
  298. middle_section_size *= MIN(1.0, (MAX(0.0, width_filled - first_section_size) / MAX(1.0, width_total - first_section_size - last_section_size)));
  299. last_section_size = MAX(0.0, last_section_size - (width_total - width_filled));
  300. first_section_size = MIN(first_section_size, width_filled);
  301. width_texture = MIN(width_texture, first_section_size + middle_section_size + last_section_size);
  302. }
  303. }
  304. switch (p_mode) {
  305. case FILL_LEFT_TO_RIGHT: {
  306. src_rect.size.x = width_texture;
  307. dst_rect.size.x = width_filled;
  308. topleft.x = first_section_size;
  309. bottomright.x = last_section_size;
  310. } break;
  311. case FILL_RIGHT_TO_LEFT: {
  312. src_rect.position.x += src_rect.size.x - width_texture;
  313. src_rect.size.x = width_texture;
  314. dst_rect.position.x += width_total - width_filled;
  315. dst_rect.size.x = width_filled;
  316. topleft.x = last_section_size;
  317. bottomright.x = first_section_size;
  318. } break;
  319. case FILL_TOP_TO_BOTTOM: {
  320. src_rect.size.y = width_texture;
  321. dst_rect.size.y = width_filled;
  322. bottomright.y = last_section_size;
  323. topleft.y = first_section_size;
  324. } break;
  325. case FILL_BOTTOM_TO_TOP: {
  326. src_rect.position.y += src_rect.size.y - width_texture;
  327. src_rect.size.y = width_texture;
  328. dst_rect.position.y += width_total - width_filled;
  329. dst_rect.size.y = width_filled;
  330. topleft.y = last_section_size;
  331. bottomright.y = first_section_size;
  332. } break;
  333. case FILL_BILINEAR_LEFT_AND_RIGHT: {
  334. double center_mapped_from_real_width = (width_total * 0.5 - topleft.x) / max_middle_real_size * max_middle_texture_size + topleft.x;
  335. double drift_from_unscaled_center = 0;
  336. if (bottomright.y != topleft.y) { // To avoid division by zero.
  337. drift_from_unscaled_center = (src_rect.size.x * 0.5 - center_mapped_from_real_width) * (last_section_size - first_section_size) / (bottomright.x - topleft.x);
  338. }
  339. src_rect.position.x += center_mapped_from_real_width + drift_from_unscaled_center - width_texture * 0.5;
  340. src_rect.size.x = width_texture;
  341. dst_rect.position.x += (width_total - width_filled) * 0.5;
  342. dst_rect.size.x = width_filled;
  343. topleft.x = first_section_size;
  344. bottomright.x = last_section_size;
  345. } break;
  346. case FILL_BILINEAR_TOP_AND_BOTTOM: {
  347. double center_mapped_from_real_width = (width_total * 0.5 - topleft.y) / max_middle_real_size * max_middle_texture_size + topleft.y;
  348. double drift_from_unscaled_center = 0;
  349. if (bottomright.y != topleft.y) { // To avoid division by zero.
  350. drift_from_unscaled_center = (src_rect.size.y * 0.5 - center_mapped_from_real_width) * (last_section_size - first_section_size) / (bottomright.y - topleft.y);
  351. }
  352. src_rect.position.y += center_mapped_from_real_width + drift_from_unscaled_center - width_texture * 0.5;
  353. src_rect.size.y = width_texture;
  354. dst_rect.position.y += (width_total - width_filled) * 0.5;
  355. dst_rect.size.y = width_filled;
  356. topleft.y = first_section_size;
  357. bottomright.y = last_section_size;
  358. } break;
  359. case FILL_CLOCKWISE:
  360. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE:
  361. case FILL_COUNTER_CLOCKWISE: {
  362. // Those modes are circular, not relevant for nine patch.
  363. } break;
  364. case FILL_MODE_MAX:
  365. break;
  366. }
  367. }
  368. if (p_texture == progress) {
  369. dst_rect.position += progress_offset;
  370. }
  371. p_texture->get_rect_region(dst_rect, src_rect, dst_rect, src_rect);
  372. RID ci = get_canvas_item();
  373. RS::get_singleton()->canvas_item_add_nine_patch(ci, dst_rect, src_rect, p_texture->get_rid(), topleft, bottomright, RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, true, p_modulate);
  374. }
  375. void TextureProgressBar::_notification(int p_what) {
  376. switch (p_what) {
  377. case NOTIFICATION_DRAW: {
  378. if (nine_patch_stretch && (mode == FILL_LEFT_TO_RIGHT || mode == FILL_RIGHT_TO_LEFT || mode == FILL_TOP_TO_BOTTOM || mode == FILL_BOTTOM_TO_TOP || mode == FILL_BILINEAR_LEFT_AND_RIGHT || mode == FILL_BILINEAR_TOP_AND_BOTTOM)) {
  379. if (under.is_valid()) {
  380. draw_nine_patch_stretched(under, mode, 1.0, tint_under);
  381. }
  382. if (progress.is_valid()) {
  383. draw_nine_patch_stretched(progress, mode, get_as_ratio(), tint_progress);
  384. }
  385. if (over.is_valid()) {
  386. draw_nine_patch_stretched(over, mode, 1.0, tint_over);
  387. }
  388. } else {
  389. if (under.is_valid()) {
  390. switch (mode) {
  391. case FILL_CLOCKWISE:
  392. case FILL_COUNTER_CLOCKWISE:
  393. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: {
  394. if (nine_patch_stretch) {
  395. Rect2 region = Rect2(Point2(), get_size());
  396. draw_texture_rect(under, region, false, tint_under);
  397. } else {
  398. draw_texture(under, Point2(), tint_under);
  399. }
  400. } break;
  401. case FILL_MODE_MAX:
  402. break;
  403. default:
  404. draw_texture(under, Point2(), tint_under);
  405. }
  406. }
  407. if (progress.is_valid()) {
  408. Size2 s = progress->get_size();
  409. switch (mode) {
  410. case FILL_LEFT_TO_RIGHT: {
  411. Rect2 region = Rect2(progress_offset, Size2(s.x * get_as_ratio(), s.y));
  412. Rect2 source = Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y));
  413. draw_texture_rect_region(progress, region, source, tint_progress);
  414. } break;
  415. case FILL_RIGHT_TO_LEFT: {
  416. Rect2 region = Rect2(progress_offset + Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y));
  417. Rect2 source = Rect2(Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y));
  418. draw_texture_rect_region(progress, region, source, tint_progress);
  419. } break;
  420. case FILL_TOP_TO_BOTTOM: {
  421. Rect2 region = Rect2(progress_offset + Point2(), Size2(s.x, s.y * get_as_ratio()));
  422. Rect2 source = Rect2(Point2(), Size2(s.x, s.y * get_as_ratio()));
  423. draw_texture_rect_region(progress, region, source, tint_progress);
  424. } break;
  425. case FILL_BOTTOM_TO_TOP: {
  426. Rect2 region = Rect2(progress_offset + Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio()));
  427. Rect2 source = Rect2(Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio()));
  428. draw_texture_rect_region(progress, region, source, tint_progress);
  429. } break;
  430. case FILL_CLOCKWISE:
  431. case FILL_COUNTER_CLOCKWISE:
  432. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: {
  433. if (nine_patch_stretch) {
  434. s = get_size();
  435. }
  436. float val = get_as_ratio() * rad_max_degrees / 360;
  437. if (val == 1) {
  438. Rect2 region = Rect2(progress_offset, s);
  439. Rect2 source = Rect2(Point2(), progress->get_size());
  440. draw_texture_rect_region(progress, region, source, tint_progress);
  441. } else if (val != 0) {
  442. LocalVector<float> pts;
  443. float direction = mode == FILL_COUNTER_CLOCKWISE ? -1 : 1;
  444. float start;
  445. if (mode == FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE) {
  446. start = rad_init_angle / 360 - val / 2;
  447. } else {
  448. start = rad_init_angle / 360;
  449. }
  450. float end = start + direction * val;
  451. float from = MIN(start, end);
  452. float to = MAX(start, end);
  453. pts.push_back(from);
  454. for (float corner = Math::floor(from * 4 + 0.5) * 0.25 + 0.125; corner < to; corner += 0.25) {
  455. pts.push_back(corner);
  456. }
  457. pts.push_back(to);
  458. Vector<Point2> uvs;
  459. Vector<Point2> points;
  460. for (const float &f : pts) {
  461. Point2 uv = unit_val_to_uv(f);
  462. if (uvs.has(uv)) {
  463. continue;
  464. }
  465. points.push_back(progress_offset + Point2(uv.x * s.x, uv.y * s.y));
  466. uvs.push_back(uv);
  467. }
  468. // Filter out an edge case where almost equal `from`, `to` were mapped to the same UV.
  469. if (points.size() >= 2) {
  470. Point2 center_point = get_relative_center();
  471. points.push_back(progress_offset + s * center_point);
  472. uvs.push_back(center_point);
  473. Vector<Color> colors;
  474. colors.push_back(tint_progress);
  475. draw_polygon(points, colors, uvs, progress);
  476. }
  477. }
  478. // Draw a reference cross.
  479. if (is_part_of_edited_scene()) {
  480. Point2 p;
  481. if (nine_patch_stretch) {
  482. p = get_size();
  483. } else {
  484. p = progress->get_size();
  485. }
  486. p *= get_relative_center();
  487. p += progress_offset;
  488. p = p.floor();
  489. draw_line(p - Point2(8, 0), p + Point2(8, 0), Color(0.9, 0.5, 0.5), 2);
  490. draw_line(p - Point2(0, 8), p + Point2(0, 8), Color(0.9, 0.5, 0.5), 2);
  491. }
  492. } break;
  493. case FILL_BILINEAR_LEFT_AND_RIGHT: {
  494. Rect2 region = Rect2(progress_offset + Point2(s.x / 2 - s.x * get_as_ratio() / 2, 0), Size2(s.x * get_as_ratio(), s.y));
  495. Rect2 source = Rect2(Point2(s.x / 2 - s.x * get_as_ratio() / 2, 0), Size2(s.x * get_as_ratio(), s.y));
  496. draw_texture_rect_region(progress, region, source, tint_progress);
  497. } break;
  498. case FILL_BILINEAR_TOP_AND_BOTTOM: {
  499. Rect2 region = Rect2(progress_offset + Point2(0, s.y / 2 - s.y * get_as_ratio() / 2), Size2(s.x, s.y * get_as_ratio()));
  500. Rect2 source = Rect2(Point2(0, s.y / 2 - s.y * get_as_ratio() / 2), Size2(s.x, s.y * get_as_ratio()));
  501. draw_texture_rect_region(progress, region, source, tint_progress);
  502. } break;
  503. case FILL_MODE_MAX:
  504. break;
  505. default:
  506. draw_texture_rect_region(progress, Rect2(progress_offset, Size2(s.x * get_as_ratio(), s.y)), Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y)), tint_progress);
  507. }
  508. }
  509. if (over.is_valid()) {
  510. switch (mode) {
  511. case FILL_CLOCKWISE:
  512. case FILL_COUNTER_CLOCKWISE:
  513. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: {
  514. if (nine_patch_stretch) {
  515. Rect2 region = Rect2(Point2(), get_size());
  516. draw_texture_rect(over, region, false, tint_over);
  517. } else {
  518. draw_texture(over, Point2(), tint_over);
  519. }
  520. } break;
  521. case FILL_MODE_MAX:
  522. break;
  523. default:
  524. draw_texture(over, Point2(), tint_over);
  525. }
  526. }
  527. }
  528. } break;
  529. }
  530. }
  531. void TextureProgressBar::set_fill_mode(int p_fill) {
  532. ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
  533. if (mode == (FillMode)p_fill) {
  534. return;
  535. }
  536. mode = (FillMode)p_fill;
  537. queue_redraw();
  538. notify_property_list_changed();
  539. }
  540. int TextureProgressBar::get_fill_mode() {
  541. return mode;
  542. }
  543. void TextureProgressBar::set_radial_initial_angle(float p_angle) {
  544. ERR_FAIL_COND_MSG(!Math::is_finite(p_angle), "Angle is non-finite.");
  545. if (p_angle < 0.0 || p_angle > 360.0) {
  546. p_angle = Math::fposmodp(p_angle, 360.0f);
  547. }
  548. if (rad_init_angle == p_angle) {
  549. return;
  550. }
  551. rad_init_angle = p_angle;
  552. queue_redraw();
  553. }
  554. float TextureProgressBar::get_radial_initial_angle() {
  555. return rad_init_angle;
  556. }
  557. void TextureProgressBar::set_fill_degrees(float p_angle) {
  558. float angle_clamped = CLAMP(p_angle, 0, 360);
  559. if (rad_max_degrees == angle_clamped) {
  560. return;
  561. }
  562. rad_max_degrees = angle_clamped;
  563. queue_redraw();
  564. }
  565. float TextureProgressBar::get_fill_degrees() {
  566. return rad_max_degrees;
  567. }
  568. void TextureProgressBar::set_radial_center_offset(const Point2 &p_off) {
  569. if (rad_center_off == p_off) {
  570. return;
  571. }
  572. rad_center_off = p_off;
  573. queue_redraw();
  574. }
  575. Point2 TextureProgressBar::get_radial_center_offset() {
  576. return rad_center_off;
  577. }
  578. void TextureProgressBar::_validate_property(PropertyInfo &p_property) const {
  579. if (p_property.name.begins_with("stretch_margin_") && !nine_patch_stretch) {
  580. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  581. }
  582. if (p_property.name.begins_with("radial_") && (mode != FillMode::FILL_CLOCKWISE && mode != FillMode::FILL_COUNTER_CLOCKWISE && mode != FillMode::FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE)) {
  583. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  584. }
  585. }
  586. void TextureProgressBar::_bind_methods() {
  587. ClassDB::bind_method(D_METHOD("set_under_texture", "tex"), &TextureProgressBar::set_under_texture);
  588. ClassDB::bind_method(D_METHOD("get_under_texture"), &TextureProgressBar::get_under_texture);
  589. ClassDB::bind_method(D_METHOD("set_progress_texture", "tex"), &TextureProgressBar::set_progress_texture);
  590. ClassDB::bind_method(D_METHOD("get_progress_texture"), &TextureProgressBar::get_progress_texture);
  591. ClassDB::bind_method(D_METHOD("set_over_texture", "tex"), &TextureProgressBar::set_over_texture);
  592. ClassDB::bind_method(D_METHOD("get_over_texture"), &TextureProgressBar::get_over_texture);
  593. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &TextureProgressBar::set_fill_mode);
  594. ClassDB::bind_method(D_METHOD("get_fill_mode"), &TextureProgressBar::get_fill_mode);
  595. ClassDB::bind_method(D_METHOD("set_tint_under", "tint"), &TextureProgressBar::set_tint_under);
  596. ClassDB::bind_method(D_METHOD("get_tint_under"), &TextureProgressBar::get_tint_under);
  597. ClassDB::bind_method(D_METHOD("set_tint_progress", "tint"), &TextureProgressBar::set_tint_progress);
  598. ClassDB::bind_method(D_METHOD("get_tint_progress"), &TextureProgressBar::get_tint_progress);
  599. ClassDB::bind_method(D_METHOD("set_tint_over", "tint"), &TextureProgressBar::set_tint_over);
  600. ClassDB::bind_method(D_METHOD("get_tint_over"), &TextureProgressBar::get_tint_over);
  601. ClassDB::bind_method(D_METHOD("set_texture_progress_offset", "offset"), &TextureProgressBar::set_progress_offset);
  602. ClassDB::bind_method(D_METHOD("get_texture_progress_offset"), &TextureProgressBar::get_progress_offset);
  603. ClassDB::bind_method(D_METHOD("set_radial_initial_angle", "mode"), &TextureProgressBar::set_radial_initial_angle);
  604. ClassDB::bind_method(D_METHOD("get_radial_initial_angle"), &TextureProgressBar::get_radial_initial_angle);
  605. ClassDB::bind_method(D_METHOD("set_radial_center_offset", "mode"), &TextureProgressBar::set_radial_center_offset);
  606. ClassDB::bind_method(D_METHOD("get_radial_center_offset"), &TextureProgressBar::get_radial_center_offset);
  607. ClassDB::bind_method(D_METHOD("set_fill_degrees", "mode"), &TextureProgressBar::set_fill_degrees);
  608. ClassDB::bind_method(D_METHOD("get_fill_degrees"), &TextureProgressBar::get_fill_degrees);
  609. ClassDB::bind_method(D_METHOD("set_stretch_margin", "margin", "value"), &TextureProgressBar::set_stretch_margin);
  610. ClassDB::bind_method(D_METHOD("get_stretch_margin", "margin"), &TextureProgressBar::get_stretch_margin);
  611. ClassDB::bind_method(D_METHOD("set_nine_patch_stretch", "stretch"), &TextureProgressBar::set_nine_patch_stretch);
  612. ClassDB::bind_method(D_METHOD("get_nine_patch_stretch"), &TextureProgressBar::get_nine_patch_stretch);
  613. ADD_PROPERTY(PropertyInfo(Variant::INT, "fill_mode", PROPERTY_HINT_ENUM, "Left to Right,Right to Left,Top to Bottom,Bottom to Top,Clockwise,Counter Clockwise,Bilinear (Left and Right),Bilinear (Top and Bottom),Clockwise and Counter Clockwise"), "set_fill_mode", "get_fill_mode");
  614. ADD_GROUP("Radial Fill", "radial_");
  615. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radial_initial_angle", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,degrees"), "set_radial_initial_angle", "get_radial_initial_angle");
  616. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radial_fill_degrees", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,degrees"), "set_fill_degrees", "get_fill_degrees");
  617. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "radial_center_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_radial_center_offset", "get_radial_center_offset");
  618. ADD_GROUP("", "");
  619. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "nine_patch_stretch"), "set_nine_patch_stretch", "get_nine_patch_stretch");
  620. ADD_GROUP("Stretch Margin", "stretch_margin_");
  621. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_LEFT);
  622. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_TOP);
  623. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_RIGHT);
  624. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_stretch_margin", "get_stretch_margin", SIDE_BOTTOM);
  625. ADD_GROUP("Textures", "texture_");
  626. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_under", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_under_texture", "get_under_texture");
  627. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_over", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_over_texture", "get_over_texture");
  628. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_progress", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_progress_texture", "get_progress_texture");
  629. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_progress_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_progress_offset", "get_texture_progress_offset");
  630. ADD_GROUP("Tint", "tint_");
  631. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_under"), "set_tint_under", "get_tint_under");
  632. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_over"), "set_tint_over", "get_tint_over");
  633. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_progress"), "set_tint_progress", "get_tint_progress");
  634. BIND_ENUM_CONSTANT(FILL_LEFT_TO_RIGHT);
  635. BIND_ENUM_CONSTANT(FILL_RIGHT_TO_LEFT);
  636. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  637. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  638. BIND_ENUM_CONSTANT(FILL_CLOCKWISE);
  639. BIND_ENUM_CONSTANT(FILL_COUNTER_CLOCKWISE);
  640. BIND_ENUM_CONSTANT(FILL_BILINEAR_LEFT_AND_RIGHT);
  641. BIND_ENUM_CONSTANT(FILL_BILINEAR_TOP_AND_BOTTOM);
  642. BIND_ENUM_CONSTANT(FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE);
  643. }
  644. TextureProgressBar::TextureProgressBar() {
  645. set_mouse_filter(MOUSE_FILTER_PASS);
  646. }