progress_bar.cpp 7.3 KB

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