texture_progress_bar.cpp 27 KB

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