progress_bar.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************************/
  2. /* progress_bar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "progress_bar.h"
  31. #include "scene/resources/text_line.h"
  32. Size2 ProgressBar::get_minimum_size() const {
  33. Ref<StyleBox> bg = get_theme_stylebox(SNAME("bg"));
  34. Ref<StyleBox> fg = get_theme_stylebox(SNAME("fg"));
  35. Ref<Font> font = get_theme_font(SNAME("font"));
  36. int font_size = get_theme_font_size(SNAME("font_size"));
  37. Size2 minimum_size = bg->get_minimum_size();
  38. minimum_size.height = MAX(minimum_size.height, fg->get_minimum_size().height);
  39. minimum_size.width = MAX(minimum_size.width, fg->get_minimum_size().width);
  40. if (percent_visible) {
  41. String txt = "100%";
  42. TextLine tl = TextLine(txt, font, font_size);
  43. minimum_size.height = MAX(minimum_size.height, bg->get_minimum_size().height + tl.get_size().y);
  44. } else { // this is needed, else the progressbar will collapse
  45. minimum_size.width = MAX(minimum_size.width, 1);
  46. minimum_size.height = MAX(minimum_size.height, 1);
  47. }
  48. return minimum_size;
  49. }
  50. void ProgressBar::_notification(int p_what) {
  51. switch (p_what) {
  52. case NOTIFICATION_DRAW: {
  53. Ref<StyleBox> bg = get_theme_stylebox(SNAME("bg"));
  54. Ref<StyleBox> fg = get_theme_stylebox(SNAME("fg"));
  55. Ref<Font> font = get_theme_font(SNAME("font"));
  56. int font_size = get_theme_font_size(SNAME("font_size"));
  57. Color font_color = get_theme_color(SNAME("font_color"));
  58. draw_style_box(bg, Rect2(Point2(), get_size()));
  59. float r = get_as_ratio();
  60. switch (mode) {
  61. case FILL_BEGIN_TO_END:
  62. case FILL_END_TO_BEGIN: {
  63. int mp = fg->get_minimum_size().width;
  64. int p = round(r * (get_size().width - mp));
  65. // We want FILL_BEGIN_TO_END to map to right to left when UI layout is RTL,
  66. // and left to right otherwise. And likewise for FILL_END_TO_BEGIN.
  67. bool right_to_left = is_layout_rtl() ? (mode == FILL_BEGIN_TO_END) : (mode == FILL_END_TO_BEGIN);
  68. if (p > 0) {
  69. if (right_to_left) {
  70. int p_remaining = round((1.0 - r) * (get_size().width - mp));
  71. draw_style_box(fg, Rect2(Point2(p_remaining, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
  72. } else {
  73. draw_style_box(fg, Rect2(Point2(0, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
  74. }
  75. }
  76. } break;
  77. case FILL_TOP_TO_BOTTOM:
  78. case FILL_BOTTOM_TO_TOP: {
  79. int mp = fg->get_minimum_size().height;
  80. int p = round(r * (get_size().height - mp));
  81. if (p > 0) {
  82. if (mode == FILL_TOP_TO_BOTTOM) {
  83. draw_style_box(fg, Rect2(Point2(0, 0), Size2(get_size().width, p + fg->get_minimum_size().height)));
  84. } else {
  85. int p_remaining = round((1.0 - r) * (get_size().height - mp));
  86. draw_style_box(fg, Rect2(Point2(0, p_remaining), Size2(get_size().width, p + fg->get_minimum_size().height)));
  87. }
  88. }
  89. } break;
  90. case FILL_MODE_MAX:
  91. break;
  92. }
  93. if (percent_visible) {
  94. String txt = TS->format_number(itos(int(get_as_ratio() * 100))) + TS->percent_sign();
  95. TextLine tl = TextLine(txt, font, font_size);
  96. Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round();
  97. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  98. int outline_size = get_theme_constant(SNAME("outline_size"));
  99. if (outline_size > 0 && font_outline_color.a > 0) {
  100. tl.draw_outline(get_canvas_item(), text_pos, outline_size, font_outline_color);
  101. }
  102. tl.draw(get_canvas_item(), text_pos, font_color);
  103. }
  104. } break;
  105. }
  106. }
  107. void ProgressBar::set_fill_mode(int p_fill) {
  108. ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
  109. mode = (FillMode)p_fill;
  110. queue_redraw();
  111. }
  112. int ProgressBar::get_fill_mode() {
  113. return mode;
  114. }
  115. void ProgressBar::set_percent_visible(bool p_visible) {
  116. if (percent_visible == p_visible) {
  117. return;
  118. }
  119. percent_visible = p_visible;
  120. update_minimum_size();
  121. queue_redraw();
  122. }
  123. bool ProgressBar::is_percent_visible() const {
  124. return percent_visible;
  125. }
  126. void ProgressBar::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &ProgressBar::set_fill_mode);
  128. ClassDB::bind_method(D_METHOD("get_fill_mode"), &ProgressBar::get_fill_mode);
  129. ClassDB::bind_method(D_METHOD("set_percent_visible", "visible"), &ProgressBar::set_percent_visible);
  130. ClassDB::bind_method(D_METHOD("is_percent_visible"), &ProgressBar::is_percent_visible);
  131. 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");
  132. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "percent_visible"), "set_percent_visible", "is_percent_visible");
  133. BIND_ENUM_CONSTANT(FILL_BEGIN_TO_END);
  134. BIND_ENUM_CONSTANT(FILL_END_TO_BEGIN);
  135. BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
  136. BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
  137. }
  138. ProgressBar::ProgressBar() {
  139. set_v_size_flags(0);
  140. set_step(0.01);
  141. }