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