Skip to content

Commit 8702b38

Browse files
committed
Merge pull request #108196 from beicause/spinbox-custom-arrow-step-snap
SpinBox: Fix `custom_arrow_step` by snapping it to `step`
2 parents 7826b6b + 453f4f1 commit 8702b38

3 files changed

Lines changed: 4 additions & 10 deletions

File tree

doc/classes/SpinBox.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
Changes the alignment of the underlying [LineEdit].
5151
</member>
5252
<member name="custom_arrow_step" type="float" setter="set_custom_arrow_step" getter="get_custom_arrow_step" default="0.0">
53-
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].
53+
If not [code]0[/code], sets the step when interacting with the arrow buttons of the [SpinBox].
54+
[b]Note:[/b] [member Range.value] will still be rounded to a multiple of [member step].
5455
</member>
5556
<member name="editable" type="bool" setter="set_editable" getter="is_editable" default="true" keywords="readonly, disabled, enabled">
5657
If [code]true[/code], the [SpinBox] will be editable. Otherwise, it will be read only.

scene/gui/spin_box.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ void SpinBox::_text_submitted(const String &p_string) {
144144

145145
Error err = expr->parse(text);
146146

147-
use_custom_arrow_step = false;
148-
149147
if (err != OK) {
150148
// If the expression failed try without converting commas to dots - they might have been for parameter separation.
151149
text = p_string;
@@ -195,12 +193,12 @@ void SpinBox::_range_click_timeout() {
195193
bool mouse_on_up_button = get_local_mouse_position().y < (get_size().height / 2);
196194
// Arrow button is being pressed. Snap the value to next step.
197195
double temp_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
196+
temp_step = Math::snapped(temp_step, get_step());
198197
double new_value = _calc_value(get_value(), temp_step);
199198
if ((mouse_on_up_button && new_value <= get_value() + CMP_EPSILON) || (!mouse_on_up_button && new_value >= get_value() - CMP_EPSILON)) {
200199
new_value = _calc_value(get_value() + (mouse_on_up_button ? temp_step : -temp_step), temp_step);
201200
}
202201
set_value(new_value);
203-
use_custom_arrow_step = true;
204202

205203
if (range_click_timer->is_one_shot()) {
206204
range_click_timer->set_wait_time(0.075);
@@ -264,12 +262,12 @@ void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
264262
if (mouse_on_up_button || mouse_on_down_button) {
265263
// Arrow button is being pressed. Snap the value to next step.
266264
double temp_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
265+
temp_step = Math::snapped(temp_step, get_step());
267266
double new_value = _calc_value(get_value(), temp_step);
268267
if ((mouse_on_up_button && new_value <= get_value() + CMP_EPSILON) || (!mouse_on_up_button && new_value >= get_value() - CMP_EPSILON)) {
269268
new_value = _calc_value(get_value() + (mouse_on_up_button ? temp_step : -temp_step), temp_step);
270269
}
271270
set_value(new_value);
272-
use_custom_arrow_step = true;
273271
}
274272
state_cache.up_button_pressed = mouse_on_up_button;
275273
state_cache.down_button_pressed = mouse_on_down_button;
@@ -285,20 +283,17 @@ void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
285283
case MouseButton::RIGHT: {
286284
line_edit->grab_focus();
287285
if (mouse_on_up_button || mouse_on_down_button) {
288-
use_custom_arrow_step = false;
289286
set_value(mouse_on_up_button ? get_max() : get_min());
290287
}
291288
} break;
292289
case MouseButton::WHEEL_UP: {
293290
if (line_edit->is_editing()) {
294-
use_custom_arrow_step = false;
295291
set_value(get_value() + step * mb->get_factor());
296292
accept_event();
297293
}
298294
} break;
299295
case MouseButton::WHEEL_DOWN: {
300296
if (line_edit->is_editing()) {
301-
use_custom_arrow_step = false;
302297
set_value(get_value() - step * mb->get_factor());
303298
accept_event();
304299
}
@@ -338,7 +333,6 @@ void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
338333
if (drag.enabled) {
339334
drag.diff_y += mm->get_relative().y;
340335
double diff_y = -0.01 * Math::pow(Math::abs(drag.diff_y), 1.8) * SIGN(drag.diff_y);
341-
use_custom_arrow_step = false;
342336
set_value(CLAMP(drag.base_val + step * diff_y, get_min(), get_max()));
343337
} else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
344338
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);

scene/gui/spin_box.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class SpinBox : public Range {
7676
String suffix;
7777
String last_text_value;
7878
double custom_arrow_step = 0.0;
79-
bool use_custom_arrow_step = false;
8079

8180
void _line_edit_input(const Ref<InputEvent> &p_event);
8281

0 commit comments

Comments
 (0)