Add EditorContextMenuPluginManager and refactor menu plugins

This commit is contained in:
kobewi 2024-09-03 23:07:19 +02:00
parent e2dd56bea7
commit ecc0ab8061
13 changed files with 288 additions and 252 deletions

View file

@ -32,9 +32,11 @@
#include "core/input/shortcut.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "scene/gui/popup_menu.h"
#include "scene/resources/texture.h"
void EditorContextMenuPlugin::add_options(const Vector<String> &p_paths) {
void EditorContextMenuPlugin::get_options(const Vector<String> &p_paths) {
GDVIRTUAL_CALL(_popup_menu, p_paths);
}
@ -42,24 +44,142 @@ void EditorContextMenuPlugin::add_menu_shortcut(const Ref<Shortcut> &p_shortcut,
context_menu_shortcuts.insert(p_shortcut, p_callable);
}
void EditorContextMenuPlugin::add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture, const Ref<Shortcut> &p_shortcut) {
void EditorContextMenuPlugin::add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture) {
ERR_FAIL_COND_MSG(context_menu_items.has(p_name), "Context menu item already registered.");
ERR_FAIL_COND_MSG(context_menu_items.size() == MAX_ITEMS, "Maximum number of context menu items reached.");
ContextMenuItem item;
item.item_name = p_name;
item.callable = p_callable;
item.icon = p_texture;
item.shortcut = p_shortcut;
item.idx = EditorData::CONTEXT_MENU_ITEM_ID_BASE + start_idx + context_menu_shortcuts.size() + context_menu_items.size();
context_menu_items.insert(p_name, item);
}
void EditorContextMenuPlugin::clear_context_menu_items() {
context_menu_items.clear();
void EditorContextMenuPlugin::add_context_menu_item_from_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut, const Ref<Texture2D> &p_texture) {
Callable *callback = context_menu_shortcuts.getptr(p_shortcut);
ERR_FAIL_NULL_MSG(callback, "Shortcut not registered. Use add_menu_shortcut() first.");
ContextMenuItem item;
item.item_name = p_name;
item.callable = *callback;
item.icon = p_texture;
item.shortcut = p_shortcut;
context_menu_items.insert(p_name, item);
}
void EditorContextMenuPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_menu_shortcut", "shortcut", "callback"), &EditorContextMenuPlugin::add_menu_shortcut);
ClassDB::bind_method(D_METHOD("add_context_menu_item", "name", "callback", "icon", "shortcut"), &EditorContextMenuPlugin::add_context_menu_item, DEFVAL(Ref<Texture2D>()), DEFVAL(Ref<Shortcut>()));
ClassDB::bind_method(D_METHOD("add_context_menu_item", "name", "callback", "icon"), &EditorContextMenuPlugin::add_context_menu_item, DEFVAL(Ref<Texture2D>()));
ClassDB::bind_method(D_METHOD("add_context_menu_item_from_shortcut", "name", "shortcut", "icon"), &EditorContextMenuPlugin::add_context_menu_item_from_shortcut, DEFVAL(Ref<Texture2D>()));
GDVIRTUAL_BIND(_popup_menu, "paths");
BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCENE_TREE);
BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM);
BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM_CREATE);
BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCRIPT_EDITOR);
}
void EditorContextMenuPluginManager::add_plugin(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin) {
ERR_FAIL_COND(p_plugin.is_null());
ERR_FAIL_COND(plugin_list.has(p_plugin));
p_plugin->slot = p_slot;
plugin_list.push_back(p_plugin);
}
void EditorContextMenuPluginManager::remove_plugin(const Ref<EditorContextMenuPlugin> &p_plugin) {
ERR_FAIL_COND(p_plugin.is_null());
ERR_FAIL_COND(!plugin_list.has(p_plugin));
plugin_list.erase(p_plugin);
}
void EditorContextMenuPluginManager::add_options_from_plugins(PopupMenu *p_popup, ContextMenuSlot p_slot, const Vector<String> &p_paths) {
bool separator_added = false;
const int icon_size = p_popup->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
int id = EditorContextMenuPlugin::BASE_ID;
for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
if (plugin->slot != p_slot) {
continue;
}
plugin->context_menu_items.clear();
plugin->get_options(p_paths);
HashMap<String, EditorContextMenuPlugin::ContextMenuItem> &items = plugin->context_menu_items;
if (items.size() > 0 && !separator_added) {
separator_added = true;
p_popup->add_separator();
}
for (KeyValue<String, EditorContextMenuPlugin::ContextMenuItem> &E : items) {
EditorContextMenuPlugin::ContextMenuItem &item = E.value;
item.id = id;
if (item.icon.is_valid()) {
p_popup->add_icon_item(item.icon, item.item_name, id);
p_popup->set_item_icon_max_width(-1, icon_size);
} else {
p_popup->add_item(item.item_name, id);
}
if (item.shortcut.is_valid()) {
p_popup->set_item_shortcut(-1, item.shortcut, true);
}
id++;
}
}
}
Callable EditorContextMenuPluginManager::match_custom_shortcut(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<InputEvent> &p_event) {
for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
if (plugin->slot != p_slot) {
continue;
}
for (KeyValue<Ref<Shortcut>, Callable> &E : plugin->context_menu_shortcuts) {
if (E.key->matches_event(p_event)) {
return E.value;
}
}
}
return Callable();
}
bool EditorContextMenuPluginManager::activate_custom_option(ContextMenuSlot p_slot, int p_option, const Variant &p_arg) {
for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
if (plugin->slot != p_slot) {
continue;
}
for (KeyValue<String, EditorContextMenuPlugin::ContextMenuItem> &E : plugin->context_menu_items) {
if (E.value.id == p_option) {
invoke_callback(E.value.callable, p_arg);
return true;
}
}
}
return false;
}
void EditorContextMenuPluginManager::invoke_callback(const Callable &p_callback, const Variant &p_arg) {
const Variant *argptr = &p_arg;
Callable::CallError ce;
Variant result;
p_callback.callp(&argptr, 1, result, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_MSG("Failed to execute context menu callback: " + Variant::get_callable_error_text(p_callback, &argptr, 1, ce) + ".");
}
}
void EditorContextMenuPluginManager::create() {
ERR_FAIL_COND(singleton != nullptr);
singleton = memnew(EditorContextMenuPluginManager);
}
void EditorContextMenuPluginManager::cleanup() {
ERR_FAIL_NULL(singleton);
memdelete(singleton);
singleton = nullptr;
}

View file

@ -34,19 +34,33 @@
#include "core/object/gdvirtual.gen.inc"
#include "core/object/ref_counted.h"
class Texture2D;
class InputEvent;
class PopupMenu;
class Shortcut;
class Texture2D;
class EditorContextMenuPlugin : public RefCounted {
GDCLASS(EditorContextMenuPlugin, RefCounted);
public:
int start_idx;
friend class EditorContextMenuPluginManager;
inline static constexpr int MAX_ITEMS = 100;
public:
enum ContextMenuSlot {
CONTEXT_SLOT_SCENE_TREE,
CONTEXT_SLOT_FILESYSTEM,
CONTEXT_SLOT_SCRIPT_EDITOR,
CONTEXT_SLOT_FILESYSTEM_CREATE,
};
inline static constexpr int BASE_ID = 2000;
private:
int slot = -1;
public:
struct ContextMenuItem {
int idx = 0;
int id = 0;
String item_name;
Callable callable;
Ref<Texture2D> icon;
@ -61,10 +75,37 @@ protected:
GDVIRTUAL1(_popup_menu, Vector<String>);
public:
virtual void add_options(const Vector<String> &p_paths);
virtual void get_options(const Vector<String> &p_paths);
void add_menu_shortcut(const Ref<Shortcut> &p_shortcut, const Callable &p_callable);
void add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture, const Ref<Shortcut> &p_shortcut);
void clear_context_menu_items();
void add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture);
void add_context_menu_item_from_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut, const Ref<Texture2D> &p_texture);
};
VARIANT_ENUM_CAST(EditorContextMenuPlugin::ContextMenuSlot);
class EditorContextMenuPluginManager : public Object {
GDCLASS(EditorContextMenuPluginManager, Object);
using ContextMenuSlot = EditorContextMenuPlugin::ContextMenuSlot;
static inline EditorContextMenuPluginManager *singleton = nullptr;
LocalVector<Ref<EditorContextMenuPlugin>> plugin_list;
public:
static EditorContextMenuPluginManager *get_singleton() { return singleton; }
void add_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin);
void remove_plugin(const Ref<EditorContextMenuPlugin> &p_plugin);
void add_options_from_plugins(PopupMenu *p_popup, ContextMenuSlot p_slot, const Vector<String> &p_paths);
Callable match_custom_shortcut(ContextMenuSlot p_slot, const Ref<InputEvent> &p_event);
bool activate_custom_option(ContextMenuSlot p_slot, int p_option, const Variant &p_arg);
void invoke_callback(const Callable &p_callback, const Variant &p_arg);
static void create();
static void cleanup();
};
#endif // EDITOR_CONTEXT_MENU_PLUGIN_H

View file

@ -47,7 +47,6 @@
#include "editor/import/editor_import_plugin.h"
#include "editor/inspector_dock.h"
#include "editor/plugins/canvas_item_editor_plugin.h"
#include "editor/plugins/editor_context_menu_plugin.h"
#include "editor/plugins/editor_debugger_plugin.h"
#include "editor/plugins/editor_resource_conversion_plugin.h"
#include "editor/plugins/node_3d_editor_plugin.h"
@ -491,14 +490,12 @@ void EditorPlugin::remove_scene_post_import_plugin(const Ref<EditorScenePostImpo
ResourceImporterScene::remove_post_importer_plugin(p_plugin);
}
void EditorPlugin::add_context_menu_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin) {
ERR_FAIL_COND(p_plugin.is_null());
EditorNode::get_editor_data().add_context_menu_plugin(EditorData::ContextMenuSlot(p_slot), p_plugin);
void EditorPlugin::add_context_menu_plugin(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin) {
EditorContextMenuPluginManager::get_singleton()->add_plugin(p_slot, p_plugin);
}
void EditorPlugin::remove_context_menu_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin) {
ERR_FAIL_COND(p_plugin.is_null());
EditorNode::get_editor_data().remove_context_menu_plugin(EditorData::ContextMenuSlot(p_slot), p_plugin);
void EditorPlugin::remove_context_menu_plugin(const Ref<EditorContextMenuPlugin> &p_plugin) {
EditorContextMenuPluginManager::get_singleton()->remove_plugin(p_plugin);
}
int find(const PackedStringArray &a, const String &v) {
@ -641,7 +638,7 @@ void EditorPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_input_event_forwarding_always_enabled"), &EditorPlugin::set_input_event_forwarding_always_enabled);
ClassDB::bind_method(D_METHOD("set_force_draw_over_forwarding_enabled"), &EditorPlugin::set_force_draw_over_forwarding_enabled);
ClassDB::bind_method(D_METHOD("add_context_menu_plugin", "slot", "plugin"), &EditorPlugin::add_context_menu_plugin);
ClassDB::bind_method(D_METHOD("remove_context_menu_plugin", "slot", "plugin"), &EditorPlugin::remove_context_menu_plugin);
ClassDB::bind_method(D_METHOD("remove_context_menu_plugin", "plugin"), &EditorPlugin::remove_context_menu_plugin);
ClassDB::bind_method(D_METHOD("get_editor_interface"), &EditorPlugin::get_editor_interface);
ClassDB::bind_method(D_METHOD("get_script_create_dialog"), &EditorPlugin::get_script_create_dialog);
@ -707,11 +704,6 @@ void EditorPlugin::_bind_methods() {
BIND_ENUM_CONSTANT(AFTER_GUI_INPUT_PASS);
BIND_ENUM_CONSTANT(AFTER_GUI_INPUT_STOP);
BIND_ENUM_CONSTANT(AFTER_GUI_INPUT_CUSTOM);
BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCENE_TREE);
BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM);
BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCRIPT_EDITOR);
BIND_ENUM_CONSTANT(CONTEXT_SUBMENU_SLOT_FILESYSTEM_CREATE);
}
EditorUndoRedoManager *EditorPlugin::get_undo_redo() {

View file

@ -32,6 +32,7 @@
#define EDITOR_PLUGIN_H
#include "core/io/config_file.h"
#include "editor/plugins/editor_context_menu_plugin.h"
#include "scene/3d/camera_3d.h"
#include "scene/gui/control.h"
@ -53,7 +54,6 @@ class EditorToolAddons;
class EditorTranslationParserPlugin;
class EditorUndoRedoManager;
class ScriptCreateDialog;
class EditorContextMenuPlugin;
class EditorPlugin : public Node {
GDCLASS(EditorPlugin, Node);
@ -103,13 +103,6 @@ public:
AFTER_GUI_INPUT_CUSTOM,
};
enum ContextMenuSlot {
CONTEXT_SLOT_SCENE_TREE,
CONTEXT_SLOT_FILESYSTEM,
CONTEXT_SLOT_SCRIPT_EDITOR,
CONTEXT_SUBMENU_SLOT_FILESYSTEM_CREATE,
};
protected:
void _notification(int p_what);
@ -257,8 +250,8 @@ public:
void add_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin);
void remove_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin);
void add_context_menu_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin);
void remove_context_menu_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin);
void add_context_menu_plugin(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin);
void remove_context_menu_plugin(const Ref<EditorContextMenuPlugin> &p_plugin);
void enable_plugin();
void disable_plugin();
@ -270,7 +263,6 @@ public:
VARIANT_ENUM_CAST(EditorPlugin::CustomControlContainer);
VARIANT_ENUM_CAST(EditorPlugin::DockSlot);
VARIANT_ENUM_CAST(EditorPlugin::AfterGUIInput);
VARIANT_ENUM_CAST(EditorPlugin::ContextMenuSlot);
typedef EditorPlugin *(*EditorPluginCreateFunc)();

View file

@ -1399,13 +1399,12 @@ void ScriptEditor::_menu_option(int p_option) {
}
}
// Context menu options.
if (p_option >= EditorData::CONTEXT_MENU_ITEM_ID_BASE) {
if (p_option >= EditorContextMenuPlugin::BASE_ID) {
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);
EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR, p_option, resource);
return;
}
@ -3315,10 +3314,16 @@ 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);
Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR, p_event);
if (custom_callback.is_valid()) {
Ref<Resource> resource;
ScriptEditorBase *current = _get_current_editor();
if (current) {
resource = current->get_edited_resource();
}
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, resource);
accept_event();
}
}
@ -3387,7 +3392,7 @@ void ScriptEditor::_make_script_list_context_menu() {
selected_paths.push_back(path);
}
}
EditorNode::get_editor_data().add_options_from_plugins(context_menu, EditorData::CONTEXT_SLOT_SCRIPT_EDITOR, selected_paths);
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();