texture_progress.cpp 27 KB

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