diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 154be126b..b07b57ff8 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -1400,6 +1400,26 @@ void LineEdit::set_text(String p_text) { scroll_offset = 0; } +void LineEdit::set_text_with_selection(const String &p_text) { + Selection selection_copy = selection; + + clear_internal(); + append_at_cursor(p_text); + _create_undo_state(); + + if (expand_to_text_length) { + minimum_size_changed(); + } + + int tlen = text.length(); + selection = selection_copy; + selection.begin = MIN(selection.begin, tlen); + selection.end = MIN(selection.end, tlen); + selection.cursor_start = MIN(selection.cursor_start, tlen); + + update(); +} + void LineEdit::clear() { clear_internal(); _text_changed(); diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index 7773728bf..f2aa5f84a 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -210,6 +210,7 @@ public: void delete_char(); void delete_text(int p_from_column, int p_to_column); void set_text(String p_text); + void set_text_with_selection(const String &p_text); // Set text, while preserving selection. String get_text() const; void set_placeholder(String p_text); String get_placeholder() const; diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index e041c106e..cf5782fdb 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -54,7 +54,7 @@ void SpinBox::_value_changed(double) { } } - line_edit->set_text(value); + line_edit->set_text_with_selection(value); } void SpinBox::_text_entered(const String &p_string) {