Merge pull request #108196 from beicause/spinbox-custom-arrow-step-snap

SpinBox: Fix `custom_arrow_step` by snapping it to `step`
This commit is contained in:
Thaddeus Crews
2025-07-17 10:34:43 -05:00
3 changed files with 4 additions and 10 deletions

View File

@ -50,7 +50,8 @@
Changes the alignment of the underlying [LineEdit].
</member>
<member name="custom_arrow_step" type="float" setter="set_custom_arrow_step" getter="get_custom_arrow_step" default="0.0">
If not [code]0[/code], [member Range.value] will always be rounded to a multiple of [member custom_arrow_step] when interacting with the arrow buttons of the [SpinBox].
If not [code]0[/code], sets the step when interacting with the arrow buttons of the [SpinBox].
[b]Note:[/b] [member Range.value] will still be rounded to a multiple of [member step].
</member>
<member name="editable" type="bool" setter="set_editable" getter="is_editable" default="true" keywords="readonly, disabled, enabled">
If [code]true[/code], the [SpinBox] will be editable. Otherwise, it will be read only.

View File

@ -144,8 +144,6 @@ void SpinBox::_text_submitted(const String &p_string) {
Error err = expr->parse(text);
use_custom_arrow_step = false;
if (err != OK) {
// If the expression failed try without converting commas to dots - they might have been for parameter separation.
text = p_string;
@ -195,12 +193,12 @@ void SpinBox::_range_click_timeout() {
bool mouse_on_up_button = get_local_mouse_position().y < (get_size().height / 2);
// Arrow button is being pressed. Snap the value to next step.
double temp_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
temp_step = Math::snapped(temp_step, get_step());
double new_value = _calc_value(get_value(), temp_step);
if ((mouse_on_up_button && new_value <= get_value() + CMP_EPSILON) || (!mouse_on_up_button && new_value >= get_value() - CMP_EPSILON)) {
new_value = _calc_value(get_value() + (mouse_on_up_button ? temp_step : -temp_step), temp_step);
}
set_value(new_value);
use_custom_arrow_step = true;
if (range_click_timer->is_one_shot()) {
range_click_timer->set_wait_time(0.075);
@ -264,12 +262,12 @@ void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
if (mouse_on_up_button || mouse_on_down_button) {
// Arrow button is being pressed. Snap the value to next step.
double temp_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
temp_step = Math::snapped(temp_step, get_step());
double new_value = _calc_value(get_value(), temp_step);
if ((mouse_on_up_button && new_value <= get_value() + CMP_EPSILON) || (!mouse_on_up_button && new_value >= get_value() - CMP_EPSILON)) {
new_value = _calc_value(get_value() + (mouse_on_up_button ? temp_step : -temp_step), temp_step);
}
set_value(new_value);
use_custom_arrow_step = true;
}
state_cache.up_button_pressed = mouse_on_up_button;
state_cache.down_button_pressed = mouse_on_down_button;
@ -285,20 +283,17 @@ void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
case MouseButton::RIGHT: {
line_edit->grab_focus();
if (mouse_on_up_button || mouse_on_down_button) {
use_custom_arrow_step = false;
set_value(mouse_on_up_button ? get_max() : get_min());
}
} break;
case MouseButton::WHEEL_UP: {
if (line_edit->is_editing()) {
use_custom_arrow_step = false;
set_value(get_value() + step * mb->get_factor());
accept_event();
}
} break;
case MouseButton::WHEEL_DOWN: {
if (line_edit->is_editing()) {
use_custom_arrow_step = false;
set_value(get_value() - step * mb->get_factor());
accept_event();
}
@ -338,7 +333,6 @@ void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
if (drag.enabled) {
drag.diff_y += mm->get_relative().y;
double diff_y = -0.01 * Math::pow(Math::abs(drag.diff_y), 1.8) * SIGN(drag.diff_y);
use_custom_arrow_step = false;
set_value(CLAMP(drag.base_val + step * diff_y, get_min(), get_max()));
} else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);

View File

@ -76,7 +76,6 @@ class SpinBox : public Range {
String suffix;
String last_text_value;
double custom_arrow_step = 0.0;
bool use_custom_arrow_step = false;
void _line_edit_input(const Ref<InputEvent> &p_event);