diff options
author | Yuri Sizov <yuris@humnom.net> | 2023-12-20 15:07:30 +0100 |
---|---|---|
committer | Yuri Sizov <yuris@humnom.net> | 2023-12-20 15:07:30 +0100 |
commit | 3a44484ab86c7639ee558601dd858caae94c5d9e (patch) | |
tree | 3d8718b93a2f39d0c561ac795c209b150de6a097 | |
parent | bc6be82f5ac37d298e817fc880bec39567f1270a (diff) | |
parent | beeca2ae8241859702ad36f6ad51e3d5bcaeadec (diff) | |
download | redot-engine-3a44484ab86c7639ee558601dd858caae94c5d9e.tar.gz |
Merge pull request #83623 from rarysson/label-progressbar-allow-greater
Make ProgressBar reflect the real value in the percent label when "allow greater" is checked
-rw-r--r-- | scene/gui/progress_bar.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/scene/gui/progress_bar.cpp b/scene/gui/progress_bar.cpp index b45319422c..b86309353b 100644 --- a/scene/gui/progress_bar.cpp +++ b/scene/gui/progress_bar.cpp @@ -91,12 +91,32 @@ void ProgressBar::_notification(int p_what) { } if (show_percentage) { - String txt = itos(int(get_as_ratio() * 100)); + double ratio = 0; + + // Avoid division by zero. + if (Math::is_equal_approx(get_max(), get_min())) { + ratio = 1; + } else if (is_ratio_exp() && get_min() >= 0 && get_value() >= 0) { + double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2); + double exp_max = Math::log(get_max()) / Math::log((double)2); + double exp_value = get_value() == 0 ? 0.0 : Math::log(get_value()) / Math::log((double)2); + double percentage = (exp_value - exp_min) / (exp_max - exp_min); + + ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1); + } else { + double percentage = (get_value() - get_min()) / (get_max() - get_min()); + + ratio = CLAMP(percentage, is_lesser_allowed() ? percentage : 0, is_greater_allowed() ? percentage : 1); + } + + String txt = itos(int(ratio * 100)); + if (is_localizing_numeral_system()) { txt = TS->format_number(txt) + TS->percent_sign(); } else { txt += String("%"); } + TextLine tl = TextLine(txt, theme_cache.font, theme_cache.font_size); Vector2 text_pos = (Point2(get_size().width - tl.get_size().x, get_size().height - tl.get_size().y) / 2).round(); |