Add support for custom items to editor right-click context menus

This commit is contained in:
citizenl 2024-07-21 16:43:53 +08:00
parent e25f3c0d38
commit 6b2348adac
12 changed files with 470 additions and 0 deletions

View file

@ -57,6 +57,7 @@
#include "editor/gui/editor_toaster.h"
#include "editor/inspector_dock.h"
#include "editor/node_dock.h"
#include "editor/plugins/editor_context_menu_plugin.h"
#include "editor/plugins/shader_editor_plugin.h"
#include "editor/plugins/text_shader_editor.h"
#include "editor/themes/editor_scale.h"
@ -1394,6 +1395,16 @@ void ScriptEditor::_menu_option(int p_option) {
}
}
// Context menu options.
if (p_option >= EditorData::CONTEXT_MENU_ITEM_ID_BASE) {
Ref<Resource> resource;
if (current) {
resource = current->get_edited_resource();
}
EditorNode::get_editor_data().script_editor_options_pressed(EditorData::CONTEXT_SLOT_SCRIPT_EDITOR, p_option, resource);
return;
}
if (current) {
switch (p_option) {
case FILE_SAVE: {
@ -3260,6 +3271,11 @@ void ScriptEditor::shortcut_input(const Ref<InputEvent> &p_event) {
_menu_option(WINDOW_MOVE_DOWN);
accept_event();
}
// Context menu shortcuts.
int match_option = EditorNode::get_editor_data().match_context_menu_shortcut(EditorData::CONTEXT_SLOT_SCRIPT_EDITOR, p_event);
if (match_option) {
_menu_option(match_option);
}
}
void ScriptEditor::_script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
@ -3318,6 +3334,17 @@ void ScriptEditor::_make_script_list_context_menu() {
context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_MOVE_DOWN), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_SORT), tab_container->get_tab_count() <= 1);
// Context menu plugin.
Vector<String> selected_paths;
if (se) {
Ref<Resource> scr = se->get_edited_resource();
if (scr.is_valid()) {
String path = scr->get_path();
selected_paths.push_back(path);
}
}
EditorNode::get_editor_data().add_options_from_plugins(context_menu, EditorData::CONTEXT_SLOT_SCRIPT_EDITOR, selected_paths);
context_menu->set_position(get_screen_position() + get_local_mouse_position());
context_menu->reset_size();
context_menu->popup();