progress_bar.cpp 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Size2 ProgressBar::get_minimum_size() const {
  32. Ref<StyleBox> bg = get_stylebox("bg");
  33. Ref<StyleBox> fg = get_stylebox("fg");
  34. Ref<Font> font = get_font("font");
  35. Size2 minimum_size = bg->get_minimum_size();
  36. minimum_size.height = MAX(minimum_size.height, fg->get_minimum_size().height);
  37. minimum_size.width = MAX(minimum_size.width, fg->get_minimum_size().width);
  38. if (percent_visible) {
  39. minimum_size.height = MAX(minimum_size.height, bg->get_minimum_size().height + font->get_height());
  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::_notification(int p_what) {
  47. if (p_what == NOTIFICATION_DRAW) {
  48. Ref<StyleBox> bg = get_stylebox("bg");
  49. Ref<StyleBox> fg = get_stylebox("fg");
  50. Ref<Font> font = get_font("font");
  51. select_font(font);
  52. Color font_color = get_color("font_color");
  53. draw_style_box(bg, Rect2(Point2(), get_size()));
  54. float r = get_as_ratio();
  55. int mp = fg->get_minimum_size().width;
  56. int p = r * (get_size().width - mp);
  57. if (p > 0) {
  58. draw_style_box(fg, Rect2(Point2(), Size2(p + fg->get_minimum_size().width, get_size().height)));
  59. }
  60. if (percent_visible) {
  61. String txt = itos(int(get_as_ratio() * 100)) + "%";
  62. font->draw_halign(get_canvas_item(), Point2(0, font->get_ascent() + (get_size().height - font->get_height()) / 2), HALIGN_CENTER, get_size().width, txt, font_color);
  63. }
  64. }
  65. }
  66. void ProgressBar::set_percent_visible(bool p_visible) {
  67. percent_visible = p_visible;
  68. update();
  69. }
  70. bool ProgressBar::is_percent_visible() const {
  71. return percent_visible;
  72. }
  73. void ProgressBar::_bind_methods() {
  74. ClassDB::bind_method(D_METHOD("set_percent_visible", "visible"), &ProgressBar::set_percent_visible);
  75. ClassDB::bind_method(D_METHOD("is_percent_visible"), &ProgressBar::is_percent_visible);
  76. ADD_GROUP("Percent", "percent_");
  77. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "percent_visible"), "set_percent_visible", "is_percent_visible");
  78. }
  79. ProgressBar::ProgressBar() {
  80. set_v_size_flags(0);
  81. set_step(0.01);
  82. percent_visible = true;
  83. }