mesh_instance_editor_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*************************************************************************/
  2. /* mesh_instance_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "mesh_instance_editor_plugin.h"
  31. #include "scene/3d/body_shape.h"
  32. #include "scene/3d/navigation_mesh.h"
  33. #include "scene/3d/physics_body.h"
  34. #include "scene/gui/box_container.h"
  35. #include "spatial_editor_plugin.h"
  36. void MeshInstanceEditor::_node_removed(Node *p_node) {
  37. if (p_node == node) {
  38. node = NULL;
  39. options->hide();
  40. }
  41. }
  42. void MeshInstanceEditor::edit(MeshInstance *p_mesh) {
  43. node = p_mesh;
  44. }
  45. void MeshInstanceEditor::_menu_option(int p_option) {
  46. Ref<Mesh> mesh = node->get_mesh();
  47. if (mesh.is_null()) {
  48. err_dialog->set_text(TTR("Mesh is empty!"));
  49. err_dialog->popup_centered_minsize();
  50. return;
  51. }
  52. switch (p_option) {
  53. case MENU_OPTION_CREATE_STATIC_TRIMESH_BODY:
  54. case MENU_OPTION_CREATE_STATIC_CONVEX_BODY: {
  55. bool trimesh_shape = (p_option == MENU_OPTION_CREATE_STATIC_TRIMESH_BODY);
  56. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  57. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  58. List<Node *> selection = editor_selection->get_selected_node_list();
  59. if (selection.empty()) {
  60. Ref<Shape> shape = trimesh_shape ? mesh->create_trimesh_shape() : mesh->create_convex_shape();
  61. if (shape.is_null())
  62. return;
  63. CollisionShape *cshape = memnew(CollisionShape);
  64. cshape->set_shape(shape);
  65. StaticBody *body = memnew(StaticBody);
  66. body->add_child(cshape);
  67. Node *owner = node == get_tree()->get_edited_scene_root() ? node : node->get_owner();
  68. if (trimesh_shape)
  69. ur->create_action(TTR("Create Static Trimesh Body"));
  70. else
  71. ur->create_action(TTR("Create Static Convex Body"));
  72. ur->add_do_method(node, "add_child", body);
  73. ur->add_do_method(body, "set_owner", owner);
  74. ur->add_do_method(cshape, "set_owner", owner);
  75. ur->add_do_reference(body);
  76. ur->add_undo_method(node, "remove_child", body);
  77. ur->commit_action();
  78. return;
  79. }
  80. if (trimesh_shape)
  81. ur->create_action(TTR("Create Static Trimesh Body"));
  82. else
  83. ur->create_action(TTR("Create Static Convex Body"));
  84. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  85. MeshInstance *instance = E->get()->cast_to<MeshInstance>();
  86. if (!instance)
  87. continue;
  88. Ref<Mesh> m = instance->get_mesh();
  89. if (m.is_null())
  90. continue;
  91. Ref<Shape> shape = trimesh_shape ? m->create_trimesh_shape() : m->create_convex_shape();
  92. if (shape.is_null())
  93. continue;
  94. CollisionShape *cshape = memnew(CollisionShape);
  95. cshape->set_shape(shape);
  96. StaticBody *body = memnew(StaticBody);
  97. body->add_child(cshape);
  98. Node *owner = instance == get_tree()->get_edited_scene_root() ? instance : instance->get_owner();
  99. ur->add_do_method(instance, "add_child", body);
  100. ur->add_do_method(body, "set_owner", owner);
  101. ur->add_do_method(cshape, "set_owner", owner);
  102. ur->add_do_reference(body);
  103. ur->add_undo_method(instance, "remove_child", body);
  104. }
  105. ur->commit_action();
  106. } break;
  107. case MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE:
  108. case MENU_OPTION_CREATE_CONVEX_COLLISION_SHAPE: {
  109. if (node == get_tree()->get_edited_scene_root()) {
  110. err_dialog->set_text(TTR("This doesn't work on scene root!"));
  111. err_dialog->popup_centered_minsize();
  112. return;
  113. }
  114. bool trimesh_shape = (p_option == MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE);
  115. Ref<Shape> shape = trimesh_shape ? mesh->create_trimesh_shape() : mesh->create_convex_shape();
  116. if (shape.is_null())
  117. return;
  118. CollisionShape *cshape = memnew(CollisionShape);
  119. cshape->set_shape(shape);
  120. Node *owner = node->get_owner();
  121. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  122. if (trimesh_shape)
  123. ur->create_action(TTR("Create Trimesh Shape"));
  124. else
  125. ur->create_action(TTR("Create Convex Shape"));
  126. ur->add_do_method(node->get_parent(), "add_child", cshape);
  127. ur->add_do_method(node->get_parent(), "move_child", cshape, node->get_index() + 1);
  128. ur->add_do_method(cshape, "set_owner", owner);
  129. ur->add_do_reference(cshape);
  130. ur->add_undo_method(node->get_parent(), "remove_child", cshape);
  131. ur->commit_action();
  132. } break;
  133. case MENU_OPTION_CREATE_NAVMESH: {
  134. Ref<NavigationMesh> nmesh = memnew(NavigationMesh);
  135. if (nmesh.is_null())
  136. return;
  137. nmesh->create_from_mesh(mesh);
  138. NavigationMeshInstance *nmi = memnew(NavigationMeshInstance);
  139. nmi->set_navigation_mesh(nmesh);
  140. Node *owner = node == get_tree()->get_edited_scene_root() ? node : node->get_owner();
  141. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  142. ur->create_action(TTR("Create Navigation Mesh"));
  143. ur->add_do_method(node, "add_child", nmi);
  144. ur->add_do_method(nmi, "set_owner", owner);
  145. ur->add_do_reference(nmi);
  146. ur->add_undo_method(node, "remove_child", nmi);
  147. ur->commit_action();
  148. } break;
  149. case MENU_OPTION_CREATE_OUTLINE_MESH: {
  150. outline_dialog->popup_centered(Vector2(200, 90));
  151. } break;
  152. }
  153. }
  154. void MeshInstanceEditor::_create_outline_mesh() {
  155. Ref<Mesh> mesh = node->get_mesh();
  156. if (mesh.is_null()) {
  157. err_dialog->set_text(TTR("MeshInstance lacks a Mesh!"));
  158. err_dialog->popup_centered_minsize();
  159. return;
  160. }
  161. if (mesh->get_surface_count() == 0) {
  162. err_dialog->set_text(TTR("Mesh has not surface to create outlines from!"));
  163. err_dialog->popup_centered_minsize();
  164. return;
  165. }
  166. Ref<Mesh> mesho = mesh->create_outline(outline_size->get_val());
  167. if (mesho.is_null()) {
  168. err_dialog->set_text(TTR("Could not create outline!"));
  169. err_dialog->popup_centered_minsize();
  170. return;
  171. }
  172. MeshInstance *mi = memnew(MeshInstance);
  173. mi->set_mesh(mesho);
  174. Node *owner = node->get_owner();
  175. if (get_tree()->get_edited_scene_root() == node) {
  176. owner = node;
  177. }
  178. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  179. ur->create_action(TTR("Create Outline"));
  180. ur->add_do_method(node, "add_child", mi);
  181. ur->add_do_method(mi, "set_owner", owner);
  182. ur->add_do_reference(mi);
  183. ur->add_undo_method(node, "remove_child", mi);
  184. ur->commit_action();
  185. }
  186. void MeshInstanceEditor::_bind_methods() {
  187. ObjectTypeDB::bind_method("_menu_option", &MeshInstanceEditor::_menu_option);
  188. ObjectTypeDB::bind_method("_create_outline_mesh", &MeshInstanceEditor::_create_outline_mesh);
  189. }
  190. MeshInstanceEditor::MeshInstanceEditor() {
  191. options = memnew(MenuButton);
  192. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  193. options->set_text(TTR("Mesh"));
  194. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MeshInstance", "EditorIcons"));
  195. options->get_popup()->add_item(TTR("Create Trimesh Static Body"), MENU_OPTION_CREATE_STATIC_TRIMESH_BODY);
  196. options->get_popup()->add_item(TTR("Create Convex Static Body"), MENU_OPTION_CREATE_STATIC_CONVEX_BODY);
  197. options->get_popup()->add_separator();
  198. options->get_popup()->add_item(TTR("Create Trimesh Collision Sibling"), MENU_OPTION_CREATE_TRIMESH_COLLISION_SHAPE);
  199. options->get_popup()->add_item(TTR("Create Convex Collision Sibling"), MENU_OPTION_CREATE_CONVEX_COLLISION_SHAPE);
  200. options->get_popup()->add_separator();
  201. options->get_popup()->add_item(TTR("Create Navigation Mesh"), MENU_OPTION_CREATE_NAVMESH);
  202. options->get_popup()->add_separator();
  203. options->get_popup()->add_item(TTR("Create Outline Mesh.."), MENU_OPTION_CREATE_OUTLINE_MESH);
  204. options->get_popup()->connect("item_pressed", this, "_menu_option");
  205. outline_dialog = memnew(ConfirmationDialog);
  206. outline_dialog->set_title(TTR("Create Outline Mesh"));
  207. outline_dialog->get_ok()->set_text(TTR("Create"));
  208. VBoxContainer *outline_dialog_vbc = memnew(VBoxContainer);
  209. outline_dialog->add_child(outline_dialog_vbc);
  210. outline_dialog->set_child_rect(outline_dialog_vbc);
  211. outline_size = memnew(SpinBox);
  212. outline_size->set_min(0.001);
  213. outline_size->set_max(1024);
  214. outline_size->set_step(0.001);
  215. outline_size->set_val(0.05);
  216. outline_dialog_vbc->add_margin_child(TTR("Outline Size:"), outline_size);
  217. add_child(outline_dialog);
  218. outline_dialog->connect("confirmed", this, "_create_outline_mesh");
  219. err_dialog = memnew(AcceptDialog);
  220. add_child(err_dialog);
  221. }
  222. void MeshInstanceEditorPlugin::edit(Object *p_object) {
  223. mesh_editor->edit(p_object->cast_to<MeshInstance>());
  224. }
  225. bool MeshInstanceEditorPlugin::handles(Object *p_object) const {
  226. return p_object->is_type("MeshInstance");
  227. }
  228. void MeshInstanceEditorPlugin::make_visible(bool p_visible) {
  229. if (p_visible) {
  230. mesh_editor->options->show();
  231. } else {
  232. mesh_editor->options->hide();
  233. mesh_editor->edit(NULL);
  234. }
  235. }
  236. MeshInstanceEditorPlugin::MeshInstanceEditorPlugin(EditorNode *p_node) {
  237. editor = p_node;
  238. mesh_editor = memnew(MeshInstanceEditor);
  239. editor->get_viewport()->add_child(mesh_editor);
  240. mesh_editor->options->hide();
  241. }
  242. MeshInstanceEditorPlugin::~MeshInstanceEditorPlugin() {
  243. }