diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index a15fe5524d2..96345f4265c 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 580a69ea83e..0b4decdb100 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -208,6 +208,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 38e255aea4e..15561cf172d 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -50,7 +50,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) {