texture_progress_bar.cpp 25 KB

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