Debian Codegraph @

editor/inspector/editor_context_menu_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_context_menu_plugin.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "editor_context_menu_plugin.h"
32
33 #include "core/input/shortcut.h"
34 #include "editor/editor_string_names.h"
35 #include "scene/gui/popup_menu.h"
36 #include "scene/resources/texture.h"
37
38 void EditorContextMenuPlugin::get_options(const Vector<String> &p_paths) {
39 GDVIRTUAL_CALL(_popup_menu, p_paths);
40 }
41
42 void EditorContextMenuPlugin::add_menu_shortcut(const Ref<Shortcut> &p_shortcut, const Callable &p_callable) {
43 context_menu_shortcuts.insert(p_shortcut, p_callable);
44 }
45
46 void EditorContextMenuPlugin::add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture) {
47 ERR_FAIL_COND_MSG(context_menu_items.has(p_name), "Context menu item already registered.");
48 ERR_FAIL_COND_MSG(context_menu_items.size() == MAX_ITEMS, "Maximum number of context menu items reached.");
49
50 ContextMenuItem item;
51 item.item_name = p_name;
52 item.callable = p_callable;
53 item.icon = p_texture;
54 context_menu_items.insert(p_name, item);
55 }
56
57 void EditorContextMenuPlugin::add_context_menu_item_from_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut, const Ref<Texture2D> &p_texture) {
58 Callable *callback = context_menu_shortcuts.getptr(p_shortcut);
59 ERR_FAIL_NULL_MSG(callback, "Shortcut not registered. Use add_menu_shortcut() first.");
60
61 ContextMenuItem item;
62 item.item_name = p_name;
63 item.callable = *callback;
64 item.icon = p_texture;
65 item.shortcut = p_shortcut;
66 context_menu_items.insert(p_name, item);
67 }
68
69 void EditorContextMenuPlugin::add_context_submenu_item(const String &p_name, PopupMenu *p_menu, const Ref<Texture2D> &p_texture) {
70 ERR_FAIL_NULL(p_menu);
71
72 ContextMenuItem item;
73 item.item_name = p_name;
74 item.icon = p_texture;
75 item.submenu = p_menu;
76 context_menu_items.insert(p_name, item);
77 }
78
79 void EditorContextMenuPlugin::_bind_methods() {
80 ClassDB::bind_method(D_METHOD("add_menu_shortcut", "shortcut", "callback"), &EditorContextMenuPlugin::add_menu_shortcut);
81 ClassDB::bind_method(D_METHOD("add_context_menu_item", "name", "callback", "icon"), &EditorContextMenuPlugin::add_context_menu_item, DEFVAL(Ref<Texture2D>()));
82 ClassDB::bind_method(D_METHOD("add_context_menu_item_from_shortcut", "name", "shortcut", "icon"), &EditorContextMenuPlugin::add_context_menu_item_from_shortcut, DEFVAL(Ref<Texture2D>()));
83 ClassDB::bind_method(D_METHOD("add_context_submenu_item", "name", "menu", "icon"), &EditorContextMenuPlugin::add_context_submenu_item, DEFVAL(Ref<Texture2D>()));
84
85 GDVIRTUAL_BIND(_popup_menu, "paths");
86
87 BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCENE_TREE);
88 BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM);
89 BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCRIPT_EDITOR);
90 BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM_CREATE);
91 BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCRIPT_EDITOR_CODE);
92 BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCENE_TABS);
93 BIND_ENUM_CONSTANT(CONTEXT_SLOT_2D_EDITOR);
94 }
95
96 void EditorContextMenuPluginManager::add_plugin(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin) {
97 ERR_FAIL_COND(p_plugin.is_null());
98 ERR_FAIL_COND(plugin_list.has(p_plugin));
99
100 p_plugin->slot = p_slot;
101 plugin_list.push_back(p_plugin);
102 }
103
104 void EditorContextMenuPluginManager::remove_plugin(const Ref<EditorContextMenuPlugin> &p_plugin) {
105 ERR_FAIL_COND(p_plugin.is_null());
106 ERR_FAIL_COND(!plugin_list.has(p_plugin));
107
108 plugin_list.erase(p_plugin);
109 }
110
111 bool EditorContextMenuPluginManager::has_plugins_for_slot(ContextMenuSlot p_slot) {
112 for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
113 if (plugin->slot == p_slot) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120 void EditorContextMenuPluginManager::add_options_from_plugins(PopupMenu *p_popup, ContextMenuSlot p_slot, const Vector<String> &p_paths, int p_id_offset) {
121 bool separator_added = false;
122 const int icon_size = p_popup->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
123 int id = EditorContextMenuPlugin::BASE_ID + p_id_offset;
124
125 for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
126 if (plugin->slot != p_slot) {
127 continue;
128 }
129 plugin->context_menu_items.clear();
130 plugin->get_options(p_paths);
131
132 HashMap<String, EditorContextMenuPlugin::ContextMenuItem> &items = plugin->context_menu_items;
133 if (items.size() > 0 && !separator_added) {
134 separator_added = true;
135 p_popup->add_separator();
136 }
137
138 for (KeyValue<String, EditorContextMenuPlugin::ContextMenuItem> &E : items) {
139 EditorContextMenuPlugin::ContextMenuItem &item = E.value;
140 item.id = id;
141
142 if (item.submenu) {
143 p_popup->add_submenu_node_item(item.item_name, item.submenu, id);
144 } else {
145 p_popup->add_item(item.item_name, id);
146 }
147
148 if (item.icon.is_valid()) {
149 p_popup->set_item_icon(-1, item.icon);
150 p_popup->set_item_icon_max_width(-1, icon_size);
151 }
152
153 if (item.shortcut.is_valid()) {
154 p_popup->set_item_shortcut(-1, item.shortcut, true);
155 }
156 id++;
157 }
158 }
159 }
160
161 Callable EditorContextMenuPluginManager::match_custom_shortcut(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<InputEvent> &p_event) {
162 for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
163 if (plugin->slot != p_slot) {
164 continue;
165 }
166
167 for (KeyValue<Ref<Shortcut>, Callable> &E : plugin->context_menu_shortcuts) {
168 if (E.key->matches_event(p_event)) {
169 return E.value;
170 }
171 }
172 }
173 return Callable();
174 }
175
176 bool EditorContextMenuPluginManager::activate_custom_option(ContextMenuSlot p_slot, int p_option, const Variant &p_arg) {
177 for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
178 if (plugin->slot != p_slot) {
179 continue;
180 }
181
182 for (KeyValue<String, EditorContextMenuPlugin::ContextMenuItem> &E : plugin->context_menu_items) {
183 if (E.value.id == p_option) {
184 invoke_callback(E.value.callable, p_arg);
185 return true;
186 }
187 }
188 }
189 return false;
190 }
191
192 void EditorContextMenuPluginManager::invoke_callback(const Callable &p_callback, const Variant &p_arg) {
193 const Variant *argptr = &p_arg;
194 Callable::CallError ce;
195 Variant result;
196 p_callback.callp(&argptr, 1, result, ce);
197
198 if (ce.error != Callable::CallError::CALL_OK) {
199 ERR_FAIL_MSG("Failed to execute context menu callback: " + Variant::get_callable_error_text(p_callback, &argptr, 1, ce) + ".");
200 }
201 }
202
203 void EditorContextMenuPluginManager::create() {
204 ERR_FAIL_COND(singleton != nullptr);
205 singleton = memnew(EditorContextMenuPluginManager);
206 }
207
208 void EditorContextMenuPluginManager::cleanup() {
209 ERR_FAIL_NULL(singleton);
210 memdelete(singleton);
211 singleton = nullptr;
212 }