editor_plugin.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*************************************************************************/
  2. /* editor_plugin.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor/import/editor_import_plugin.h"
  33. #include "io/config_file.h"
  34. #include "scene/gui/tool_button.h"
  35. #include "scene/main/node.h"
  36. #include "scene/resources/texture.h"
  37. #include "undo_redo.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. class EditorNode;
  42. class Spatial;
  43. class Camera;
  44. class EditorSelection;
  45. class EditorExport;
  46. class EditorSettings;
  47. class SpatialEditorGizmo;
  48. class EditorImportPlugin;
  49. class EditorExportPlugin;
  50. class EditorResourcePreview;
  51. class EditorFileSystem;
  52. class EditorToolAddons;
  53. class ScriptEditor;
  54. class EditorInterface : public Node {
  55. GDCLASS(EditorInterface, Node)
  56. protected:
  57. static void _bind_methods();
  58. static EditorInterface *singleton;
  59. Array _make_mesh_previews(const Array &p_meshes, int p_preview_size);
  60. public:
  61. static EditorInterface *get_singleton() { return singleton; }
  62. Control *get_editor_viewport();
  63. void edit_resource(const Ref<Resource> &p_resource);
  64. void open_scene_from_path(const String &scene_path);
  65. void reload_scene_from_path(const String &scene_path);
  66. Node *get_edited_scene_root();
  67. Array get_open_scenes() const;
  68. ScriptEditor *get_script_editor();
  69. void inspect_object(Object *p_obj, const String &p_for_property = String());
  70. EditorSelection *get_selection();
  71. //EditorImportExport *get_import_export();
  72. EditorSettings *get_editor_settings();
  73. EditorResourcePreview *get_resource_previewer();
  74. EditorFileSystem *get_resource_file_system();
  75. Control *get_base_control();
  76. Error save_scene();
  77. void save_scene_as(const String &p_scene, bool p_with_preview = true);
  78. Vector<Ref<Texture> > make_mesh_previews(const Vector<Ref<Mesh> > &p_meshes, int p_preview_size);
  79. EditorInterface();
  80. };
  81. class EditorPlugin : public Node {
  82. GDCLASS(EditorPlugin, Node);
  83. friend class EditorData;
  84. UndoRedo *undo_redo;
  85. UndoRedo *_get_undo_redo() { return undo_redo; }
  86. bool input_event_forwarding_always_enabled;
  87. String last_main_screen_name;
  88. protected:
  89. static void _bind_methods();
  90. UndoRedo &get_undo_redo() { return *undo_redo; }
  91. void add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture> &p_icon);
  92. void remove_custom_type(const String &p_type);
  93. public:
  94. enum CustomControlContainer {
  95. CONTAINER_TOOLBAR,
  96. CONTAINER_SPATIAL_EDITOR_MENU,
  97. CONTAINER_SPATIAL_EDITOR_SIDE,
  98. CONTAINER_SPATIAL_EDITOR_BOTTOM,
  99. CONTAINER_CANVAS_EDITOR_MENU,
  100. CONTAINER_CANVAS_EDITOR_SIDE,
  101. CONTAINER_CANVAS_EDITOR_BOTTOM,
  102. CONTAINER_PROPERTY_EDITOR_BOTTOM
  103. };
  104. enum DockSlot {
  105. DOCK_SLOT_LEFT_UL,
  106. DOCK_SLOT_LEFT_BL,
  107. DOCK_SLOT_LEFT_UR,
  108. DOCK_SLOT_LEFT_BR,
  109. DOCK_SLOT_RIGHT_UL,
  110. DOCK_SLOT_RIGHT_BL,
  111. DOCK_SLOT_RIGHT_UR,
  112. DOCK_SLOT_RIGHT_BR,
  113. DOCK_SLOT_MAX
  114. };
  115. //TODO: send a resource for editing to the editor node?
  116. void add_control_to_container(CustomControlContainer p_location, Control *p_control);
  117. ToolButton *add_control_to_bottom_panel(Control *p_control, const String &p_title);
  118. void add_control_to_dock(DockSlot p_slot, Control *p_control);
  119. void remove_control_from_docks(Control *p_control);
  120. void remove_control_from_bottom_panel(Control *p_control);
  121. void add_tool_menu_item(const String &p_name, Object *p_handler, const String &p_callback, const Variant &p_ud = Variant());
  122. void add_tool_submenu_item(const String &p_name, Object *p_submenu);
  123. void remove_tool_menu_item(const String &p_name);
  124. void set_input_event_forwarding_always_enabled();
  125. bool is_input_event_forwarding_always_enabled() { return input_event_forwarding_always_enabled; }
  126. void notify_main_screen_changed(const String &screen_name);
  127. void notify_scene_changed(const Node *scn_root);
  128. void notify_scene_closed(const String &scene_filepath);
  129. virtual Ref<SpatialEditorGizmo> create_spatial_gizmo(Spatial *p_spatial);
  130. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
  131. virtual void forward_draw_over_canvas(Control *p_canvas);
  132. virtual bool forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event);
  133. virtual String get_name() const;
  134. virtual bool has_main_screen() const;
  135. virtual void make_visible(bool p_visible);
  136. virtual void selected_notify() {} //notify that it was raised by the user, not the editor
  137. virtual void edit(Object *p_object);
  138. virtual bool handles(Object *p_object) const;
  139. virtual Dictionary get_state() const; //save editor state so it can't be reloaded when reloading scene
  140. virtual void set_state(const Dictionary &p_state); //restore editor state (likely was saved with the scene)
  141. virtual void clear(); // clear any temporary data in te editor, reset it (likely new scene or load another scene)
  142. virtual void save_external_data(); // if editor references external resources/scenes, save them
  143. virtual void apply_changes(); // if changes are pending in editor, apply them
  144. virtual void get_breakpoints(List<String> *p_breakpoints);
  145. virtual bool get_remove_list(List<Node *> *p_list);
  146. virtual void set_window_layout(Ref<ConfigFile> p_layout);
  147. virtual void get_window_layout(Ref<ConfigFile> p_layout);
  148. virtual void edited_scene_changed() {} // if changes are pending in editor, apply them
  149. EditorInterface *get_editor_interface();
  150. void update_canvas();
  151. void queue_save_layout() const;
  152. void make_bottom_panel_item_visible(Control *p_item);
  153. void hide_bottom_panel();
  154. virtual void restore_global_state();
  155. virtual void save_global_state();
  156. void add_import_plugin(const Ref<EditorImportPlugin> &p_importer);
  157. void remove_import_plugin(const Ref<EditorImportPlugin> &p_importer);
  158. void add_export_plugin(const Ref<EditorExportPlugin> &p_exporter);
  159. void remove_export_plugin(const Ref<EditorExportPlugin> &p_exporter);
  160. EditorPlugin();
  161. virtual ~EditorPlugin();
  162. };
  163. VARIANT_ENUM_CAST(EditorPlugin::CustomControlContainer);
  164. VARIANT_ENUM_CAST(EditorPlugin::DockSlot);
  165. typedef EditorPlugin *(*EditorPluginCreateFunc)(EditorNode *);
  166. class EditorPlugins {
  167. enum {
  168. MAX_CREATE_FUNCS = 64
  169. };
  170. static EditorPluginCreateFunc creation_funcs[MAX_CREATE_FUNCS];
  171. static int creation_func_count;
  172. template <class T>
  173. static EditorPlugin *creator(EditorNode *p_node) {
  174. return memnew(T(p_node));
  175. }
  176. public:
  177. static int get_plugin_count() { return creation_func_count; }
  178. static EditorPlugin *create(int p_idx, EditorNode *p_editor) {
  179. ERR_FAIL_INDEX_V(p_idx, creation_func_count, NULL);
  180. return creation_funcs[p_idx](p_editor);
  181. }
  182. template <class T>
  183. static void add_by_type() {
  184. add_create_func(creator<T>);
  185. }
  186. static void add_create_func(EditorPluginCreateFunc p_func) {
  187. ERR_FAIL_COND(creation_func_count >= MAX_CREATE_FUNCS);
  188. creation_funcs[creation_func_count++] = p_func;
  189. }
  190. };
  191. #endif