editor_plugin.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/object/undo_redo.h"
  34. #include "editor/debugger/editor_debugger_node.h"
  35. #include "editor/editor_inspector.h"
  36. #include "editor/editor_translation_parser.h"
  37. #include "editor/import/editor_import_plugin.h"
  38. #include "editor/import/resource_importer_scene.h"
  39. #include "editor/script_create_dialog.h"
  40. #include "scene/3d/camera_3d.h"
  41. #include "scene/main/node.h"
  42. #include "scene/resources/texture.h"
  43. class EditorNode;
  44. class Node3D;
  45. class Camera3D;
  46. class EditorCommandPalette;
  47. class EditorSelection;
  48. class EditorExport;
  49. class EditorSettings;
  50. class EditorImportPlugin;
  51. class EditorExportPlugin;
  52. class EditorNode3DGizmoPlugin;
  53. class EditorResourcePreview;
  54. class EditorFileSystem;
  55. class EditorToolAddons;
  56. class EditorPaths;
  57. class FileSystemDock;
  58. class ScriptEditor;
  59. class EditorInterface : public Node {
  60. GDCLASS(EditorInterface, Node);
  61. protected:
  62. static void _bind_methods();
  63. static EditorInterface *singleton;
  64. Array _make_mesh_previews(const Array &p_meshes, int p_preview_size);
  65. public:
  66. static EditorInterface *get_singleton() { return singleton; }
  67. Control *get_editor_main_control();
  68. void edit_resource(const Ref<Resource> &p_resource);
  69. void edit_node(Node *p_node);
  70. void open_scene_from_path(const String &scene_path);
  71. void reload_scene_from_path(const String &scene_path);
  72. void play_main_scene();
  73. void play_current_scene();
  74. void play_custom_scene(const String &scene_path);
  75. void stop_playing_scene();
  76. bool is_playing_scene() const;
  77. String get_playing_scene() const;
  78. Node *get_edited_scene_root();
  79. Array get_open_scenes() const;
  80. ScriptEditor *get_script_editor();
  81. EditorCommandPalette *get_command_palette() const;
  82. void select_file(const String &p_file);
  83. String get_selected_path() const;
  84. String get_current_path() const;
  85. void inspect_object(Object *p_obj, const String &p_for_property = String(), bool p_inspector_only = false);
  86. EditorSelection *get_selection();
  87. //EditorImportExport *get_import_export();
  88. Ref<EditorSettings> get_editor_settings();
  89. EditorPaths *get_editor_paths();
  90. EditorResourcePreview *get_resource_previewer();
  91. EditorFileSystem *get_resource_file_system();
  92. FileSystemDock *get_file_system_dock();
  93. Control *get_base_control();
  94. float get_editor_scale() const;
  95. void set_plugin_enabled(const String &p_plugin, bool p_enabled);
  96. bool is_plugin_enabled(const String &p_plugin) const;
  97. EditorInspector *get_inspector() const;
  98. Error save_scene();
  99. void save_scene_as(const String &p_scene, bool p_with_preview = true);
  100. Vector<Ref<Texture2D>> make_mesh_previews(const Vector<Ref<Mesh>> &p_meshes, Vector<Transform3D> *p_transforms, int p_preview_size);
  101. void set_main_screen_editor(const String &p_name);
  102. void set_distraction_free_mode(bool p_enter);
  103. bool is_distraction_free_mode_enabled() const;
  104. EditorInterface();
  105. };
  106. class EditorPlugin : public Node {
  107. GDCLASS(EditorPlugin, Node);
  108. friend class EditorData;
  109. UndoRedo *undo_redo = nullptr;
  110. UndoRedo *_get_undo_redo() { return undo_redo; }
  111. bool input_event_forwarding_always_enabled = false;
  112. bool force_draw_over_forwarding_enabled = false;
  113. String last_main_screen_name;
  114. void _editor_project_settings_changed();
  115. protected:
  116. void _notification(int p_what);
  117. static void _bind_methods();
  118. UndoRedo &get_undo_redo() { return *undo_redo; }
  119. void add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon);
  120. void remove_custom_type(const String &p_type);
  121. GDVIRTUAL1R(bool, _forward_canvas_gui_input, Ref<InputEvent>)
  122. GDVIRTUAL1(_forward_canvas_draw_over_viewport, Control *)
  123. GDVIRTUAL1(_forward_canvas_force_draw_over_viewport, Control *)
  124. GDVIRTUAL2R(int, _forward_3d_gui_input, Camera3D *, Ref<InputEvent>)
  125. GDVIRTUAL1(_forward_3d_draw_over_viewport, Control *)
  126. GDVIRTUAL1(_forward_3d_force_draw_over_viewport, Control *)
  127. GDVIRTUAL0RC(String, _get_plugin_name)
  128. GDVIRTUAL0RC(Ref<Texture2D>, _get_plugin_icon)
  129. GDVIRTUAL0RC(bool, _has_main_screen)
  130. GDVIRTUAL1(_make_visible, bool)
  131. GDVIRTUAL1(_edit, Variant)
  132. GDVIRTUAL1RC(bool, _handles, Variant)
  133. GDVIRTUAL0RC(Dictionary, _get_state)
  134. GDVIRTUAL1(_set_state, Dictionary)
  135. GDVIRTUAL0(_clear)
  136. GDVIRTUAL0(_save_external_data)
  137. GDVIRTUAL0(_apply_changes)
  138. GDVIRTUAL0RC(Vector<String>, _get_breakpoints)
  139. GDVIRTUAL1(_set_window_layout, Ref<ConfigFile>)
  140. GDVIRTUAL1(_get_window_layout, Ref<ConfigFile>)
  141. GDVIRTUAL0R(bool, _build)
  142. GDVIRTUAL0(_enable_plugin)
  143. GDVIRTUAL0(_disable_plugin)
  144. public:
  145. enum CustomControlContainer {
  146. CONTAINER_TOOLBAR,
  147. CONTAINER_SPATIAL_EDITOR_MENU,
  148. CONTAINER_SPATIAL_EDITOR_SIDE_LEFT,
  149. CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT,
  150. CONTAINER_SPATIAL_EDITOR_BOTTOM,
  151. CONTAINER_CANVAS_EDITOR_MENU,
  152. CONTAINER_CANVAS_EDITOR_SIDE_LEFT,
  153. CONTAINER_CANVAS_EDITOR_SIDE_RIGHT,
  154. CONTAINER_CANVAS_EDITOR_BOTTOM,
  155. CONTAINER_PROPERTY_EDITOR_BOTTOM,
  156. CONTAINER_PROJECT_SETTING_TAB_LEFT,
  157. CONTAINER_PROJECT_SETTING_TAB_RIGHT,
  158. };
  159. enum DockSlot {
  160. DOCK_SLOT_LEFT_UL,
  161. DOCK_SLOT_LEFT_BL,
  162. DOCK_SLOT_LEFT_UR,
  163. DOCK_SLOT_LEFT_BR,
  164. DOCK_SLOT_RIGHT_UL,
  165. DOCK_SLOT_RIGHT_BL,
  166. DOCK_SLOT_RIGHT_UR,
  167. DOCK_SLOT_RIGHT_BR,
  168. DOCK_SLOT_MAX
  169. };
  170. enum AfterGUIInput {
  171. AFTER_GUI_INPUT_PASS,
  172. AFTER_GUI_INPUT_STOP,
  173. AFTER_GUI_INPUT_DESELECT
  174. };
  175. //TODO: send a resource for editing to the editor node?
  176. void add_control_to_container(CustomControlContainer p_location, Control *p_control);
  177. void remove_control_from_container(CustomControlContainer p_location, Control *p_control);
  178. Button *add_control_to_bottom_panel(Control *p_control, const String &p_title);
  179. void add_control_to_dock(DockSlot p_slot, Control *p_control);
  180. void remove_control_from_docks(Control *p_control);
  181. void remove_control_from_bottom_panel(Control *p_control);
  182. void add_tool_menu_item(const String &p_name, const Callable &p_callable);
  183. void add_tool_submenu_item(const String &p_name, Object *p_submenu);
  184. void remove_tool_menu_item(const String &p_name);
  185. void set_input_event_forwarding_always_enabled();
  186. bool is_input_event_forwarding_always_enabled() { return input_event_forwarding_always_enabled; }
  187. void set_force_draw_over_forwarding_enabled();
  188. bool is_force_draw_over_forwarding_enabled() { return force_draw_over_forwarding_enabled; }
  189. void notify_main_screen_changed(const String &screen_name);
  190. void notify_scene_changed(const Node *scn_root);
  191. void notify_scene_closed(const String &scene_filepath);
  192. void notify_resource_saved(const Ref<Resource> &p_resource);
  193. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
  194. virtual void forward_canvas_draw_over_viewport(Control *p_overlay);
  195. virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay);
  196. virtual EditorPlugin::AfterGUIInput forward_spatial_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event);
  197. virtual void forward_spatial_draw_over_viewport(Control *p_overlay);
  198. virtual void forward_spatial_force_draw_over_viewport(Control *p_overlay);
  199. virtual String get_name() const;
  200. virtual const Ref<Texture2D> get_icon() const;
  201. virtual bool has_main_screen() const;
  202. virtual void make_visible(bool p_visible);
  203. virtual void selected_notify() {} //notify that it was raised by the user, not the editor
  204. virtual void edit(Object *p_object);
  205. virtual bool handles(Object *p_object) const;
  206. virtual Dictionary get_state() const; //save editor state so it can't be reloaded when reloading scene
  207. virtual void set_state(const Dictionary &p_state); //restore editor state (likely was saved with the scene)
  208. virtual void clear(); // clear any temporary data in the editor, reset it (likely new scene or load another scene)
  209. virtual void save_external_data(); // if editor references external resources/scenes, save them
  210. virtual void apply_changes(); // if changes are pending in editor, apply them
  211. virtual void get_breakpoints(List<String> *p_breakpoints);
  212. virtual bool get_remove_list(List<Node *> *p_list);
  213. virtual void set_window_layout(Ref<ConfigFile> p_layout);
  214. virtual void get_window_layout(Ref<ConfigFile> p_layout);
  215. virtual void edited_scene_changed() {} // if changes are pending in editor, apply them
  216. virtual bool build(); // builds with external tools. Returns true if safe to continue running scene.
  217. EditorInterface *get_editor_interface();
  218. ScriptCreateDialog *get_script_create_dialog();
  219. void add_undo_redo_inspector_hook_callback(Callable p_callable);
  220. void remove_undo_redo_inspector_hook_callback(Callable p_callable);
  221. int update_overlays() const;
  222. void queue_save_layout();
  223. void make_bottom_panel_item_visible(Control *p_item);
  224. void hide_bottom_panel();
  225. virtual void restore_global_state();
  226. virtual void save_global_state();
  227. void add_translation_parser_plugin(const Ref<EditorTranslationParserPlugin> &p_parser);
  228. void remove_translation_parser_plugin(const Ref<EditorTranslationParserPlugin> &p_parser);
  229. void add_import_plugin(const Ref<EditorImportPlugin> &p_importer);
  230. void remove_import_plugin(const Ref<EditorImportPlugin> &p_importer);
  231. void add_export_plugin(const Ref<EditorExportPlugin> &p_exporter);
  232. void remove_export_plugin(const Ref<EditorExportPlugin> &p_exporter);
  233. void add_spatial_gizmo_plugin(const Ref<EditorNode3DGizmoPlugin> &p_gizmo_plugin);
  234. void remove_spatial_gizmo_plugin(const Ref<EditorNode3DGizmoPlugin> &p_gizmo_plugin);
  235. void add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  236. void remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin);
  237. void add_scene_format_importer_plugin(const Ref<EditorSceneFormatImporter> &p_importer);
  238. void remove_scene_format_importer_plugin(const Ref<EditorSceneFormatImporter> &p_importer);
  239. void add_scene_post_import_plugin(const Ref<EditorScenePostImportPlugin> &p_importer);
  240. void remove_scene_post_import_plugin(const Ref<EditorScenePostImportPlugin> &p_importer);
  241. void add_autoload_singleton(const String &p_name, const String &p_path);
  242. void remove_autoload_singleton(const String &p_name);
  243. void add_debugger_plugin(const Ref<Script> &p_script);
  244. void remove_debugger_plugin(const Ref<Script> &p_script);
  245. void enable_plugin();
  246. void disable_plugin();
  247. EditorPlugin() {}
  248. virtual ~EditorPlugin() {}
  249. };
  250. VARIANT_ENUM_CAST(EditorPlugin::CustomControlContainer);
  251. VARIANT_ENUM_CAST(EditorPlugin::DockSlot);
  252. typedef EditorPlugin *(*EditorPluginCreateFunc)(EditorNode *);
  253. class EditorPlugins {
  254. enum {
  255. MAX_CREATE_FUNCS = 64
  256. };
  257. static EditorPluginCreateFunc creation_funcs[MAX_CREATE_FUNCS];
  258. static int creation_func_count;
  259. template <class T>
  260. static EditorPlugin *creator(EditorNode *p_node) {
  261. return memnew(T(p_node));
  262. }
  263. public:
  264. static int get_plugin_count() { return creation_func_count; }
  265. static EditorPlugin *create(int p_idx, EditorNode *p_editor) {
  266. ERR_FAIL_INDEX_V(p_idx, creation_func_count, nullptr);
  267. return creation_funcs[p_idx](p_editor);
  268. }
  269. template <class T>
  270. static void add_by_type() {
  271. add_create_func(creator<T>);
  272. }
  273. static void add_create_func(EditorPluginCreateFunc p_func) {
  274. ERR_FAIL_COND(creation_func_count >= MAX_CREATE_FUNCS);
  275. creation_funcs[creation_func_count++] = p_func;
  276. }
  277. };
  278. #endif