Skip to content

Commit 3c1ac98

Browse files
committed
Allow overriding SpinBox value on focus_exited
1 parent 89001f9 commit 3c1ac98

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

scene/gui/spin_box.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Size2 SpinBox::get_minimum_size() const {
4040
return ms;
4141
}
4242

43-
void SpinBox::_update_text(bool p_keep_line_edit) {
43+
void SpinBox::_update_text(bool p_only_update_if_value_changed) {
4444
double step = get_step();
4545
if (use_custom_arrow_step && custom_arrow_step != 0.0) {
4646
step = custom_arrow_step;
@@ -50,6 +50,11 @@ void SpinBox::_update_text(bool p_keep_line_edit) {
5050
value = TS->format_number(value);
5151
}
5252

53+
if (p_only_update_if_value_changed && value == last_text_value) {
54+
return;
55+
}
56+
last_text_value = value;
57+
5358
if (!line_edit->is_editing()) {
5459
if (!prefix.is_empty()) {
5560
value = prefix + " " + value;
@@ -58,17 +63,12 @@ void SpinBox::_update_text(bool p_keep_line_edit) {
5863
value += " " + suffix;
5964
}
6065
}
61-
62-
if (p_keep_line_edit && value == last_updated_text && value != line_edit->get_text()) {
63-
return;
64-
}
65-
6666
line_edit->set_text_with_selection(value);
67-
last_updated_text = value;
6867
}
6968

7069
void SpinBox::_text_submitted(const String &p_string) {
7170
if (p_string.is_empty()) {
71+
_update_text();
7272
return;
7373
}
7474

@@ -288,14 +288,12 @@ void SpinBox::_line_edit_editing_toggled(bool p_toggled_on) {
288288
line_edit->select_all();
289289
}
290290
} else {
291-
// Discontinue because the focus_exit was caused by canceling or the text is empty.
292291
if (Input::get_singleton()->is_action_pressed("ui_cancel") || line_edit->get_text().is_empty()) {
293-
_update_text();
294-
return;
292+
_update_text(); // Revert text if editing was canceled.
293+
} else {
294+
_update_text(true); // Update text in case value was changed this frame (e.g. on `focus_exited`).
295+
_text_submitted(line_edit->get_text());
295296
}
296-
297-
line_edit->deselect();
298-
_text_submitted(line_edit->get_text());
299297
}
300298
}
301299

scene/gui/spin_box.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ class SpinBox : public Range {
5858
void _range_click_timeout();
5959
void _release_mouse_from_drag_mode();
6060

61-
void _update_text(bool p_keep_line_edit = false);
61+
void _update_text(bool p_only_update_if_value_changed = false);
6262
void _text_submitted(const String &p_string);
6363
void _text_changed(const String &p_string);
6464

6565
String prefix;
6666
String suffix;
67-
String last_updated_text;
67+
String last_text_value;
6868
double custom_arrow_step = 0.0;
6969
bool use_custom_arrow_step = false;
7070

0 commit comments

Comments
 (0)