diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2016-07-23 22:54:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-23 22:54:32 +0200 |
commit | ebefdaa598e29791ba83598fd3e3aa3f52f2bf31 (patch) | |
tree | 7667d59bf72614ba31808d4c4971c21ccbb23eee | |
parent | 990a23e48ea6dee7d311d450f1032c4e3526b0ac (diff) | |
parent | fc16954fa2d87707fa69293b7507dd43b028a96f (diff) |
Merge pull request #5737 from neikeq/pr-issue-5269
TextEdit: Scroll search results to the center
-rw-r--r-- | scene/gui/text_edit.cpp | 28 | ||||
-rw-r--r-- | scene/gui/text_edit.h | 2 | ||||
-rw-r--r-- | tools/editor/code_editor.cpp | 5 |
3 files changed, 33 insertions, 2 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 50b44c55a9..af04fbd201 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -2993,6 +2993,34 @@ void TextEdit::adjust_viewport_to_cursor() { } +void TextEdit::center_viewport_to_cursor() { + + if (cursor.line_ofs>cursor.line) + cursor.line_ofs=cursor.line; + + int visible_width=cache.size.width-cache.style_normal->get_minimum_size().width-cache.line_number_w-cache.breakpoint_gutter_width; + if (v_scroll->is_visible()) + visible_width-=v_scroll->get_combined_minimum_size().width; + visible_width-=20; // give it a little more space + + int visible_rows = get_visible_rows(); + if (h_scroll->is_visible()) + visible_rows-=((h_scroll->get_combined_minimum_size().height-1)/get_row_height()); + + int max_ofs = text.size()-(scroll_past_end_of_file_enabled?1:visible_rows); + cursor.line_ofs=CLAMP(cursor.line-(visible_rows/2),0,max_ofs); + + int cursor_x = get_column_x_offset( cursor.column, text[cursor.line] ); + + if (cursor_x>(cursor.x_ofs+visible_width)) + cursor.x_ofs=cursor_x-visible_width+1; + + if (cursor_x < cursor.x_ofs) + cursor.x_ofs=cursor_x; + + update(); +} + void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) { if (p_col<0) diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 65e9615911..c3bdf7c856 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -396,6 +396,8 @@ public: } void set_auto_indent(bool p_auto_indent); + void center_viewport_to_cursor(); + void cursor_set_column(int p_col, bool p_adjust_viewport=true); void cursor_set_line(int p_row, bool p_adjust_viewport=true); diff --git a/tools/editor/code_editor.cpp b/tools/editor/code_editor.cpp index 71ae171dfe..6c8d25aa01 100644 --- a/tools/editor/code_editor.cpp +++ b/tools/editor/code_editor.cpp @@ -133,8 +133,9 @@ bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) if (found) { if (!preserve_cursor) { - text_edit->cursor_set_line(line); - text_edit->cursor_set_column(col+text.length()); + text_edit->cursor_set_line(line, false); + text_edit->cursor_set_column(col+text.length(), false); + text_edit->center_viewport_to_cursor(); } text_edit->set_search_text(text); |