Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding SpinBox value on focus_exited #100860

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions scene/gui/spin_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Size2 SpinBox::get_minimum_size() const {
return ms;
}

void SpinBox::_update_text(bool p_keep_line_edit) {
void SpinBox::_update_text(bool p_only_update_if_value_changed) {
double step = get_step();
if (use_custom_arrow_step && custom_arrow_step != 0.0) {
step = custom_arrow_step;
Expand All @@ -50,6 +50,11 @@ void SpinBox::_update_text(bool p_keep_line_edit) {
value = TS->format_number(value);
}

if (p_only_update_if_value_changed && value == last_text_value) {
return;
}
last_text_value = value;

if (!line_edit->is_editing()) {
if (!prefix.is_empty()) {
value = prefix + " " + value;
Expand All @@ -58,17 +63,12 @@ void SpinBox::_update_text(bool p_keep_line_edit) {
value += " " + suffix;
}
}

if (p_keep_line_edit && value == last_updated_text && value != line_edit->get_text()) {
return;
}

line_edit->set_text_with_selection(value);
last_updated_text = value;
}

void SpinBox::_text_submitted(const String &p_string) {
if (p_string.is_empty()) {
_update_text();
return;
}

Expand Down Expand Up @@ -288,14 +288,12 @@ void SpinBox::_line_edit_editing_toggled(bool p_toggled_on) {
line_edit->select_all();
}
} else {
// Discontinue because the focus_exit was caused by canceling or the text is empty.
if (Input::get_singleton()->is_action_pressed("ui_cancel") || line_edit->get_text().is_empty()) {
_update_text();
return;
_update_text(); // Revert text if editing was canceled.
} else {
_update_text(true); // Update text in case value was changed this frame (e.g. on `focus_exited`).
_text_submitted(line_edit->get_text());
}

line_edit->deselect();
_text_submitted(line_edit->get_text());
}
}

Expand Down
4 changes: 2 additions & 2 deletions scene/gui/spin_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ class SpinBox : public Range {
void _range_click_timeout();
void _release_mouse_from_drag_mode();

void _update_text(bool p_keep_line_edit = false);
void _update_text(bool p_only_update_if_value_changed = false);
void _text_submitted(const String &p_string);
void _text_changed(const String &p_string);

String prefix;
String suffix;
String last_updated_text;
String last_text_value;
double custom_arrow_step = 0.0;
bool use_custom_arrow_step = false;

Expand Down