editor_context_menu_plugin.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "editor_context_menu_plugin.h"
  31. #include "core/input/shortcut.h"
  32. #include "editor/editor_string_names.h"
  33. #include "scene/gui/popup_menu.h"
  34. #include "scene/resources/texture.h"
  35. void EditorContextMenuPlugin::get_options(const Vector<String> &p_paths) {
  36. GDVIRTUAL_CALL(_popup_menu, p_paths);
  37. }
  38. void EditorContextMenuPlugin::add_menu_shortcut(const Ref<Shortcut> &p_shortcut, const Callable &p_callable) {
  39. context_menu_shortcuts.insert(p_shortcut, p_callable);
  40. }
  41. void EditorContextMenuPlugin::add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture) {
  42. ERR_FAIL_COND_MSG(context_menu_items.has(p_name), "Context menu item already registered.");
  43. ERR_FAIL_COND_MSG(context_menu_items.size() == MAX_ITEMS, "Maximum number of context menu items reached.");
  44. ContextMenuItem item;
  45. item.item_name = p_name;
  46. item.callable = p_callable;
  47. item.icon = p_texture;
  48. context_menu_items.insert(p_name, item);
  49. }
  50. void EditorContextMenuPlugin::add_context_menu_item_from_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut, const Ref<Texture2D> &p_texture) {
  51. Callable *callback = context_menu_shortcuts.getptr(p_shortcut);
  52. ERR_FAIL_NULL_MSG(callback, "Shortcut not registered. Use add_menu_shortcut() first.");
  53. ContextMenuItem item;
  54. item.item_name = p_name;
  55. item.callable = *callback;
  56. item.icon = p_texture;
  57. item.shortcut = p_shortcut;
  58. context_menu_items.insert(p_name, item);
  59. }
  60. void EditorContextMenuPlugin::add_context_submenu_item(const String &p_name, PopupMenu *p_menu, const Ref<Texture2D> &p_texture) {
  61. ERR_FAIL_NULL(p_menu);
  62. ContextMenuItem item;
  63. item.item_name = p_name;
  64. item.icon = p_texture;
  65. item.submenu = p_menu;
  66. context_menu_items.insert(p_name, item);
  67. }
  68. void EditorContextMenuPlugin::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("add_menu_shortcut", "shortcut", "callback"), &EditorContextMenuPlugin::add_menu_shortcut);
  70. ClassDB::bind_method(D_METHOD("add_context_menu_item", "name", "callback", "icon"), &EditorContextMenuPlugin::add_context_menu_item, DEFVAL(Ref<Texture2D>()));
  71. ClassDB::bind_method(D_METHOD("add_context_menu_item_from_shortcut", "name", "shortcut", "icon"), &EditorContextMenuPlugin::add_context_menu_item_from_shortcut, DEFVAL(Ref<Texture2D>()));
  72. ClassDB::bind_method(D_METHOD("add_context_submenu_item", "name", "menu", "icon"), &EditorContextMenuPlugin::add_context_submenu_item, DEFVAL(Ref<Texture2D>()));
  73. GDVIRTUAL_BIND(_popup_menu, "paths");
  74. BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCENE_TREE);
  75. BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM);
  76. BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCRIPT_EDITOR);
  77. BIND_ENUM_CONSTANT(CONTEXT_SLOT_FILESYSTEM_CREATE);
  78. BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCRIPT_EDITOR_CODE);
  79. BIND_ENUM_CONSTANT(CONTEXT_SLOT_SCENE_TABS);
  80. BIND_ENUM_CONSTANT(CONTEXT_SLOT_2D_EDITOR);
  81. }
  82. void EditorContextMenuPluginManager::add_plugin(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin) {
  83. ERR_FAIL_COND(p_plugin.is_null());
  84. ERR_FAIL_COND(plugin_list.has(p_plugin));
  85. p_plugin->slot = p_slot;
  86. plugin_list.push_back(p_plugin);
  87. }
  88. void EditorContextMenuPluginManager::remove_plugin(const Ref<EditorContextMenuPlugin> &p_plugin) {
  89. ERR_FAIL_COND(p_plugin.is_null());
  90. ERR_FAIL_COND(!plugin_list.has(p_plugin));
  91. plugin_list.erase(p_plugin);
  92. }
  93. bool EditorContextMenuPluginManager::has_plugins_for_slot(ContextMenuSlot p_slot) {
  94. for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
  95. if (plugin->slot == p_slot) {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. void EditorContextMenuPluginManager::add_options_from_plugins(PopupMenu *p_popup, ContextMenuSlot p_slot, const Vector<String> &p_paths) {
  102. bool separator_added = false;
  103. const int icon_size = p_popup->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  104. int id = EditorContextMenuPlugin::BASE_ID;
  105. for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
  106. if (plugin->slot != p_slot) {
  107. continue;
  108. }
  109. plugin->context_menu_items.clear();
  110. plugin->get_options(p_paths);
  111. HashMap<String, EditorContextMenuPlugin::ContextMenuItem> &items = plugin->context_menu_items;
  112. if (items.size() > 0 && !separator_added) {
  113. separator_added = true;
  114. p_popup->add_separator();
  115. }
  116. for (KeyValue<String, EditorContextMenuPlugin::ContextMenuItem> &E : items) {
  117. EditorContextMenuPlugin::ContextMenuItem &item = E.value;
  118. item.id = id;
  119. if (item.submenu) {
  120. p_popup->add_submenu_node_item(item.item_name, item.submenu, id);
  121. } else {
  122. p_popup->add_item(item.item_name, id);
  123. }
  124. if (item.icon.is_valid()) {
  125. p_popup->set_item_icon(-1, item.icon);
  126. p_popup->set_item_icon_max_width(-1, icon_size);
  127. }
  128. if (item.shortcut.is_valid()) {
  129. p_popup->set_item_shortcut(-1, item.shortcut, true);
  130. }
  131. id++;
  132. }
  133. }
  134. }
  135. Callable EditorContextMenuPluginManager::match_custom_shortcut(EditorContextMenuPlugin::ContextMenuSlot p_slot, const Ref<InputEvent> &p_event) {
  136. for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
  137. if (plugin->slot != p_slot) {
  138. continue;
  139. }
  140. for (KeyValue<Ref<Shortcut>, Callable> &E : plugin->context_menu_shortcuts) {
  141. if (E.key->matches_event(p_event)) {
  142. return E.value;
  143. }
  144. }
  145. }
  146. return Callable();
  147. }
  148. bool EditorContextMenuPluginManager::activate_custom_option(ContextMenuSlot p_slot, int p_option, const Variant &p_arg) {
  149. for (Ref<EditorContextMenuPlugin> &plugin : plugin_list) {
  150. if (plugin->slot != p_slot) {
  151. continue;
  152. }
  153. for (KeyValue<String, EditorContextMenuPlugin::ContextMenuItem> &E : plugin->context_menu_items) {
  154. if (E.value.id == p_option) {
  155. invoke_callback(E.value.callable, p_arg);
  156. return true;
  157. }
  158. }
  159. }
  160. return false;
  161. }
  162. void EditorContextMenuPluginManager::invoke_callback(const Callable &p_callback, const Variant &p_arg) {
  163. const Variant *argptr = &p_arg;
  164. Callable::CallError ce;
  165. Variant result;
  166. p_callback.callp(&argptr, 1, result, ce);
  167. if (ce.error != Callable::CallError::CALL_OK) {
  168. ERR_FAIL_MSG("Failed to execute context menu callback: " + Variant::get_callable_error_text(p_callback, &argptr, 1, ce) + ".");
  169. }
  170. }
  171. void EditorContextMenuPluginManager::create() {
  172. ERR_FAIL_COND(singleton != nullptr);
  173. singleton = memnew(EditorContextMenuPluginManager);
  174. }
  175. void EditorContextMenuPluginManager::cleanup() {
  176. ERR_FAIL_NULL(singleton);
  177. memdelete(singleton);
  178. singleton = nullptr;
  179. }