texture_progress_bar.cpp 27 KB

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