| 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 | |
| 31 | #include "progress_bar.h" |
| 32 | |
| 33 | #include "core/string/translation_server.h" |
| 34 | #include "scene/resources/text_line.h" |
| 35 | #include "scene/theme/theme_db.h" |
| 36 | |
| 37 | Size2 ProgressBar::get_minimum_size() const { |
| 38 | Size2 minimum_size = theme_cache.background_style->get_minimum_size(); |
| 39 | minimum_size = minimum_size.max(theme_cache.fill_style->get_minimum_size()); |
| 40 | if (show_percentage) { |
| 41 | String txt = "100%"; |
| 42 | TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size); |
| 43 | minimum_size.height = MAX(minimum_size.height, theme_cache.background_style->get_minimum_size().height + tl.get_size().y); |
| 44 | } else { // this is needed, else the progressbar will collapse |
| 45 | minimum_size = minimum_size.maxf(1); |
| 46 | } |
| 47 | return minimum_size; |
| 48 | } |
| 49 | |
| 50 | void ProgressBar::_notification(int p_what) { |
| 51 | switch (p_what) { |
| 52 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 53 | if (is_visible_in_tree()) { |
| 54 | _indeterminate_fill_progress += get_process_delta_time() * MAX(indeterminate_min_speed, MAX(get_size().width, get_size().height) / 2); |
| 55 | queue_redraw(); |
| 56 | } |
| 57 | } break; |
| 58 | |
| 59 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 60 | RID ae = get_accessibility_element(); |
| 61 | ERR_FAIL_COND(ae.is_null()); |
| 62 | |
| 63 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_PROGRESS_INDICATOR); |
| 64 | } break; |
| 65 | |
| 66 | case NOTIFICATION_DRAW: { |
| 67 | draw_style_box(theme_cache.background_style, Rect2(Point2(), get_size())); |
| 68 | |
| 69 | if (indeterminate) { |
| 70 | Size2 size = get_size(); |
| 71 | real_t fill_size = MIN(size.width, size.height) * 2; |
| 72 | |
| 73 | if (is_part_of_edited_scene() && !editor_preview_indeterminate) { |
| 74 | // Center the filled bar when we're not previewing the animation. |
| 75 | _indeterminate_fill_progress = (MAX(size.width, size.height) / 2) + (fill_size / 2); |
| 76 | } |
| 77 | |
| 78 | switch (mode) { |
| 79 | case FILL_END_TO_BEGIN: |
| 80 | case FILL_BEGIN_TO_END: { |
| 81 | // Follow the RTL layout with the animation to match how the bar would fill. |
| 82 | bool right_to_left = mode == (is_layout_rtl() ? FILL_BEGIN_TO_END : FILL_END_TO_BEGIN); |
| 83 | |
| 84 | if (_indeterminate_fill_progress > size.width + fill_size) { |
| 85 | _indeterminate_fill_progress = right_to_left ? -fill_size : 0; |
| 86 | } |
| 87 | |
| 88 | real_t x = right_to_left ? size.width - _indeterminate_fill_progress : _indeterminate_fill_progress - fill_size; |
| 89 | draw_style_box(theme_cache.fill_style, Rect2(x, 0, fill_size, size.height).intersection(Rect2(Point2(), size))); |
| 90 | } break; |
| 91 | case FILL_TOP_TO_BOTTOM: { |
| 92 | if (_indeterminate_fill_progress > size.height + fill_size) { |
| 93 | _indeterminate_fill_progress = 0; |
| 94 | } |
| 95 | |
| 96 | draw_style_box(theme_cache.fill_style, Rect2(0, _indeterminate_fill_progress - fill_size, size.width, fill_size).intersection(Rect2(Point2(), size))); |
| 97 | } break; |
| 98 | case FILL_BOTTOM_TO_TOP: { |
| 99 | if (_indeterminate_fill_progress > size.height + fill_size) { |
| 100 | _indeterminate_fill_progress = -fill_size; |
| 101 | } |
| 102 | |
| 103 | draw_style_box(theme_cache.fill_style, Rect2(0, size.height - _indeterminate_fill_progress, size.width, fill_size).intersection(Rect2(Point2(), size))); |
| 104 | } break; |
| 105 | case FILL_MODE_MAX: |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | float r = get_as_ratio(); |
| 113 | |
| 114 | switch (mode) { |
| 115 | case FILL_BEGIN_TO_END: |
| 116 | case FILL_END_TO_BEGIN: { |
| 117 | int mp = theme_cache.fill_style->get_minimum_size().width; |
| 118 | int p = std::round(r * (get_size().width - mp)); |
| 119 | // We want FILL_BEGIN_TO_END to map to right to left when UI layout is RTL, |
| 120 | // and left to right otherwise. And likewise for FILL_END_TO_BEGIN. |
| 121 | bool right_to_left = mode == (is_layout_rtl() ? FILL_BEGIN_TO_END : FILL_END_TO_BEGIN); |
| 122 | if (p > 0) { |
| 123 | if (right_to_left) { |
| 124 | int p_remaining = std::round((1.0 - r) * (get_size().width - mp)); |
| 125 | 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))); |
| 126 | } else { |
| 127 | draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(p + theme_cache.fill_style->get_minimum_size().width, get_size().height))); |
| 128 | } |
| 129 | } |
| 130 | } break; |
| 131 | case FILL_TOP_TO_BOTTOM: |
| 132 | case FILL_BOTTOM_TO_TOP: { |
| 133 | int mp = theme_cache.fill_style->get_minimum_size().height; |
| 134 | int p = std::round(r * (get_size().height - mp)); |
| 135 | |
| 136 | if (p > 0) { |
| 137 | if (mode == FILL_TOP_TO_BOTTOM) { |
| 138 | draw_style_box(theme_cache.fill_style, Rect2(Point2(0, 0), Size2(get_size().width, p + theme_cache.fill_style->get_minimum_size().height))); |
| 139 | } else { |
| 140 | int p_remaining = std::round((1.0 - r) * (get_size().height - mp)); |
| 141 | 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))); |
| 142 | } |
| 143 | } |
| 144 | } break; |
| 145 | case FILL_MODE_MAX: |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (show_percentage) { |
| 150 | double ratio = 0; |
| 151 | |
| 152 | // Avoid division by zero. |
| 153 | if (Math::is_equal_approx(get_max(), get_min())) { |
| 154 | ratio = 1; |
| 155 | } else if (is_ratio_exp() && get_min() >= 0 && get_value() >= 0) { |
| 156 | double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2); |
| 157 | double exp_max = Math::log(get_max()) / Math::log((double)2); |
| 158 | double exp_value = get_value() == 0 ? 0.0 : Math::log(get_value()) / Math::log((double)2); |
| 159 | double percentage = (exp_value - exp_min) / (exp_max - exp_min); |
| 160 | |
| 161 | ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1); |
| 162 | } else { |
| 163 | double percentage = (get_value() - get_min()) / (get_max() - get_min()); |
| 164 | |
| 165 | ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1); |
| 166 | } |
| 167 | |
| 168 | String txt = itos(int(Math::round(ratio * 100))); |
| 169 | |
| 170 | if (is_localizing_numeral_system()) { |
| 171 | const String &lang = _get_locale(); |
| 172 | txt = TranslationServer::get_singleton()->format_number(txt, lang) + TranslationServer::get_singleton()->get_percent_sign(lang); |
| 173 | } else { |
| 174 | txt += String("%"); |
| 175 | } |
| 176 | |
| 177 | TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size); |
| 178 | Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round(); |
| 179 | |
| 180 | if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) { |
| 181 | tl.draw_outline(get_canvas_item(), text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color); |
| 182 | } |
| 183 | |
| 184 | tl.draw(get_canvas_item(), text_pos, theme_cache.font_color); |
| 185 | } |
| 186 | } break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void ProgressBar::_validate_property(PropertyInfo &p_property) const { |
| 191 | if (Engine::get_singleton()->is_editor_hint() && indeterminate && p_property.name == "show_percentage") { |
| 192 | p_property.usage |= PROPERTY_USAGE_READ_ONLY; |
| 193 | } |
| 194 | if (!indeterminate && p_property.name == "editor_preview_indeterminate") { |
| 195 | p_property.usage = PROPERTY_USAGE_NONE; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void ProgressBar::set_fill_mode(int p_fill) { |
| 200 | ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX); |
| 201 | mode = (FillMode)p_fill; |
| 202 | _indeterminate_fill_progress = 0; |
| 203 | queue_redraw(); |
| 204 | } |
| 205 | |
| 206 | int ProgressBar::get_fill_mode() { |
| 207 | return mode; |
| 208 | } |
| 209 | |
| 210 | void ProgressBar::set_show_percentage(bool p_visible) { |
| 211 | if (show_percentage == p_visible) { |
| 212 | return; |
| 213 | } |
| 214 | show_percentage = p_visible; |
| 215 | update_minimum_size(); |
| 216 | queue_redraw(); |
| 217 | } |
| 218 | |
| 219 | bool ProgressBar::is_percentage_shown() const { |
| 220 | return show_percentage; |
| 221 | } |
| 222 | |
| 223 | void ProgressBar::set_indeterminate(bool p_indeterminate) { |
| 224 | if (indeterminate == p_indeterminate) { |
| 225 | return; |
| 226 | } |
| 227 | indeterminate = p_indeterminate; |
| 228 | _indeterminate_fill_progress = 0; |
| 229 | |
| 230 | bool should_process = !is_part_of_edited_scene() || editor_preview_indeterminate; |
| 231 | set_process_internal(indeterminate && should_process); |
| 232 | |
| 233 | notify_property_list_changed(); |
| 234 | update_minimum_size(); |
| 235 | queue_redraw(); |
| 236 | } |
| 237 | |
| 238 | bool ProgressBar::is_indeterminate() const { |
| 239 | return indeterminate; |
| 240 | } |
| 241 | |
| 242 | void ProgressBar::set_editor_preview_indeterminate(bool p_preview_indeterminate) { |
| 243 | if (editor_preview_indeterminate == p_preview_indeterminate) { |
| 244 | return; |
| 245 | } |
| 246 | editor_preview_indeterminate = p_preview_indeterminate; |
| 247 | |
| 248 | if (is_part_of_edited_scene()) { |
| 249 | _indeterminate_fill_progress = 0; |
| 250 | set_process_internal(indeterminate && editor_preview_indeterminate); |
| 251 | queue_redraw(); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | bool ProgressBar::is_editor_preview_indeterminate_enabled() const { |
| 256 | return editor_preview_indeterminate; |
| 257 | } |
| 258 | |
| 259 | void ProgressBar::_bind_methods() { |
| 260 | ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &ProgressBar::set_fill_mode); |
| 261 | ClassDB::bind_method(D_METHOD("get_fill_mode"), &ProgressBar::get_fill_mode); |
| 262 | ClassDB::bind_method(D_METHOD("set_show_percentage", "visible"), &ProgressBar::set_show_percentage); |
| 263 | ClassDB::bind_method(D_METHOD("is_percentage_shown"), &ProgressBar::is_percentage_shown); |
| 264 | ClassDB::bind_method(D_METHOD("set_indeterminate", "indeterminate"), &ProgressBar::set_indeterminate); |
| 265 | ClassDB::bind_method(D_METHOD("is_indeterminate"), &ProgressBar::is_indeterminate); |
| 266 | ClassDB::bind_method(D_METHOD("set_editor_preview_indeterminate", "preview_indeterminate"), &ProgressBar::set_editor_preview_indeterminate); |
| 267 | ClassDB::bind_method(D_METHOD("is_editor_preview_indeterminate_enabled"), &ProgressBar::is_editor_preview_indeterminate_enabled); |
| 268 | |
| 269 | 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"); |
| 270 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_percentage"), "set_show_percentage", "is_percentage_shown"); |
| 271 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indeterminate"), "set_indeterminate", "is_indeterminate"); |
| 272 | ADD_GROUP("Editor", "editor_"); |
| 273 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_preview_indeterminate"), "set_editor_preview_indeterminate", "is_editor_preview_indeterminate_enabled"); |
| 274 | |
| 275 | BIND_ENUM_CONSTANT(FILL_BEGIN_TO_END); |
| 276 | BIND_ENUM_CONSTANT(FILL_END_TO_BEGIN); |
| 277 | BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM); |
| 278 | BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP); |
| 279 | |
| 280 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ProgressBar, background_style, "background"); |
| 281 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ProgressBar, fill_style, "fill"); |
| 282 | |
| 283 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, ProgressBar, font); |
| 284 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, ProgressBar, font_size); |
| 285 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ProgressBar, font_color); |
| 286 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ProgressBar, font_outline_size, "outline_size"); |
| 287 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ProgressBar, font_outline_color); |
| 288 | } |
| 289 | |
| 290 | ProgressBar::ProgressBar() { |
| 291 | set_v_size_flags(0); |
| 292 | set_step(0.01); |
| 293 | } |