editor_plugin.h 11 KB

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