skeleton_editor_plugin.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*************************************************************************/
  2. /* skeleton_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 "skeleton_editor_plugin.h"
  31. #include "scene/3d/collision_shape.h"
  32. #include "scene/3d/physics_body.h"
  33. #include "scene/3d/physics_joint.h"
  34. #include "scene/resources/capsule_shape.h"
  35. #include "scene/resources/sphere_shape.h"
  36. #include "spatial_editor_plugin.h"
  37. void SkeletonEditor::_on_click_option(int p_option) {
  38. if (!skeleton) {
  39. return;
  40. }
  41. switch (p_option) {
  42. case MENU_OPTION_CREATE_PHYSICAL_SKELETON: {
  43. create_physical_skeleton();
  44. } break;
  45. }
  46. }
  47. void SkeletonEditor::create_physical_skeleton() {
  48. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  49. Node *owner = skeleton == get_tree()->get_edited_scene_root() ? skeleton : skeleton->get_owner();
  50. const int bc = skeleton->get_bone_count();
  51. if (!bc) {
  52. return;
  53. }
  54. Vector<BoneInfo> bones_infos;
  55. bones_infos.resize(bc);
  56. for (int bone_id = 0; bc > bone_id; ++bone_id) {
  57. const int parent = skeleton->get_bone_parent(bone_id);
  58. const int parent_parent = skeleton->get_bone_parent(parent);
  59. if (parent < 0) {
  60. bones_infos.write[bone_id].relative_rest = skeleton->get_bone_rest(bone_id);
  61. } else {
  62. bones_infos.write[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id);
  63. /// create physical bone on parent
  64. if (!bones_infos[parent].physical_bone) {
  65. bones_infos.write[parent].physical_bone = create_physical_bone(parent, bone_id, bones_infos);
  66. ur->create_action(TTR("Create physical bones"));
  67. ur->add_do_method(skeleton, "add_child", bones_infos[parent].physical_bone);
  68. ur->add_do_reference(bones_infos[parent].physical_bone);
  69. ur->add_undo_method(skeleton, "remove_child", bones_infos[parent].physical_bone);
  70. ur->commit_action();
  71. bones_infos[parent].physical_bone->set_bone_name(skeleton->get_bone_name(parent));
  72. bones_infos[parent].physical_bone->set_owner(owner);
  73. bones_infos[parent].physical_bone->get_child(0)->set_owner(owner); // set shape owner
  74. /// Create joint between parent of parent
  75. if (-1 != parent_parent) {
  76. bones_infos[parent].physical_bone->set_joint_type(PhysicalBone::JOINT_TYPE_PIN);
  77. }
  78. }
  79. }
  80. }
  81. }
  82. PhysicalBone *SkeletonEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos) {
  83. real_t half_height(skeleton->get_bone_rest(bone_child_id).origin.length() * 0.5);
  84. real_t radius(half_height * 0.2);
  85. CapsuleShape *bone_shape_capsule = memnew(CapsuleShape);
  86. bone_shape_capsule->set_height((half_height - radius) * 2);
  87. bone_shape_capsule->set_radius(radius);
  88. CollisionShape *bone_shape = memnew(CollisionShape);
  89. bone_shape->set_shape(bone_shape_capsule);
  90. Transform body_transform;
  91. body_transform.origin = Vector3(0, 0, -half_height);
  92. Transform joint_transform;
  93. joint_transform.origin = Vector3(0, 0, half_height);
  94. PhysicalBone *physical_bone = memnew(PhysicalBone);
  95. physical_bone->add_child(bone_shape);
  96. physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id));
  97. physical_bone->set_body_offset(body_transform);
  98. physical_bone->set_joint_offset(joint_transform);
  99. return physical_bone;
  100. }
  101. void SkeletonEditor::edit(Skeleton *p_node) {
  102. skeleton = p_node;
  103. }
  104. void SkeletonEditor::_notification(int p_what) {
  105. if (p_what == NOTIFICATION_ENTER_TREE) {
  106. get_tree()->connect("node_removed", this, "_node_removed");
  107. }
  108. }
  109. void SkeletonEditor::_node_removed(Node *p_node) {
  110. if (p_node == skeleton) {
  111. skeleton = NULL;
  112. options->hide();
  113. }
  114. }
  115. void SkeletonEditor::_bind_methods() {
  116. ClassDB::bind_method("_on_click_option", &SkeletonEditor::_on_click_option);
  117. ClassDB::bind_method("_node_removed", &SkeletonEditor::_node_removed);
  118. }
  119. SkeletonEditor::SkeletonEditor() {
  120. skeleton = NULL;
  121. options = memnew(MenuButton);
  122. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  123. options->set_text(TTR("Skeleton"));
  124. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Skeleton", "EditorIcons"));
  125. options->get_popup()->add_item(TTR("Create physical skeleton"), MENU_OPTION_CREATE_PHYSICAL_SKELETON);
  126. options->get_popup()->connect("id_pressed", this, "_on_click_option");
  127. options->hide();
  128. }
  129. SkeletonEditor::~SkeletonEditor() {}
  130. void SkeletonEditorPlugin::edit(Object *p_object) {
  131. skeleton_editor->edit(Object::cast_to<Skeleton>(p_object));
  132. }
  133. bool SkeletonEditorPlugin::handles(Object *p_object) const {
  134. return p_object->is_class("Skeleton");
  135. }
  136. void SkeletonEditorPlugin::make_visible(bool p_visible) {
  137. if (p_visible) {
  138. skeleton_editor->options->show();
  139. } else {
  140. skeleton_editor->options->hide();
  141. skeleton_editor->edit(NULL);
  142. }
  143. }
  144. SkeletonEditorPlugin::SkeletonEditorPlugin(EditorNode *p_node) {
  145. editor = p_node;
  146. skeleton_editor = memnew(SkeletonEditor);
  147. editor->get_viewport()->add_child(skeleton_editor);
  148. }
  149. SkeletonEditorPlugin::~SkeletonEditorPlugin() {}