1/**************************************************************************/
2/* skeleton_modification_2d_physicalbones.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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_modification_2d_physicalbones.h"
32#include "scene/2d/physical_bone_2d.h"
33#include "scene/2d/skeleton_2d.h"
34
35bool SkeletonModification2DPhysicalBones::_set(const StringName &p_path, const Variant &p_value) {
36 String path = p_path;
37
38#ifdef TOOLS_ENABLED
39 // Exposes a way to fetch the PhysicalBone2D nodes from the Godot editor.
40 if (is_setup) {
41 if (Engine::get_singleton()->is_editor_hint()) {
42 if (path.begins_with("fetch_bones")) {
43 fetch_physical_bones();
44 notify_property_list_changed();
45 return true;
46 }
47 }
48 }
49#endif //TOOLS_ENABLED
50
51 if (path.begins_with("joint_")) {
52 int which = path.get_slicec('_', 1).to_int();
53 String what = path.get_slicec('_', 2);
54 ERR_FAIL_INDEX_V(which, physical_bone_chain.size(), false);
55
56 if (what == "nodepath") {
57 set_physical_bone_node(which, p_value);
58 }
59 return true;
60 }
61 return true;
62}
63
64bool SkeletonModification2DPhysicalBones::_get(const StringName &p_path, Variant &r_ret) const {
65 String path = p_path;
66
67#ifdef TOOLS_ENABLED
68 if (Engine::get_singleton()->is_editor_hint()) {
69 if (path.begins_with("fetch_bones")) {
70 return true; // Do nothing!
71 }
72 }
73#endif //TOOLS_ENABLED
74
75 if (path.begins_with("joint_")) {
76 int which = path.get_slicec('_', 1).to_int();
77 String what = path.get_slicec('_', 2);
78 ERR_FAIL_INDEX_V(which, physical_bone_chain.size(), false);
79
80 if (what == "nodepath") {
81 r_ret = get_physical_bone_node(which);
82 }
83 return true;
84 }
85 return true;
86}
87
88void SkeletonModification2DPhysicalBones::_get_property_list(List<PropertyInfo> *p_list) const {
89#ifdef TOOLS_ENABLED
90 if (Engine::get_singleton()->is_editor_hint()) {
91 p_list->push_back(PropertyInfo(Variant::BOOL, "fetch_bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
92 }
93#endif //TOOLS_ENABLED
94
95 for (int i = 0; i < physical_bone_chain.size(); i++) {
96 String base_string = "joint_" + itos(i) + "_";
97
98 p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicalBone2D", PROPERTY_USAGE_DEFAULT));
99 }
100}
101
102void SkeletonModification2DPhysicalBones::_execute(float p_delta) {
103 ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
104 "Modification is not setup and therefore cannot execute!");
105 if (!enabled) {
106 return;
107 }
108
109 if (_simulation_state_dirty) {
110 _update_simulation_state();
111 }
112
113 for (int i = 0; i < physical_bone_chain.size(); i++) {
114 PhysicalBone_Data2D bone_data = physical_bone_chain[i];
115 if (bone_data.physical_bone_node_cache.is_null()) {
116 WARN_PRINT_ONCE("PhysicalBone2D cache " + itos(i) + " is out of date. Attempting to update...");
117 _physical_bone_update_cache(i);
118 continue;
119 }
120
121 PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(ObjectDB::get_instance(bone_data.physical_bone_node_cache));
122 if (!physical_bone) {
123 ERR_PRINT_ONCE("PhysicalBone2D not found at index " + itos(i) + "!");
124 return;
125 }
126 if (physical_bone->get_bone2d_index() < 0 || physical_bone->get_bone2d_index() > stack->skeleton->get_bone_count()) {
127 ERR_PRINT_ONCE("PhysicalBone2D at index " + itos(i) + " has invalid Bone2D!");
128 return;
129 }
130 Bone2D *bone_2d = stack->skeleton->get_bone(physical_bone->get_bone2d_index());
131
132 if (physical_bone->get_simulate_physics() && !physical_bone->get_follow_bone_when_simulating()) {
133 bone_2d->set_global_transform(physical_bone->get_global_transform());
134 stack->skeleton->set_bone_local_pose_override(physical_bone->get_bone2d_index(), bone_2d->get_transform(), stack->strength, true);
135 }
136 }
137}
138
139void SkeletonModification2DPhysicalBones::_setup_modification(SkeletonModificationStack2D *p_stack) {
140 stack = p_stack;
141
142 if (stack) {
143 is_setup = true;
144
145 if (stack->skeleton) {
146 for (int i = 0; i < physical_bone_chain.size(); i++) {
147 _physical_bone_update_cache(i);
148 }
149 }
150 }
151}
152
153void SkeletonModification2DPhysicalBones::_physical_bone_update_cache(int p_joint_idx) {
154 ERR_FAIL_INDEX_MSG(p_joint_idx, physical_bone_chain.size(), "Cannot update PhysicalBone2D cache: joint index out of range!");
155 if (!is_setup || !stack) {
156 if (!stack) {
157 ERR_PRINT_ONCE("Cannot update PhysicalBone2D cache: modification is not properly setup!");
158 }
159 return;
160 }
161
162 physical_bone_chain.write[p_joint_idx].physical_bone_node_cache = ObjectID();
163 if (stack->skeleton) {
164 if (stack->skeleton->is_inside_tree()) {
165 if (stack->skeleton->has_node(physical_bone_chain[p_joint_idx].physical_bone_node)) {
166 Node *node = stack->skeleton->get_node(physical_bone_chain[p_joint_idx].physical_bone_node);
167 ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
168 "Cannot update Physical Bone2D " + itos(p_joint_idx) + " cache: node is this modification's skeleton or cannot be found!");
169 ERR_FAIL_COND_MSG(!node->is_inside_tree(),
170 "Cannot update Physical Bone2D " + itos(p_joint_idx) + " cache: node is not in scene tree!");
171 physical_bone_chain.write[p_joint_idx].physical_bone_node_cache = node->get_instance_id();
172 }
173 }
174 }
175}
176
177int SkeletonModification2DPhysicalBones::get_physical_bone_chain_length() {
178 return physical_bone_chain.size();
179}
180
181void SkeletonModification2DPhysicalBones::set_physical_bone_chain_length(int p_length) {
182 ERR_FAIL_COND(p_length < 0);
183 physical_bone_chain.resize(p_length);
184 notify_property_list_changed();
185}
186
187void SkeletonModification2DPhysicalBones::fetch_physical_bones() {
188 ERR_FAIL_NULL_MSG(stack, "No modification stack found! Cannot fetch physical bones!");
189 ERR_FAIL_COND_MSG(!stack->skeleton, "No skeleton found! Cannot fetch physical bones!");
190
191 physical_bone_chain.clear();
192
193 List<Node *> node_queue = List<Node *>();
194 node_queue.push_back(stack->skeleton);
195
196 while (node_queue.size() > 0) {
197 Node *node_to_process = node_queue[0];
198 node_queue.pop_front();
199
200 if (node_to_process != nullptr) {
201 PhysicalBone2D *potential_bone = Object::cast_to<PhysicalBone2D>(node_to_process);
202 if (potential_bone) {
203 PhysicalBone_Data2D new_data = PhysicalBone_Data2D();
204 new_data.physical_bone_node = stack->skeleton->get_path_to(potential_bone);
205 new_data.physical_bone_node_cache = potential_bone->get_instance_id();
206 physical_bone_chain.push_back(new_data);
207 }
208 for (int i = 0; i < node_to_process->get_child_count(); i++) {
209 node_queue.push_back(node_to_process->get_child(i));
210 }
211 }
212 }
213}
214
215void SkeletonModification2DPhysicalBones::start_simulation(const TypedArray<StringName> &p_bones) {
216 _simulation_state_dirty = true;
217 _simulation_state_dirty_names = p_bones;
218 _simulation_state_dirty_process = true;
219
220 if (is_setup) {
221 _update_simulation_state();
222 }
223}
224
225void SkeletonModification2DPhysicalBones::stop_simulation(const TypedArray<StringName> &p_bones) {
226 _simulation_state_dirty = true;
227 _simulation_state_dirty_names = p_bones;
228 _simulation_state_dirty_process = false;
229
230 if (is_setup) {
231 _update_simulation_state();
232 }
233}
234
235void SkeletonModification2DPhysicalBones::_update_simulation_state() {
236 if (!_simulation_state_dirty) {
237 return;
238 }
239 _simulation_state_dirty = false;
240
241 if (_simulation_state_dirty_names.size() <= 0) {
242 for (int i = 0; i < physical_bone_chain.size(); i++) {
243 PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(stack->skeleton->get_node(physical_bone_chain[i].physical_bone_node));
244 if (!physical_bone) {
245 continue;
246 }
247
248 physical_bone->set_simulate_physics(_simulation_state_dirty_process);
249 }
250 } else {
251 for (int i = 0; i < physical_bone_chain.size(); i++) {
252 PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(ObjectDB::get_instance(physical_bone_chain[i].physical_bone_node_cache));
253 if (!physical_bone) {
254 continue;
255 }
256 if (_simulation_state_dirty_names.has(physical_bone->get_name())) {
257 physical_bone->set_simulate_physics(_simulation_state_dirty_process);
258 }
259 }
260 }
261}
262
263void SkeletonModification2DPhysicalBones::set_physical_bone_node(int p_joint_idx, const NodePath &p_nodepath) {
264 ERR_FAIL_INDEX_MSG(p_joint_idx, physical_bone_chain.size(), "Joint index out of range!");
265 physical_bone_chain.write[p_joint_idx].physical_bone_node = p_nodepath;
266 _physical_bone_update_cache(p_joint_idx);
267}
268
269NodePath SkeletonModification2DPhysicalBones::get_physical_bone_node(int p_joint_idx) const {
270 ERR_FAIL_INDEX_V_MSG(p_joint_idx, physical_bone_chain.size(), NodePath(), "Joint index out of range!");
271 return physical_bone_chain[p_joint_idx].physical_bone_node;
272}
273
274void SkeletonModification2DPhysicalBones::_bind_methods() {
275 ClassDB::bind_method(D_METHOD("set_physical_bone_chain_length", "length"), &SkeletonModification2DPhysicalBones::set_physical_bone_chain_length);
276 ClassDB::bind_method(D_METHOD("get_physical_bone_chain_length"), &SkeletonModification2DPhysicalBones::get_physical_bone_chain_length);
277
278 ClassDB::bind_method(D_METHOD("set_physical_bone_node", "joint_idx", "physicalbone2d_node"), &SkeletonModification2DPhysicalBones::set_physical_bone_node);
279 ClassDB::bind_method(D_METHOD("get_physical_bone_node", "joint_idx"), &SkeletonModification2DPhysicalBones::get_physical_bone_node);
280
281 ClassDB::bind_method(D_METHOD("fetch_physical_bones"), &SkeletonModification2DPhysicalBones::fetch_physical_bones);
282 ClassDB::bind_method(D_METHOD("start_simulation", "bones"), &SkeletonModification2DPhysicalBones::start_simulation, DEFVAL(Array()));
283 ClassDB::bind_method(D_METHOD("stop_simulation", "bones"), &SkeletonModification2DPhysicalBones::stop_simulation, DEFVAL(Array()));
284
285 ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_bone_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_physical_bone_chain_length", "get_physical_bone_chain_length");
286}
287
288SkeletonModification2DPhysicalBones::SkeletonModification2DPhysicalBones() {
289 stack = nullptr;
290 is_setup = false;
291 physical_bone_chain = Vector<PhysicalBone_Data2D>();
292 enabled = true;
293 editor_draw_gizmo = false; // Nothing to really show in a gizmo right now.
294}
295
296SkeletonModification2DPhysicalBones::~SkeletonModification2DPhysicalBones() {
297}
298