editor_plugin.h 11 KB

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