progress_bar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**************************************************************************/
  2. /* progress_bar.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "progress_bar.h"
  31. #include "scene/resources/text_line.h"
  32. #include "scene/theme/theme_db.h"
  33. Size2 ProgressBar::get_minimum_size() const {
  34. Size2 minimum_size = theme_cache.background_style->get_minimum_size();
  35. minimum_size = minimum_size.max(theme_cache.fill_style->get_minimum_size());
  36. if (show_percentage) {
  37. String txt = "100%";
  38. TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
  39. minimum_size.height = MAX(minimum_size.height, theme_cache.background_style->get_minimum_size().height + tl.get_size().y);
  40. } else { // this is needed, else the progressbar will collapse
  41. minimum_size = minimum_size.maxf(1);
  42. }
  43. return minimum_size;
  44. }
  45. void ProgressBar::_notification(int p_what) {
  46. switch (p_what) {
  47. case NOTIFICATION_INTERNAL_PROCESS: {
  48. if (is_visible_in_tree()) {
  49. _indeterminate_fill_progress += get_process_delta_time() * MAX(indeterminate_min_speed, MAX(get_size().width, get_size().height) / 2);
  50. queue_redraw();
  51. }
  52. } break;
  53. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  54. RID ae = get_accessibility_element();
  55. ERR_FAIL_COND(ae.is_null());
  56. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_PROGRESS_INDICATOR);
  57. } break;
  58. case NOTIFICATION_DRAW: {
  59. draw_style_box(theme_cache.background_style, Rect2(Point2(), get_size()));
  60. if (indeterminate) {
  61. Size2 size = get_size();
  62. real_t fill_size = MIN(size.width, size.height) * 2;
  63. if (is_part_of_edited_scene() && !editor_preview_indeterminate) {
  64. // Center the filled bar when we're not previewing the animation.
  65. _indeterminate_fill_progress = (MAX(size.width, size.height) / 2) + (fill_size / 2);
  66. }
  67. switch (mode) {
  68. case FILL_END_TO_BEGIN:
  69. case FILL_BEGIN_TO_END: {
  70. // Follow the RTL layout with the animation to match how the bar would fill.
  71. bool right_to_left = mode == (is_layout_rtl() ? FILL_BEGIN_TO_END : FILL_END_TO_BEGIN);
  72. if (_indeterminate_fill_progress > size.width + fill_size) {
  73. _indeterminate_fill_progress = right_to_left ? -fill_size : 0;
  74. }
  75. real_t x = right_to_left ? size.width - _indeterminate_fill_progress : _indeterminate_fill_progress - fill_size;
  76. draw_style_box(theme_cache.fill_style, Rect2(x, 0, fill_size, size.height).intersection(Rect2(Point2(), size)));
  77. } break;
  78. case FILL_TOP_TO_BOTTOM: {
  79. if (_indeterminate_fill_progress > size.height + fill_size) {
  80. _indeterminate_fill_progress = 0;
  81. }
  82. draw_style_box(theme_cache.fill_style, Rect2(0, _indeterminate_fill_progress - fill_size, size.width, fill_size).intersection(Rect2(Point2(), size)));
  83. } break;
  84. case FILL_BOTTOM_TO_TOP: {
  85. if (_indeterminate_fill_progress > size.height + fill_size) {
  86. _indeterminate_fill_progress = -fill_size;
  87. }
  88. draw_style_box(theme_cache.fill_style, Rect2(0, size.height - _indeterminate_fill_progress, size.width, fill_size).intersection(Rect2(Point2(), size)));
  89. } break;
  90. case FILL_MODE_MAX:
  91. break;
  92. }
  93. return;
  94. }
  95. float r = get_as_ratio();
  96. switch (mode) {
  97. case FILL_BEGIN_TO_END:
  98. case FILL_END_TO_BEGIN: {
  99. int mp = theme_cache.fill_style->get_minimum_size().width;
  100. int p = std::round(r * (get_size().width - mp));
  101. // We want FILL_BEGIN_TO_END to map to right to left when UI layout is RTL,
  102. // and left to right otherwise. And likewise for FILL_END_TO_BEGIN.
  103. bool right_to_left = mode == (is_layout_rtl() ? FILL_BEGIN_TO_END : FILL_END_TO_BEGIN);
  104. if (p > 0) {
  105. if (right_to_left) {
  106. int p_remaining = std::round((1.0 - r) * (get_size().width - mp));
  107. draw_style_box(theme_cache.fill_style, Rect2(Point2(p_remaining, 0), Size2(p + theme_cache.fill_style->get_minimum_size().width, get_size().height)));
  108. } else {
  109. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(p + theme_cache.fill_style->get_minimum_size().width, get_size().height)));
  110. }
  111. }
  112. } break;
  113. case FILL_TOP_TO_BOTTOM:
  114. case FILL_BOTTOM_TO_TOP: {
  115. int mp = theme_cache.fill_style->get_minimum_size().height;
  116. int p = std::round(r * (get_size().height - mp));
  117. if (p > 0) {
  118. if (mode == FILL_TOP_TO_BOTTOM) {
  119. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(get_size().width, p + theme_cache.fill_style->get_minimum_size().height)));
  120. } else {
  121. int p_remaining = std::round((1.0 - r) * (get_size().height - mp));
  122. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, p_remaining), Size2(get_size().width, p + theme_cache.fill_style->get_minimum_size().height)));
  123. }
  124. }
  125. } break;
  126. case FILL_MODE_MAX:
  127. break;
  128. }
  129. if (show_percentage) {
  130. double ratio = 0;
  131. // Avoid division by zero.
  132. if (Math::is_equal_approx(get_max(), get_min())) {
  133. ratio = 1;
  134. } else if (is_ratio_exp() && get_min() >= 0 && get_value() >= 0) {
  135. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  136. double exp_max = Math::log(get_max()) / Math::log((double)2);
  137. double exp_value = get_value() == 0 ? 0.0 : Math::log(get_value()) / Math::log((double)2);
  138. double percentage = (exp_value - exp_min) / (exp_max - exp_min);
  139. ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1);
  140. } else {
  141. double percentage = (get_value() - get_min()) / (get_max() - get_min());
  142. ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1);
  143. }
  144. String txt = itos(int(ratio * 100));
  145. if (is_localizing_numeral_system()) {
  146. txt = TS->format_number(txt) + TS->percent_sign();
  147. } else {
  148. txt += String("%");
  149. }
  150. TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
  151. Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round();
  152. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  153. tl.draw_outline(get_canvas_item(), text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  154. }
  155. tl.draw(get_canvas_item(), text_pos, theme_cache.font_color);
  156. }
  157. } break;
  158. }
  159. }
  160. void ProgressBar::_validate_property(PropertyInfo &p_property) const {
  161. if (Engine::get_singleton()->is_editor_hint() && indeterminate && p_property.name == "show_percentage") {
  162. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  163. }
  164. if (!indeterminate && p_property.name == "editor_preview_indeterminate") {
  165. p_property.usage = PROPERTY_USAGE_NONE;
  166. }
  167. }
  168. void ProgressBar::set_fill_mode(int p_fill) {
  169. ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
  170. mode = (FillMode)p_fill;
  171. _indeterminate_fill_progress = 0;
  172. queue_redraw();
  173. }
  174. int ProgressBar::get_fill_mode() {
  175. return mode;
  176. }
  177. void ProgressBar::set_show_percentage(bool p_visible) {
  178. if (show_percentage == p_visible) {
  179. return;
  180. }
  181. show_percentage = p_visible;
  182. update_minimum_size();
  183. queue_redraw();
  184. }
  185. bool ProgressBar::is_percentage_shown() const {
  186. return show_percentage;
  187. }
  188. void ProgressBar::set_indeterminate(bool p_indeterminate) {
  189. if (indeterminate == p_indeterminate) {
  190. return;
  191. }
  192. indeterminate = p_indeterminate;
  193. _indeterminate_fill_progress = 0;
  194. bool should_process = !is_part_of_edited_scene() || editor_preview_indeterminate;
  195. set_process_internal(indeterminate && should_process);
  196. notify_property_list_changed();
  197. update_minimum_size();
  198. queue_redraw();
  199. }
  200. bool ProgressBar::is_indeterminate() const {
  201. return indeterminate;
  202. }
  203. void ProgressBar::set_editor_preview_indeterminate(bool p_preview_indeterminate) {
  204. if (editor_preview_indeterminate == p_preview_indeterminate) {
  205. return;
  206. }
  207. editor_preview_indeterminate = p_preview_indeterminate;
  208. if (is_part_of_edited_scene()) {
  209. _indeterminate_fill_progress = 0;
  210. set_process_internal(indeterminate && editor_preview_indeterminate);
  211. queue_redraw();
  212. }
  213. }
  214. bool ProgressBar::is_editor_preview_indeterminate_enabled() const {
  215. return editor_preview_indeterminate;
  216. }
  217. void ProgressBar::_bind_methods() {
  218. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &ProgressBar::set_fill_mode);
  219. ClassDB::bind_method(D_METHOD("get_fill_mode"), &ProgressBar::get_fill_mode);
  220. ClassDB::bind_method(D_METHOD("set_show_percentage", "visible"), &ProgressBar::set_show_percentage);
  221. ClassDB::bind_method(D_METHOD("is_percentage_shown"), &ProgressBar::is_percentage_shown);
  222. ClassDB::bind_method(D_METHOD("set_indeterminate", "indeterminate"), &ProgressBar::set_indeterminate);
  223. ClassDB::bind_method(D_METHOD("is_indeterminate"), &ProgressBar::is_indeterminate);
  224. ClassDB::bind_method(D_METHOD("set_editor_preview_indeterminate", "preview_indeterminate"), &ProgressBar::set_editor_preview_indeterminate);
  225. ClassDB::bind_method(D_METHOD("is_editor_preview_indeterminate_enabled"), &ProgressBar::is_editor_preview_indeterminate_enabled);
  226. ADD_PROPERTY(PropertyInfo(Variant::INT, "fill_mode", PROPERTY_HINT_ENUM, "Begin to End,End to Begin,Top to Bottom,Bottom to Top"), "set_fill_mode", "get_fill_mode");
  227. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_percentage"), "set_show_percentage", "is_percentage_shown");
  228. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indeterminate"), "set_indeterminate", "is_indeterminate");
  229. ADD_GROUP("Editor", "editor_");
  230. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_preview_indeterminate"), "set_editor_preview_indeterminate", "is_editor_preview_indeterminate_enabled");
  231. BIND_ENUM_CONSTANT(FILL_BEGIN_TO_END);
  232. BIND_ENUM_CONSTANT(FILL_END_TO_BEGIN);
  233. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  234. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  235. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ProgressBar, background_style, "background");
  236. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ProgressBar, fill_style, "fill");
  237. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, ProgressBar, font);
  238. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, ProgressBar, font_size);
  239. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ProgressBar, font_color);
  240. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ProgressBar, font_outline_size, "outline_size");
  241. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ProgressBar, font_outline_color);
  242. }
  243. ProgressBar::ProgressBar() {
  244. set_v_size_flags(0);
  245. set_step(0.01);
  246. }