editor_plugin.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*************************************************************************/
  2. /* editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. print_line("aabb: " + aabb);
  89. Vector3 ofs = aabb.position + aabb.size * 0.5;
  90. aabb.position -= ofs;
  91. Transform xform;
  92. xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.25);
  93. xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.25) * xform.basis;
  94. AABB rot_aabb = xform.xform(aabb);
  95. print_line("rot_aabb: " + rot_aabb);
  96. float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
  97. if (m == 0) {
  98. textures.push_back(Ref<Texture>());
  99. continue;
  100. }
  101. m = 1.0 / m;
  102. m *= 0.5;
  103. print_line("scale: " + rtos(m));
  104. xform.basis.scale(Vector3(m, m, m));
  105. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  106. xform.origin.z -= rot_aabb.size.z * 2;
  107. RID inst = VS::get_singleton()->instance_create2(mesh->get_rid(), scenario);
  108. VS::get_singleton()->instance_set_transform(inst, xform);
  109. ep.step(TTR("Thumbnail.."), i);
  110. Main::iteration();
  111. Main::iteration();
  112. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  113. ERR_CONTINUE(!img.is_valid() || img->empty());
  114. Ref<ImageTexture> it(memnew(ImageTexture));
  115. it->create_from_image(img);
  116. //print_line("loaded image, size: "+rtos(m)+" dist: "+rtos(dist)+" empty?"+itos(img.empty())+" w: "+itos(it->get_width())+" h: "+itos(it->get_height()));
  117. VS::get_singleton()->free(inst);
  118. textures.push_back(it);
  119. }
  120. VS::get_singleton()->free(mesh_instance);
  121. VS::get_singleton()->free(viewport);
  122. VS::get_singleton()->free(light);
  123. VS::get_singleton()->free(light_instance);
  124. VS::get_singleton()->free(light2);
  125. VS::get_singleton()->free(light_instance2);
  126. VS::get_singleton()->free(camera);
  127. VS::get_singleton()->free(scenario);
  128. return textures;
  129. }
  130. Control *EditorInterface::get_editor_viewport() {
  131. return EditorNode::get_singleton()->get_viewport();
  132. }
  133. void EditorInterface::edit_resource(const Ref<Resource> &p_resource) {
  134. EditorNode::get_singleton()->edit_resource(p_resource);
  135. }
  136. void EditorInterface::open_scene_from_path(const String &scene_path) {
  137. if (EditorNode::get_singleton()->is_changing_scene()) {
  138. return;
  139. }
  140. EditorNode::get_singleton()->open_request(scene_path);
  141. }
  142. void EditorInterface::reload_scene_from_path(const String &scene_path) {
  143. if (EditorNode::get_singleton()->is_changing_scene()) {
  144. return;
  145. }
  146. EditorNode::get_singleton()->reload_scene(scene_path);
  147. }
  148. Node *EditorInterface::get_edited_scene_root() {
  149. return EditorNode::get_singleton()->get_edited_scene();
  150. }
  151. Array EditorInterface::get_open_scenes() const {
  152. Array ret;
  153. Vector<EditorData::EditedScene> scenes = EditorNode::get_singleton()->get_editor_data().get_edited_scenes();
  154. int scns_amount = scenes.size();
  155. for (int idx_scn = 0; idx_scn < scns_amount; idx_scn++) {
  156. if (scenes[idx_scn].root == NULL)
  157. continue;
  158. ret.push_back(scenes[idx_scn].root->get_filename());
  159. }
  160. return ret;
  161. }
  162. ScriptEditor *EditorInterface::get_script_editor() {
  163. return ScriptEditor::get_singleton();
  164. }
  165. void EditorInterface::select_file(const String &p_file) {
  166. return EditorNode::get_singleton()->get_filesystem_dock()->select_file(p_file);
  167. }
  168. String EditorInterface::get_selected_path() const {
  169. return EditorNode::get_singleton()->get_filesystem_dock()->get_selected_path();
  170. }
  171. void EditorInterface::inspect_object(Object *p_obj, const String &p_for_property) {
  172. EditorNode::get_singleton()->push_item(p_obj, p_for_property);
  173. }
  174. EditorFileSystem *EditorInterface::get_resource_file_system() {
  175. return EditorFileSystem::get_singleton();
  176. }
  177. EditorSelection *EditorInterface::get_selection() {
  178. return EditorNode::get_singleton()->get_editor_selection();
  179. }
  180. EditorSettings *EditorInterface::get_editor_settings() {
  181. return EditorSettings::get_singleton();
  182. }
  183. EditorResourcePreview *EditorInterface::get_resource_previewer() {
  184. return EditorResourcePreview::get_singleton();
  185. }
  186. Control *EditorInterface::get_base_control() {
  187. return EditorNode::get_singleton()->get_gui_base();
  188. }
  189. Error EditorInterface::save_scene() {
  190. if (!get_edited_scene_root())
  191. return ERR_CANT_CREATE;
  192. if (get_edited_scene_root()->get_filename() == String())
  193. return ERR_CANT_CREATE;
  194. save_scene_as(get_edited_scene_root()->get_filename());
  195. return OK;
  196. }
  197. void EditorInterface::save_scene_as(const String &p_scene, bool p_with_preview) {
  198. EditorNode::get_singleton()->save_scene_to_path(p_scene, p_with_preview);
  199. }
  200. EditorInterface *EditorInterface::singleton = NULL;
  201. void EditorInterface::_bind_methods() {
  202. ClassDB::bind_method(D_METHOD("inspect_object", "object", "for_property"), &EditorInterface::inspect_object, DEFVAL(String()));
  203. ClassDB::bind_method(D_METHOD("get_selection"), &EditorInterface::get_selection);
  204. ClassDB::bind_method(D_METHOD("get_editor_settings"), &EditorInterface::get_editor_settings);
  205. ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor);
  206. ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control);
  207. ClassDB::bind_method(D_METHOD("edit_resource", "resource"), &EditorInterface::edit_resource);
  208. ClassDB::bind_method(D_METHOD("open_scene_from_path", "scene_filepath"), &EditorInterface::open_scene_from_path);
  209. ClassDB::bind_method(D_METHOD("reload_scene_from_path", "scene_filepath"), &EditorInterface::reload_scene_from_path);
  210. ClassDB::bind_method(D_METHOD("get_open_scenes"), &EditorInterface::get_open_scenes);
  211. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &EditorInterface::get_edited_scene_root);
  212. ClassDB::bind_method(D_METHOD("get_resource_previewer"), &EditorInterface::get_resource_previewer);
  213. ClassDB::bind_method(D_METHOD("get_resource_filesystem"), &EditorInterface::get_resource_file_system);
  214. ClassDB::bind_method(D_METHOD("get_editor_viewport"), &EditorInterface::get_editor_viewport);
  215. ClassDB::bind_method(D_METHOD("make_mesh_previews", "meshes", "preview_size"), &EditorInterface::_make_mesh_previews);
  216. ClassDB::bind_method(D_METHOD("select_file", "p_file"), &EditorInterface::select_file);
  217. ClassDB::bind_method(D_METHOD("get_selected_path"), &EditorInterface::get_selected_path);
  218. ClassDB::bind_method(D_METHOD("save_scene"), &EditorInterface::save_scene);
  219. ClassDB::bind_method(D_METHOD("save_scene_as", "path", "with_preview"), &EditorInterface::save_scene_as, DEFVAL(true));
  220. }
  221. EditorInterface::EditorInterface() {
  222. singleton = this;
  223. }
  224. ///////////////////////////////////////////
  225. void EditorPlugin::add_custom_type(const String &p_type, const String &p_base, const Ref<Script> &p_script, const Ref<Texture> &p_icon) {
  226. EditorNode::get_editor_data().add_custom_type(p_type, p_base, p_script, p_icon);
  227. }
  228. void EditorPlugin::remove_custom_type(const String &p_type) {
  229. EditorNode::get_editor_data().remove_custom_type(p_type);
  230. }
  231. ToolButton *EditorPlugin::add_control_to_bottom_panel(Control *p_control, const String &p_title) {
  232. return EditorNode::get_singleton()->add_bottom_panel_item(p_title, p_control);
  233. }
  234. void EditorPlugin::add_control_to_dock(DockSlot p_slot, Control *p_control) {
  235. ERR_FAIL_NULL(p_control);
  236. EditorNode::get_singleton()->add_control_to_dock(EditorNode::DockSlot(p_slot), p_control);
  237. }
  238. void EditorPlugin::remove_control_from_docks(Control *p_control) {
  239. ERR_FAIL_NULL(p_control);
  240. EditorNode::get_singleton()->remove_control_from_dock(p_control);
  241. }
  242. void EditorPlugin::remove_control_from_bottom_panel(Control *p_control) {
  243. ERR_FAIL_NULL(p_control);
  244. EditorNode::get_singleton()->remove_bottom_panel_item(p_control);
  245. }
  246. void EditorPlugin::add_control_to_container(CustomControlContainer p_location, Control *p_control) {
  247. switch (p_location) {
  248. case CONTAINER_TOOLBAR: {
  249. EditorNode::get_menu_hb()->add_child(p_control);
  250. } break;
  251. case CONTAINER_SPATIAL_EDITOR_MENU: {
  252. SpatialEditor::get_singleton()->add_control_to_menu_panel(p_control);
  253. } break;
  254. case CONTAINER_SPATIAL_EDITOR_SIDE: {
  255. SpatialEditor::get_singleton()->get_palette_split()->add_child(p_control);
  256. SpatialEditor::get_singleton()->get_palette_split()->move_child(p_control, 0);
  257. } break;
  258. case CONTAINER_SPATIAL_EDITOR_BOTTOM: {
  259. SpatialEditor::get_singleton()->get_shader_split()->add_child(p_control);
  260. } break;
  261. case CONTAINER_CANVAS_EDITOR_MENU: {
  262. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(p_control);
  263. } break;
  264. case CONTAINER_CANVAS_EDITOR_SIDE: {
  265. CanvasItemEditor::get_singleton()->get_palette_split()->add_child(p_control);
  266. CanvasItemEditor::get_singleton()->get_palette_split()->move_child(p_control, 0);
  267. } break;
  268. case CONTAINER_CANVAS_EDITOR_BOTTOM: {
  269. CanvasItemEditor::get_singleton()->get_bottom_split()->add_child(p_control);
  270. } break;
  271. case CONTAINER_PROPERTY_EDITOR_BOTTOM: {
  272. EditorNode::get_singleton()->get_property_editor_vb()->add_child(p_control);
  273. } break;
  274. }
  275. }
  276. void EditorPlugin::add_tool_menu_item(const String &p_name, Object *p_handler, const String &p_callback, const Variant &p_ud) {
  277. //EditorNode::get_singleton()->add_tool_menu_item(p_name, p_handler, p_callback, p_ud);
  278. }
  279. void EditorPlugin::add_tool_submenu_item(const String &p_name, Object *p_submenu) {
  280. ERR_FAIL_NULL(p_submenu);
  281. PopupMenu *submenu = Object::cast_to<PopupMenu>(p_submenu);
  282. ERR_FAIL_NULL(submenu);
  283. //EditorNode::get_singleton()->add_tool_submenu_item(p_name, submenu);
  284. }
  285. void EditorPlugin::remove_tool_menu_item(const String &p_name) {
  286. //EditorNode::get_singleton()->remove_tool_menu_item(p_name);
  287. }
  288. void EditorPlugin::set_input_event_forwarding_always_enabled() {
  289. input_event_forwarding_always_enabled = true;
  290. EditorPluginList *always_input_forwarding_list = EditorNode::get_singleton()->get_editor_plugins_force_input_forwarding();
  291. always_input_forwarding_list->add_plugin(this);
  292. }
  293. void EditorPlugin::set_force_draw_over_forwarding_enabled() {
  294. force_draw_over_forwarding_enabled = true;
  295. EditorPluginList *always_draw_over_forwarding_list = EditorNode::get_singleton()->get_editor_plugins_force_over();
  296. always_draw_over_forwarding_list->add_plugin(this);
  297. }
  298. void EditorPlugin::notify_scene_changed(const Node *scn_root) {
  299. if (scn_root == NULL) return;
  300. emit_signal("scene_changed", scn_root);
  301. }
  302. void EditorPlugin::notify_main_screen_changed(const String &screen_name) {
  303. if (screen_name == last_main_screen_name)
  304. return;
  305. emit_signal("main_screen_changed", screen_name);
  306. last_main_screen_name = screen_name;
  307. }
  308. void EditorPlugin::notify_scene_closed(const String &scene_filepath) {
  309. emit_signal("scene_closed", scene_filepath);
  310. }
  311. Ref<SpatialEditorGizmo> EditorPlugin::create_spatial_gizmo(Spatial *p_spatial) {
  312. //??
  313. if (get_script_instance() && get_script_instance()->has_method("create_spatial_gizmo")) {
  314. return get_script_instance()->call("create_spatial_gizmo", p_spatial);
  315. }
  316. return Ref<SpatialEditorGizmo>();
  317. }
  318. bool EditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  319. if (get_script_instance() && get_script_instance()->has_method("forward_canvas_gui_input")) {
  320. return get_script_instance()->call("forward_canvas_gui_input", p_event);
  321. }
  322. return false;
  323. }
  324. void EditorPlugin::forward_draw_over_viewport(Control *p_overlay) {
  325. if (get_script_instance() && get_script_instance()->has_method("forward_draw_over_viewport")) {
  326. get_script_instance()->call("forward_draw_over_viewport", p_overlay);
  327. }
  328. }
  329. void EditorPlugin::forward_force_draw_over_viewport(Control *p_overlay) {
  330. if (get_script_instance() && get_script_instance()->has_method("forward_force_draw_over_viewport")) {
  331. get_script_instance()->call("forward_force_draw_over_viewport", p_overlay);
  332. }
  333. }
  334. // Updates the overlays of the 2D viewport or, if in 3D mode, of every 3D viewport.
  335. int EditorPlugin::update_overlays() const {
  336. if (SpatialEditor::get_singleton()->is_visible()) {
  337. int count = 0;
  338. for (int i = 0; i < SpatialEditor::VIEWPORTS_COUNT; i++) {
  339. SpatialEditorViewport *vp = SpatialEditor::get_singleton()->get_editor_viewport(i);
  340. if (vp->is_visible()) {
  341. vp->update_surface();
  342. count++;
  343. }
  344. }
  345. return count;
  346. } else {
  347. // This will update the normal viewport itself as well
  348. CanvasItemEditor::get_singleton()->get_viewport_control()->update();
  349. return 1;
  350. }
  351. }
  352. bool EditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) {
  353. if (get_script_instance() && get_script_instance()->has_method("forward_spatial_gui_input")) {
  354. return get_script_instance()->call("forward_spatial_gui_input", p_camera, p_event);
  355. }
  356. return false;
  357. }
  358. String EditorPlugin::get_name() const {
  359. if (get_script_instance() && get_script_instance()->has_method("get_plugin_name")) {
  360. return get_script_instance()->call("get_plugin_name");
  361. }
  362. return String();
  363. }
  364. const Ref<Texture> EditorPlugin::get_icon() const {
  365. if (get_script_instance() && get_script_instance()->has_method("get_plugin_icon")) {
  366. return get_script_instance()->call("get_plugin_icon");
  367. }
  368. return Ref<Texture>();
  369. }
  370. bool EditorPlugin::has_main_screen() const {
  371. if (get_script_instance() && get_script_instance()->has_method("has_main_screen")) {
  372. return get_script_instance()->call("has_main_screen");
  373. }
  374. return false;
  375. }
  376. void EditorPlugin::make_visible(bool p_visible) {
  377. if (get_script_instance() && get_script_instance()->has_method("make_visible")) {
  378. get_script_instance()->call("make_visible", p_visible);
  379. }
  380. }
  381. void EditorPlugin::edit(Object *p_object) {
  382. if (get_script_instance() && get_script_instance()->has_method("edit")) {
  383. if (p_object->is_class("Resource")) {
  384. get_script_instance()->call("edit", Ref<Resource>(Object::cast_to<Resource>(p_object)));
  385. } else {
  386. get_script_instance()->call("edit", p_object);
  387. }
  388. }
  389. }
  390. bool EditorPlugin::handles(Object *p_object) const {
  391. if (get_script_instance() && get_script_instance()->has_method("handles")) {
  392. return get_script_instance()->call("handles", p_object);
  393. }
  394. return false;
  395. }
  396. Dictionary EditorPlugin::get_state() const {
  397. if (get_script_instance() && get_script_instance()->has_method("get_state")) {
  398. return get_script_instance()->call("get_state");
  399. }
  400. return Dictionary();
  401. }
  402. void EditorPlugin::set_state(const Dictionary &p_state) {
  403. if (get_script_instance() && get_script_instance()->has_method("set_state")) {
  404. get_script_instance()->call("set_state", p_state);
  405. }
  406. }
  407. void EditorPlugin::clear() {
  408. if (get_script_instance() && get_script_instance()->has_method("clear")) {
  409. get_script_instance()->call("clear");
  410. }
  411. }
  412. // if editor references external resources/scenes, save them
  413. void EditorPlugin::save_external_data() {
  414. if (get_script_instance() && get_script_instance()->has_method("save_external_data")) {
  415. get_script_instance()->call("save_external_data");
  416. }
  417. }
  418. // if changes are pending in editor, apply them
  419. void EditorPlugin::apply_changes() {
  420. if (get_script_instance() && get_script_instance()->has_method("apply_changes")) {
  421. get_script_instance()->call("apply_changes");
  422. }
  423. }
  424. void EditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  425. if (get_script_instance() && get_script_instance()->has_method("get_breakpoints")) {
  426. PoolStringArray arr = get_script_instance()->call("get_breakpoints");
  427. for (int i = 0; i < arr.size(); i++)
  428. p_breakpoints->push_back(arr[i]);
  429. }
  430. }
  431. bool EditorPlugin::get_remove_list(List<Node *> *p_list) {
  432. return false;
  433. }
  434. void EditorPlugin::restore_global_state() {}
  435. void EditorPlugin::save_global_state() {}
  436. void EditorPlugin::add_import_plugin(const Ref<EditorImportPlugin> &p_importer) {
  437. ResourceFormatImporter::get_singleton()->add_importer(p_importer);
  438. EditorFileSystem::get_singleton()->call_deferred("scan");
  439. }
  440. void EditorPlugin::remove_import_plugin(const Ref<EditorImportPlugin> &p_importer) {
  441. ResourceFormatImporter::get_singleton()->remove_importer(p_importer);
  442. EditorFileSystem::get_singleton()->call_deferred("scan");
  443. }
  444. void EditorPlugin::add_export_plugin(const Ref<EditorExportPlugin> &p_exporter) {
  445. EditorExport::get_singleton()->add_export_plugin(p_exporter);
  446. }
  447. void EditorPlugin::remove_export_plugin(const Ref<EditorExportPlugin> &p_exporter) {
  448. EditorExport::get_singleton()->remove_export_plugin(p_exporter);
  449. }
  450. void EditorPlugin::add_scene_import_plugin(const Ref<EditorSceneImporter> &p_importer) {
  451. ResourceImporterScene::get_singleton()->add_importer(p_importer);
  452. }
  453. void EditorPlugin::remove_scene_import_plugin(const Ref<EditorSceneImporter> &p_importer) {
  454. ResourceImporterScene::get_singleton()->remove_importer(p_importer);
  455. }
  456. void EditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  457. if (get_script_instance() && get_script_instance()->has_method("set_window_layout")) {
  458. get_script_instance()->call("set_window_layout", p_layout);
  459. }
  460. }
  461. void EditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  462. if (get_script_instance() && get_script_instance()->has_method("get_window_layout")) {
  463. get_script_instance()->call("get_window_layout", p_layout);
  464. }
  465. }
  466. void EditorPlugin::queue_save_layout() const {
  467. EditorNode::get_singleton()->save_layout();
  468. }
  469. void EditorPlugin::make_bottom_panel_item_visible(Control *p_item) {
  470. EditorNode::get_singleton()->make_bottom_panel_item_visible(p_item);
  471. }
  472. void EditorPlugin::hide_bottom_panel() {
  473. EditorNode::get_singleton()->hide_bottom_panel();
  474. }
  475. EditorInterface *EditorPlugin::get_editor_interface() {
  476. return EditorInterface::get_singleton();
  477. }
  478. void EditorPlugin::_bind_methods() {
  479. ClassDB::bind_method(D_METHOD("add_control_to_container", "container", "control"), &EditorPlugin::add_control_to_container);
  480. ClassDB::bind_method(D_METHOD("add_control_to_bottom_panel", "control", "title"), &EditorPlugin::add_control_to_bottom_panel);
  481. ClassDB::bind_method(D_METHOD("add_control_to_dock", "slot", "control"), &EditorPlugin::add_control_to_dock);
  482. ClassDB::bind_method(D_METHOD("remove_control_from_docks", "control"), &EditorPlugin::remove_control_from_docks);
  483. ClassDB::bind_method(D_METHOD("remove_control_from_bottom_panel", "control"), &EditorPlugin::remove_control_from_bottom_panel);
  484. //ClassDB::bind_method(D_METHOD("add_tool_menu_item", "name", "handler", "callback", "ud"),&EditorPlugin::add_tool_menu_item,DEFVAL(Variant()));
  485. ClassDB::bind_method(D_METHOD("add_tool_submenu_item", "name", "submenu"), &EditorPlugin::add_tool_submenu_item);
  486. //ClassDB::bind_method(D_METHOD("remove_tool_menu_item", "name"),&EditorPlugin::remove_tool_menu_item);
  487. ClassDB::bind_method(D_METHOD("add_custom_type", "type", "base", "script", "icon"), &EditorPlugin::add_custom_type);
  488. ClassDB::bind_method(D_METHOD("remove_custom_type", "type"), &EditorPlugin::remove_custom_type);
  489. ClassDB::bind_method(D_METHOD("update_overlays"), &EditorPlugin::update_overlays);
  490. ClassDB::bind_method(D_METHOD("make_bottom_panel_item_visible", "item"), &EditorPlugin::make_bottom_panel_item_visible);
  491. ClassDB::bind_method(D_METHOD("hide_bottom_panel"), &EditorPlugin::hide_bottom_panel);
  492. ClassDB::bind_method(D_METHOD("get_undo_redo"), &EditorPlugin::_get_undo_redo);
  493. ClassDB::bind_method(D_METHOD("queue_save_layout"), &EditorPlugin::queue_save_layout);
  494. ClassDB::bind_method(D_METHOD("add_import_plugin", "importer"), &EditorPlugin::add_import_plugin);
  495. ClassDB::bind_method(D_METHOD("remove_import_plugin", "importer"), &EditorPlugin::remove_import_plugin);
  496. ClassDB::bind_method(D_METHOD("add_scene_import_plugin", "scene_importer"), &EditorPlugin::add_scene_import_plugin);
  497. ClassDB::bind_method(D_METHOD("remove_scene_import_plugin", "scene_importer"), &EditorPlugin::remove_scene_import_plugin);
  498. ClassDB::bind_method(D_METHOD("add_export_plugin", "exporter"), &EditorPlugin::add_export_plugin);
  499. ClassDB::bind_method(D_METHOD("remove_export_plugin", "exporter"), &EditorPlugin::remove_export_plugin);
  500. ClassDB::bind_method(D_METHOD("set_input_event_forwarding_always_enabled"), &EditorPlugin::set_input_event_forwarding_always_enabled);
  501. ClassDB::bind_method(D_METHOD("set_force_draw_over_forwarding_enabled"), &EditorPlugin::set_force_draw_over_forwarding_enabled);
  502. ClassDB::bind_method(D_METHOD("get_editor_interface"), &EditorPlugin::get_editor_interface);
  503. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_canvas_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  504. ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
  505. ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_force_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
  506. 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")));
  507. MethodInfo gizmo = MethodInfo(Variant::OBJECT, "create_spatial_gizmo", PropertyInfo(Variant::OBJECT, "for_spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial"));
  508. gizmo.return_val.hint = PROPERTY_HINT_RESOURCE_TYPE;
  509. gizmo.return_val.hint_string = "EditorSpatialGizmo";
  510. ClassDB::add_virtual_method(get_class_static(), gizmo);
  511. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_plugin_name"));
  512. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::OBJECT, "get_plugin_icon"));
  513. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "has_main_screen"));
  514. ClassDB::add_virtual_method(get_class_static(), MethodInfo("make_visible", PropertyInfo(Variant::BOOL, "visible")));
  515. ClassDB::add_virtual_method(get_class_static(), MethodInfo("edit", PropertyInfo(Variant::OBJECT, "object")));
  516. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles", PropertyInfo(Variant::OBJECT, "object")));
  517. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::DICTIONARY, "get_state"));
  518. ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_state", PropertyInfo(Variant::DICTIONARY, "state")));
  519. ClassDB::add_virtual_method(get_class_static(), MethodInfo("clear"));
  520. ClassDB::add_virtual_method(get_class_static(), MethodInfo("save_external_data"));
  521. ClassDB::add_virtual_method(get_class_static(), MethodInfo("apply_changes"));
  522. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::POOL_STRING_ARRAY, "get_breakpoints"));
  523. ClassDB::add_virtual_method(get_class_static(), MethodInfo("set_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile")));
  524. ClassDB::add_virtual_method(get_class_static(), MethodInfo("get_window_layout", PropertyInfo(Variant::OBJECT, "layout", PROPERTY_HINT_RESOURCE_TYPE, "ConfigFile")));
  525. ADD_SIGNAL(MethodInfo("scene_changed", PropertyInfo(Variant::OBJECT, "scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  526. ADD_SIGNAL(MethodInfo("scene_closed", PropertyInfo(Variant::STRING, "filepath")));
  527. ADD_SIGNAL(MethodInfo("main_screen_changed", PropertyInfo(Variant::STRING, "screen_name")));
  528. BIND_ENUM_CONSTANT(CONTAINER_TOOLBAR);
  529. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_MENU);
  530. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_SIDE);
  531. BIND_ENUM_CONSTANT(CONTAINER_SPATIAL_EDITOR_BOTTOM);
  532. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_MENU);
  533. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_SIDE);
  534. BIND_ENUM_CONSTANT(CONTAINER_CANVAS_EDITOR_BOTTOM);
  535. BIND_ENUM_CONSTANT(CONTAINER_PROPERTY_EDITOR_BOTTOM);
  536. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_UL);
  537. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_BL);
  538. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_UR);
  539. BIND_ENUM_CONSTANT(DOCK_SLOT_LEFT_BR);
  540. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_UL);
  541. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_BL);
  542. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_UR);
  543. BIND_ENUM_CONSTANT(DOCK_SLOT_RIGHT_BR);
  544. BIND_ENUM_CONSTANT(DOCK_SLOT_MAX);
  545. }
  546. EditorPlugin::EditorPlugin() {
  547. undo_redo = NULL;
  548. input_event_forwarding_always_enabled = false;
  549. force_draw_over_forwarding_enabled = false;
  550. last_main_screen_name = "";
  551. }
  552. EditorPlugin::~EditorPlugin() {
  553. }
  554. EditorPluginCreateFunc EditorPlugins::creation_funcs[MAX_CREATE_FUNCS];
  555. int EditorPlugins::creation_func_count = 0;