editor_context_menu_plugin.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**************************************************************************/
  2. /* editor_context_menu_plugin.h */
  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. #ifndef EDITOR_CONTEXT_MENU_PLUGIN_H
  31. #define EDITOR_CONTEXT_MENU_PLUGIN_H
  32. #include "core/object/gdvirtual.gen.inc"
  33. #include "core/object/ref_counted.h"
  34. class InputEvent;
  35. class PopupMenu;
  36. class Shortcut;
  37. class Texture2D;
  38. class EditorContextMenuPlugin : public RefCounted {
  39. GDCLASS(EditorContextMenuPlugin, RefCounted);
  40. friend class EditorContextMenuPluginManager;
  41. inline static constexpr int MAX_ITEMS = 100;
  42. public:
  43. enum ContextMenuSlot {
  44. CONTEXT_SLOT_SCENE_TREE,
  45. CONTEXT_SLOT_FILESYSTEM,
  46. CONTEXT_SLOT_SCRIPT_EDITOR,
  47. CONTEXT_SLOT_FILESYSTEM_CREATE,
  48. };
  49. inline static constexpr int BASE_ID = 2000;
  50. private:
  51. int slot = -1;
  52. public:
  53. struct ContextMenuItem {
  54. int id = 0;
  55. String item_name;
  56. Callable callable;
  57. Ref<Texture2D> icon;
  58. Ref<Shortcut> shortcut;
  59. PopupMenu *submenu = nullptr;
  60. };
  61. HashMap<String, ContextMenuItem> context_menu_items;
  62. HashMap<Ref<Shortcut>, Callable> context_menu_shortcuts;
  63. protected:
  64. static void _bind_methods();
  65. GDVIRTUAL1(_popup_menu, Vector<String>);
  66. public:
  67. virtual void get_options(const Vector<String> &p_paths);
  68. void add_menu_shortcut(const Ref<Shortcut> &p_shortcut, const Callable &p_callable);
  69. void add_context_menu_item(const String &p_name, const Callable &p_callable, const Ref<Texture2D> &p_texture);
  70. void add_context_menu_item_from_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut, const Ref<Texture2D> &p_texture);
  71. void add_context_submenu_item(const String &p_name, PopupMenu *p_menu, const Ref<Texture2D> &p_texture);
  72. };
  73. VARIANT_ENUM_CAST(EditorContextMenuPlugin::ContextMenuSlot);
  74. class EditorContextMenuPluginManager : public Object {
  75. GDCLASS(EditorContextMenuPluginManager, Object);
  76. using ContextMenuSlot = EditorContextMenuPlugin::ContextMenuSlot;
  77. static inline EditorContextMenuPluginManager *singleton = nullptr;
  78. LocalVector<Ref<EditorContextMenuPlugin>> plugin_list;
  79. public:
  80. static EditorContextMenuPluginManager *get_singleton() { return singleton; }
  81. void add_plugin(ContextMenuSlot p_slot, const Ref<EditorContextMenuPlugin> &p_plugin);
  82. void remove_plugin(const Ref<EditorContextMenuPlugin> &p_plugin);
  83. void add_options_from_plugins(PopupMenu *p_popup, ContextMenuSlot p_slot, const Vector<String> &p_paths);
  84. Callable match_custom_shortcut(ContextMenuSlot p_slot, const Ref<InputEvent> &p_event);
  85. bool activate_custom_option(ContextMenuSlot p_slot, int p_option, const Variant &p_arg);
  86. void invoke_callback(const Callable &p_callback, const Variant &p_arg);
  87. static void create();
  88. static void cleanup();
  89. };
  90. #endif // EDITOR_CONTEXT_MENU_PLUGIN_H