diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index cf2bbcb0a19..a73e952eca7 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -1527,6 +1527,22 @@ void LineEdit::set_text(String p_text) { scroll_offset = 0.0; } +void LineEdit::set_text_with_selection(const String &p_text) { + Selection selection_copy = selection; + + clear_internal(); + insert_text_at_caret(p_text); + _create_undo_state(); + + int tlen = text.length(); + selection = selection_copy; + selection.begin = MIN(selection.begin, tlen); + selection.end = MIN(selection.end, tlen); + selection.start_column = MIN(selection.start_column, tlen); + + queue_redraw(); +} + void LineEdit::set_text_direction(Control::TextDirection p_text_direction) { ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); if (text_direction != p_text_direction) { diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index c7ea16b4c8c..8acb4896c55 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -283,6 +283,7 @@ public: void set_text(String p_text); String get_text() const; + void set_text_with_selection(const String &p_text); // Set text, while preserving selection. void set_text_direction(TextDirection p_text_direction); TextDirection get_text_direction() const; diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cpp index 4f4754add56..7cb54f24ea9 100644 --- a/scene/gui/spin_box.cpp +++ b/scene/gui/spin_box.cpp @@ -54,7 +54,7 @@ void SpinBox::_update_text() { } } - line_edit->set_text(value); + line_edit->set_text_with_selection(value); } void SpinBox::_text_submitted(const String &p_string) {