editor_plugin.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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_export.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/filesystem_dock.h"
  35. #include "editor/project_settings_editor.h"
  36. #include "editor_resource_preview.h"
  37. #include "main/main.h"
  38. #include "plugins/canvas_item_editor_plugin.h"
  39. #include "plugins/spatial_editor_plugin.h"
  40. #include "scene/3d/camera.h"
  41. #include "scene/gui/popup_menu.h"
  42. #include "servers/visual_server.h"
  43. Array EditorInterface::_make_mesh_previews(const Array &p_meshes, int p_preview_size) {
  44. Vector<Ref<Mesh> > meshes;
  45. for (int i = 0; i < p_meshes.size(); i++) {
  46. meshes.push_back(p_meshes[i]);
  47. }
  48. Vector<Ref<Texture2D> > textures = make_mesh_previews(meshes, NULL, p_preview_size);
  49. Array ret;
  50. for (int i = 0; i < textures.size(); i++) {
  51. ret.push_back(textures[i]);
  52. }
  53. return ret;
  54. }
  55. Vector<Ref<Texture2D> > EditorInterface::make_mesh_previews(const Vector<Ref<Mesh> > &p_meshes, Vector<Transform> *p_transforms, int p_preview_size) {
  56. int size = p_preview_size;
  57. RID scenario = VS::get_singleton()->scenario_create();
  58. RID viewport = VS::get_singleton()->viewport_create();
  59. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ALWAYS);
  60. VS::get_singleton()->viewport_set_scenario(viewport, scenario);
  61. VS::get_singleton()->viewport_set_size(viewport, size, size);
  62. VS::get_singleton()->viewport_set_transparent_background(viewport, true);
  63. VS::get_singleton()->viewport_set_active(viewport, true);
  64. RID viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  65. RID camera = VS::get_singleton()->camera_create();
  66. VS::get_singleton()->viewport_attach_camera(viewport, camera);
  67. RID light = VS::get_singleton()->directional_light_create();
  68. RID light_instance = VS::get_singleton()->instance_create2(light, scenario);
  69. RID light2 = VS::get_singleton()->directional_light_create();
  70. VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  71. RID light_instance2 = VS::get_singleton()->instance_create2(light2, scenario);
  72. EditorProgress ep("mlib", TTR("Creating Mesh Previews"), p_meshes.size());
  73. Vector<Ref<Texture2D> > textures;
  74. for (int i = 0; i < p_meshes.size(); i++) {
  75. Ref<Mesh> mesh = p_meshes[i];
  76. if (!mesh.is_valid()) {
  77. textures.push_back(Ref<Texture2D>());
  78. continue;
  79. }
  80. Transform mesh_xform;
  81. if (p_transforms != NULL) {
  82. mesh_xform = (*p_transforms)[i];
  83. }
  84. RID inst = VS::get_singleton()->instance_create2(mesh->get_rid(), scenario);
  85. VS::get_singleton()->instance_set_transform(inst, mesh_xform);
  86. AABB aabb = mesh->get_aabb();
  87. Vector3 ofs = aabb.position + aabb.size * 0.5;
  88. aabb.position -= ofs;
  89. Transform xform;
  90. xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI / 6);
  91. xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI / 6) * xform.basis;
  92. AABB rot_aabb = xform.xform(aabb);
  93. float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
  94. if (m == 0) {
  95. textures.push_back(Ref<Texture2D>());
  96. continue;
  97. }
  98. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  99. xform.origin.z -= rot_aabb.size.z * 2;
  100. xform.invert();
  101. xform = mesh_xform * xform;
  102. VS::get_singleton()->camera_set_transform(camera, xform * Transform(Basis(), Vector3(0, 0, 3)));
  103. VS::get_singleton()->camera_set_orthogonal(camera, m * 2, 0.01, 1000.0);
  104. VS::get_singleton()->instance_set_transform(light_instance, xform * Transform().looking_at(Vector3(-2, -1, -1), Vector3(0, 1, 0)));
  105. VS::get_singleton()->instance_set_transform(light_instance2, xform * Transform().looking_at(Vector3(+1, -1, -2), Vector3(0, 1, 0)));
  106. ep.step(TTR("Thumbnail..."), i);
  107. Main::iteration();
  108. Main::iteration();
  109. Ref<Image> img = VS::get_singleton()->texture_2d_get(viewport_texture);
  110. ERR_CONTINUE(!img.is_valid() || img->empty());
  111. Ref<ImageTexture> it(memnew(ImageTexture));
  112. it->create_from_image(img);
  113. VS::get_singleton()->free(inst);
  114. textures.push_back(it);
  115. }
  116. VS::get_singleton()->free(viewport);
  117. VS::get_singleton()->free(light);
  118. VS::get_singleton()->free(light_instance);
  119. VS::get_singleton()->free(light2);
  120. VS::get_singleton()->free(light_instance2);
  121. VS::get_singleton()->free(camera);
  122. VS::get_singleton()->free(scenario);
  123. return textures;
  124. }
  125. void EditorInterface::set_main_screen_editor(const String &p_name) {
  126. EditorNode::get_singleton()->select_editor_by_name(p_name);
  127. }
  128. Control *EditorInterface::get_editor_viewport() {
  129. return EditorNode::get_singleton()->get_viewport();
  130. }
  131. void EditorInterface::edit_resource(const Ref<Resource> &p_resource) {
  132. EditorNode::get_singleton()->edit_resource(p_resource);
  133. }
  134. void EditorInterface::open_scene_from_path(const String &scene_path) {
  135. if (EditorNode::get_singleton()->is_changing_scene()) {
  136. return;
  137. }
  138. EditorNode::get_singleton()->open_request(scene_path);
  139. }
  140. void EditorInterface::reload_scene_from_path(const String &scene_path) {
  141. if (EditorNode::get_singleton()->is_changing_scene()) {
  142. return;
  143. }
  144. EditorNode::get_singleton()->reload_scene(scene_path);
  145. }
  146. Node *EditorInterface::get_edited_scene_root() {
  147. return EditorNode::get_singleton()->get_edited_scene();
  148. }
  149. Array EditorInterface::get_open_scenes() const {
  150. Array ret;
  151. Vector<EditorData::EditedScene> scenes = EditorNode::get_editor_data().get_edited_scenes();
  152. int scns_amount = scenes.size();
  153. for (int idx_scn = 0; idx_scn < scns_amount; idx_scn++) {
  154. if (scenes[idx_scn].root == NULL)
  155. continue;
  156. ret.push_back(scenes[idx_scn].root->get_filename());
  157. }
  158. return ret;
  159. }
  160. ScriptEditor *EditorInterface::get_script_editor() {
  161. return ScriptEditor::get_singleton();
  162. }
  163. void EditorInterface::select_file(const String &p_file) {
  164. EditorNode::get_singleton()->get_filesystem_dock()->select_file(p_file);
  165. }
  166. String EditorInterface::get_selected_path() const {
  167. return EditorNode::get_singleton()->get_filesystem_dock()->get_selected_path();
  168. }
  169. String EditorInterface::get_current_path() const {
  170. return EditorNode::get_singleton()->get_filesystem_dock()->get_current_path();
  171. }
  172. void EditorInterface::inspect_object(Object *p_obj, const String &p_for_property) {
  173. EditorNode::get_singleton()->push_item(p_obj, p_for_property);
  174. }
  175. EditorFileSystem *EditorInterface::get_resource_file_system() {
  176. return EditorFileSystem::get_singleton();
  177. }
  178. EditorSelection *EditorInterface::get_selection() {
  179. return EditorNode::get_singleton()->get_editor_selection();
  180. }
  181. Ref<EditorSettings> EditorInterface::get_editor_settings() {
  182. return EditorSettings::get_singleton();
  183. }
  184. EditorResourcePreview *EditorInterface::get_resource_previewer() {
  185. return EditorResourcePreview::get_singleton();
  186. }
  187. Control *EditorInterface::get_base_control() {
  188. return EditorNode::get_singleton()->get_gui_base();
  189. }
  190. void EditorInterface::set_plugin_enabled(const String &p_plugin, bool p_enabled) {
  191. EditorNode::get_singleton()->set_addon_plugin_enabled(p_plugin, p_enabled, true);
  192. }
  193. bool EditorInterface::is_plugin_enabled(const String &p_plugin) const {
  194. return EditorNode::get_singleton()->is_addon_plugin_enabled(p_plugin);
  195. }
  196. EditorInspector *EditorInterface::get_inspector() const {
  197. return EditorNode::get_singleton()->get_inspector();
  198. }
  199. Error EditorInterface::save_scene() {
  200. if (!get_edited_scene_root())
  201. return ERR_CANT_CREATE;
  202. if (get_edited_scene_root()->get_filename() == String())
  203. return ERR_CANT_CREATE;
  204. save_scene_as(get_edited_scene_root()->get_filename());
  205. return OK;
  206. }
  207. void EditorInterface::save_scene_as(const String &p_scene, bool p_with_preview) {
  208. EditorNode::get_singleton()->save_scene_to_path(p_scene, p_with_preview);
  209. }
  210. void EditorInterface::set_distraction_free_mode(bool p_enter) {
  211. EditorNode::get_singleton()->set_distraction_free_mode(p_enter);
  212. }
  213. EditorInterface *EditorInterface::singleton = NULL;
  214. void EditorInterface::_bind_methods() {
  215. ClassDB::bind_method(D_METHOD("inspect_object", "object", "for_property"), &EditorInterface::inspect_object, DEFVAL(String()));
  216. ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection);
  217. ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings);
  218. ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor);
  219. ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control);
  220. ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorInterface::edit_resource);
  221. ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath"), &EditorInterface::open_scene_from_path);
  222. ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorInterface::reload_scene_from_path);
  223. ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorInterface::get_open_scenes);
  224. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root);
  225. ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);
  226. ClassDB::bind_method(D_METHOD("get_resource_filesystem"), &EditorInterface::get_resource_file_system);
  227. ClassDB::bind_method(D_METHOD("get_editor_viewport"), &EditorInterface::get_editor_viewport);
  228. ClassDB::bind_method(D_METHOD("make_mesh_previews", "meshes", "preview_size"), &EditorInterface::_make_mesh_previews);
  229. ClassDB::bind_method(D_METHOD("select_file", "file"), &EditorInterface::select_file);
  230. ClassDB::bind_method(D_METHOD("get_selected_path"), &EditorInterface::get_selected_path);
  231. ClassDB::bind_method(D_METHOD("get_current_path"), &EditorInterface::get_current_path);
  232. ClassDB::bind_method(D_METHOD("set_plugin_enabled", "plugin", "enabled"), &EditorInterface::set_plugin_enabled);
  233. ClassDB::bind_method(D_METHOD("is_plugin_enabled", "plugin"), &EditorInterface::is_plugin_enabled);
  234. ClassDB::bind_method(D_METHOD("get_inspector"), &EditorInterface::get_inspector);
  235. ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene);
  236. ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true));
  237. ClassDB::bind_method(D_METHOD("set_main_screen_editor", "name"), &EditorInterface::set_main_screen_editor);
  238. ClassDB::bind_method(D_METHOD("set_distraction_free_mode", "enter"), &EditorInterface::set_distraction_free_mode);
  239. }
  240. EditorInterface::EditorInterface() {
  241. singleton = this;
  242. }
  243. ///////////////////////////////////////////
  244. void EditorPlugin::add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon) {
  245. EditorNode::get_editor_data().add_custom_type(p_type, p_base, p_script, p_icon);
  246. }
  247. void EditorPlugin::remove_custom_type(const String &p_type) {
  248. EditorNode::get_editor_data().remove_custom_type(p_type);
  249. }
  250. void EditorPlugin::add_autoload_singleton(const String &p_name, const String &p_path) {
  251. EditorNode::get_singleton()->get_project_settings()->get_autoload_settings()->autoload_add(p_name, p_path);
  252. }
  253. void EditorPlugin::remove_autoload_singleton(const String &p_name) {
  254. EditorNode::get_singleton()->get_project_settings()->get_autoload_settings()->autoload_remove(p_name);
  255. }
  256. ToolButton *EditorPlugin::add_control_to_bottom_panel(Control *p_control, const String &p_title) {
  257. ERR_FAIL_NULL_V(p_control, NULL);
  258. return EditorNode::get_singleton()->add_bottom_panel_item(p_title, p_control);
  259. }
  260. void EditorPlugin::add_control_to_dock(DockSlot p_slot, Control *p_control) {
  261. ERR_FAIL_NULL(p_control);
  262. EditorNode::get_singleton()->add_control_to_dock(EditorNode::DockSlot(p_slot), p_control);
  263. }
  264. void EditorPlugin::remove_control_from_docks(Control *p_control) {
  265. ERR_FAIL_NULL(p_control);
  266. EditorNode::get_singleton()->remove_control_from_dock(p_control);
  267. }
  268. void EditorPlugin::remove_control_from_bottom_panel(Control *p_control) {
  269. ERR_FAIL_NULL(p_control);
  270. EditorNode::get_singleton()->remove_bottom_panel_item(p_control);
  271. }
  272. void EditorPlugin::add_control_to_container(CustomControlContainer p_location, Control *p_control) {
  273. ERR_FAIL_NULL(p_control);
  274. switch (p_location) {
  275. case CONTAINER_TOOLBAR: {
  276. EditorNode::get_menu_hb()->add_child(p_control);
  277. } break;
  278. case CONTAINER_SPATIAL_EDITOR_MENU: {
  279. SpatialEditor::get_singleton()->add_control_to_menu_panel(p_control);
  280. } break;
  281. case CONTAINER_SPATIAL_EDITOR_SIDE_LEFT: {
  282. SpatialEditor::get_singleton()->get_palette_split()->add_child(p_control);
  283. SpatialEditor::get_singleton()->get_palette_split()->move_child(p_control, 0);
  284. } break;
  285. case CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT: {
  286. SpatialEditor::get_singleton()->get_palette_split()->add_child(p_control);
  287. SpatialEditor::get_singleton()->get_palette_split()->move_child(p_control, 1);
  288. } break;
  289. case CONTAINER_SPATIAL_EDITOR_BOTTOM: {
  290. SpatialEditor::get_singleton()->get_shader_split()->add_child(p_control);
  291. } break;
  292. case CONTAINER_CANVAS_EDITOR_MENU: {
  293. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(p_control);
  294. } break;
  295. case CONTAINER_CANVAS_EDITOR_SIDE_LEFT: {
  296. CanvasItemEditor::get_singleton()->get_palette_split()->add_child(p_control);
  297. CanvasItemEditor::get_singleton()->get_palette_split()->move_child(p_control, 0);
  298. } break;
  299. case CONTAINER_CANVAS_EDITOR_SIDE_RIGHT: {
  300. CanvasItemEditor::get_singleton()->get_palette_split()->add_child(p_control);
  301. CanvasItemEditor::get_singleton()->get_palette_split()->move_child(p_control, 1);
  302. } break;
  303. case CONTAINER_CANVAS_EDITOR_BOTTOM: {
  304. CanvasItemEditor::get_singleton()->get_bottom_split()->add_child(p_control);
  305. } break;
  306. case CONTAINER_PROPERTY_EDITOR_BOTTOM: {
  307. EditorNode::get_singleton()->get_inspector_dock_addon_area()->add_child(p_control);
  308. } break;
  309. case CONTAINER_PROJECT_SETTING_TAB_LEFT: {
  310. ProjectSettingsEditor::get_singleton()->get_tabs()->add_child(p_control);
  311. ProjectSettingsEditor::get_singleton()->get_tabs()->move_child(p_control, 0);
  312. } break;
  313. case CONTAINER_PROJECT_SETTING_TAB_RIGHT: {
  314. ProjectSettingsEditor::get_singleton()->get_tabs()->add_child(p_control);
  315. ProjectSettingsEditor::get_singleton()->get_tabs()->move_child(p_control, 1);
  316. } break;
  317. }
  318. }
  319. void EditorPlugin::remove_control_from_container(CustomControlContainer p_location, Control *p_control) {
  320. ERR_FAIL_NULL(p_control);
  321. switch (p_location) {
  322. case CONTAINER_TOOLBAR: {
  323. EditorNode::get_menu_hb()->remove_child(p_control);
  324. } break;
  325. case CONTAINER_SPATIAL_EDITOR_MENU: {
  326. SpatialEditor::get_singleton()->remove_control_from_menu_panel(p_control);
  327. } break;
  328. case CONTAINER_SPATIAL_EDITOR_SIDE_LEFT:
  329. case CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT: {
  330. SpatialEditor::get_singleton()->get_palette_split()->remove_child(p_control);
  331. } break;
  332. case CONTAINER_SPATIAL_EDITOR_BOTTOM: {
  333. SpatialEditor::get_singleton()->get_shader_split()->remove_child(p_control);
  334. } break;
  335. case CONTAINER_CANVAS_EDITOR_MENU: {
  336. CanvasItemEditor::get_singleton()->remove_control_from_menu_panel(p_control);
  337. } break;
  338. case CONTAINER_CANVAS_EDITOR_SIDE_LEFT:
  339. case CONTAINER_CANVAS_EDITOR_SIDE_RIGHT: {
  340. CanvasItemEditor::get_singleton()->get_palette_split()->remove_child(p_control);
  341. } break;
  342. case CONTAINER_CANVAS_EDITOR_BOTTOM: {
  343. CanvasItemEditor::get_singleton()->get_bottom_split()->remove_child(p_control);
  344. } break;
  345. case CONTAINER_PROPERTY_EDITOR_BOTTOM: {
  346. EditorNode::get_singleton()->get_inspector_dock_addon_area()->remove_child(p_control);
  347. } break;
  348. case CONTAINER_PROJECT_SETTING_TAB_LEFT:
  349. case CONTAINER_PROJECT_SETTING_TAB_RIGHT: {
  350. ProjectSettingsEditor::get_singleton()->get_tabs()->remove_child(p_control);
  351. } break;
  352. }
  353. }
  354. void EditorPlugin::add_tool_menu_item(const String &p_name, Object *p_handler, const String &p_callback, const Variant &p_ud) {
  355. EditorNode::get_singleton()->add_tool_menu_item(p_name, p_handler, p_callback, p_ud);
  356. }
  357. void EditorPlugin::add_tool_submenu_item(const String &p_name, Object *p_submenu) {
  358. ERR_FAIL_NULL(p_submenu);
  359. PopupMenu *submenu = Object::cast_to<PopupMenu>(p_submenu);
  360. ERR_FAIL_NULL(submenu);
  361. EditorNode::get_singleton()->add_tool_submenu_item(p_name, submenu);
  362. }
  363. void EditorPlugin::remove_tool_menu_item(const String &p_name) {
  364. EditorNode::get_singleton()->remove_tool_menu_item(p_name);
  365. }
  366. void EditorPlugin::set_input_event_forwarding_always_enabled() {
  367. input_event_forwarding_always_enabled = true;
  368. EditorPluginList *always_input_forwarding_list = EditorNode::get_singleton()->get_editor_plugins_force_input_forwarding();
  369. always_input_forwarding_list->add_plugin(this);
  370. }
  371. void EditorPlugin::set_force_draw_over_forwarding_enabled() {
  372. force_draw_over_forwarding_enabled = true;
  373. EditorPluginList *always_draw_over_forwarding_list = EditorNode::get_singleton()->get_editor_plugins_force_over();
  374. always_draw_over_forwarding_list->add_plugin(this);
  375. }
  376. void EditorPlugin::notify_scene_changed(const Node *scn_root) {
  377. emit_signal("scene_changed", scn_root);
  378. }
  379. void EditorPlugin::notify_main_screen_changed(const String &screen_name) {
  380. if (screen_name == last_main_screen_name)
  381. return;
  382. emit_signal("main_screen_changed", screen_name);
  383. last_main_screen_name = screen_name;
  384. }
  385. void EditorPlugin::notify_scene_closed(const String &scene_filepath) {
  386. emit_signal("scene_closed", scene_filepath);
  387. }
  388. void EditorPlugin::notify_resource_saved(const Ref<Resource> &p_resource) {
  389. emit_signal("resource_saved", p_resource);
  390. }
  391. bool EditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  392. if (get_script_instance() && get_script_instance()->has_method("forward_canvas_gui_input")) {
  393. return get_script_instance()->call("forward_canvas_gui_input", p_event);
  394. }
  395. return false;
  396. }
  397. void EditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {
  398. if (get_script_instance() && get_script_instance()->has_method("forward_canvas_draw_over_viewport")) {
  399. get_script_instance()->call("forward_canvas_draw_over_viewport", p_overlay);
  400. }
  401. }
  402. void EditorPlugin::forward_canvas_force_draw_over_viewport(Control *p_overlay) {
  403. if (get_script_instance() && get_script_instance()->has_method("forward_canvas_force_draw_over_viewport")) {
  404. get_script_instance()->call("forward_canvas_force_draw_over_viewport", p_overlay);
  405. }
  406. }
  407. // Updates the overlays of the 2D viewport or, if in 3D mode, of every 3D viewport.
  408. int EditorPlugin::update_overlays() const {
  409. if (SpatialEditor::get_singleton()->is_visible()) {
  410. int count = 0;
  411. for (uint32_t i = 0; i < SpatialEditor::VIEWPORTS_COUNT; i++) {
  412. SpatialEditorViewport *vp = SpatialEditor::get_singleton()->get_editor_viewport(i);
  413. if (vp->is_visible()) {
  414. vp->update_surface();
  415. count++;
  416. }
  417. }
  418. return count;
  419. } else {
  420. // This will update the normal viewport itself as well
  421. CanvasItemEditor::get_singleton()->get_viewport_control()->update();
  422. return 1;
  423. }
  424. }
  425. bool EditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) {
  426. if (get_script_instance() && get_script_instance()->has_method("forward_spatial_gui_input")) {
  427. return get_script_instance()->call("forward_spatial_gui_input", p_camera, p_event);
  428. }
  429. return false;
  430. }
  431. void EditorPlugin::forward_spatial_draw_over_viewport(Control *p_overlay) {
  432. if (get_script_instance() && get_script_instance()->has_method("forward_spatial_draw_over_viewport")) {
  433. get_script_instance()->call("forward_spatial_draw_over_viewport", p_overlay);
  434. }
  435. }
  436. void EditorPlugin::forward_spatial_force_draw_over_viewport(Control *p_overlay) {
  437. if (get_script_instance() && get_script_instance()->has_method("forward_spatial_force_draw_over_viewport")) {
  438. get_script_instance()->call("forward_spatial_force_draw_over_viewport", p_overlay);
  439. }
  440. }
  441. String EditorPlugin::get_name() const {
  442. if (get_script_instance() && get_script_instance()->has_method("get_plugin_name")) {
  443. return get_script_instance()->call("get_plugin_name");
  444. }
  445. return String();
  446. }
  447. const Ref<Texture2D> EditorPlugin::get_icon() const {
  448. if (get_script_instance() && get_script_instance()->has_method("get_plugin_icon")) {
  449. return get_script_instance()->call("get_plugin_icon");
  450. }
  451. return Ref<Texture2D>();
  452. }
  453. bool EditorPlugin::has_main_screen() const {
  454. if (get_script_instance() && get_script_instance()->has_method("has_main_screen")) {
  455. return get_script_instance()->call("has_main_screen");
  456. }
  457. return false;
  458. }
  459. void EditorPlugin::make_visible(bool p_visible) {
  460. if (get_script_instance() && get_script_instance()->has_method("make_visible")) {
  461. get_script_instance()->call("make_visible", p_visible);
  462. }
  463. }
  464. void EditorPlugin::edit(Object *p_object) {
  465. if (get_script_instance() && get_script_instance()->has_method("edit")) {
  466. if (p_object->is_class("Resource")) {
  467. get_script_instance()->call("edit", Ref<Resource>(Object::cast_to<Resource>(p_object)));
  468. } else {
  469. get_script_instance()->call("edit", p_object);
  470. }
  471. }
  472. }
  473. bool EditorPlugin::handles(Object *p_object) const {
  474. if (get_script_instance() && get_script_instance()->has_method("handles")) {
  475. return get_script_instance()->call("handles", p_object);
  476. }
  477. return false;
  478. }
  479. Dictionary EditorPlugin::get_state() const {
  480. if (get_script_instance() && get_script_instance()->has_method("get_state")) {
  481. return get_script_instance()->call("get_state");
  482. }
  483. return Dictionary();
  484. }
  485. void EditorPlugin::set_state(const Dictionary &p_state) {
  486. if (get_script_instance() && get_script_instance()->has_method("set_state")) {
  487. get_script_instance()->call("set_state", p_state);
  488. }
  489. }
  490. void EditorPlugin::clear() {
  491. if (get_script_instance() && get_script_instance()->has_method("clear")) {
  492. get_script_instance()->call("clear");
  493. }
  494. }
  495. // if editor references external resources/scenes, save them
  496. void EditorPlugin::save_external_data() {
  497. if (get_script_instance() && get_script_instance()->has_method("save_external_data")) {
  498. get_script_instance()->call("save_external_data");
  499. }
  500. }
  501. // if changes are pending in editor, apply them
  502. void EditorPlugin::apply_changes() {
  503. if (get_script_instance() && get_script_instance()->has_method("apply_changes")) {
  504. get_script_instance()->call("apply_changes");
  505. }
  506. }
  507. void EditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  508. if (get_script_instance() && get_script_instance()->has_method("get_breakpoints")) {
  509. PackedStringArray arr = get_script_instance()->call("get_breakpoints");
  510. for (int i = 0; i < arr.size(); i++)
  511. p_breakpoints->push_back(arr[i]);
  512. }
  513. }
  514. bool EditorPlugin::get_remove_list(List<Node *> *p_list) {
  515. return false;
  516. }
  517. void EditorPlugin::restore_global_state() {}
  518. void EditorPlugin::save_global_state() {}
  519. void EditorPlugin::add_import_plugin(const Ref<EditorImportPlugin> &p_importer) {
  520. ResourceFormatImporter::get_singleton()->add_importer(p_importer);
  521. EditorFileSystem::get_singleton()->call_deferred("scan");
  522. }
  523. void EditorPlugin::remove_import_plugin(const Ref<EditorImportPlugin> &p_importer) {
  524. ResourceFormatImporter::get_singleton()->remove_importer(p_importer);
  525. EditorFileSystem::get_singleton()->call_deferred("scan");
  526. }
  527. void EditorPlugin::add_export_plugin(const Ref<EditorExportPlugin> &p_exporter) {
  528. EditorExport::get_singleton()->add_export_plugin(p_exporter);
  529. }
  530. void EditorPlugin::remove_export_plugin(const Ref<EditorExportPlugin> &p_exporter) {
  531. EditorExport::get_singleton()->remove_export_plugin(p_exporter);
  532. }
  533. void EditorPlugin::add_spatial_gizmo_plugin(const Ref<EditorSpatialGizmoPlugin> &p_gizmo_plugin) {
  534. SpatialEditor::get_singleton()->add_gizmo_plugin(p_gizmo_plugin);
  535. }
  536. void EditorPlugin::remove_spatial_gizmo_plugin(const Ref<EditorSpatialGizmoPlugin> &p_gizmo_plugin) {
  537. SpatialEditor::get_singleton()->remove_gizmo_plugin(p_gizmo_plugin);
  538. }
  539. void EditorPlugin::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  540. EditorInspector::add_inspector_plugin(p_plugin);
  541. }
  542. void EditorPlugin::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  543. EditorInspector::remove_inspector_plugin(p_plugin);
  544. }
  545. void EditorPlugin::add_scene_import_plugin(const Ref<EditorSceneImporter> &p_importer) {
  546. ResourceImporterScene::get_singleton()->add_importer(p_importer);
  547. }
  548. void EditorPlugin::remove_scene_import_plugin(const Ref<EditorSceneImporter> &p_importer) {
  549. ResourceImporterScene::get_singleton()->remove_importer(p_importer);
  550. }
  551. int find(const PackedStringArray &a, const String &v) {
  552. const String *r = a.ptr();
  553. for (int j = 0; j < a.size(); ++j) {
  554. if (r[j] == v) {
  555. return j;
  556. }
  557. }
  558. return -1;
  559. }
  560. void EditorPlugin::enable_plugin() {
  561. // Called when the plugin gets enabled in project settings, after it's added to the tree.
  562. // You can implement it to register autoloads.
  563. if (get_script_instance() && get_script_instance()->has_method("enable_plugin")) {
  564. get_script_instance()->call("enable_plugin");
  565. }
  566. }
  567. void EditorPlugin::disable_plugin() {
  568. // Last function called when the plugin gets disabled in project settings.
  569. // Implement it to cleanup things from the project, such as unregister autoloads.
  570. if (get_script_instance() && get_script_instance()->has_method("disable_plugin")) {
  571. get_script_instance()->call("disable_plugin");
  572. }
  573. }
  574. void EditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  575. if (get_script_instance() && get_script_instance()->has_method("set_window_layout")) {
  576. get_script_instance()->call("set_window_layout", p_layout);
  577. }
  578. }
  579. void EditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  580. if (get_script_instance() && get_script_instance()->has_method("get_window_layout")) {
  581. get_script_instance()->call("get_window_layout", p_layout);
  582. }
  583. }
  584. bool EditorPlugin::build() {
  585. if (get_script_instance() && get_script_instance()->has_method("build")) {
  586. return get_script_instance()->call("build");
  587. }
  588. return true;
  589. }
  590. void EditorPlugin::queue_save_layout() const {
  591. EditorNode::get_singleton()->save_layout();
  592. }
  593. void EditorPlugin::make_bottom_panel_item_visible(Control *p_item) {
  594. EditorNode::get_singleton()->make_bottom_panel_item_visible(p_item);
  595. }
  596. void EditorPlugin::hide_bottom_panel() {
  597. EditorNode::get_singleton()->hide_bottom_panel();
  598. }
  599. EditorInterface *EditorPlugin::get_editor_interface() {
  600. return EditorInterface::get_singleton();
  601. }
  602. ScriptCreateDialog *EditorPlugin::get_script_create_dialog() {
  603. return EditorNode::get_singleton()->get_script_create_dialog();
  604. }
  605. void EditorPlugin::_bind_methods() {
  606. ClassDB::bind_method(D_METHOD("add_control_to_container", "container", "control"), &EditorPlugin::add_control_to_container);
  607. ClassDB::bind_method(D_METHOD("add_control_to_bottom_panel", "control", "title"), &EditorPlugin::add_control_to_bottom_panel);
  608. ClassDB::bind_method(D_METHOD("add_control_to_dock", "slot", "control"), &EditorPlugin::add_control_to_dock);
  609. ClassDB::bind_method(D_METHOD("remove_control_from_docks", "control"), &EditorPlugin::remove_control_from_docks);
  610. ClassDB::bind_method(D_METHOD("remove_control_from_bottom_panel", "control"), &EditorPlugin::remove_control_from_bottom_panel);
  611. ClassDB::bind_method(D_METHOD("remove_control_from_container", "container", "control"), &EditorPlugin::remove_control_from_container);
  612. ClassDB::bind_method(D_METHOD("add_tool_menu_item", "name", "handler", "callback", "ud"), &EditorPlugin::add_tool_menu_item, DEFVAL(Variant()));
  613. ClassDB::bind_method(D_METHOD("add_tool_submenu_item", "name", "submenu"), &EditorPlugin::add_tool_submenu_item);
  614. ClassDB::bind_method(D_METHOD("remove_tool_menu_item", "name"), &EditorPlugin::remove_tool_menu_item);
  615. ClassDB::bind_method(D_METHOD("add_custom_type", "type", "base", "script", "icon"), &EditorPlugin::add_custom_type);
  616. ClassDB::bind_method(D_METHOD("remove_custom_type", "type"), &EditorPlugin::remove_custom_type);
  617. ClassDB::bind_method(D_METHOD("add_autoload_singleton", "name", "path"), &EditorPlugin::add_autoload_singleton);
  618. ClassDB::bind_method(D_METHOD("remove_autoload_singleton", "name"), &EditorPlugin::remove_autoload_singleton);
  619. ClassDB::bind_method(D_METHOD("update_overlays"), &EditorPlugin::update_overlays);
  620. ClassDB::bind_method(D_METHOD("make_bottom_panel_item_visible", "item"), &EditorPlugin::make_bottom_panel_item_visible);
  621. ClassDB::bind_method(D_METHOD("hide_bottom_panel"), &EditorPlugin::hide_bottom_panel);
  622. ClassDB::bind_method(D_METHOD("get_undo_redo"), &EditorPlugin::_get_undo_redo);
  623. ClassDB::bind_method(D_METHOD("queue_save_layout"), &EditorPlugin::queue_save_layout);
  624. ClassDB::bind_method(D_METHOD("add_import_plugin", "importer"), &EditorPlugin::add_import_plugin);
  625. ClassDB::bind_method(D_METHOD("remove_import_plugin", "importer"), &EditorPlugin::remove_import_plugin);
  626. ClassDB::bind_method(D_METHOD("add_scene_import_plugin", "scene_importer"), &EditorPlugin::add_scene_import_plugin);
  627. ClassDB::bind_method(D_METHOD("remove_scene_import_plugin", "scene_importer"), &EditorPlugin::remove_scene_import_plugin);
  628. ClassDB::bind_method(D_METHOD("add_export_plugin", "plugin"), &EditorPlugin::add_export_plugin);
  629. ClassDB::bind_method(D_METHOD("remove_export_plugin", "plugin"), &EditorPlugin::remove_export_plugin);
  630. ClassDB::bind_method(D_METHOD("add_spatial_gizmo_plugin", "plugin"), &EditorPlugin::add_spatial_gizmo_plugin);
  631. ClassDB::bind_method(D_METHOD("remove_spatial_gizmo_plugin", "plugin"), &EditorPlugin::remove_spatial_gizmo_plugin);
  632. ClassDB::bind_method(D_METHOD("add_inspector_plugin", "plugin"), &EditorPlugin::add_inspector_plugin);
  633. ClassDB::bind_method(D_METHOD("remove_inspector_plugin", "plugin"), &EditorPlugin::remove_inspector_plugin);
  634. ClassDB::bind_method(D_METHOD("set_input_event_forwarding_always_enabled"), &EditorPlugin::set_input_event_forwarding_always_enabled);
  635. ClassDB::bind_method(D_METHOD("set_force_draw_over_forwarding_enabled"), &EditorPlugin::set_force_draw_over_forwarding_enabled);
  636. ClassDB::bind_method(D_METHOD("get_editor_interface"), &EditorPlugin::get_editor_interface);
  637. ClassDB::bind_method(D_METHOD("get_script_create_dialog"), &EditorPlugin::get_script_create_dialog);
  638. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_canvas_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  639. ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_canvas_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
  640. ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_canvas_force_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
  641. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_spatial_gui_input", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  642. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_plugin_name"));
  643. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::OBJECT, "get_plugin_icon"));
  644. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "has_main_screen"));
  645. ClassDB::add_virtual_method(get_class_static(), MethodInfo("make_visible", PropertyInfo(Variant::BOOL, "visible")));
  646. ClassDB::add_virtual_method(get_class_static(), MethodInfo("edit", PropertyInfo(Variant::OBJECT, "object")));
  647. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles", PropertyInfo(Variant::OBJECT, "object")));
  648. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::DICTIONARY, "get_state"));
  649. ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_state", PropertyInfo(Variant::DICTIONARY, "state")));
  650. ClassDB::add_virtual_method(get_class_static(), MethodInfo("clear"));
  651. ClassDB::add_virtual_method(get_class_static(), MethodInfo("save_external_data"));
  652. ClassDB::add_virtual_method(get_class_static(), MethodInfo("apply_changes"));
  653. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::PACKED_STRING_ARRAY, "get_breakpoints"));
  654. ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile")));
  655. ClassDB::add_virtual_method(get_class_static(), MethodInfo("get_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile")));
  656. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "build"));
  657. ClassDB::add_virtual_method(get_class_static(), MethodInfo("enable_plugin"));
  658. ClassDB::add_virtual_method(get_class_static(), MethodInfo("disable_plugin"));
  659. ADD_SIGNAL(MethodInfo("scene_changed", PropertyInfo(Variant::OBJECT, "scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  660. ADD_SIGNAL(MethodInfo("scene_closed", PropertyInfo(Variant::STRING, "filepath")));
  661. ADD_SIGNAL(MethodInfo("main_screen_changed", PropertyInfo(Variant::STRING, "screen_name")));
  662. ADD_SIGNAL(MethodInfo("resource_saved", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  663. BIND_ENUM_CONSTANT(CONTAINER_TOOLBAR);
  664. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_MENU);
  665. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_SIDE_LEFT);
  666. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT);
  667. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_BOTTOM);
  668. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_MENU);
  669. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_SIDE_LEFT);
  670. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_SIDE_RIGHT);
  671. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_BOTTOM);
  672. BIND_ENUM_CONSTANT(CONTAINER_PROPERTY_EDITOR_BOTTOM);
  673. BIND_ENUM_CONSTANT(CONTAINER_PROJECT_SETTING_TAB_LEFT);
  674. BIND_ENUM_CONSTANT(CONTAINER_PROJECT_SETTING_TAB_RIGHT);
  675. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_UL);
  676. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_BL);
  677. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_UR);
  678. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_BR);
  679. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_UL);
  680. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_BL);
  681. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_UR);
  682. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_BR);
  683. BIND_ENUM_CONSTANT(DOCK_SLOT_MAX);
  684. }
  685. EditorPlugin::EditorPlugin() :
  686. undo_redo(NULL),
  687. input_event_forwarding_always_enabled(false),
  688. force_draw_over_forwarding_enabled(false),
  689. last_main_screen_name("") {
  690. }
  691. EditorPlugin::~EditorPlugin() {
  692. }
  693. EditorPluginCreateFunc EditorPlugins::creation_funcs[MAX_CREATE_FUNCS];
  694. int EditorPlugins::creation_func_count = 0;