From b75efcee950d32586deceff14f6132f30004dac1 Mon Sep 17 00:00:00 2001 From: shahriarlabib000 Date: Sat, 28 Sep 2024 12:05:17 +0600 Subject: [PATCH] Fix Spinbox display does not round properly when using decimal custom arrow steps --- scene/gui/spin_box.cpp | 35 +++++++++++++++++++++++++++++------ scene/gui/spin_box.h | 1 + 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index 01c2b9bffe1..6a6eb7ebdd4 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -41,7 +41,13 @@ Size2 SpinBox::get_minimum_size() const { } void SpinBox::_update_text(bool p_keep_line_edit) { - String value = String::num(get_value(), Math::range_step_decimals(get_step())); + double step = 0.0; + if (get_step() != 0.0 && ((get_custom_arrow_step() == 0.0) != (get_step() < get_custom_arrow_step()))) { + step = get_step(); + } else { + step = get_custom_arrow_step(); + } + String value = String::num(get_value(), Math::range_step_decimals(step)); if (is_localizing_numeral_system()) { value = TS->format_number(value); } @@ -114,8 +120,14 @@ void SpinBox::_line_edit_input(const Ref &p_event) { void SpinBox::_range_click_timeout() { if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) { bool up = get_local_mouse_position().y < (get_size().height / 2); - double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step(); - set_value(get_value() + (up ? step : -step)); + bool original_do_round = is_using_rounded_values(); + double step = get_step(); + double temp_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step(); + _rounded_values = false; + _set_step_no_signal(temp_step); + set_value(get_value() + (up ? temp_step : -temp_step)); + _set_step_no_signal(step); + _rounded_values = original_do_round; if (range_click_timer->is_one_shot()) { range_click_timer->set_wait_time(0.075); @@ -156,8 +168,7 @@ void SpinBox::gui_input(const Ref &p_event) { Ref mb = p_event; Ref mm = p_event; - double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step(); - + double step = get_step(); Vector2 mpos; bool mouse_on_up_button = false; bool mouse_on_down_button = false; @@ -177,7 +188,13 @@ void SpinBox::gui_input(const Ref &p_event) { line_edit->grab_focus(); if (mouse_on_up_button || mouse_on_down_button) { - set_value(get_value() + (mouse_on_up_button ? step : -step)); + bool original_do_round = is_using_rounded_values(); + double temp_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step(); + _rounded_values = false; + _set_step_no_signal(temp_step); + set_value(get_value() + (mouse_on_up_button ? temp_step : -temp_step)); + _rounded_values = original_do_round; + _set_step_no_signal(step); } state_cache.up_button_pressed = mouse_on_up_button; state_cache.down_button_pressed = mouse_on_down_button; @@ -519,6 +536,12 @@ void SpinBox::_update_buttons_state_for_current_value() { } } +void SpinBox::_set_step_no_signal(double p_step) { + set_block_signals(true); + set_step(p_step); + set_block_signals(false); +} + void SpinBox::_bind_methods() { ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &SpinBox::set_horizontal_alignment); ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &SpinBox::get_horizontal_alignment); diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.h index 294dc3e5d5c..1c084a77716 100644 --- a/scene/gui/spin_box.h +++ b/scene/gui/spin_box.h @@ -133,6 +133,7 @@ class SpinBox : public Range { void _mouse_exited(); void _update_buttons_state_for_current_value(); + void _set_step_no_signal(double p_step); protected: virtual void gui_input(const Ref &p_event) override;