progress_bar.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Size2 ProgressBar::get_minimum_size() const {
  33. Size2 minimum_size = theme_cache.background_style->get_minimum_size();
  34. minimum_size.height = MAX(minimum_size.height, theme_cache.fill_style->get_minimum_size().height);
  35. minimum_size.width = MAX(minimum_size.width, theme_cache.fill_style->get_minimum_size().width);
  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.width = MAX(minimum_size.width, 1);
  42. minimum_size.height = MAX(minimum_size.height, 1);
  43. }
  44. return minimum_size;
  45. }
  46. void ProgressBar::_update_theme_item_cache() {
  47. Range::_update_theme_item_cache();
  48. theme_cache.background_style = get_theme_stylebox(SNAME("background"));
  49. theme_cache.fill_style = get_theme_stylebox(SNAME("fill"));
  50. theme_cache.font = get_theme_font(SNAME("font"));
  51. theme_cache.font_size = get_theme_font_size(SNAME("font_size"));
  52. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  53. theme_cache.font_outline_size = get_theme_constant(SNAME("outline_size"));
  54. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  55. }
  56. void ProgressBar::_notification(int p_what) {
  57. switch (p_what) {
  58. case NOTIFICATION_DRAW: {
  59. draw_style_box(theme_cache.background_style, Rect2(Point2(), get_size()));
  60. float r = get_as_ratio();
  61. switch (mode) {
  62. case FILL_BEGIN_TO_END:
  63. case FILL_END_TO_BEGIN: {
  64. int mp = theme_cache.fill_style->get_minimum_size().width;
  65. int p = round(r * (get_size().width - mp));
  66. // We want FILL_BEGIN_TO_END to map to right to left when UI layout is RTL,
  67. // and left to right otherwise. And likewise for FILL_END_TO_BEGIN.
  68. bool right_to_left = is_layout_rtl() ? (mode == FILL_BEGIN_TO_END) : (mode == FILL_END_TO_BEGIN);
  69. if (p > 0) {
  70. if (right_to_left) {
  71. int p_remaining = round((1.0 - r) * (get_size().width - mp));
  72. 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)));
  73. } else {
  74. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(p + theme_cache.fill_style->get_minimum_size().width, get_size().height)));
  75. }
  76. }
  77. } break;
  78. case FILL_TOP_TO_BOTTOM:
  79. case FILL_BOTTOM_TO_TOP: {
  80. int mp = theme_cache.fill_style->get_minimum_size().height;
  81. int p = round(r * (get_size().height - mp));
  82. if (p > 0) {
  83. if (mode == FILL_TOP_TO_BOTTOM) {
  84. draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(get_size().width, p + theme_cache.fill_style->get_minimum_size().height)));
  85. } else {
  86. int p_remaining = round((1.0 - r) * (get_size().height - mp));
  87. 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)));
  88. }
  89. }
  90. } break;
  91. case FILL_MODE_MAX:
  92. break;
  93. }
  94. if (show_percentage) {
  95. String txt = itos(int(get_as_ratio() * 100));
  96. if (is_localizing_numeral_system()) {
  97. txt = TS->format_number(txt) + TS->percent_sign();
  98. } else {
  99. txt += String("%");
  100. }
  101. TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size);
  102. Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round();
  103. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  104. tl.draw_outline(get_canvas_item(), text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  105. }
  106. tl.draw(get_canvas_item(), text_pos, theme_cache.font_color);
  107. }
  108. } break;
  109. }
  110. }
  111. void ProgressBar::set_fill_mode(int p_fill) {
  112. ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
  113. mode = (FillMode)p_fill;
  114. queue_redraw();
  115. }
  116. int ProgressBar::get_fill_mode() {
  117. return mode;
  118. }
  119. void ProgressBar::set_show_percentage(bool p_visible) {
  120. if (show_percentage == p_visible) {
  121. return;
  122. }
  123. show_percentage = p_visible;
  124. update_minimum_size();
  125. queue_redraw();
  126. }
  127. bool ProgressBar::is_percentage_shown() const {
  128. return show_percentage;
  129. }
  130. void ProgressBar::_bind_methods() {
  131. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &ProgressBar::set_fill_mode);
  132. ClassDB::bind_method(D_METHOD("get_fill_mode"), &ProgressBar::get_fill_mode);
  133. ClassDB::bind_method(D_METHOD("set_show_percentage", "visible"), &ProgressBar::set_show_percentage);
  134. ClassDB::bind_method(D_METHOD("is_percentage_shown"), &ProgressBar::is_percentage_shown);
  135. 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");
  136. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_percentage"), "set_show_percentage", "is_percentage_shown");
  137. BIND_ENUM_CONSTANT(FILL_BEGIN_TO_END);
  138. BIND_ENUM_CONSTANT(FILL_END_TO_BEGIN);
  139. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  140. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  141. }
  142. ProgressBar::ProgressBar() {
  143. set_v_size_flags(0);
  144. set_step(0.01);
  145. }