texture_progress.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /**************************************************************************/
  2. /* texture_progress.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.h"
  31. #include "core/engine.h"
  32. void TextureProgress::set_under_texture(const Ref<Texture> &p_texture) {
  33. under = p_texture;
  34. update();
  35. minimum_size_changed();
  36. }
  37. Ref<Texture> TextureProgress::get_under_texture() const {
  38. return under;
  39. }
  40. void TextureProgress::set_over_texture(const Ref<Texture> &p_texture) {
  41. over = p_texture;
  42. update();
  43. if (under.is_null()) {
  44. minimum_size_changed();
  45. }
  46. }
  47. Ref<Texture> TextureProgress::get_over_texture() const {
  48. return over;
  49. }
  50. void TextureProgress::set_stretch_margin(Margin p_margin, int p_size) {
  51. ERR_FAIL_INDEX((int)p_margin, 4);
  52. stretch_margin[p_margin] = p_size;
  53. update();
  54. minimum_size_changed();
  55. }
  56. int TextureProgress::get_stretch_margin(Margin p_margin) const {
  57. ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
  58. return stretch_margin[p_margin];
  59. }
  60. void TextureProgress::set_nine_patch_stretch(bool p_stretch) {
  61. nine_patch_stretch = p_stretch;
  62. update();
  63. minimum_size_changed();
  64. }
  65. bool TextureProgress::get_nine_patch_stretch() const {
  66. return nine_patch_stretch;
  67. }
  68. Size2 TextureProgress::get_minimum_size() const {
  69. if (nine_patch_stretch) {
  70. return Size2(stretch_margin[MARGIN_LEFT] + stretch_margin[MARGIN_RIGHT], stretch_margin[MARGIN_TOP] + stretch_margin[MARGIN_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 TextureProgress::set_progress_texture(const Ref<Texture> &p_texture) {
  81. progress = p_texture;
  82. update();
  83. minimum_size_changed();
  84. }
  85. Ref<Texture> TextureProgress::get_progress_texture() const {
  86. return progress;
  87. }
  88. void TextureProgress::set_progress_offset(Point2 p_offset) {
  89. progress_offset = p_offset;
  90. update();
  91. }
  92. Point2 TextureProgress::get_progress_offset() const {
  93. return progress_offset;
  94. }
  95. void TextureProgress::set_tint_under(const Color &p_tint) {
  96. tint_under = p_tint;
  97. update();
  98. }
  99. Color TextureProgress::get_tint_under() const {
  100. return tint_under;
  101. }
  102. void TextureProgress::set_tint_progress(const Color &p_tint) {
  103. tint_progress = p_tint;
  104. update();
  105. }
  106. Color TextureProgress::get_tint_progress() const {
  107. return tint_progress;
  108. }
  109. void TextureProgress::set_tint_over(const Color &p_tint) {
  110. tint_over = p_tint;
  111. update();
  112. }
  113. Color TextureProgress::get_tint_over() const {
  114. return tint_over;
  115. }
  116. Point2 TextureProgress::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;
  132. float cq = 0;
  133. float cr = 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 TextureProgress::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 TextureProgress::draw_nine_patch_stretched(const Ref<Texture> &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[MARGIN_LEFT], stretch_margin[MARGIN_TOP]);
  190. Vector2 bottomright = Vector2(stretch_margin[MARGIN_RIGHT], stretch_margin[MARGIN_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. VS::get_singleton()->canvas_item_add_nine_patch(ci, dst_rect, src_rect, p_texture->get_rid(), topleft, bottomright, VS::NINE_PATCH_STRETCH, VS::NINE_PATCH_STRETCH, true, p_modulate);
  344. }
  345. void TextureProgress::_notification(int p_what) {
  346. switch (p_what) {
  347. case NOTIFICATION_DRAW: {
  348. 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)) {
  349. if (under.is_valid()) {
  350. draw_nine_patch_stretched(under, mode, 1.0, tint_under);
  351. }
  352. if (progress.is_valid()) {
  353. draw_nine_patch_stretched(progress, mode, get_as_ratio(), tint_progress);
  354. }
  355. if (over.is_valid()) {
  356. draw_nine_patch_stretched(over, mode, 1.0, tint_over);
  357. }
  358. } else {
  359. if (under.is_valid()) {
  360. switch (mode) {
  361. case FILL_CLOCKWISE:
  362. case FILL_COUNTER_CLOCKWISE:
  363. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: {
  364. if (nine_patch_stretch) {
  365. Rect2 region = Rect2(Point2(), get_size());
  366. draw_texture_rect(under, region, false, tint_under);
  367. } else {
  368. draw_texture(under, Point2(), tint_under);
  369. }
  370. } break;
  371. case FILL_MODE_MAX:
  372. break;
  373. default:
  374. draw_texture(under, Point2(), tint_under);
  375. }
  376. }
  377. if (progress.is_valid()) {
  378. Size2 s = progress->get_size();
  379. switch (mode) {
  380. case FILL_LEFT_TO_RIGHT: {
  381. Rect2 region = Rect2(progress_offset, Size2(s.x * get_as_ratio(), s.y));
  382. Rect2 source = Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y));
  383. draw_texture_rect_region(progress, region, source, tint_progress);
  384. } break;
  385. case FILL_RIGHT_TO_LEFT: {
  386. Rect2 region = Rect2(progress_offset + Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y));
  387. Rect2 source = Rect2(Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y));
  388. draw_texture_rect_region(progress, region, source, tint_progress);
  389. } break;
  390. case FILL_TOP_TO_BOTTOM: {
  391. Rect2 region = Rect2(progress_offset + Point2(), Size2(s.x, s.y * get_as_ratio()));
  392. Rect2 source = Rect2(Point2(), Size2(s.x, s.y * get_as_ratio()));
  393. draw_texture_rect_region(progress, region, source, tint_progress);
  394. } break;
  395. case FILL_BOTTOM_TO_TOP: {
  396. Rect2 region = Rect2(progress_offset + Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio()));
  397. Rect2 source = Rect2(Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio()));
  398. draw_texture_rect_region(progress, region, source, tint_progress);
  399. } break;
  400. case FILL_CLOCKWISE:
  401. case FILL_COUNTER_CLOCKWISE:
  402. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: {
  403. if (nine_patch_stretch) {
  404. s = get_size();
  405. }
  406. float val = get_as_ratio() * rad_max_degrees / 360;
  407. if (val == 1) {
  408. Rect2 region = Rect2(progress_offset, s);
  409. Rect2 source = Rect2(Point2(), progress->get_size());
  410. draw_texture_rect_region(progress, region, source, tint_progress);
  411. } else if (val != 0) {
  412. Array pts;
  413. float direction = mode == FILL_COUNTER_CLOCKWISE ? -1 : 1;
  414. float start;
  415. if (mode == FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE) {
  416. start = rad_init_angle / 360 - val / 2;
  417. } else {
  418. start = rad_init_angle / 360;
  419. }
  420. float end = start + direction * val;
  421. float from = MIN(start, end);
  422. float to = MAX(start, end);
  423. pts.append(from);
  424. for (float corner = Math::floor(from * 4 + 0.5) * 0.25 + 0.125; corner < to; corner += 0.25) {
  425. pts.append(corner);
  426. }
  427. pts.append(to);
  428. Ref<AtlasTexture> atlas_progress = progress;
  429. bool valid_atlas_progress = atlas_progress.is_valid() && atlas_progress->get_atlas().is_valid();
  430. Rect2 region_rect;
  431. Size2 atlas_size;
  432. if (valid_atlas_progress) {
  433. region_rect = atlas_progress->get_region();
  434. atlas_size = atlas_progress->get_atlas()->get_size();
  435. }
  436. Vector<Point2> uvs;
  437. Vector<Point2> points;
  438. for (int i = 0; i < pts.size(); i++) {
  439. Point2 uv = unit_val_to_uv(pts[i]);
  440. if (uvs.find(uv) >= 0) {
  441. continue;
  442. }
  443. points.push_back(progress_offset + Point2(uv.x * s.x, uv.y * s.y));
  444. if (valid_atlas_progress) {
  445. uv.x = Math::range_lerp(uv.x, 0, 1, region_rect.position.x / atlas_size.x, (region_rect.position.x + region_rect.size.x) / atlas_size.x);
  446. uv.y = Math::range_lerp(uv.y, 0, 1, region_rect.position.y / atlas_size.y, (region_rect.position.y + region_rect.size.y) / atlas_size.y);
  447. }
  448. uvs.push_back(uv);
  449. }
  450. Point2 center_point = get_relative_center();
  451. points.push_back(progress_offset + s * center_point);
  452. if (valid_atlas_progress) {
  453. center_point.x = Math::range_lerp(center_point.x, 0, 1, region_rect.position.x / atlas_size.x, (region_rect.position.x + region_rect.size.x) / atlas_size.x);
  454. center_point.y = Math::range_lerp(center_point.y, 0, 1, region_rect.position.y / atlas_size.y, (region_rect.position.y + region_rect.size.y) / atlas_size.y);
  455. }
  456. uvs.push_back(center_point);
  457. Vector<Color> colors;
  458. colors.push_back(tint_progress);
  459. draw_polygon(points, colors, uvs, progress);
  460. }
  461. // Draw a reference cross.
  462. if (Engine::get_singleton()->is_editor_hint()) {
  463. Point2 p;
  464. if (nine_patch_stretch) {
  465. p = get_size();
  466. } else {
  467. p = progress->get_size();
  468. }
  469. p *= get_relative_center();
  470. p += progress_offset;
  471. p = p.floor();
  472. draw_line(p - Point2(8, 0), p + Point2(8, 0), Color(0.9, 0.5, 0.5), 2);
  473. draw_line(p - Point2(0, 8), p + Point2(0, 8), Color(0.9, 0.5, 0.5), 2);
  474. }
  475. } break;
  476. case FILL_BILINEAR_LEFT_AND_RIGHT: {
  477. Rect2 region = Rect2(progress_offset + Point2(s.x / 2 - s.x * get_as_ratio() / 2, 0), Size2(s.x * get_as_ratio(), s.y));
  478. Rect2 source = Rect2(Point2(s.x / 2 - s.x * get_as_ratio() / 2, 0), Size2(s.x * get_as_ratio(), s.y));
  479. draw_texture_rect_region(progress, region, source, tint_progress);
  480. } break;
  481. case FILL_BILINEAR_TOP_AND_BOTTOM: {
  482. Rect2 region = Rect2(progress_offset + Point2(0, s.y / 2 - s.y * get_as_ratio() / 2), Size2(s.x, s.y * get_as_ratio()));
  483. Rect2 source = Rect2(Point2(0, s.y / 2 - s.y * get_as_ratio() / 2), Size2(s.x, s.y * get_as_ratio()));
  484. draw_texture_rect_region(progress, region, source, tint_progress);
  485. } break;
  486. case FILL_MODE_MAX:
  487. break;
  488. default:
  489. 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);
  490. }
  491. }
  492. if (over.is_valid()) {
  493. switch (mode) {
  494. case FILL_CLOCKWISE:
  495. case FILL_COUNTER_CLOCKWISE:
  496. case FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE: {
  497. if (nine_patch_stretch) {
  498. Rect2 region = Rect2(Point2(), get_size());
  499. draw_texture_rect(over, region, false, tint_over);
  500. } else {
  501. draw_texture(over, Point2(), tint_over);
  502. }
  503. } break;
  504. case FILL_MODE_MAX:
  505. break;
  506. default:
  507. draw_texture(over, Point2(), tint_over);
  508. }
  509. }
  510. }
  511. } break;
  512. }
  513. }
  514. void TextureProgress::set_fill_mode(int p_fill) {
  515. ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
  516. mode = (FillMode)p_fill;
  517. update();
  518. }
  519. int TextureProgress::get_fill_mode() {
  520. return mode;
  521. }
  522. void TextureProgress::set_radial_initial_angle(float p_angle) {
  523. while (p_angle > 360) {
  524. p_angle -= 360;
  525. }
  526. while (p_angle < 0) {
  527. p_angle += 360;
  528. }
  529. rad_init_angle = p_angle;
  530. update();
  531. }
  532. float TextureProgress::get_radial_initial_angle() {
  533. return rad_init_angle;
  534. }
  535. void TextureProgress::set_fill_degrees(float p_angle) {
  536. rad_max_degrees = CLAMP(p_angle, 0, 360);
  537. update();
  538. }
  539. float TextureProgress::get_fill_degrees() {
  540. return rad_max_degrees;
  541. }
  542. void TextureProgress::set_radial_center_offset(const Point2 &p_off) {
  543. rad_center_off = p_off;
  544. update();
  545. }
  546. Point2 TextureProgress::get_radial_center_offset() {
  547. return rad_center_off;
  548. }
  549. void TextureProgress::_bind_methods() {
  550. ClassDB::bind_method(D_METHOD("set_under_texture", "tex"), &TextureProgress::set_under_texture);
  551. ClassDB::bind_method(D_METHOD("get_under_texture"), &TextureProgress::get_under_texture);
  552. ClassDB::bind_method(D_METHOD("set_progress_texture", "tex"), &TextureProgress::set_progress_texture);
  553. ClassDB::bind_method(D_METHOD("get_progress_texture"), &TextureProgress::get_progress_texture);
  554. ClassDB::bind_method(D_METHOD("set_over_texture", "tex"), &TextureProgress::set_over_texture);
  555. ClassDB::bind_method(D_METHOD("get_over_texture"), &TextureProgress::get_over_texture);
  556. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &TextureProgress::set_fill_mode);
  557. ClassDB::bind_method(D_METHOD("get_fill_mode"), &TextureProgress::get_fill_mode);
  558. ClassDB::bind_method(D_METHOD("set_tint_under", "tint"), &TextureProgress::set_tint_under);
  559. ClassDB::bind_method(D_METHOD("get_tint_under"), &TextureProgress::get_tint_under);
  560. ClassDB::bind_method(D_METHOD("set_tint_progress", "tint"), &TextureProgress::set_tint_progress);
  561. ClassDB::bind_method(D_METHOD("get_tint_progress"), &TextureProgress::get_tint_progress);
  562. ClassDB::bind_method(D_METHOD("set_tint_over", "tint"), &TextureProgress::set_tint_over);
  563. ClassDB::bind_method(D_METHOD("get_tint_over"), &TextureProgress::get_tint_over);
  564. ClassDB::bind_method(D_METHOD("set_texture_progress_offset", "offset"), &TextureProgress::set_progress_offset);
  565. ClassDB::bind_method(D_METHOD("get_texture_progress_offset"), &TextureProgress::get_progress_offset);
  566. ClassDB::bind_method(D_METHOD("set_radial_initial_angle", "mode"), &TextureProgress::set_radial_initial_angle);
  567. ClassDB::bind_method(D_METHOD("get_radial_initial_angle"), &TextureProgress::get_radial_initial_angle);
  568. ClassDB::bind_method(D_METHOD("set_radial_center_offset", "mode"), &TextureProgress::set_radial_center_offset);
  569. ClassDB::bind_method(D_METHOD("get_radial_center_offset"), &TextureProgress::get_radial_center_offset);
  570. ClassDB::bind_method(D_METHOD("set_fill_degrees", "mode"), &TextureProgress::set_fill_degrees);
  571. ClassDB::bind_method(D_METHOD("get_fill_degrees"), &TextureProgress::get_fill_degrees);
  572. ClassDB::bind_method(D_METHOD("set_stretch_margin", "margin", "value"), &TextureProgress::set_stretch_margin);
  573. ClassDB::bind_method(D_METHOD("get_stretch_margin", "margin"), &TextureProgress::get_stretch_margin);
  574. ClassDB::bind_method(D_METHOD("set_nine_patch_stretch", "stretch"), &TextureProgress::set_nine_patch_stretch);
  575. ClassDB::bind_method(D_METHOD("get_nine_patch_stretch"), &TextureProgress::get_nine_patch_stretch);
  576. ADD_GROUP("Textures", "texture_");
  577. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_under", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_under_texture", "get_under_texture");
  578. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_over", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_over_texture", "get_over_texture");
  579. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_progress", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_progress_texture", "get_progress_texture");
  580. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "texture_progress_offset"), "set_texture_progress_offset", "get_texture_progress_offset");
  581. 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");
  582. ADD_GROUP("Tint", "tint_");
  583. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_under"), "set_tint_under", "get_tint_under");
  584. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_over"), "set_tint_over", "get_tint_over");
  585. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_progress"), "set_tint_progress", "get_tint_progress");
  586. ADD_GROUP("Radial Fill", "radial_");
  587. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radial_initial_angle", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,slider"), "set_radial_initial_angle", "get_radial_initial_angle");
  588. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radial_fill_degrees", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,slider"), "set_fill_degrees", "get_fill_degrees");
  589. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "radial_center_offset"), "set_radial_center_offset", "get_radial_center_offset");
  590. ADD_GROUP("Stretch", "stretch_");
  591. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "nine_patch_stretch"), "set_nine_patch_stretch", "get_nine_patch_stretch");
  592. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_LEFT);
  593. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_TOP);
  594. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_RIGHT);
  595. ADD_PROPERTYI(PropertyInfo(Variant::INT, "stretch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_BOTTOM);
  596. BIND_ENUM_CONSTANT(FILL_LEFT_TO_RIGHT);
  597. BIND_ENUM_CONSTANT(FILL_RIGHT_TO_LEFT);
  598. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  599. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  600. BIND_ENUM_CONSTANT(FILL_CLOCKWISE);
  601. BIND_ENUM_CONSTANT(FILL_COUNTER_CLOCKWISE);
  602. BIND_ENUM_CONSTANT(FILL_BILINEAR_LEFT_AND_RIGHT);
  603. BIND_ENUM_CONSTANT(FILL_BILINEAR_TOP_AND_BOTTOM);
  604. BIND_ENUM_CONSTANT(FILL_CLOCKWISE_AND_COUNTER_CLOCKWISE);
  605. }
  606. TextureProgress::TextureProgress() {
  607. mode = FILL_LEFT_TO_RIGHT;
  608. rad_init_angle = 0;
  609. rad_center_off = Point2();
  610. rad_max_degrees = 360;
  611. set_mouse_filter(MOUSE_FILTER_PASS);
  612. nine_patch_stretch = false;
  613. stretch_margin[MARGIN_LEFT] = 0;
  614. stretch_margin[MARGIN_RIGHT] = 0;
  615. stretch_margin[MARGIN_BOTTOM] = 0;
  616. stretch_margin[MARGIN_TOP] = 0;
  617. tint_under = tint_progress = tint_over = Color(1, 1, 1);
  618. }