2
0

editor_plugin.cpp 34 KB

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