mesh_library_editor_plugin.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*************************************************************************/
  2. /* mesh_library_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 "mesh_library_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "main/main.h"
  34. #include "scene/3d/mesh_instance.h"
  35. #include "scene/3d/navigation_mesh.h"
  36. #include "scene/3d/physics_body.h"
  37. #include "scene/main/viewport.h"
  38. #include "scene/resources/packed_scene.h"
  39. #include "spatial_editor_plugin.h"
  40. void MeshLibraryEditor::edit(const Ref<MeshLibrary> &p_mesh_library) {
  41. mesh_library = p_mesh_library;
  42. if (mesh_library.is_valid())
  43. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), !mesh_library->has_meta("_editor_source_scene"));
  44. }
  45. void MeshLibraryEditor::_menu_confirm() {
  46. switch (option) {
  47. case MENU_OPTION_REMOVE_ITEM: {
  48. mesh_library->remove_item(to_erase);
  49. } break;
  50. case MENU_OPTION_UPDATE_FROM_SCENE: {
  51. String existing = mesh_library->get_meta("_editor_source_scene");
  52. ERR_FAIL_COND(existing == "");
  53. _import_scene_cbk(existing);
  54. } break;
  55. default: {
  56. };
  57. }
  58. }
  59. void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library, bool p_merge) {
  60. if (!p_merge)
  61. p_library->clear();
  62. for (int i = 0; i < p_scene->get_child_count(); i++) {
  63. Node *child = p_scene->get_child(i);
  64. if (!Object::cast_to<MeshInstance>(child)) {
  65. if (child->get_child_count() > 0) {
  66. child = child->get_child(0);
  67. if (!Object::cast_to<MeshInstance>(child)) {
  68. continue;
  69. }
  70. } else
  71. continue;
  72. }
  73. MeshInstance *mi = Object::cast_to<MeshInstance>(child);
  74. Ref<Mesh> mesh = mi->get_mesh();
  75. if (mesh.is_null())
  76. continue;
  77. int id = p_library->find_item_by_name(mi->get_name());
  78. if (id < 0) {
  79. id = p_library->get_last_unused_item_id();
  80. p_library->create_item(id);
  81. p_library->set_item_name(id, mi->get_name());
  82. }
  83. p_library->set_item_mesh(id, mesh);
  84. Vector<MeshLibrary::ShapeData> collisions;
  85. for (int j = 0; j < mi->get_child_count(); j++) {
  86. Node *child2 = mi->get_child(j);
  87. if (!Object::cast_to<StaticBody>(child2))
  88. continue;
  89. StaticBody *sb = Object::cast_to<StaticBody>(child2);
  90. List<uint32_t> shapes;
  91. sb->get_shape_owners(&shapes);
  92. for (List<uint32_t>::Element *E = shapes.front(); E; E = E->next()) {
  93. if (sb->is_shape_owner_disabled(E->get()))
  94. continue;
  95. //Transform shape_transform = sb->shape_owner_get_transform(E->get());
  96. //shape_transform.set_origin(shape_transform.get_origin() - phys_offset);
  97. for (int k = 0; k < sb->shape_owner_get_shape_count(E->get()); k++) {
  98. Ref<Shape> collision = sb->shape_owner_get_shape(E->get(), k);
  99. if (!collision.is_valid())
  100. continue;
  101. MeshLibrary::ShapeData shape_data;
  102. shape_data.shape = collision;
  103. shape_data.local_transform = sb->get_transform() * sb->shape_owner_get_transform(E->get());
  104. collisions.push_back(shape_data);
  105. }
  106. }
  107. }
  108. p_library->set_item_shapes(id, collisions);
  109. Ref<NavigationMesh> navmesh;
  110. Transform navmesh_transform;
  111. for (int j = 0; j < mi->get_child_count(); j++) {
  112. Node *child2 = mi->get_child(j);
  113. if (!Object::cast_to<NavigationMeshInstance>(child2))
  114. continue;
  115. NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
  116. navmesh = sb->get_navigation_mesh();
  117. navmesh_transform = sb->get_transform();
  118. if (!navmesh.is_null())
  119. break;
  120. }
  121. if (!navmesh.is_null()) {
  122. p_library->set_item_navmesh(id, navmesh);
  123. p_library->set_item_navmesh_transform(id, navmesh_transform);
  124. }
  125. }
  126. //generate previews!
  127. if (1) {
  128. Vector<Ref<Mesh> > meshes;
  129. Vector<int> ids = p_library->get_item_list();
  130. for (int i = 0; i < ids.size(); i++) {
  131. meshes.push_back(p_library->get_item_mesh(ids[i]));
  132. }
  133. Vector<Ref<Texture> > textures = EditorInterface::get_singleton()->make_mesh_previews(meshes, EditorSettings::get_singleton()->get("editors/grid_map/preview_size"));
  134. for (int i = 0; i < ids.size(); i++) {
  135. p_library->set_item_preview(ids[i], textures[i]);
  136. }
  137. }
  138. }
  139. void MeshLibraryEditor::_import_scene_cbk(const String &p_str) {
  140. Ref<PackedScene> ps = ResourceLoader::load(p_str, "PackedScene");
  141. ERR_FAIL_COND(ps.is_null());
  142. Node *scene = ps->instance();
  143. _import_scene(scene, mesh_library, option == MENU_OPTION_UPDATE_FROM_SCENE);
  144. memdelete(scene);
  145. mesh_library->set_meta("_editor_source_scene", p_str);
  146. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), false);
  147. }
  148. Error MeshLibraryEditor::update_library_file(Node *p_base_scene, Ref<MeshLibrary> ml, bool p_merge) {
  149. _import_scene(p_base_scene, ml, p_merge);
  150. return OK;
  151. }
  152. void MeshLibraryEditor::_menu_cbk(int p_option) {
  153. option = p_option;
  154. switch (p_option) {
  155. case MENU_OPTION_ADD_ITEM: {
  156. mesh_library->create_item(mesh_library->get_last_unused_item_id());
  157. } break;
  158. case MENU_OPTION_REMOVE_ITEM: {
  159. String p = editor->get_inspector()->get_selected_path();
  160. if (p.begins_with("/MeshLibrary/item") && p.get_slice_count("/") >= 3) {
  161. to_erase = p.get_slice("/", 3).to_int();
  162. cd->set_text(vformat(TTR("Remove item %d?"), to_erase));
  163. cd->popup_centered(Size2(300, 60));
  164. }
  165. } break;
  166. case MENU_OPTION_IMPORT_FROM_SCENE: {
  167. file->popup_centered_ratio();
  168. } break;
  169. case MENU_OPTION_UPDATE_FROM_SCENE: {
  170. cd->set_text("Update from existing scene?:\n" + String(mesh_library->get_meta("_editor_source_scene")));
  171. cd->popup_centered(Size2(500, 60));
  172. } break;
  173. }
  174. }
  175. void MeshLibraryEditor::_bind_methods() {
  176. ClassDB::bind_method("_menu_cbk", &MeshLibraryEditor::_menu_cbk);
  177. ClassDB::bind_method("_menu_confirm", &MeshLibraryEditor::_menu_confirm);
  178. ClassDB::bind_method("_import_scene_cbk", &MeshLibraryEditor::_import_scene_cbk);
  179. }
  180. MeshLibraryEditor::MeshLibraryEditor(EditorNode *p_editor) {
  181. file = memnew(EditorFileDialog);
  182. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  183. //not for now?
  184. List<String> extensions;
  185. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  186. file->clear_filters();
  187. file->set_title(TTR("Import Scene"));
  188. for (int i = 0; i < extensions.size(); i++) {
  189. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  190. }
  191. add_child(file);
  192. file->connect("file_selected", this, "_import_scene_cbk");
  193. menu = memnew(MenuButton);
  194. SpatialEditor::get_singleton()->add_control_to_menu_panel(menu);
  195. menu->set_position(Point2(1, 1));
  196. menu->set_text("Mesh Library");
  197. menu->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MeshLibrary", "EditorIcons"));
  198. menu->get_popup()->add_item(TTR("Add Item"), MENU_OPTION_ADD_ITEM);
  199. menu->get_popup()->add_item(TTR("Remove Selected Item"), MENU_OPTION_REMOVE_ITEM);
  200. menu->get_popup()->add_separator();
  201. menu->get_popup()->add_item(TTR("Import from Scene"), MENU_OPTION_IMPORT_FROM_SCENE);
  202. menu->get_popup()->add_item(TTR("Update from Scene"), MENU_OPTION_UPDATE_FROM_SCENE);
  203. menu->get_popup()->set_item_disabled(menu->get_popup()->get_item_index(MENU_OPTION_UPDATE_FROM_SCENE), true);
  204. menu->get_popup()->connect("id_pressed", this, "_menu_cbk");
  205. menu->hide();
  206. editor = p_editor;
  207. cd = memnew(ConfirmationDialog);
  208. add_child(cd);
  209. cd->get_ok()->connect("pressed", this, "_menu_confirm");
  210. }
  211. void MeshLibraryEditorPlugin::edit(Object *p_node) {
  212. if (Object::cast_to<MeshLibrary>(p_node)) {
  213. mesh_library_editor->edit(Object::cast_to<MeshLibrary>(p_node));
  214. mesh_library_editor->show();
  215. } else
  216. mesh_library_editor->hide();
  217. }
  218. bool MeshLibraryEditorPlugin::handles(Object *p_node) const {
  219. return p_node->is_class("MeshLibrary");
  220. }
  221. void MeshLibraryEditorPlugin::make_visible(bool p_visible) {
  222. if (p_visible) {
  223. mesh_library_editor->show();
  224. mesh_library_editor->get_menu_button()->show();
  225. } else {
  226. mesh_library_editor->hide();
  227. mesh_library_editor->get_menu_button()->hide();
  228. }
  229. }
  230. MeshLibraryEditorPlugin::MeshLibraryEditorPlugin(EditorNode *p_node) {
  231. EDITOR_DEF("editors/grid_map/preview_size", 64);
  232. mesh_library_editor = memnew(MeshLibraryEditor(p_node));
  233. p_node->get_viewport()->add_child(mesh_library_editor);
  234. mesh_library_editor->set_anchors_and_margins_preset(Control::PRESET_TOP_WIDE);
  235. mesh_library_editor->set_end(Point2(0, 22));
  236. mesh_library_editor->hide();
  237. }