editor_plugin.h 11 KB

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