Преглед изворни кода

Allow overriding SpinBox value on `focus_exited`

Lars Pettersson пре 8 месеци
родитељ
комит
3c1ac98f37
2 измењених фајлова са 13 додато и 15 уклоњено
  1. 11 13
      scene/gui/spin_box.cpp
  2. 2 2
      scene/gui/spin_box.h

+ 11 - 13
scene/gui/spin_box.cpp

@@ -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;
@@ -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;
@@ -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;
 	}
 
@@ -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());
 	}
 }
 

+ 2 - 2
scene/gui/spin_box.h

@@ -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;