progress_bar.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*************************************************************************/
  2. /* progress_bar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. if (p_what == NOTIFICATION_DRAW) {
  52. Ref<StyleBox> bg = get_theme_stylebox(SNAME("bg"));
  53. Ref<StyleBox> fg = get_theme_stylebox(SNAME("fg"));
  54. Ref<Font> font = get_theme_font(SNAME("font"));
  55. int font_size = get_theme_font_size(SNAME("font_size"));
  56. Color font_color = get_theme_color(SNAME("font_color"));
  57. draw_style_box(bg, Rect2(Point2(), get_size()));
  58. float r = get_as_ratio();
  59. int mp = fg->get_minimum_size().width;
  60. int p = r * (get_size().width - mp);
  61. if (p > 0) {
  62. if (is_layout_rtl()) {
  63. draw_style_box(fg, Rect2(Point2(p, 0), Size2(fg->get_minimum_size().width, get_size().height)));
  64. } else {
  65. draw_style_box(fg, Rect2(Point2(0, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
  66. }
  67. }
  68. if (percent_visible) {
  69. String txt = TS->format_number(itos(int(get_as_ratio() * 100))) + TS->percent_sign();
  70. TextLine tl = TextLine(txt, font, font_size);
  71. Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round();
  72. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  73. int outline_size = get_theme_constant(SNAME("outline_size"));
  74. if (outline_size > 0 && font_outline_color.a > 0) {
  75. tl.draw_outline(get_canvas_item(), text_pos, outline_size, font_outline_color);
  76. }
  77. tl.draw(get_canvas_item(), text_pos, font_color);
  78. }
  79. }
  80. }
  81. void ProgressBar::set_percent_visible(bool p_visible) {
  82. percent_visible = p_visible;
  83. update();
  84. }
  85. bool ProgressBar::is_percent_visible() const {
  86. return percent_visible;
  87. }
  88. void ProgressBar::_bind_methods() {
  89. ClassDB::bind_method(D_METHOD("set_percent_visible", "visible"), &ProgressBar::set_percent_visible);
  90. ClassDB::bind_method(D_METHOD("is_percent_visible"), &ProgressBar::is_percent_visible);
  91. ADD_GROUP("Percent", "percent_");
  92. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "percent_visible"), "set_percent_visible", "is_percent_visible");
  93. }
  94. ProgressBar::ProgressBar() {
  95. set_v_size_flags(0);
  96. set_step(0.01);
  97. }