From 3df68b4fa6b1eb629876ce575f4aabe2d40bc624 Mon Sep 17 00:00:00 2001 From: Paulb23 Date: Sun, 1 Dec 2019 13:54:09 +0000 Subject: [PATCH] Fixed LineEdit alignment when removing text or undo / redo --- scene/gui/line_edit.cpp | 14 ++++++++++++++ scene/gui/line_edit.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index ab6f80bfa92..85287813e09 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -970,6 +970,8 @@ void LineEdit::undo() { undo_stack_pos = undo_stack_pos->prev(); TextOperation op = undo_stack_pos->get(); text = op.text; + cached_width = op.cached_width; + window_pos = op.window_pos; set_cursor_position(op.cursor_pos); if (expand_to_text_length) @@ -988,6 +990,8 @@ void LineEdit::redo() { undo_stack_pos = undo_stack_pos->next(); TextOperation op = undo_stack_pos->get(); text = op.text; + cached_width = op.cached_width; + window_pos = op.window_pos; set_cursor_position(op.cursor_pos); if (expand_to_text_length) @@ -1169,6 +1173,10 @@ void LineEdit::delete_char() { set_cursor_position(get_cursor_position() - 1); + if (align == ALIGN_CENTER || align == ALIGN_RIGHT) { + window_pos = CLAMP(window_pos - 1, 0, text.length() - 1); + } + _text_changed(); } @@ -1196,6 +1204,10 @@ void LineEdit::delete_text(int p_from_column, int p_to_column) { window_pos = cursor_pos; } + if (align == ALIGN_CENTER || align == ALIGN_RIGHT) { + window_pos = CLAMP(window_pos - (p_to_column - p_from_column), 0, text.length() - 1); + } + if (!text_changed_dirty) { if (is_inside_tree()) { MessageQueue::get_singleton()->push_call(this, "_text_changed"); @@ -1677,7 +1689,9 @@ void LineEdit::_clear_undo_stack() { void LineEdit::_create_undo_state() { TextOperation op; op.text = text; + op.cached_width = cached_width; op.cursor_pos = cursor_pos; + op.window_pos = window_pos; undo_stack.push_back(op); } diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index 3424131dad8..ebe49091ebf 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -106,6 +106,8 @@ private: struct TextOperation { int cursor_pos; + int window_pos; + int cached_width; String text; }; List undo_stack;