mesh_library_editor_plugin.cpp 10.0 KB

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