summaryrefslogtreecommitdiff
path: root/scene/gui/text_edit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r--scene/gui/text_edit.cpp136
1 files changed, 88 insertions, 48 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index f8c31121be..0464cc1ac8 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -559,7 +559,7 @@ void TextEdit::_update_selection_mode_line() {
click_select_held->start();
}
-void TextEdit::_update_minimap_scroll() {
+void TextEdit::_update_minimap_click() {
Point2 mp = get_local_mouse_position();
int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT);
@@ -573,6 +573,13 @@ void TextEdit::_update_minimap_scroll() {
int row;
_get_minimap_mouse_row(Point2i(mp.x, mp.y), row);
+ if (row >= get_first_visible_line() && (row < get_last_visible_line() || row >= (text.size() - 1))) {
+ minimap_scroll_ratio = v_scroll->get_as_ratio();
+ minimap_scroll_click_pos = mp.y;
+ can_drag_minimap = true;
+ return;
+ }
+
int wi;
int first_line = row - num_lines_from_rows(row, 0, -get_visible_rows() / 2, wi) + 1;
double delta = get_scroll_pos_for_line(first_line, wi) - get_v_scroll();
@@ -583,11 +590,27 @@ void TextEdit::_update_minimap_scroll() {
}
}
+void TextEdit::_update_minimap_drag() {
+
+ if (!can_drag_minimap) {
+ return;
+ }
+
+ int control_height = _get_control_height();
+ int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing);
+ if (control_height > scroll_height) {
+ control_height = scroll_height;
+ }
+
+ Point2 mp = get_local_mouse_position();
+ double diff = (mp.y - minimap_scroll_click_pos) / control_height;
+ v_scroll->set_as_ratio(minimap_scroll_ratio + diff);
+}
+
void TextEdit::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
-
_update_caches();
if (cursor_changed_dirty)
MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
@@ -596,12 +619,16 @@ void TextEdit::_notification(int p_what) {
_update_wrap_at();
} break;
case NOTIFICATION_RESIZED: {
-
_update_scrollbars();
- call_deferred("_update_wrap_at");
+ _update_wrap_at();
+ } break;
+ case NOTIFICATION_VISIBILITY_CHANGED: {
+ if (is_visible()) {
+ call_deferred("_update_scrollbars");
+ call_deferred("_update_wrap_at");
+ }
} break;
case NOTIFICATION_THEME_CHANGED: {
-
_update_caches();
_update_wrap_at();
syntax_highlighting_cache.clear();
@@ -899,12 +926,7 @@ void TextEdit::_notification(int p_what) {
// calculate viewport size and y offset
int viewport_height = (draw_amount - 1) * minimap_line_height;
- int control_height = size.height;
- control_height -= cache.style_normal->get_minimum_size().height;
- if (h_scroll->is_visible_in_tree()) {
- control_height -= h_scroll->get_size().height;
- }
- control_height -= viewport_height;
+ int control_height = _get_control_height() - viewport_height;
int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
// calculate the first line.
@@ -918,8 +940,7 @@ void TextEdit::_notification(int p_what) {
int minimap_draw_amount = minimap_visible_lines + times_line_wraps(minimap_line + 1);
// draw the minimap
- Color viewport_color = cache.current_line_color;
- viewport_color.a /= 2;
+ Color viewport_color = (cache.background_color.get_v() < 0.5) ? Color(1, 1, 1, 0.1) : Color(0, 0, 0, 0.1);
VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2((xmargin_end + 2), viewport_offset_y, cache.minimap_width, viewport_height), viewport_color);
for (int i = 0; i < minimap_draw_amount; i++) {
@@ -2071,12 +2092,7 @@ void TextEdit::_get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const
// calculate viewport size and y offset
int viewport_height = (draw_amount - 1) * minimap_line_height;
- int control_height = get_size().height;
- control_height -= cache.style_normal->get_minimum_size().height;
- if (h_scroll->is_visible_in_tree()) {
- control_height -= h_scroll->get_size().height;
- }
- control_height -= viewport_height;
+ int control_height = _get_control_height() - viewport_height;
int viewport_offset_y = round(get_scroll_pos_for_line(first_visible_line) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount));
// calculate the first line.
@@ -2187,12 +2203,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
int row, col;
_get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
- if (mb->get_command() && highlighted_word != String()) {
-
- emit_signal("symbol_lookup", highlighted_word, row, col);
- return;
- }
-
// Toggle breakpoint on gutter click.
if (draw_breakpoint_gutter) {
int gutter = cache.style_normal->get_margin(MARGIN_LEFT);
@@ -2240,7 +2250,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
// minimap
if (draw_minimap) {
- _update_minimap_scroll();
+ _update_minimap_click();
if (dragging_minimap) {
return;
}
@@ -2361,8 +2371,17 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
} else {
if (mb->get_button_index() == BUTTON_LEFT) {
+ if (mb->get_command() && highlighted_word != String()) {
+ int row, col;
+ _get_mouse_pos(Point2i(mb->get_position().x, mb->get_position().y), row, col);
+
+ emit_signal("symbol_lookup", highlighted_word, row, col);
+ return;
+ }
+
dragging_minimap = false;
dragging_selection = false;
+ can_drag_minimap = false;
click_select_held->stop();
}
@@ -2411,7 +2430,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
_reset_caret_blink_timer();
if (draw_minimap && !dragging_selection) {
- _update_minimap_scroll();
+ _update_minimap_drag();
}
if (!dragging_minimap) {
@@ -3506,6 +3525,10 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
} break;
case KEY_Z: {
+ if (readonly) {
+ break;
+ }
+
if (!k->get_command()) {
scancode_handled = false;
break;
@@ -3518,6 +3541,10 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) {
} break;
case KEY_Y: {
+ if (readonly) {
+ break;
+ }
+
if (!k->get_command()) {
scancode_handled = false;
break;
@@ -3992,7 +4019,7 @@ void TextEdit::_line_edited_from(int p_line) {
if (syntax_highlighting_cache.size() > 0) {
cache_size = syntax_highlighting_cache.back()->key();
- for (int i = p_line - 1; i < cache_size; i++) {
+ for (int i = p_line - 1; i <= cache_size; i++) {
if (syntax_highlighting_cache.has(i)) {
syntax_highlighting_cache.erase(i);
}
@@ -4019,23 +4046,21 @@ Size2 TextEdit::get_minimum_size() const {
return cache.style_normal->get_minimum_size();
}
-int TextEdit::get_visible_rows() const {
+int TextEdit::_get_control_height() const {
+ int control_height = get_size().height;
+ control_height -= cache.style_normal->get_minimum_size().height;
+ if (h_scroll->is_visible_in_tree()) {
+ control_height -= h_scroll->get_size().height;
+ }
+ return control_height;
+}
- int total = get_size().height;
- total -= cache.style_normal->get_minimum_size().height;
- if (h_scroll->is_visible_in_tree())
- total -= h_scroll->get_size().height;
- total /= get_row_height();
- return total;
+int TextEdit::get_visible_rows() const {
+ return _get_control_height() / get_row_height();
}
int TextEdit::_get_minimap_visible_rows() const {
- int total = get_size().height;
- total -= cache.style_normal->get_minimum_size().height;
- if (h_scroll->is_visible_in_tree())
- total -= h_scroll->get_size().height;
- total /= (minimap_char_size.y + minimap_line_spacing);
- return total;
+ return _get_control_height() / (minimap_char_size.y + minimap_line_spacing);
}
int TextEdit::get_total_visible_rows() const {
@@ -4599,7 +4624,7 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
return CURSOR_ARROW;
} else {
int xmargin_end = get_size().width - cache.style_normal->get_margin(MARGIN_RIGHT);
- if (p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
+ if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) {
return CURSOR_ARROW;
}
@@ -5340,6 +5365,9 @@ bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_l
break;
}
pos_from = last_pos - p_key.length();
+ if (pos_from < 0) {
+ break;
+ }
}
} else {
while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
@@ -6126,10 +6154,7 @@ int TextEdit::get_last_visible_line_wrap_index() const {
double TextEdit::get_visible_rows_offset() const {
- double total = get_size().height;
- total -= cache.style_normal->get_minimum_size().height;
- if (h_scroll->is_visible_in_tree())
- total -= h_scroll->get_size().height;
+ double total = _get_control_height();
total /= (double)get_row_height();
total = total - floor(total);
total = -CLAMP(total, 0.001, 1) + 1;
@@ -6539,9 +6564,21 @@ void TextEdit::set_line(int line, String new_text) {
}
void TextEdit::insert_at(const String &p_text, int at) {
- cursor_set_column(0);
- cursor_set_line(at, false, true);
_insert_text(at, 0, p_text + "\n");
+ if (cursor.line >= at) {
+ // offset cursor when located after inserted line
+ ++cursor.line;
+ }
+ if (is_selection_active()) {
+ if (selection.from_line >= at) {
+ // offset selection when located after inserted line
+ ++selection.from_line;
+ ++selection.to_line;
+ } else if (selection.to_line >= at) {
+ // extend selection that includes inserted line
+ ++selection.to_line;
+ }
+ }
}
void TextEdit::set_show_line_numbers(bool p_show) {
@@ -7019,6 +7056,9 @@ TextEdit::TextEdit() {
scrolling = false;
minimap_clicked = false;
dragging_minimap = false;
+ can_drag_minimap = false;
+ minimap_scroll_ratio = 0;
+ minimap_scroll_click_pos = 0;
dragging_selection = false;
target_v_scroll = 0;
v_scroll_speed = 80;