editor_plugin.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*************************************************************************/
  2. /* editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_PLUGIN_H
  31. #define EDITOR_PLUGIN_H
  32. #include "core/io/config_file.h"
  33. #include "core/undo_redo.h"
  34. #include "editor/editor_inspector.h"
  35. #include "editor/import/editor_import_plugin.h"
  36. #include "editor/import/resource_importer_scene.h"
  37. #include "editor/script_create_dialog.h"
  38. #include "scene/gui/tool_button.h"
  39. #include "scene/main/node.h"
  40. #include "scene/resources/texture.h"
  41. class EditorNode;
  42. class Spatial;
  43. class Camera;
  44. class EditorSelection;
  45. class EditorExport;
  46. class EditorSettings;
  47. class EditorImportPlugin;
  48. class EditorExportPlugin;
  49. class EditorSpatialGizmoPlugin;
  50. class EditorResourcePreview;
  51. class EditorFileSystem;
  52. class EditorToolAddons;
  53. class FileSystemDock;
  54. class ScriptEditor;
  55. class EditorInterface : public Node {
  56. GDCLASS(EditorInterface, Node);
  57. protected:
  58. static void _bind_methods();
  59. static EditorInterface *singleton;
  60. Array _make_mesh_previews(const Array &p_meshes, int p_preview_size);
  61. public:
  62. static EditorInterface *get_singleton() { return singleton; }
  63. Control *get_editor_viewport();
  64. void edit_resource(const Ref<Resource> &p_resource);
  65. void edit_node(Node *p_node);
  66. void open_scene_from_path(const String &scene_path);
  67. void reload_scene_from_path(const String &scene_path);
  68. void play_main_scene();
  69. void play_current_scene();
  70. void play_custom_scene(const String &scene_path);
  71. void stop_playing_scene();
  72. bool is_playing_scene() const;
  73. String get_playing_scene() const;
  74. Node *get_edited_scene_root();
  75. Array get_open_scenes() const;
  76. ScriptEditor *get_script_editor();
  77. void select_file(const String &p_file);
  78. String get_selected_path() const;
  79. String get_current_path() const;
  80. void inspect_object(Object *p_obj, const String &p_for_property = String(), bool p_inspector_only = false);
  81. EditorSelection *get_selection();
  82. //EditorImportExport *get_import_export();
  83. Ref<EditorSettings> get_editor_settings();
  84. EditorResourcePreview *get_resource_previewer();
  85. EditorFileSystem *get_resource_file_system();
  86. FileSystemDock *get_file_system_dock();
  87. Control *get_base_control();
  88. float get_editor_scale() const;
  89. void set_plugin_enabled(const String &p_plugin, bool p_enabled);
  90. bool is_plugin_enabled(const String &p_plugin) const;
  91. EditorInspector *get_inspector() const;
  92. Error save_scene();
  93. void save_scene_as(const String &p_scene, bool p_with_preview = true);
  94. Vector<Ref<Texture> > make_mesh_previews(const Vector<Ref<Mesh> > &p_meshes, Vector<Transform> *p_transforms, int p_preview_size);
  95. void set_main_screen_editor(const String &p_name);
  96. void set_distraction_free_mode(bool p_enter);
  97. bool is_distraction_free_mode_enabled() const;
  98. EditorInterface();
  99. };
  100. class EditorPlugin : public Node {
  101. GDCLASS(EditorPlugin, Node);
  102. friend class EditorData;
  103. UndoRedo *undo_redo;
  104. UndoRedo *_get_undo_redo() { return undo_redo; }
  105. bool input_event_forwarding_always_enabled;
  106. bool force_draw_over_forwarding_enabled;
  107. String last_main_screen_name;
  108. protected:
  109. static void _bind_methods();
  110. UndoRedo &get_undo_redo() { return *undo_redo; }
  111. void add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture> &p_icon);
  112. void remove_custom_type(const String &p_type);
  113. public:
  114. enum CustomControlContainer {
  115. CONTAINER_TOOLBAR,
  116. CONTAINER_SPATIAL_EDITOR_MENU,
  117. CONTAINER_SPATIAL_EDITOR_SIDE_LEFT,
  118. CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT,
  119. CONTAINER_SPATIAL_EDITOR_BOTTOM,
  120. CONTAINER_CANVAS_EDITOR_MENU,
  121. CONTAINER_CANVAS_EDITOR_SIDE_LEFT,
  122. CONTAINER_CANVAS_EDITOR_SIDE_RIGHT,
  123. CONTAINER_CANVAS_EDITOR_BOTTOM,
  124. CONTAINER_PROPERTY_EDITOR_BOTTOM,
  125. CONTAINER_PROJECT_SETTING_TAB_LEFT,
  126. CONTAINER_PROJECT_SETTING_TAB_RIGHT,
  127. };
  128. enum DockSlot {
  129. DOCK_SLOT_LEFT_UL,
  130. DOCK_SLOT_LEFT_BL,
  131. DOCK_SLOT_LEFT_UR,
  132. DOCK_SLOT_LEFT_BR,
  133. DOCK_SLOT_RIGHT_UL,
  134. DOCK_SLOT_RIGHT_BL,
  135. DOCK_SLOT_RIGHT_UR,
  136. DOCK_SLOT_RIGHT_BR,
  137. DOCK_SLOT_MAX
  138. };
  139. //TODO: send a resource for editing to the editor node?
  140. void add_control_to_container(CustomControlContainer p_location, Control *p_control);
  141. void remove_control_from_container(CustomControlContainer p_location, Control *p_control);
  142. ToolButton *add_control_to_bottom_panel(Control *p_control, const String &p_title);
  143. void add_control_to_dock(DockSlot p_slot, Control *p_control);
  144. void remove_control_from_docks(Control *p_control);
  145. void remove_control_from_bottom_panel(Control *p_control);
  146. void add_tool_menu_item(const String &p_name, Object *p_handler, const String &p_callback, const Variant &p_ud = Variant());
  147. void add_tool_submenu_item(const String &p_name, Object *p_submenu);
  148. void remove_tool_menu_item(const String &p_name);
  149. void set_input_event_forwarding_always_enabled();
  150. bool is_input_event_forwarding_always_enabled() { return input_event_forwarding_always_enabled; }
  151. void set_force_draw_over_forwarding_enabled();
  152. bool is_force_draw_over_forwarding_enabled() { return force_draw_over_forwarding_enabled; }
  153. void notify_main_screen_changed(const String &screen_name);
  154. void notify_scene_changed(const Node *scn_root);
  155. void notify_scene_closed(const String &scene_filepath);
  156. void notify_resource_saved(const Ref<Resource> &p_resource);
  157. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
  158. virtual void forward_canvas_draw_over_viewport(Control *p_overlay);
  159. virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay);
  160. virtual bool forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event);
  161. virtual void forward_spatial_draw_over_viewport(Control *p_overlay);
  162. virtual void forward_spatial_force_draw_over_viewport(Control *p_overlay);
  163. virtual String get_name() const;
  164. virtual const Ref<Texture> get_icon() const;
  165. virtual bool has_main_screen() const;
  166. virtual void make_visible(bool p_visible);
  167. virtual void selected_notify() {} //notify that it was raised by the user, not the editor
  168. virtual void edit(Object *p_object);
  169. virtual bool handles(Object *p_object) const;
  170. virtual Dictionary get_state() const; //save editor state so it can't be reloaded when reloading scene
  171. virtual void set_state(const Dictionary &p_state); //restore editor state (likely was saved with the scene)
  172. virtual void clear(); // clear any temporary data in the editor, reset it (likely new scene or load another scene)
  173. virtual void save_external_data(); // if editor references external resources/scenes, save them
  174. virtual void apply_changes(); // if changes are pending in editor, apply them
  175. virtual void get_breakpoints(List<String> *p_breakpoints);
  176. virtual bool get_remove_list(List<Node *> *p_list);
  177. virtual void set_window_layout(Ref<ConfigFile> p_layout);
  178. virtual void get_window_layout(Ref<ConfigFile> p_layout);
  179. virtual void edited_scene_changed() {} // if changes are pending in editor, apply them
  180. virtual bool build(); // builds with external tools. Returns true if safe to continue running scene.
  181. EditorInterface *get_editor_interface();
  182. ScriptCreateDialog *get_script_create_dialog();
  183. int update_overlays() const;
  184. void queue_save_layout() const;
  185. void make_bottom_panel_item_visible(Control *p_item);
  186. void hide_bottom_panel();
  187. virtual void restore_global_state();
  188. virtual void save_global_state();
  189. void add_import_plugin(const Ref<EditorImportPlugin> &p_importer);
  190. void remove_import_plugin(const Ref<EditorImportPlugin> &p_importer);
  191. void add_export_plugin(const Ref<EditorExportPlugin> &p_exporter);
  192. void remove_export_plugin(const Ref<EditorExportPlugin> &p_exporter);
  193. void add_spatial_gizmo_plugin(const Ref<EditorSpatialGizmoPlugin> &p_gizmo_plugin);
  194. void remove_spatial_gizmo_plugin(const Ref<EditorSpatialGizmoPlugin> &p_gizmo_plugin);
  195. void add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  196. void remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  197. void add_scene_import_plugin(const Ref<EditorSceneImporter> &p_importer);
  198. void remove_scene_import_plugin(const Ref<EditorSceneImporter> &p_importer);
  199. void add_autoload_singleton(const String &p_name, const String &p_path);
  200. void remove_autoload_singleton(const String &p_name);
  201. void enable_plugin();
  202. void disable_plugin();
  203. EditorPlugin();
  204. virtual ~EditorPlugin();
  205. };
  206. VARIANT_ENUM_CAST(EditorPlugin::CustomControlContainer);
  207. VARIANT_ENUM_CAST(EditorPlugin::DockSlot);
  208. typedef EditorPlugin *(*EditorPluginCreateFunc)(EditorNode *);
  209. class EditorPlugins {
  210. enum {
  211. MAX_CREATE_FUNCS = 64
  212. };
  213. static EditorPluginCreateFunc creation_funcs[MAX_CREATE_FUNCS];
  214. static int creation_func_count;
  215. template <class T>
  216. static EditorPlugin *creator(EditorNode *p_node) {
  217. return memnew(T(p_node));
  218. }
  219. public:
  220. static int get_plugin_count() { return creation_func_count; }
  221. static EditorPlugin *create(int p_idx, EditorNode *p_editor) {
  222. ERR_FAIL_INDEX_V(p_idx, creation_func_count, NULL);
  223. return creation_funcs[p_idx](p_editor);
  224. }
  225. template <class T>
  226. static void add_by_type() {
  227. add_create_func(creator<T>);
  228. }
  229. static void add_create_func(EditorPluginCreateFunc p_func) {
  230. ERR_FAIL_COND(creation_func_count >= MAX_CREATE_FUNCS);
  231. creation_funcs[creation_func_count++] = p_func;
  232. }
  233. };
  234. #endif