editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*************************************************************************/
  2. /* editor_plugin.cpp */
  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. #include "editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "plugins/canvas_item_editor_plugin.h"
  34. #include "plugins/spatial_editor_plugin.h"
  35. #include "scene/3d/camera.h"
  36. void EditorPlugin::add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture> &p_icon) {
  37. EditorNode::get_editor_data().add_custom_type(p_type, p_base, p_script, p_icon);
  38. }
  39. void EditorPlugin::remove_custom_type(const String &p_type) {
  40. EditorNode::get_editor_data().remove_custom_type(p_type);
  41. }
  42. ToolButton *EditorPlugin::add_control_to_bottom_panel(Control *p_control, const String &p_title) {
  43. return EditorNode::get_singleton()->add_bottom_panel_item(p_title, p_control);
  44. }
  45. void EditorPlugin::add_control_to_dock(DockSlot p_slot, Control *p_control) {
  46. ERR_FAIL_NULL(p_control);
  47. EditorNode::get_singleton()->add_control_to_dock(EditorNode::DockSlot(p_slot), p_control);
  48. }
  49. void EditorPlugin::remove_control_from_docks(Control *p_control) {
  50. ERR_FAIL_NULL(p_control);
  51. EditorNode::get_singleton()->remove_control_from_dock(p_control);
  52. }
  53. void EditorPlugin::remove_control_from_bottom_panel(Control *p_control) {
  54. ERR_FAIL_NULL(p_control);
  55. EditorNode::get_singleton()->remove_bottom_panel_item(p_control);
  56. }
  57. Control *EditorPlugin::get_editor_viewport() {
  58. return EditorNode::get_singleton()->get_viewport();
  59. }
  60. void EditorPlugin::edit_resource(const Ref<Resource> &p_resource) {
  61. EditorNode::get_singleton()->edit_resource(p_resource);
  62. }
  63. void EditorPlugin::add_control_to_container(CustomControlContainer p_location, Control *p_control) {
  64. switch (p_location) {
  65. case CONTAINER_TOOLBAR: {
  66. EditorNode::get_menu_hb()->add_child(p_control);
  67. } break;
  68. case CONTAINER_SPATIAL_EDITOR_MENU: {
  69. SpatialEditor::get_singleton()->add_control_to_menu_panel(p_control);
  70. } break;
  71. case CONTAINER_SPATIAL_EDITOR_SIDE: {
  72. SpatialEditor::get_singleton()->get_palette_split()->add_child(p_control);
  73. SpatialEditor::get_singleton()->get_palette_split()->move_child(p_control, 0);
  74. } break;
  75. case CONTAINER_SPATIAL_EDITOR_BOTTOM: {
  76. SpatialEditor::get_singleton()->get_shader_split()->add_child(p_control);
  77. } break;
  78. case CONTAINER_CANVAS_EDITOR_MENU: {
  79. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(p_control);
  80. } break;
  81. case CONTAINER_CANVAS_EDITOR_SIDE: {
  82. CanvasItemEditor::get_singleton()->get_palette_split()->add_child(p_control);
  83. CanvasItemEditor::get_singleton()->get_palette_split()->move_child(p_control, 0);
  84. } break;
  85. case CONTAINER_CANVAS_EDITOR_BOTTOM: {
  86. CanvasItemEditor::get_singleton()->get_bottom_split()->add_child(p_control);
  87. } break;
  88. case CONTAINER_PROPERTY_EDITOR_BOTTOM: {
  89. EditorNode::get_singleton()->get_property_editor_vb()->add_child(p_control);
  90. } break;
  91. }
  92. }
  93. Ref<SpatialEditorGizmo> EditorPlugin::create_spatial_gizmo(Spatial *p_spatial) {
  94. //??
  95. if (get_script_instance() && get_script_instance()->has_method("create_spatial_gizmo")) {
  96. return get_script_instance()->call("create_spatial_gizmo", p_spatial);
  97. }
  98. return Ref<SpatialEditorGizmo>();
  99. }
  100. bool EditorPlugin::forward_input_event(const InputEvent &p_event) {
  101. if (get_script_instance() && get_script_instance()->has_method("forward_input_event")) {
  102. return get_script_instance()->call("forward_input_event", p_event);
  103. }
  104. return false;
  105. }
  106. bool EditorPlugin::forward_spatial_input_event(Camera *p_camera, const InputEvent &p_event) {
  107. if (get_script_instance() && get_script_instance()->has_method("forward_spatial_input_event")) {
  108. return get_script_instance()->call("forward_spatial_input_event", p_camera, p_event);
  109. }
  110. return false;
  111. }
  112. String EditorPlugin::get_name() const {
  113. if (get_script_instance() && get_script_instance()->has_method("get_name")) {
  114. return get_script_instance()->call("get_name");
  115. }
  116. return String();
  117. }
  118. bool EditorPlugin::has_main_screen() const {
  119. if (get_script_instance() && get_script_instance()->has_method("has_main_screen")) {
  120. return get_script_instance()->call("has_main_screen");
  121. }
  122. return false;
  123. }
  124. void EditorPlugin::make_visible(bool p_visible) {
  125. if (get_script_instance() && get_script_instance()->has_method("make_visible")) {
  126. get_script_instance()->call("make_visible", p_visible);
  127. }
  128. }
  129. void EditorPlugin::edit(Object *p_object) {
  130. if (get_script_instance() && get_script_instance()->has_method("edit")) {
  131. Resource *obj = p_object ? p_object->cast_to<Resource>() : NULL;
  132. if (obj) {
  133. get_script_instance()->call("edit", Ref<Resource>(obj));
  134. } else {
  135. get_script_instance()->call("edit", p_object);
  136. }
  137. }
  138. }
  139. bool EditorPlugin::handles(Object *p_object) const {
  140. if (get_script_instance() && get_script_instance()->has_method("handles")) {
  141. return get_script_instance()->call("handles", p_object);
  142. }
  143. return false;
  144. }
  145. Dictionary EditorPlugin::get_state() const {
  146. if (get_script_instance() && get_script_instance()->has_method("get_state")) {
  147. return get_script_instance()->call("get_state");
  148. }
  149. return Dictionary();
  150. }
  151. void EditorPlugin::set_state(const Dictionary &p_state) {
  152. if (get_script_instance() && get_script_instance()->has_method("set_state")) {
  153. get_script_instance()->call("set_state", p_state);
  154. }
  155. }
  156. void EditorPlugin::clear() {
  157. if (get_script_instance() && get_script_instance()->has_method("clear")) {
  158. get_script_instance()->call("clear");
  159. }
  160. }
  161. // if editor references external resources/scenes, save them
  162. void EditorPlugin::save_external_data() {
  163. if (get_script_instance() && get_script_instance()->has_method("save_external_data")) {
  164. get_script_instance()->call("save_external_data");
  165. }
  166. }
  167. // if changes are pending in editor, apply them
  168. void EditorPlugin::apply_changes() {
  169. if (get_script_instance() && get_script_instance()->has_method("apply_changes")) {
  170. get_script_instance()->call("apply_changes");
  171. }
  172. }
  173. void EditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  174. if (get_script_instance() && get_script_instance()->has_method("get_breakpoints")) {
  175. StringArray arr = get_script_instance()->call("get_breakpoints");
  176. for (int i = 0; i < arr.size(); i++)
  177. p_breakpoints->push_back(arr[i]);
  178. }
  179. }
  180. bool EditorPlugin::get_remove_list(List<Node *> *p_list) {
  181. return false;
  182. }
  183. void EditorPlugin::restore_global_state() {}
  184. void EditorPlugin::save_global_state() {}
  185. void EditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  186. if (get_script_instance() && get_script_instance()->has_method("set_window_layout")) {
  187. get_script_instance()->call("set_window_layout", p_layout);
  188. }
  189. }
  190. void EditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  191. if (get_script_instance() && get_script_instance()->has_method("get_window_layout")) {
  192. get_script_instance()->call("get_window_layout", p_layout);
  193. }
  194. }
  195. void EditorPlugin::queue_save_layout() const {
  196. EditorNode::get_singleton()->save_layout();
  197. }
  198. EditorSelection *EditorPlugin::get_selection() {
  199. return EditorNode::get_singleton()->get_editor_selection();
  200. }
  201. EditorSettings *EditorPlugin::get_editor_settings() {
  202. return EditorSettings::get_singleton();
  203. }
  204. void EditorPlugin::add_import_plugin(const Ref<EditorImportPlugin> &p_editor_import) {
  205. EditorNode::get_singleton()->add_editor_import_plugin(p_editor_import);
  206. }
  207. void EditorPlugin::remove_import_plugin(const Ref<EditorImportPlugin> &p_editor_import) {
  208. EditorNode::get_singleton()->remove_editor_import_plugin(p_editor_import);
  209. }
  210. void EditorPlugin::add_export_plugin(const Ref<EditorExportPlugin> &p_editor_export) {
  211. EditorImportExport::get_singleton()->add_export_plugin(p_editor_export);
  212. }
  213. void EditorPlugin::remove_export_plugin(const Ref<EditorExportPlugin> &p_editor_export) {
  214. EditorImportExport::get_singleton()->remove_export_plugin(p_editor_export);
  215. }
  216. Control *EditorPlugin::get_base_control() {
  217. return EditorNode::get_singleton()->get_gui_base();
  218. }
  219. void EditorPlugin::_bind_methods() {
  220. ObjectTypeDB::bind_method(_MD("add_control_to_container", "container", "control:Control"), &EditorPlugin::add_control_to_container);
  221. ObjectTypeDB::bind_method(_MD("add_control_to_bottom_panel:ToolButton", "control:Control", "title"), &EditorPlugin::add_control_to_bottom_panel);
  222. ObjectTypeDB::bind_method(_MD("add_control_to_dock", "slot", "control:Control"), &EditorPlugin::add_control_to_dock);
  223. ObjectTypeDB::bind_method(_MD("remove_control_from_docks", "control:Control"), &EditorPlugin::remove_control_from_docks);
  224. ObjectTypeDB::bind_method(_MD("remove_control_from_bottom_panel", "control:Control"), &EditorPlugin::remove_control_from_bottom_panel);
  225. ObjectTypeDB::bind_method(_MD("add_custom_type", "type", "base", "script:Script", "icon:Texture"), &EditorPlugin::add_custom_type);
  226. ObjectTypeDB::bind_method(_MD("remove_custom_type", "type"), &EditorPlugin::remove_custom_type);
  227. ObjectTypeDB::bind_method(_MD("get_editor_viewport:Control"), &EditorPlugin::get_editor_viewport);
  228. ObjectTypeDB::bind_method(_MD("add_import_plugin", "plugin:EditorImportPlugin"), &EditorPlugin::add_import_plugin);
  229. ObjectTypeDB::bind_method(_MD("remove_import_plugin", "plugin:EditorImportPlugin"), &EditorPlugin::remove_import_plugin);
  230. ObjectTypeDB::bind_method(_MD("add_export_plugin", "plugin:EditorExportPlugin"), &EditorPlugin::add_export_plugin);
  231. ObjectTypeDB::bind_method(_MD("remove_export_plugin", "plugin:EditorExportPlugin"), &EditorPlugin::remove_export_plugin);
  232. ObjectTypeDB::bind_method(_MD("get_base_control:Control"), &EditorPlugin::get_base_control);
  233. ObjectTypeDB::bind_method(_MD("get_undo_redo:UndoRedo"), &EditorPlugin::_get_undo_redo);
  234. ObjectTypeDB::bind_method(_MD("get_selection:EditorSelection"), &EditorPlugin::get_selection);
  235. ObjectTypeDB::bind_method(_MD("get_editor_settings:EditorSettings"), &EditorPlugin::get_editor_settings);
  236. ObjectTypeDB::bind_method(_MD("queue_save_layout"), &EditorPlugin::queue_save_layout);
  237. ObjectTypeDB::bind_method(_MD("edit_resource"), &EditorPlugin::edit_resource);
  238. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::BOOL, "forward_input_event", PropertyInfo(Variant::INPUT_EVENT, "event")));
  239. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::BOOL, "forward_spatial_input_event", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::INPUT_EVENT, "event")));
  240. MethodInfo gizmo = MethodInfo(Variant::OBJECT, "create_spatial_gizmo", PropertyInfo(Variant::OBJECT, "for_spatial:Spatial"));
  241. gizmo.return_val.hint = PROPERTY_HINT_RESOURCE_TYPE;
  242. gizmo.return_val.hint_string = "EditorSpatialGizmo";
  243. ObjectTypeDB::add_virtual_method(get_type_static(), gizmo);
  244. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::STRING, "get_name"));
  245. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::BOOL, "has_main_screen"));
  246. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("make_visible", PropertyInfo(Variant::BOOL, "visible")));
  247. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("edit", PropertyInfo(Variant::OBJECT, "object")));
  248. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::BOOL, "handles", PropertyInfo(Variant::OBJECT, "object")));
  249. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::DICTIONARY, "get_state"));
  250. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("set_state", PropertyInfo(Variant::DICTIONARY, "state")));
  251. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("clear"));
  252. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("save_external_data"));
  253. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("apply_changes"));
  254. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::STRING_ARRAY, "get_breakpoints"));
  255. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("set_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile")));
  256. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("get_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile")));
  257. BIND_CONSTANT(CONTAINER_TOOLBAR);
  258. BIND_CONSTANT(CONTAINER_SPATIAL_EDITOR_MENU);
  259. BIND_CONSTANT(CONTAINER_SPATIAL_EDITOR_SIDE);
  260. BIND_CONSTANT(CONTAINER_SPATIAL_EDITOR_BOTTOM);
  261. BIND_CONSTANT(CONTAINER_CANVAS_EDITOR_MENU);
  262. BIND_CONSTANT(CONTAINER_CANVAS_EDITOR_SIDE);
  263. BIND_CONSTANT(CONTAINER_PROPERTY_EDITOR_BOTTOM);
  264. BIND_CONSTANT(DOCK_SLOT_LEFT_UL);
  265. BIND_CONSTANT(DOCK_SLOT_LEFT_BL);
  266. BIND_CONSTANT(DOCK_SLOT_LEFT_UR);
  267. BIND_CONSTANT(DOCK_SLOT_LEFT_BR);
  268. BIND_CONSTANT(DOCK_SLOT_RIGHT_UL);
  269. BIND_CONSTANT(DOCK_SLOT_RIGHT_BL);
  270. BIND_CONSTANT(DOCK_SLOT_RIGHT_UR);
  271. BIND_CONSTANT(DOCK_SLOT_RIGHT_BR);
  272. BIND_CONSTANT(DOCK_SLOT_MAX);
  273. }
  274. EditorPlugin::EditorPlugin() {
  275. undo_redo = NULL;
  276. }
  277. EditorPlugin::~EditorPlugin() {
  278. }
  279. EditorPluginCreateFunc EditorPlugins::creation_funcs[MAX_CREATE_FUNCS];
  280. int EditorPlugins::creation_func_count = 0;