texture_progress.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* texture_progress.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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.h"
  31. #include "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(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(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. return Size2(1, 1);
  78. }
  79. void TextureProgress::set_progress_texture(const Ref<Texture> &p_texture) {
  80. progress = p_texture;
  81. update();
  82. minimum_size_changed();
  83. }
  84. Ref<Texture> TextureProgress::get_progress_texture() const {
  85. return progress;
  86. }
  87. Point2 TextureProgress::unit_val_to_uv(float val) {
  88. if (progress.is_null())
  89. return Point2();
  90. if (val < 0)
  91. val += 1;
  92. if (val > 1)
  93. val -= 1;
  94. Point2 p = get_relative_center();
  95. if (val < 0.125)
  96. return Point2(p.x + (1 - p.x) * val * 8, 0);
  97. if (val < 0.25)
  98. return Point2(1, p.y * (val - 0.125) * 8);
  99. if (val < 0.375)
  100. return Point2(1, p.y + (1 - p.y) * (val - 0.25) * 8);
  101. if (val < 0.5)
  102. return Point2(1 - (1 - p.x) * (val - 0.375) * 8, 1);
  103. if (val < 0.625)
  104. return Point2(p.x * (1 - (val - 0.5) * 8), 1);
  105. if (val < 0.75)
  106. return Point2(0, 1 - ((1 - p.y) * (val - 0.625) * 8));
  107. if (val < 0.875)
  108. return Point2(0, p.y - p.y * (val - 0.75) * 8);
  109. else
  110. return Point2(p.x * (val - 0.875) * 8, 0);
  111. }
  112. Point2 TextureProgress::get_relative_center() {
  113. if (progress.is_null())
  114. return Point2();
  115. Point2 p = progress->get_size() / 2;
  116. p += rad_center_off;
  117. p.x /= progress->get_width();
  118. p.y /= progress->get_height();
  119. p.x = CLAMP(p.x, 0, 1);
  120. p.y = CLAMP(p.y, 0, 1);
  121. return p;
  122. }
  123. void TextureProgress::draw_nine_patch_stretched(const Ref<Texture> &p_texture, FillMode p_mode, double p_ratio) {
  124. Vector2 texture_size = p_texture->get_size();
  125. Vector2 topleft = Vector2(stretch_margin[MARGIN_LEFT], stretch_margin[MARGIN_TOP]);
  126. Vector2 bottomright = Vector2(stretch_margin[MARGIN_RIGHT], stretch_margin[MARGIN_BOTTOM]);
  127. Rect2 src_rect = Rect2(Point2(), texture_size);
  128. Rect2 dst_rect = Rect2(Point2(), get_size());
  129. if (p_ratio < 1.0) {
  130. // Drawing a partially-filled 9-patch is a little tricky -
  131. // texture is divided by 3 sections toward fill direction,
  132. // then middle section is streching while the other two aren't.
  133. double width_total = 0.0;
  134. double width_texture = 0.0;
  135. double first_section_size = 0.0;
  136. double last_section_size = 0.0;
  137. switch (mode) {
  138. case FILL_LEFT_TO_RIGHT:
  139. case FILL_RIGHT_TO_LEFT: {
  140. width_total = dst_rect.size.x;
  141. width_texture = texture_size.x;
  142. first_section_size = topleft.x;
  143. last_section_size = bottomright.x;
  144. } break;
  145. case FILL_TOP_TO_BOTTOM:
  146. case FILL_BOTTOM_TO_TOP: {
  147. width_total = dst_rect.size.y;
  148. width_texture = texture_size.y;
  149. first_section_size = topleft.y;
  150. last_section_size = bottomright.y;
  151. } break;
  152. }
  153. double width_filled = width_total * p_ratio;
  154. double middle_section_size = MAX(0.0, width_texture - first_section_size - last_section_size);
  155. 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)));
  156. last_section_size = MAX(0.0, last_section_size - (width_total - width_filled));
  157. width_texture = MIN(width_texture, first_section_size + middle_section_size + last_section_size);
  158. switch (mode) {
  159. case FILL_LEFT_TO_RIGHT: {
  160. src_rect.size.x = width_texture;
  161. dst_rect.size.x = width_filled;
  162. bottomright.x = last_section_size;
  163. } break;
  164. case FILL_RIGHT_TO_LEFT: {
  165. src_rect.position.x += src_rect.size.x - width_texture;
  166. src_rect.size.x = width_texture;
  167. dst_rect.position.x += width_total - width_filled;
  168. dst_rect.size.x = width_filled;
  169. topleft.x = last_section_size;
  170. } break;
  171. case FILL_TOP_TO_BOTTOM: {
  172. src_rect.size.y = width_texture;
  173. dst_rect.size.y = width_filled;
  174. bottomright.y = last_section_size;
  175. } break;
  176. case FILL_BOTTOM_TO_TOP: {
  177. src_rect.position.y += src_rect.size.y - width_texture;
  178. src_rect.size.y = width_texture;
  179. dst_rect.position.y += width_total - width_filled;
  180. dst_rect.size.y = width_filled;
  181. topleft.y = last_section_size;
  182. } break;
  183. }
  184. }
  185. RID ci = get_canvas_item();
  186. VS::get_singleton()->canvas_item_add_nine_patch(ci, dst_rect, src_rect, p_texture->get_rid(), topleft, bottomright);
  187. }
  188. void TextureProgress::_notification(int p_what) {
  189. 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 };
  190. switch (p_what) {
  191. case NOTIFICATION_DRAW: {
  192. if (nine_patch_stretch && (mode == FILL_LEFT_TO_RIGHT || mode == FILL_RIGHT_TO_LEFT || mode == FILL_TOP_TO_BOTTOM || mode == FILL_BOTTOM_TO_TOP)) {
  193. if (under.is_valid()) {
  194. draw_nine_patch_stretched(under, FILL_LEFT_TO_RIGHT, 1.0);
  195. }
  196. if (progress.is_valid()) {
  197. draw_nine_patch_stretched(progress, mode, get_as_ratio());
  198. }
  199. if (over.is_valid()) {
  200. draw_nine_patch_stretched(over, FILL_LEFT_TO_RIGHT, 1.0);
  201. }
  202. } else {
  203. if (under.is_valid())
  204. draw_texture(under, Point2());
  205. if (progress.is_valid()) {
  206. Size2 s = progress->get_size();
  207. switch (mode) {
  208. case FILL_LEFT_TO_RIGHT: {
  209. Rect2 region = Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y));
  210. draw_texture_rect_region(progress, region, region);
  211. } break;
  212. case FILL_RIGHT_TO_LEFT: {
  213. Rect2 region = Rect2(Point2(s.x - s.x * get_as_ratio(), 0), Size2(s.x * get_as_ratio(), s.y));
  214. draw_texture_rect_region(progress, region, region);
  215. } break;
  216. case FILL_TOP_TO_BOTTOM: {
  217. Rect2 region = Rect2(Point2(), Size2(s.x, s.y * get_as_ratio()));
  218. draw_texture_rect_region(progress, region, region);
  219. } break;
  220. case FILL_BOTTOM_TO_TOP: {
  221. Rect2 region = Rect2(Point2(0, s.y - s.y * get_as_ratio()), Size2(s.x, s.y * get_as_ratio()));
  222. draw_texture_rect_region(progress, region, region);
  223. } break;
  224. case FILL_CLOCKWISE:
  225. case FILL_COUNTER_CLOCKWISE: {
  226. float val = get_as_ratio() * rad_max_degrees / 360;
  227. if (val == 1) {
  228. Rect2 region = Rect2(Point2(), s);
  229. draw_texture_rect_region(progress, region, region);
  230. } else if (val != 0) {
  231. Array pts;
  232. float direction = mode == FILL_CLOCKWISE ? 1 : -1;
  233. float start = rad_init_angle / 360;
  234. float end = start + direction * val;
  235. pts.append(start);
  236. pts.append(end);
  237. float from = MIN(start, end);
  238. float to = MAX(start, end);
  239. for (int i = 0; i < 12; i++)
  240. if (corners[i] > from && corners[i] < to)
  241. pts.append(corners[i]);
  242. pts.sort();
  243. Vector<Point2> uvs;
  244. Vector<Point2> points;
  245. uvs.push_back(get_relative_center());
  246. points.push_back(Point2(s.x * get_relative_center().x, s.y * get_relative_center().y));
  247. for (int i = 0; i < pts.size(); i++) {
  248. Point2 uv = unit_val_to_uv(pts[i]);
  249. if (uvs.find(uv) >= 0)
  250. continue;
  251. uvs.push_back(uv);
  252. points.push_back(Point2(uv.x * s.x, uv.y * s.y));
  253. }
  254. draw_polygon(points, Vector<Color>(), uvs, progress);
  255. }
  256. if (Engine::get_singleton()->is_editor_hint()) {
  257. Point2 p = progress->get_size();
  258. p.x *= get_relative_center().x;
  259. p.y *= get_relative_center().y;
  260. p = p.floor();
  261. draw_line(p - Point2(8, 0), p + Point2(8, 0), Color(0.9, 0.5, 0.5), 2);
  262. draw_line(p - Point2(0, 8), p + Point2(0, 8), Color(0.9, 0.5, 0.5), 2);
  263. }
  264. } break;
  265. default:
  266. draw_texture_rect_region(progress, Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y)), Rect2(Point2(), Size2(s.x * get_as_ratio(), s.y)));
  267. }
  268. }
  269. if (over.is_valid())
  270. draw_texture(over, Point2());
  271. }
  272. } break;
  273. }
  274. }
  275. void TextureProgress::set_fill_mode(int p_fill) {
  276. ERR_FAIL_INDEX(p_fill, 6);
  277. mode = (FillMode)p_fill;
  278. update();
  279. }
  280. int TextureProgress::get_fill_mode() {
  281. return mode;
  282. }
  283. void TextureProgress::set_radial_initial_angle(float p_angle) {
  284. while (p_angle > 360)
  285. p_angle -= 360;
  286. while (p_angle < 0)
  287. p_angle += 360;
  288. rad_init_angle = p_angle;
  289. update();
  290. }
  291. float TextureProgress::get_radial_initial_angle() {
  292. return rad_init_angle;
  293. }
  294. void TextureProgress::set_fill_degrees(float p_angle) {
  295. rad_max_degrees = CLAMP(p_angle, 0, 360);
  296. update();
  297. }
  298. float TextureProgress::get_fill_degrees() {
  299. return rad_max_degrees;
  300. }
  301. void TextureProgress::set_radial_center_offset(const Point2 &p_off) {
  302. rad_center_off = p_off;
  303. update();
  304. }
  305. Point2 TextureProgress::get_radial_center_offset() {
  306. return rad_center_off;
  307. }
  308. void TextureProgress::_bind_methods() {
  309. ClassDB::bind_method(D_METHOD("set_under_texture", "tex"), &TextureProgress::set_under_texture);
  310. ClassDB::bind_method(D_METHOD("get_under_texture"), &TextureProgress::get_under_texture);
  311. ClassDB::bind_method(D_METHOD("set_progress_texture", "tex"), &TextureProgress::set_progress_texture);
  312. ClassDB::bind_method(D_METHOD("get_progress_texture"), &TextureProgress::get_progress_texture);
  313. ClassDB::bind_method(D_METHOD("set_over_texture", "tex"), &TextureProgress::set_over_texture);
  314. ClassDB::bind_method(D_METHOD("get_over_texture"), &TextureProgress::get_over_texture);
  315. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &TextureProgress::set_fill_mode);
  316. ClassDB::bind_method(D_METHOD("get_fill_mode"), &TextureProgress::get_fill_mode);
  317. ClassDB::bind_method(D_METHOD("set_radial_initial_angle", "mode"), &TextureProgress::set_radial_initial_angle);
  318. ClassDB::bind_method(D_METHOD("get_radial_initial_angle"), &TextureProgress::get_radial_initial_angle);
  319. ClassDB::bind_method(D_METHOD("set_radial_center_offset", "mode"), &TextureProgress::set_radial_center_offset);
  320. ClassDB::bind_method(D_METHOD("get_radial_center_offset"), &TextureProgress::get_radial_center_offset);
  321. ClassDB::bind_method(D_METHOD("set_fill_degrees", "mode"), &TextureProgress::set_fill_degrees);
  322. ClassDB::bind_method(D_METHOD("get_fill_degrees"), &TextureProgress::get_fill_degrees);
  323. ClassDB::bind_method(D_METHOD("set_stretch_margin", "margin", "value"), &TextureProgress::set_stretch_margin);
  324. ClassDB::bind_method(D_METHOD("get_stretch_margin", "margin"), &TextureProgress::get_stretch_margin);
  325. ClassDB::bind_method(D_METHOD("set_nine_patch_stretch", "stretch"), &TextureProgress::set_nine_patch_stretch);
  326. ClassDB::bind_method(D_METHOD("get_nine_patch_stretch"), &TextureProgress::get_nine_patch_stretch);
  327. ADD_GROUP("Textures", "texture_");
  328. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_under", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_under_texture", "get_under_texture");
  329. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_over", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_over_texture", "get_over_texture");
  330. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_progress", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_progress_texture", "get_progress_texture");
  331. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "fill_mode", PROPERTY_HINT_ENUM, "Left to Right,Right to Left,Top to Bottom,Bottom to Top,Clockwise,Counter Clockwise"), "set_fill_mode", "get_fill_mode");
  332. ADD_GROUP("Radial Fill", "radial_");
  333. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "radial_initial_angle", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,slider"), "set_radial_initial_angle", "get_radial_initial_angle");
  334. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "radial_fill_degrees", PROPERTY_HINT_RANGE, "0.0,360.0,0.1,slider"), "set_fill_degrees", "get_fill_degrees");
  335. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "radial_center_offset"), "set_radial_center_offset", "get_radial_center_offset");
  336. ADD_GROUP("Stretch", "stretch_");
  337. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "nine_patch_stretch"), "set_nine_patch_stretch", "get_nine_patch_stretch");
  338. ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "stretch_margin_left", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_LEFT);
  339. ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "stretch_margin_top", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_TOP);
  340. ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "stretch_margin_right", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_RIGHT);
  341. ADD_PROPERTYINZ(PropertyInfo(Variant::INT, "stretch_margin_bottom", PROPERTY_HINT_RANGE, "0,16384,1"), "set_stretch_margin", "get_stretch_margin", MARGIN_BOTTOM);
  342. BIND_ENUM_CONSTANT(FILL_LEFT_TO_RIGHT);
  343. BIND_ENUM_CONSTANT(FILL_RIGHT_TO_LEFT);
  344. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  345. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  346. BIND_ENUM_CONSTANT(FILL_CLOCKWISE);
  347. BIND_ENUM_CONSTANT(FILL_COUNTER_CLOCKWISE);
  348. }
  349. TextureProgress::TextureProgress() {
  350. mode = FILL_LEFT_TO_RIGHT;
  351. rad_init_angle = 0;
  352. rad_center_off = Point2();
  353. rad_max_degrees = 360;
  354. set_mouse_filter(MOUSE_FILTER_PASS);
  355. nine_patch_stretch = false;
  356. stretch_margin[MARGIN_LEFT] = 0;
  357. stretch_margin[MARGIN_RIGHT] = 0;
  358. stretch_margin[MARGIN_BOTTOM] = 0;
  359. stretch_margin[MARGIN_TOP] = 0;
  360. }