texture_progress_bar.cpp 27 KB

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