progress_bar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "core/string/translation_server.h"
  32. #include "scene/resources/text_line.h"
  33. #include "scene/theme/theme_db.h"
  34. Size2 ProgressBar::get_minimum_size() const {
  35. Size2 minimum_size = theme_cache.background_style->get_minimum_size();
  36. minimum_size = minimum_size.max(theme_cache.fill_style->get_minimum_size());
  37. if (show_percentage) {
  38. String txt = "100%";
  39. TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
  40. minimum_size.height = MAX(minimum_size.height, theme_cache.background_style->get_minimum_size().height + tl.get_size().y);
  41. } else { // this is needed, else the progressbar will collapse
  42. minimum_size = minimum_size.maxf(1);
  43. }
  44. return minimum_size;
  45. }
  46. void ProgressBar::_notification(int p_what) {
  47. switch (p_what) {
  48. case NOTIFICATION_INTERNAL_PROCESS: {
  49. if (is_visible_in_tree()) {
  50. _indeterminate_fill_progress += get_process_delta_time() * MAX(indeterminate_min_speed, MAX(get_size().width, get_size().height) / 2);
  51. queue_redraw();
  52. }
  53. } break;
  54. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  55. RID ae = get_accessibility_element();
  56. ERR_FAIL_COND(ae.is_null());
  57. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_PROGRESS_INDICATOR);
  58. } break;
  59. case NOTIFICATION_DRAW: {
  60. draw_style_box(theme_cache.background_style, Rect2(Point2(), get_size()));
  61. if (indeterminate) {
  62. Size2 size = get_size();
  63. real_t fill_size = MIN(size.width, size.height) * 2;
  64. if (is_part_of_edited_scene() && !editor_preview_indeterminate) {
  65. // Center the filled bar when we're not previewing the animation.
  66. _indeterminate_fill_progress = (MAX(size.width, size.height) / 2) + (fill_size / 2);
  67. }
  68. switch (mode) {
  69. case FILL_END_TO_BEGIN:
  70. case FILL_BEGIN_TO_END: {
  71. // Follow the RTL layout with the animation to match how the bar would fill.
  72. bool right_to_left = mode == (is_layout_rtl() ? FILL_BEGIN_TO_END : FILL_END_TO_BEGIN);
  73. if (_indeterminate_fill_progress > size.width + fill_size) {
  74. _indeterminate_fill_progress = right_to_left ? -fill_size : 0;
  75. }
  76. real_t x = right_to_left ? size.width - _indeterminate_fill_progress : _indeterminate_fill_progress - fill_size;
  77. draw_style_box(theme_cache.fill_style, Rect2(x, 0, fill_size, size.height).intersection(Rect2(Point2(), size)));
  78. } break;
  79. case FILL_TOP_TO_BOTTOM: {
  80. if (_indeterminate_fill_progress > size.height + fill_size) {
  81. _indeterminate_fill_progress = 0;
  82. }
  83. draw_style_box(theme_cache.fill_style, Rect2(0, _indeterminate_fill_progress - fill_size, size.width, fill_size).intersection(Rect2(Point2(), size)));
  84. } break;
  85. case FILL_BOTTOM_TO_TOP: {
  86. if (_indeterminate_fill_progress > size.height + fill_size) {
  87. _indeterminate_fill_progress = -fill_size;
  88. }
  89. draw_style_box(theme_cache.fill_style, Rect2(0, size.height - _indeterminate_fill_progress, size.width, fill_size).intersection(Rect2(Point2(), size)));
  90. } break;
  91. case FILL_MODE_MAX:
  92. break;
  93. }
  94. return;
  95. }
  96. float r = get_as_ratio();
  97. switch (mode) {
  98. case FILL_BEGIN_TO_END:
  99. case FILL_END_TO_BEGIN: {
  100. int mp = theme_cache.fill_style->get_minimum_size().width;
  101. int p = std::round(r * (get_size().width - mp));
  102. // We want FILL_BEGIN_TO_END to map to right to left when UI layout is RTL,
  103. // and left to right otherwise. And likewise for FILL_END_TO_BEGIN.
  104. bool right_to_left = mode == (is_layout_rtl() ? FILL_BEGIN_TO_END : FILL_END_TO_BEGIN);
  105. if (p > 0) {
  106. if (right_to_left) {
  107. int p_remaining = std::round((1.0 - r) * (get_size().width - mp));
  108. 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)));
  109. } else {
  110. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(p + theme_cache.fill_style->get_minimum_size().width, get_size().height)));
  111. }
  112. }
  113. } break;
  114. case FILL_TOP_TO_BOTTOM:
  115. case FILL_BOTTOM_TO_TOP: {
  116. int mp = theme_cache.fill_style->get_minimum_size().height;
  117. int p = std::round(r * (get_size().height - mp));
  118. if (p > 0) {
  119. if (mode == FILL_TOP_TO_BOTTOM) {
  120. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(get_size().width, p + theme_cache.fill_style->get_minimum_size().height)));
  121. } else {
  122. int p_remaining = std::round((1.0 - r) * (get_size().height - mp));
  123. 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)));
  124. }
  125. }
  126. } break;
  127. case FILL_MODE_MAX:
  128. break;
  129. }
  130. if (show_percentage) {
  131. double ratio = 0;
  132. // Avoid division by zero.
  133. if (Math::is_equal_approx(get_max(), get_min())) {
  134. ratio = 1;
  135. } else if (is_ratio_exp() && get_min() >= 0 && get_value() >= 0) {
  136. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  137. double exp_max = Math::log(get_max()) / Math::log((double)2);
  138. double exp_value = get_value() == 0 ? 0.0 : Math::log(get_value()) / Math::log((double)2);
  139. double percentage = (exp_value - exp_min) / (exp_max - exp_min);
  140. ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1);
  141. } else {
  142. double percentage = (get_value() - get_min()) / (get_max() - get_min());
  143. ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1);
  144. }
  145. String txt = itos(int(Math::round(ratio * 100)));
  146. if (is_localizing_numeral_system()) {
  147. const String &lang = _get_locale();
  148. txt = TranslationServer::get_singleton()->format_number(txt, lang) + TranslationServer::get_singleton()->get_percent_sign(lang);
  149. } else {
  150. txt += String("%");
  151. }
  152. TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
  153. Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round();
  154. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  155. tl.draw_outline(get_canvas_item(), text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  156. }
  157. tl.draw(get_canvas_item(), text_pos, theme_cache.font_color);
  158. }
  159. } break;
  160. }
  161. }
  162. void ProgressBar::_validate_property(PropertyInfo &p_property) const {
  163. if (Engine::get_singleton()->is_editor_hint() && indeterminate && p_property.name == "show_percentage") {
  164. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  165. }
  166. if (!indeterminate && p_property.name == "editor_preview_indeterminate") {
  167. p_property.usage = PROPERTY_USAGE_NONE;
  168. }
  169. }
  170. void ProgressBar::set_fill_mode(int p_fill) {
  171. ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
  172. mode = (FillMode)p_fill;
  173. _indeterminate_fill_progress = 0;
  174. queue_redraw();
  175. }
  176. int ProgressBar::get_fill_mode() {
  177. return mode;
  178. }
  179. void ProgressBar::set_show_percentage(bool p_visible) {
  180. if (show_percentage == p_visible) {
  181. return;
  182. }
  183. show_percentage = p_visible;
  184. update_minimum_size();
  185. queue_redraw();
  186. }
  187. bool ProgressBar::is_percentage_shown() const {
  188. return show_percentage;
  189. }
  190. void ProgressBar::set_indeterminate(bool p_indeterminate) {
  191. if (indeterminate == p_indeterminate) {
  192. return;
  193. }
  194. indeterminate = p_indeterminate;
  195. _indeterminate_fill_progress = 0;
  196. bool should_process = !is_part_of_edited_scene() || editor_preview_indeterminate;
  197. set_process_internal(indeterminate && should_process);
  198. notify_property_list_changed();
  199. update_minimum_size();
  200. queue_redraw();
  201. }
  202. bool ProgressBar::is_indeterminate() const {
  203. return indeterminate;
  204. }
  205. void ProgressBar::set_editor_preview_indeterminate(bool p_preview_indeterminate) {
  206. if (editor_preview_indeterminate == p_preview_indeterminate) {
  207. return;
  208. }
  209. editor_preview_indeterminate = p_preview_indeterminate;
  210. if (is_part_of_edited_scene()) {
  211. _indeterminate_fill_progress = 0;
  212. set_process_internal(indeterminate && editor_preview_indeterminate);
  213. queue_redraw();
  214. }
  215. }
  216. bool ProgressBar::is_editor_preview_indeterminate_enabled() const {
  217. return editor_preview_indeterminate;
  218. }
  219. void ProgressBar::_bind_methods() {
  220. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &ProgressBar::set_fill_mode);
  221. ClassDB::bind_method(D_METHOD("get_fill_mode"), &ProgressBar::get_fill_mode);
  222. ClassDB::bind_method(D_METHOD("set_show_percentage", "visible"), &ProgressBar::set_show_percentage);
  223. ClassDB::bind_method(D_METHOD("is_percentage_shown"), &ProgressBar::is_percentage_shown);
  224. ClassDB::bind_method(D_METHOD("set_indeterminate", "indeterminate"), &ProgressBar::set_indeterminate);
  225. ClassDB::bind_method(D_METHOD("is_indeterminate"), &ProgressBar::is_indeterminate);
  226. ClassDB::bind_method(D_METHOD("set_editor_preview_indeterminate", "preview_indeterminate"), &ProgressBar::set_editor_preview_indeterminate);
  227. ClassDB::bind_method(D_METHOD("is_editor_preview_indeterminate_enabled"), &ProgressBar::is_editor_preview_indeterminate_enabled);
  228. 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");
  229. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_percentage"), "set_show_percentage", "is_percentage_shown");
  230. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indeterminate"), "set_indeterminate", "is_indeterminate");
  231. ADD_GROUP("Editor", "editor_");
  232. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_preview_indeterminate"), "set_editor_preview_indeterminate", "is_editor_preview_indeterminate_enabled");
  233. BIND_ENUM_CONSTANT(FILL_BEGIN_TO_END);
  234. BIND_ENUM_CONSTANT(FILL_END_TO_BEGIN);
  235. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  236. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  237. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ProgressBar, background_style, "background");
  238. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ProgressBar, fill_style, "fill");
  239. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, ProgressBar, font);
  240. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, ProgressBar, font_size);
  241. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ProgressBar, font_color);
  242. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ProgressBar, font_outline_size, "outline_size");
  243. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ProgressBar, font_outline_color);
  244. }
  245. ProgressBar::ProgressBar() {
  246. set_v_size_flags(0);
  247. set_step(0.01);
  248. }