/**************************************************************************/ /* script_editor_plugin.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "script_editor_plugin.h" #include "core/config/project_settings.h" #include "core/io/config_file.h" #include "core/io/file_access.h" #include "core/io/json.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" #include "core/os/os.h" #include "core/string/fuzzy_search.h" #include "core/variant/dictionary.h" #include "core/version.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/debugger/script_editor_debugger.h" #include "editor/doc/editor_help.h" #include "editor/doc/editor_help_search.h" #include "editor/docks/filesystem_dock.h" #include "editor/docks/inspector_dock.h" #include "editor/editor_interface.h" #include "editor/editor_main_screen.h" #include "editor/editor_node.h" #include "editor/editor_string_names.h" #include "editor/file_system/editor_paths.h" #include "editor/gui/code_editor.h" #include "editor/gui/editor_file_dialog.h" #include "editor/gui/editor_toaster.h" #include "editor/gui/filter_line_edit.h" #include "editor/gui/window_wrapper.h" #include "editor/inspector/editor_context_menu_plugin.h" #include "editor/run/editor_run_bar.h" #include "editor/scene/editor_scene_tabs.h" #include "editor/script/find_in_files.h" #include "editor/script/script_editor_base.h" #include "editor/script/script_editor_navigation_marker.h" #include "editor/script/script_text_editor.h" #include "editor/script/syntax_highlighters.h" #include "editor/script/text_editor.h" #include "editor/settings/editor_command_palette.h" #include "editor/settings/editor_settings.h" #include "editor/shader/shader_create_dialog.h" #include "editor/shader/shader_editor_plugin.h" #include "editor/shader/shader_text_editor.h" #include "editor/themes/editor_scale.h" #include "editor/themes/editor_theme_manager.h" #include "scene/gui/separator.h" #include "scene/gui/tab_container.h" #include "scene/gui/texture_rect.h" #include "scene/main/node.h" #include "scene/main/scene_tree.h" #include "scene/main/window.h" #include "servers/display/display_server.h" void ScriptEditorQuickOpen::popup_dialog(const Vector &p_functions, bool p_dontclear) { popup_centered_ratio(0.6); if (p_dontclear) { search_box->select_all(); } else { search_box->clear(); } search_box->grab_focus(); functions = p_functions; _update_search(); } void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) { _update_search(); } void ScriptEditorQuickOpen::_update_search() { search_options->clear(); TreeItem *root = search_options->create_item(); for (int i = 0; i < functions.size(); i++) { String file = functions[i]; if ((search_box->get_text().is_empty() || file.containsn(search_box->get_text()))) { TreeItem *ti = search_options->create_item(root); ti->set_text(0, file); if (root->get_first_child() == ti) { ti->select(0); } } } get_ok_button()->set_disabled(root->get_first_child() == nullptr); } void ScriptEditorQuickOpen::_confirmed() { TreeItem *ti = search_options->get_selected(); if (!ti) { return; } int line = ti->get_text(0).get_slicec(':', 1).to_int(); emit_signal(SNAME("goto_line"), line - 1); hide(); } void ScriptEditorQuickOpen::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { connect(SceneStringName(confirmed), callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); } break; case NOTIFICATION_EXIT_TREE: { disconnect(SceneStringName(confirmed), callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); } break; } } void ScriptEditorQuickOpen::_bind_methods() { ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line"))); } ScriptEditorQuickOpen::ScriptEditorQuickOpen() { set_ok_button_text(TTRC("Open")); get_ok_button()->set_disabled(true); set_hide_on_ok(false); VBoxContainer *vbc = memnew(VBoxContainer); add_child(vbc); search_box = memnew(FilterLineEdit); vbc->add_margin_child(TTRC("Search:"), search_box); search_box->connect(SceneStringName(text_changed), callable_mp(this, &ScriptEditorQuickOpen::_text_changed)); register_text_enter(search_box); search_options = memnew(Tree); search_box->set_forward_control(search_options); search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); search_options->set_hide_root(true); search_options->set_hide_folding(true); search_options->add_theme_constant_override("draw_guides", 1); vbc->add_margin_child(TTRC("Matches:"), search_options, true); search_options->connect("item_activated", callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); } ///////////////////////////////// void DocumentList::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: { filter->set_right_icon(get_editor_theme_icon(SNAME("Search"))); item_list->set_fixed_icon_size(Vector2i(1, 1) * get_theme_constant("class_icon_size", EditorStringName(Editor))); const Ref &help_icon = get_editor_theme_icon(SNAME("Help")); for (ItemData &item : items) { Control *editor = script_editor->tab_container->get_tab_control(item.index); ScriptEditorBase *seb = Object::cast_to(editor); EditorHelp *eh = Object::cast_to(editor); if (seb) { item.icon = seb->get_theme_icon(); } else if (eh) { item.icon = help_icon; } } _update_item_list(); } break; } } Variant DocumentList::get_drag_data_fw(const Point2 &p_point, Control *p_from) { Node *cur_node = script_editor->tab_container->get_current_tab_control(); if (cur_node == nullptr) { return Variant(); } HBoxContainer *drag_preview = memnew(HBoxContainer); String preview_name; Ref preview_icon; ScriptEditorBase *seb = Object::cast_to(cur_node); if (seb) { preview_name = seb->get_document_name(); preview_icon = seb->get_theme_icon(); } EditorHelp *eh = Object::cast_to(cur_node); if (eh) { preview_name = eh->get_class(); preview_icon = get_editor_theme_icon(SNAME("Help")); } if (preview_icon.is_valid()) { TextureRect *tf = memnew(TextureRect); tf->set_texture(preview_icon); tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED); drag_preview->add_child(tf); } Label *label = memnew(Label(preview_name)); label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate script names and class names. drag_preview->add_child(label); set_drag_preview(drag_preview); Dictionary drag_data; drag_data["type"] = "script_list_element"; drag_data["script_list_element"] = cur_node; drag_data["editor"] = script_editor->config_section; return drag_data; } bool DocumentList::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { Dictionary d = p_data; const String type = d.get("type", ""); if (type == "script_list_element") { if (d["editor"] != script_editor->config_section) { return false; } Node *node = Object::cast_to(d["script_list_element"]); if (Object::cast_to(node) || Object::cast_to(node)) { return true; } } if (type == "files") { Vector files = d["files"]; if (files.is_empty()) { return false; // Weird. } for (const String &file : files) { if (script_editor->can_open_file(file)) { return true; } } return false; } return false; } void DocumentList::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { if (!can_drop_data_fw(p_point, p_data, p_from)) { return; } Dictionary d = p_data; const String type = d.get("type", ""); if (type == "script_list_element") { Node *node = Object::cast_to(d["script_list_element"]); ScriptEditorBase *seb = Object::cast_to(node); EditorHelp *eh = Object::cast_to(node); if (seb || eh) { int new_index = 0; if (item_list->get_item_count() > 0) { int pos = 0; if (p_point == Vector2(Math::INF, Math::INF)) { if (item_list->is_anything_selected()) { pos = item_list->get_selected_items()[0]; } } else { pos = item_list->get_item_at_position(p_point); } new_index = item_list->get_item_metadata(pos); } TabContainer *tab_container = script_editor->tab_container; tab_container->move_child(node, new_index); tab_container->set_current_tab(new_index); update_list(); } } if (type == "files") { Vector files = d["files"]; int new_index = 0; if (item_list->get_item_count() > 0) { int pos = 0; if (p_point == Vector2(Math::INF, Math::INF)) { if (item_list->is_anything_selected()) { pos = item_list->get_selected_items()[0]; } } else { pos = item_list->get_item_at_position(p_point); } new_index = item_list->get_item_metadata(pos); } TabContainer *tab_container = script_editor->tab_container; int num_tabs_before = tab_container->get_tab_count(); for (const String &file : files) { if (!script_editor->can_open_file(file)) { continue; } Ref res = script_editor->open_file(file); if (res.is_valid()) { const int num_tabs = tab_container->get_tab_count(); if (num_tabs > num_tabs_before) { tab_container->move_child(tab_container->get_tab_control(tab_container->get_tab_count() - 1), new_index); num_tabs_before = num_tabs; } else if (num_tabs > 0) { // Maybe script was already open. tab_container->move_child(tab_container->get_current_tab_control(), new_index); } } } if (tab_container->get_tab_count() > 0) { tab_container->set_current_tab(new_index); } update_list(); } } void DocumentList::_document_selected(int p_index) { script_editor->_go_to_tab(item_list->get_item_metadata(p_index), true); } void DocumentList::_document_clicked(int p_index, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) { if (p_mouse_button_index == MouseButton::MIDDLE) { _document_selected(p_index); script_editor->_menu_option(ScriptEditor::FILE_MENU_CLOSE); } if (p_mouse_button_index == MouseButton::RIGHT) { _show_context_menu(); } } void DocumentList::_show_context_menu() { context_menu->clear(); int selected = script_editor->tab_container->get_current_tab(); if (selected < 0) { return; } ScriptEditorBase *seb = Object::cast_to(script_editor->tab_container->get_tab_control(selected)); script_editor->_setup_popup_menu(context_menu, true); script_editor->_prepare_popup_menu(context_menu, true); // Context menu plugin. PackedStringArray selected_paths; if (seb) { Ref res = seb->get_edited_resource(); if (res.is_valid()) { String path = res->get_path(); selected_paths.push_back(path); } } EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR, selected_paths); context_menu->set_position(get_screen_position() + get_local_mouse_position()); context_menu->reset_size(); context_menu->popup(); } static void _find_scripts(Node *p_base, Node *p_current, HashSet> &r_used_scripts) { if (p_current != p_base && p_current->get_owner() != p_base) { return; } if (p_current->get_script_instance()) { Ref