diff options
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r-- | scene/gui/text_edit.cpp | 184 |
1 files changed, 96 insertions, 88 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index b2a5317f09..836b11c349 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -42,14 +42,14 @@ #define TAB_PIXELS -static bool _is_text_char(CharType c) { +inline bool _is_symbol(CharType c) { - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; + return is_symbol(c); } -static bool _is_symbol(CharType c) { +static bool _is_text_char(CharType c) { - return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' '); + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; } static bool _is_whitespace(CharType c) { @@ -1695,10 +1695,9 @@ void TextEdit::indent_right() { // fix selection and cursor being off by one on the last line if (is_selection_active()) { - selection.to_column++; - selection.from_column++; + select(selection.from_line, selection.from_column + 1, selection.to_line, selection.to_column + 1); } - cursor.column++; + cursor_set_column(cursor.column + 1, false); end_complex_operation(); update(); } @@ -1737,14 +1736,9 @@ void TextEdit::indent_left() { // fix selection and cursor being off by one on the last line if (is_selection_active() && last_line_text != get_line(end_line)) { - if (selection.to_column > 0) - selection.to_column--; - if (selection.from_column > 0) - selection.from_column--; - } - if (cursor.column > 0) { - cursor.column--; + select(selection.from_line, selection.from_column - 1, selection.to_line, selection.to_column - 1); } + cursor_set_column(cursor.column - 1, false); end_complex_operation(); update(); } @@ -1962,7 +1956,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } else if (mb->is_doubleclick() && text[cursor.line].length()) { - //doubleclick select world + //doubleclick select word selection.selecting_mode = Selection::MODE_WORD; _update_selection_mode_word(); last_dblclk = OS::get_singleton()->get_ticks_msec(); @@ -2445,26 +2439,44 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { //simple unindent int cc = cursor.column; + + const int len = text[cursor.line].length(); + const String &line = text[cursor.line]; + + int left = 0; // number of whitespace chars at beginning of line + while (left < len && (line[left] == '\t' || line[left] == ' ')) + left++; + cc = MIN(cc, left); + + while (cc < indent_size && cc < left && line[cc] == ' ') + cc++; + if (cc > 0 && cc <= text[cursor.line].length()) { - if (text[cursor.line][cursor.column - 1] == '\t') { - backspace_at_cursor(); + if (text[cursor.line][cc - 1] == '\t') { + _remove_text(cursor.line, cc - 1, cursor.line, cc); + if (cursor.column >= left) + cursor_set_column(MAX(0, cursor.column - 1)); + update(); } else { - if (cursor.column - indent_size >= 0) { + int n = 0; - bool unindent = true; - for (int i = 1; i <= indent_size; i++) { - if (text[cursor.line][cursor.column - i] != ' ') { - unindent = false; - break; - } + for (int i = 1; i <= MIN(cc, indent_size); i++) { + if (line[cc - i] != ' ') { + break; } + n++; + } - if (unindent) { - _remove_text(cursor.line, cursor.column - indent_size, cursor.line, cursor.column); - cursor_set_column(cursor.column - indent_size); - } + if (n > 0) { + _remove_text(cursor.line, cc - n, cursor.line, cc); + if (cursor.column > left - n) // inside text? + cursor_set_column(MAX(0, cursor.column - n)); + update(); } } + } else if (cc == 0 && line.length() > 0 && line[0] == '\t') { + _remove_text(cursor.line, 0, cursor.line, 1); + update(); } } else { //simple indent @@ -2569,22 +2581,19 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (cc == 0 && cursor.line > 0) { cursor_set_line(cursor.line - 1); cursor_set_column(text[cursor.line].length()); - break; - } - - while (cc > 0) { - - bool ischar = _is_text_char(text[cursor.line][cc - 1]); + } else { + while (cc > 0) { + bool ischar = _is_text_char(text[cursor.line][cc - 1]); - if (prev_char && !ischar) - break; + if (prev_char && !ischar) + break; - prev_char = ischar; - cc--; + prev_char = ischar; + cc--; + } + cursor_set_column(cc); } - cursor_set_column(cc); - } else if (cursor.column == 0) { if (cursor.line > 0) { @@ -2633,21 +2642,18 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (cc == text[cursor.line].length() && cursor.line < text.size() - 1) { cursor_set_line(cursor.line + 1); cursor_set_column(0); - break; - } - - while (cc < text[cursor.line].length()) { - - bool ischar = _is_text_char(text[cursor.line][cc]); + } else { + while (cc < text[cursor.line].length()) { + bool ischar = _is_text_char(text[cursor.line][cc]); - if (prev_char && !ischar) - break; - prev_char = ischar; - cc++; + if (prev_char && !ischar) + break; + prev_char = ischar; + cc++; + } + cursor_set_column(cc); } - cursor_set_column(cc); - } else if (cursor.column == text[cursor.line].length()) { if (cursor.line < text.size() - 1) { @@ -2719,6 +2725,8 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { _scroll_lines_down(); break; } + + { #else if (k->get_command() && k->get_alt()) { _scroll_lines_down(); @@ -2727,9 +2735,15 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (k->get_command()) cursor_set_line(text.size() - 1, true, false); - else + else { #endif - cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false); + if (!is_last_visible_line(cursor.line)) { + cursor_set_line(cursor_get_line() + num_lines_from(CLAMP(cursor.line + 1, 0, text.size() - 1), 1), true, false); + } else { + cursor_set_line(text.size() - 1); + cursor_set_column(get_line(cursor.line).length(), true); + } + } if (k->get_shift()) _post_shift_selection(); @@ -4201,11 +4215,15 @@ void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_t p_from_line = text.size() - 1; if (p_from_column >= text[p_from_line].length()) p_from_column = text[p_from_line].length(); + if (p_from_column < 0) + p_from_column = 0; if (p_to_line >= text.size()) p_to_line = text.size() - 1; if (p_to_column >= text[p_to_line].length()) p_to_column = text[p_to_line].length(); + if (p_to_column < 0) + p_to_column = 0; selection.from_line = p_from_line; selection.from_column = p_from_column; @@ -4574,6 +4592,24 @@ int TextEdit::num_lines_from(int p_line_from, int unhidden_amount) const { return num_total; } +bool TextEdit::is_last_visible_line(int p_line) const { + + ERR_FAIL_INDEX_V(p_line, text.size(), false); + + if (p_line == text.size() - 1) + return true; + + if (!is_hiding_enabled()) + return false; + + for (int i = p_line + 1; i < text.size(); i++) { + if (!is_line_hidden(i)) + return false; + } + + return true; +} + int TextEdit::get_indent_level(int p_line) const { ERR_FAIL_INDEX_V(p_line, text.size(), 0); @@ -5214,12 +5250,8 @@ String TextEdit::get_word_at_pos(const Vector2 &p_pos) const { String s = text[row]; if (s.length() == 0) return ""; - int beg = CLAMP(col, 0, s.length()); - int end = beg; - - if (s[beg] > 32 || beg == s.length()) { - - bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this + int beg, end; + if (select_word(s, col, beg, end)) { bool inside_quotes = false; int qbegin = 0, qend = 0; @@ -5238,16 +5270,6 @@ String TextEdit::get_word_at_pos(const Vector2 &p_pos) const { } } - while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) { - beg--; - } - while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) { - end++; - } - - if (end < s.length()) - end += 1; - return s.substr(beg, end - beg); } @@ -5264,22 +5286,8 @@ String TextEdit::get_tooltip(const Point2 &p_pos) const { String s = text[row]; if (s.length() == 0) return Control::get_tooltip(p_pos); - int beg = CLAMP(col, 0, s.length()); - int end = beg; - - if (s[beg] > 32 || beg == s.length()) { - - bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this - - while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) { - beg--; - } - while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) { - end++; - } - - if (end < s.length()) - end += 1; + int beg, end; + if (select_word(s, col, beg, end)) { String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud); |