diff options
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r-- | scene/gui/text_edit.cpp | 142 |
1 files changed, 128 insertions, 14 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 7bcef5f9ab..7227942ccb 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -647,7 +647,7 @@ void TextEdit::_notification(int p_what) { if (scrolling && get_v_scroll() != target_v_scroll) { double target_y = target_v_scroll - get_v_scroll(); double dist = sqrt(target_y * target_y); - // To ensure minimap is responsive overide the speed setting. + // To ensure minimap is responsive override the speed setting. double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_physics_process_delta_time(); if (Math::abs(vel) >= dist) { @@ -2081,6 +2081,44 @@ void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) co r_col = col; } +Vector2i TextEdit::_get_cursor_pixel_pos() { + adjust_viewport_to_cursor(); + int row = (cursor.line - get_first_visible_line() - cursor.wrap_ofs); + // Correct for hidden and wrapped lines + for (int i = get_first_visible_line(); i < cursor.line; i++) { + if (is_line_hidden(i)) { + row -= 1; + continue; + } + row += times_line_wraps(i); + } + // Row might be wrapped. Adjust row and r_column + Vector<String> rows2 = get_wrap_rows_text(cursor.line); + while (rows2.size() > 1) { + if (cursor.column >= rows2[0].length()) { + cursor.column -= rows2[0].length(); + rows2.remove(0); + row++; + } else { + break; + } + } + + // Calculate final pixel position + int y = (row - get_v_scroll_offset() + 1 /*Bottom of line*/) * get_row_height(); + int x = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width + cache.fold_gutter_width + cache.info_gutter_width - cursor.x_ofs; + int ix = 0; + while (ix < rows2[0].size() && ix < cursor.column) { + if (cache.font != NULL) { + x += cache.font->get_char_size(rows2[0].get(ix)).width; + } + ix++; + } + x += get_indent_level(cursor.line) * cache.font->get_char_size(' ').width; + + return Vector2i(x, y); +} + void TextEdit::_get_minimap_mouse_row(const Point2i &p_mouse, int &r_row) const { float rows = p_mouse.y; @@ -3222,7 +3260,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (readonly) break; - if (k->get_shift() && !k->get_command() && !k->get_alt()) { + if (k->get_shift() && !k->get_command() && !k->get_alt() && is_shortcut_keys_enabled()) { cut(); break; } @@ -3453,13 +3491,15 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { scancode_handled = false; break; } - select_all(); + if (is_shortcut_keys_enabled()) { + select_all(); + } #else if ((!k->get_command() && !k->get_control())) { scancode_handled = false; break; } - if (!k->get_shift() && k->get_command()) + if (!k->get_shift() && k->get_command() && is_shortcut_keys_enabled()) select_all(); else if (k->get_control()) { if (k->get_shift()) @@ -3515,8 +3555,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { scancode_handled = false; break; } - - cut(); + if (is_shortcut_keys_enabled()) { + cut(); + } } break; case KEY_C: { @@ -3526,7 +3567,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { break; } - copy(); + if (is_shortcut_keys_enabled()) { + copy(); + } } break; case KEY_Z: { @@ -3540,10 +3583,12 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { break; } - if (k->get_shift()) - redo(); - else - undo(); + if (is_shortcut_keys_enabled()) { + if (k->get_shift()) + redo(); + else + undo(); + } } break; case KEY_Y: { @@ -3556,7 +3601,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { break; } - redo(); + if (is_shortcut_keys_enabled()) { + redo(); + } } break; case KEY_V: { if (readonly) { @@ -3567,7 +3614,9 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { break; } - paste(); + if (is_shortcut_keys_enabled()) { + paste(); + } } break; case KEY_SPACE: { @@ -3585,6 +3634,16 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } break; + case KEY_MENU: { + if (context_menu_enabled) { + menu->set_position(get_global_transform().xform(_get_cursor_pixel_pos())); + menu->set_size(Vector2(1, 1)); + menu->set_scale(get_global_transform().get_scale()); + menu->popup(); + menu->grab_focus(); + } + } break; + default: { scancode_handled = false; @@ -4061,6 +4120,25 @@ int TextEdit::_get_control_height() const { return control_height; } +void TextEdit::_generate_context_menu() { + // Reorganize context menu. + menu->clear(); + if (!readonly) + menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0); + menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0); + if (!readonly) + menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0); + menu->add_separator(); + if (is_selecting_enabled()) + menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0); + if (!readonly) { + menu->add_item(RTR("Clear"), MENU_CLEAR); + menu->add_separator(); + menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0); + menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0); + } +} + int TextEdit::get_visible_rows() const { return _get_control_height() / get_row_height(); } @@ -4769,6 +4847,7 @@ void TextEdit::set_readonly(bool p_readonly) { return; readonly = p_readonly; + _generate_context_menu(); // Reorganize context menu. menu->clear(); @@ -5108,6 +5187,8 @@ void TextEdit::paste() { } void TextEdit::select_all() { + if (!selecting_enabled) + return; if (text.size() == 1 && text[0].length() == 0) return; @@ -5132,6 +5213,8 @@ void TextEdit::deselect() { } void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { + if (!selecting_enabled) + return; if (p_from_line < 0) p_from_line = 0; @@ -6247,7 +6330,7 @@ void TextEdit::_confirm_completion() { CharType last_completion_char = completion_current.insert_text[completion_current.insert_text.length() - 1]; if ((last_completion_char == '"' || last_completion_char == '\'') && last_completion_char == next_char) { - _base_remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); + _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); } if (last_completion_char == '(') { @@ -6775,6 +6858,29 @@ bool TextEdit::is_context_menu_enabled() { return context_menu_enabled; } +void TextEdit::set_shortcut_keys_enabled(bool p_enabled) { + shortcut_keys_enabled = p_enabled; + + _generate_context_menu(); +} + +void TextEdit::set_selecting_enabled(bool p_enabled) { + selecting_enabled = p_enabled; + + if (!selecting_enabled) + deselect(); + + _generate_context_menu(); +} + +bool TextEdit::is_selecting_enabled() const { + return selecting_enabled; +} + +bool TextEdit::is_shortcut_keys_enabled() const { + return shortcut_keys_enabled; +} + PopupMenu *TextEdit::get_menu() const { return menu; } @@ -6830,6 +6936,10 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("is_wrap_enabled"), &TextEdit::is_wrap_enabled); ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &TextEdit::set_context_menu_enabled); ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled); + ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &TextEdit::set_shortcut_keys_enabled); + ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled); + ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled); + ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled); ClassDB::bind_method(D_METHOD("cut"), &TextEdit::cut); ClassDB::bind_method(D_METHOD("copy"), &TextEdit::copy); @@ -6920,6 +7030,8 @@ void TextEdit::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_selected_font_color"), "set_override_selected_font_color", "is_overriding_selected_font_color"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "smooth_scrolling"), "set_smooth_scroll_enable", "is_smooth_scroll_enabled"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_scroll_speed"), "set_v_scroll_speed", "get_v_scroll_speed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hiding_enabled"), "set_hiding_enabled", "is_hiding_enabled"); @@ -7078,7 +7190,9 @@ TextEdit::TextEdit() { minimap_char_size = Point2(1, 2); minimap_line_spacing = 1; + selecting_enabled = true; context_menu_enabled = true; + shortcut_keys_enabled = true; menu = memnew(PopupMenu); add_child(menu); readonly = true; // Initialise to opposite first, so we get past the early-out in set_readonly. |