From f81d0095259c3affeec0de79e4ad1f38ea9bba39 Mon Sep 17 00:00:00 2001 From: Andreas Haas Date: Thu, 29 Sep 2016 09:12:45 +0200 Subject: Add inline ColorPicker to Script text editor. Adds an option to the script editor context menu that lets you open a ColorPicker in order to easily edit `Color()` constructors. To do this, right click on the word `Color` and select `Pick Color`. A side effect of this change is that the script editor now has its own context menu instead of re-using the one from TextEdit. It's now possible to indent left/right and to toggle comments via this menu. I also felt free to make it more context-sensitive than before: Now "Cut" and "Copy" will only be shown if text has actually been selected. I also added default shortcuts for indent left/right. (alt + left/right) Closes #6232 --- tools/editor/plugins/script_text_editor.cpp | 114 +++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 3 deletions(-) (limited to 'tools/editor/plugins/script_text_editor.cpp') diff --git a/tools/editor/plugins/script_text_editor.cpp b/tools/editor/plugins/script_text_editor.cpp index ca0398f069..2f807eaa19 100644 --- a/tools/editor/plugins/script_text_editor.cpp +++ b/tools/editor/plugins/script_text_editor.cpp @@ -498,6 +498,7 @@ void ScriptTextEditor::_code_complete_scripts(void* p_ud,const String& p_code, L void ScriptTextEditor::_code_complete_script(const String& p_code, List* r_options) { + if (color_panel->is_visible()) return; Node *base = get_tree()->get_edited_scene_root(); if (base) { base = _find_node_for_script(base,base,script); @@ -882,6 +883,9 @@ void ScriptTextEditor::_edit_option(int p_op) { case EDIT_TRIM_TRAILING_WHITESAPCE: { trim_trailing_whitespace(); } break; + case EDIT_PICK_COLOR: { + color_panel->popup(); + } break; case SEARCH_FIND: { @@ -989,7 +993,8 @@ void ScriptTextEditor::_bind_methods() { ObjectTypeDB::bind_method("_edit_option",&ScriptTextEditor::_edit_option); ObjectTypeDB::bind_method("_goto_line",&ScriptTextEditor::_goto_line); ObjectTypeDB::bind_method("_lookup_symbol",&ScriptTextEditor::_lookup_symbol); - + ObjectTypeDB::bind_method("_text_edit_input_event", &ScriptTextEditor::_text_edit_input_event); + ObjectTypeDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed); ObjectTypeDB::bind_method("get_drag_data_fw",&ScriptTextEditor::get_drag_data_fw); @@ -1168,6 +1173,96 @@ void ScriptTextEditor::drop_data_fw(const Point2& p_point,const Variant& p_data, } +void ScriptTextEditor::_text_edit_input_event(const InputEvent& ev) { + if (ev.type == InputEvent::MOUSE_BUTTON) { + InputEventMouseButton mb = ev.mouse_button; + if (mb.button_index == BUTTON_RIGHT && !mb.pressed) { + + int col, row; + TextEdit* tx = code_editor->get_text_edit(); + tx->_get_mouse_pos(Point2i(mb.global_x, mb.global_y)-tx->get_global_pos(), row, col); + Vector2 mpos = Vector2(mb.global_x, mb.global_y)-tx->get_global_pos(); + bool have_selection = (tx->get_selection_text().length() > 0); + bool have_color = (tx->get_word_at_pos(mpos) == "Color"); + if (have_color) { + + String line = tx->get_line(row); + color_line = row; + int begin = 0; + int end = 0; + bool valid = false; + for (int i = col; i < line.length(); i++) { + if (line[i] == '(') { + begin = i; + continue; + } + else if (line[i] == ')') { + end = i+1; + valid = true; + break; + } + } + if (valid) { + color_args = line.substr(begin, end-begin); + String stripped = color_args.replace(" ", "").replace("(", "").replace(")", ""); + Vector color = stripped.split_floats(","); + if (color.size() > 2) { + float alpha = color.size() > 3 ? color[3] : 1.0f; + color_picker->set_color(Color(color[0], color[1], color[2], alpha)); + } + color_panel->set_pos(get_global_transform().xform(get_local_mouse_pos())); + Size2 ms = Size2(300, color_picker->get_combined_minimum_size().height+10); + color_panel->set_size(ms); + } else { + have_color = false; + } + } + _make_context_menu(have_selection, have_color); + } + } +} + +void ScriptTextEditor::_color_changed(const Color& p_color) { + String new_args; + if (p_color.a == 1.0f) { + new_args = String("("+rtos(p_color.r)+", "+rtos(p_color.g)+", "+rtos(p_color.b)+")"); + } else { + new_args = String("("+rtos(p_color.r)+", "+rtos(p_color.g)+", "+rtos(p_color.b)+", "+rtos(p_color.a)+")"); + } + + String line = code_editor->get_text_edit()->get_line(color_line); + String new_line = line.replace(color_args, new_args); + color_args = new_args; + code_editor->get_text_edit()->set_line(color_line, new_line); +} + +void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) { + + context_menu->clear(); + if (p_selection) { + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut")); + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy")); + } + + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste")); + context_menu->add_separator(); + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all")); + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo")); + + if (p_selection) { + context_menu->add_separator(); + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left")); + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right")); + context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment")); + } + if (p_color) { + context_menu->add_separator(); + context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR); + } + context_menu->set_pos(get_global_transform().xform(get_local_mouse_pos())); + context_menu->set_size(Vector2(1, 1)); + context_menu->popup(); +} ScriptTextEditor::ScriptTextEditor() { @@ -1197,6 +1292,19 @@ ScriptTextEditor::ScriptTextEditor() { EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset")); code_editor->get_text_edit()->set_select_identifiers_on_hover(true); + code_editor->get_text_edit()->set_context_menu_enabled(false); + code_editor->get_text_edit()->connect("input_event", this, "_text_edit_input_event"); + + context_menu = memnew(PopupMenu); + add_child(context_menu); + context_menu->connect("item_pressed", this, "_edit_option"); + + color_panel = memnew(PopupPanel); + add_child(color_panel); + color_picker = memnew(ColorPicker); + color_panel->add_child(color_picker); + color_panel->set_child_rect(color_picker); + color_picker->connect("color_changed", this, "_color_changed"); edit_hb = memnew (HBoxContainer); @@ -1279,8 +1387,8 @@ void ScriptTextEditor::register_editor() { ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD|KEY_A); ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT|KEY_UP); ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT|KEY_DOWN); - ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0); - ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0); + ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), KEY_MASK_ALT|KEY_LEFT); + ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), KEY_MASK_ALT|KEY_RIGHT); ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD|KEY_K); ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD|KEY_B); #ifdef OSX_ENABLED -- cgit v1.2.3 From d9c1729a8f1d3eceb259ef540b378b70beb55f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 10 Oct 2016 10:34:51 +0200 Subject: Add line length guideline to code editors --- tools/editor/plugins/script_text_editor.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tools/editor/plugins/script_text_editor.cpp') diff --git a/tools/editor/plugins/script_text_editor.cpp b/tools/editor/plugins/script_text_editor.cpp index 2f807eaa19..214582f759 100644 --- a/tools/editor/plugins/script_text_editor.cpp +++ b/tools/editor/plugins/script_text_editor.cpp @@ -254,6 +254,8 @@ void ScriptTextEditor::update_settings() { code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size")); code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs")); code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers")); + code_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline")); + code_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column")); code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting")); code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences")); code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink")); @@ -1281,6 +1283,8 @@ ScriptTextEditor::ScriptTextEditor() { code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size")); code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs")); code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers")); + code_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline")); + code_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column")); code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting")); code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences")); code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink")); -- cgit v1.2.3 From 0159e4f96918990ee7bc3e9616ba073e566ad6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 10 Oct 2016 10:38:12 +0200 Subject: Refactor duplicated code --- tools/editor/plugins/script_text_editor.cpp | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) (limited to 'tools/editor/plugins/script_text_editor.cpp') diff --git a/tools/editor/plugins/script_text_editor.cpp b/tools/editor/plugins/script_text_editor.cpp index 214582f759..40fc3a7bda 100644 --- a/tools/editor/plugins/script_text_editor.cpp +++ b/tools/editor/plugins/script_text_editor.cpp @@ -249,19 +249,7 @@ void ScriptTextEditor::add_callback(const String& p_function,StringArray p_args) void ScriptTextEditor::update_settings() { - code_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete")); - code_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file")); - code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size")); - code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs")); - code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers")); - code_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline")); - code_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column")); - code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting")); - code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences")); - code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink")); - code_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed")); - code_editor->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter")); - code_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret")); + code_editor->update_editor_settings(); } bool ScriptTextEditor::is_unsaved() { @@ -1277,20 +1265,8 @@ ScriptTextEditor::ScriptTextEditor() { code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled"); code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol"); + update_settings(); - code_editor->get_text_edit()->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file")); - code_editor->get_text_edit()->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete")); - code_editor->get_text_edit()->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size")); - code_editor->get_text_edit()->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs")); - code_editor->get_text_edit()->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers")); - code_editor->get_text_edit()->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline")); - code_editor->get_text_edit()->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column")); - code_editor->get_text_edit()->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting")); - code_editor->get_text_edit()->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences")); - code_editor->get_text_edit()->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink")); - code_editor->get_text_edit()->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed")); - code_editor->get_text_edit()->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter")); - code_editor->get_text_edit()->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret")); code_editor->get_text_edit()->set_callhint_settings( EditorSettings::get_singleton()->get("text_editor/put_callhint_tooltip_below_current_line"), EditorSettings::get_singleton()->get("text_editor/callhint_tooltip_offset")); -- cgit v1.2.3