Debian Codegraph @

scene/resources/2d/skeleton/skeleton_modification_2d_ccdik.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* skeleton_modification_2d_ccdik.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_ccdik.h"
32 #include "scene/2d/skeleton_2d.h"
33
34 bool SkeletonModification2DCCDIK::_set(const StringName &p_path, const Variant &p_value) {
35 String path = p_path;
36
37 if (path.begins_with("joint_data/")) {
38 int which = path.get_slicec('/', 1).to_int();
39 String what = path.get_slicec('/', 2);
40 ERR_FAIL_INDEX_V(which, ccdik_data_chain.size(), false);
41
42 if (what == "bone2d_node") {
43 set_ccdik_joint_bone2d_node(which, p_value);
44 } else if (what == "bone_index") {
45 set_ccdik_joint_bone_index(which, p_value);
46 } else if (what == "rotate_from_joint") {
47 set_ccdik_joint_rotate_from_joint(which, p_value);
48 } else if (what == "enable_constraint") {
49 set_ccdik_joint_enable_constraint(which, p_value);
50 } else if (what == "constraint_angle_min") {
51 set_ccdik_joint_constraint_angle_min(which, Math::deg_to_rad(float(p_value)));
52 } else if (what == "constraint_angle_max") {
53 set_ccdik_joint_constraint_angle_max(which, Math::deg_to_rad(float(p_value)));
54 } else if (what == "constraint_angle_invert") {
55 set_ccdik_joint_constraint_angle_invert(which, p_value);
56 } else if (what == "constraint_in_localspace") {
57 set_ccdik_joint_constraint_in_localspace(which, p_value);
58 }
59 #ifdef TOOLS_ENABLED
60 else if (what.begins_with("editor_draw_gizmo")) {
61 set_ccdik_joint_editor_draw_gizmo(which, p_value);
62 }
63 #endif // TOOLS_ENABLED
64 else {
65 return false;
66 }
67 }
68 #ifdef TOOLS_ENABLED
69 else if (path.begins_with("editor/draw_gizmo")) {
70 set_editor_draw_gizmo(p_value);
71 }
72 #endif // TOOLS_ENABLED
73 else {
74 return false;
75 }
76
77 return true;
78 }
79
80 bool SkeletonModification2DCCDIK::_get(const StringName &p_path, Variant &r_ret) const {
81 String path = p_path;
82
83 if (path.begins_with("joint_data/")) {
84 int which = path.get_slicec('/', 1).to_int();
85 String what = path.get_slicec('/', 2);
86 ERR_FAIL_INDEX_V(which, ccdik_data_chain.size(), false);
87
88 if (what == "bone2d_node") {
89 r_ret = get_ccdik_joint_bone2d_node(which);
90 } else if (what == "bone_index") {
91 r_ret = get_ccdik_joint_bone_index(which);
92 } else if (what == "rotate_from_joint") {
93 r_ret = get_ccdik_joint_rotate_from_joint(which);
94 } else if (what == "enable_constraint") {
95 r_ret = get_ccdik_joint_enable_constraint(which);
96 } else if (what == "constraint_angle_min") {
97 r_ret = Math::rad_to_deg(get_ccdik_joint_constraint_angle_min(which));
98 } else if (what == "constraint_angle_max") {
99 r_ret = Math::rad_to_deg(get_ccdik_joint_constraint_angle_max(which));
100 } else if (what == "constraint_angle_invert") {
101 r_ret = get_ccdik_joint_constraint_angle_invert(which);
102 } else if (what == "constraint_in_localspace") {
103 r_ret = get_ccdik_joint_constraint_in_localspace(which);
104 }
105 #ifdef TOOLS_ENABLED
106 else if (what.begins_with("editor_draw_gizmo")) {
107 r_ret = get_ccdik_joint_editor_draw_gizmo(which);
108 }
109 #endif // TOOLS_ENABLED
110 else {
111 return false;
112 }
113 }
114 #ifdef TOOLS_ENABLED
115 else if (path.begins_with("editor/draw_gizmo")) {
116 r_ret = get_editor_draw_gizmo();
117 }
118 #endif // TOOLS_ENABLED
119 else {
120 return false;
121 }
122
123 return true;
124 }
125
126 void SkeletonModification2DCCDIK::_get_property_list(List<PropertyInfo> *p_list) const {
127 for (int i = 0; i < ccdik_data_chain.size(); i++) {
128 String base_string = "joint_data/" + itos(i) + "/";
129
130 p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
131 p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));
132 p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "rotate_from_joint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
133
134 p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "enable_constraint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
135 if (ccdik_data_chain[i].enable_constraint) {
136 p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "constraint_angle_min", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
137 p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "constraint_angle_max", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
138 p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "constraint_angle_invert", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
139 p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "constraint_in_localspace", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
140 }
141
142 #ifdef TOOLS_ENABLED
143 if (Engine::get_singleton()->is_editor_hint()) {
144 p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "editor_draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
145 }
146 #endif // TOOLS_ENABLED
147 }
148
149 #ifdef TOOLS_ENABLED
150 if (Engine::get_singleton()->is_editor_hint()) {
151 p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
152 }
153 #endif // TOOLS_ENABLED
154 }
155
156 void SkeletonModification2DCCDIK::_execute(float p_delta) {
157 ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
158 "Modification is not setup and therefore cannot execute!");
159 if (!enabled) {
160 return;
161 }
162
163 if (target_node_cache.is_null()) {
164 WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
165 update_target_cache();
166 return;
167 }
168 if (tip_node_cache.is_null()) {
169 WARN_PRINT_ONCE("Tip cache is out of date. Attempting to update...");
170 update_tip_cache();
171 return;
172 }
173
174 Node2D *target = ObjectDB::get_instance<Node2D>(target_node_cache);
175 if (!target || !target->is_inside_tree()) {
176 ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
177 return;
178 }
179
180 Node2D *tip = ObjectDB::get_instance<Node2D>(tip_node_cache);
181 if (!tip || !tip->is_inside_tree()) {
182 ERR_PRINT_ONCE("Tip node is not in the scene tree. Cannot execute modification!");
183 return;
184 }
185
186 for (int i = 0; i < ccdik_data_chain.size(); i++) {
187 _execute_ccdik_joint(i, target, tip);
188 }
189 }
190
191 void SkeletonModification2DCCDIK::_execute_ccdik_joint(int p_joint_idx, Node2D *p_target, Node2D *p_tip) {
192 CCDIK_Joint_Data2D ccdik_data = ccdik_data_chain[p_joint_idx];
193 if (ccdik_data.bone_idx < 0 || ccdik_data.bone_idx > stack->skeleton->get_bone_count()) {
194 ERR_PRINT_ONCE("2D CCDIK joint: bone index not found!");
195 return;
196 }
197
198 Bone2D *operation_bone = stack->skeleton->get_bone(ccdik_data.bone_idx);
199 Transform2D operation_transform = operation_bone->get_global_transform();
200
201 if (ccdik_data.rotate_from_joint) {
202 // To rotate from the joint, simply look at the target!
203 operation_transform.set_rotation(
204 operation_transform.looking_at(p_target->get_global_position()).get_rotation() - operation_bone->get_bone_angle());
205 } else {
206 // How to rotate from the tip: get the difference of rotation needed from the tip to the target, from the perspective of the joint.
207 // Because we are only using the offset, we do not need to account for the bone angle of the Bone2D node.
208 float joint_to_tip = p_tip->get_global_position().angle_to_point(operation_transform.get_origin());
209 float joint_to_target = p_target->get_global_position().angle_to_point(operation_transform.get_origin());
210 operation_transform.set_rotation(
211 operation_transform.get_rotation() + (joint_to_target - joint_to_tip));
212 }
213
214 // Reset scale
215 operation_transform.set_scale(operation_bone->get_global_scale());
216
217 // Apply constraints in globalspace:
218 if (ccdik_data.enable_constraint && !ccdik_data.constraint_in_localspace) {
219 operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), ccdik_data.constraint_angle_min, ccdik_data.constraint_angle_max, ccdik_data.constraint_angle_invert));
220 }
221
222 // Convert from a global transform to a delta and then apply the delta to the local transform.
223 operation_bone->set_global_transform(operation_transform);
224 operation_transform = operation_bone->get_transform();
225
226 // Apply constraints in localspace:
227 if (ccdik_data.enable_constraint && ccdik_data.constraint_in_localspace) {
228 operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), ccdik_data.constraint_angle_min, ccdik_data.constraint_angle_max, ccdik_data.constraint_angle_invert));
229 }
230
231 // Set the local pose override, and to make sure child bones are also updated, set the transform of the bone.
232 stack->skeleton->set_bone_local_pose_override(ccdik_data.bone_idx, operation_transform, stack->strength, true);
233 operation_bone->set_transform(operation_transform);
234 operation_bone->notification(operation_bone->NOTIFICATION_TRANSFORM_CHANGED);
235 }
236
237 void SkeletonModification2DCCDIK::_setup_modification(SkeletonModificationStack2D *p_stack) {
238 stack = p_stack;
239
240 if (stack != nullptr) {
241 is_setup = true;
242 update_target_cache();
243 update_tip_cache();
244 }
245 }
246
247 void SkeletonModification2DCCDIK::_draw_editor_gizmo() {
248 if (!enabled || !is_setup) {
249 return;
250 }
251
252 for (int i = 0; i < ccdik_data_chain.size(); i++) {
253 if (!ccdik_data_chain[i].editor_draw_gizmo) {
254 continue;
255 }
256 if (ccdik_data_chain[i].bone_idx < 0) {
257 continue;
258 }
259
260 Bone2D *operation_bone = stack->skeleton->get_bone(ccdik_data_chain[i].bone_idx);
261 editor_draw_angle_constraints(operation_bone, ccdik_data_chain[i].constraint_angle_min, ccdik_data_chain[i].constraint_angle_max,
262 ccdik_data_chain[i].enable_constraint, ccdik_data_chain[i].constraint_in_localspace, ccdik_data_chain[i].constraint_angle_invert);
263 }
264 }
265
266 void SkeletonModification2DCCDIK::update_target_cache() {
267 if (!is_setup || !stack) {
268 if (is_setup) {
269 ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
270 }
271 return;
272 }
273
274 target_node_cache = ObjectID();
275 if (stack->skeleton) {
276 if (stack->skeleton->is_inside_tree()) {
277 if (stack->skeleton->has_node(target_node)) {
278 Node *node = stack->skeleton->get_node(target_node);
279 ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
280 "Cannot update target cache: node is this modification's skeleton or cannot be found!");
281 ERR_FAIL_COND_MSG(!node->is_inside_tree(),
282 "Cannot update target cache: node is not in the scene tree!");
283 target_node_cache = node->get_instance_id();
284 }
285 }
286 }
287 }
288
289 void SkeletonModification2DCCDIK::update_tip_cache() {
290 if (!is_setup || !stack) {
291 if (is_setup) {
292 ERR_PRINT_ONCE("Cannot update tip cache: modification is not properly setup!");
293 }
294 return;
295 }
296
297 tip_node_cache = ObjectID();
298 if (stack->skeleton) {
299 if (stack->skeleton->is_inside_tree()) {
300 if (stack->skeleton->has_node(tip_node)) {
301 Node *node = stack->skeleton->get_node(tip_node);
302 ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
303 "Cannot update tip cache: node is this modification's skeleton or cannot be found!");
304 ERR_FAIL_COND_MSG(!node->is_inside_tree(),
305 "Cannot update tip cache: node is not in the scene tree!");
306 tip_node_cache = node->get_instance_id();
307 }
308 }
309 }
310 }
311
312 void SkeletonModification2DCCDIK::ccdik_joint_update_bone2d_cache(int p_joint_idx) {
313 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");
314 if (!is_setup || !stack) {
315 if (is_setup) {
316 ERR_PRINT_ONCE("Cannot update CCDIK Bone2D cache: modification is not properly setup!");
317 }
318 return;
319 }
320
321 ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();
322 if (stack->skeleton) {
323 if (stack->skeleton->is_inside_tree()) {
324 if (stack->skeleton->has_node(ccdik_data_chain[p_joint_idx].bone2d_node)) {
325 Node *node = stack->skeleton->get_node(ccdik_data_chain[p_joint_idx].bone2d_node);
326 ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
327 "Cannot update CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");
328 ERR_FAIL_COND_MSG(!node->is_inside_tree(),
329 "Cannot update CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: node is not in the scene tree!");
330 ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();
331
332 Bone2D *bone = Object::cast_to<Bone2D>(node);
333 if (bone) {
334 ccdik_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();
335 } else {
336 ERR_FAIL_MSG("CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
337 }
338 }
339 }
340 }
341 }
342
343 void SkeletonModification2DCCDIK::set_target_node(const NodePath &p_target_node) {
344 target_node = p_target_node;
345 update_target_cache();
346 }
347
348 NodePath SkeletonModification2DCCDIK::get_target_node() const {
349 return target_node;
350 }
351
352 void SkeletonModification2DCCDIK::set_tip_node(const NodePath &p_tip_node) {
353 tip_node = p_tip_node;
354 update_tip_cache();
355 }
356
357 NodePath SkeletonModification2DCCDIK::get_tip_node() const {
358 return tip_node;
359 }
360
361 void SkeletonModification2DCCDIK::set_ccdik_data_chain_length(int p_length) {
362 ccdik_data_chain.resize(p_length);
363 notify_property_list_changed();
364 }
365
366 int SkeletonModification2DCCDIK::get_ccdik_data_chain_length() {
367 return ccdik_data_chain.size();
368 }
369
370 void SkeletonModification2DCCDIK::set_ccdik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {
371 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
372 ccdik_data_chain.write[p_joint_idx].bone2d_node = p_target_node;
373 ccdik_joint_update_bone2d_cache(p_joint_idx);
374
375 notify_property_list_changed();
376 }
377
378 NodePath SkeletonModification2DCCDIK::get_ccdik_joint_bone2d_node(int p_joint_idx) const {
379 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), NodePath(), "CCDIK joint out of range!");
380 return ccdik_data_chain[p_joint_idx].bone2d_node;
381 }
382
383 void SkeletonModification2DCCDIK::set_ccdik_joint_bone_index(int p_joint_idx, int p_bone_idx) {
384 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCCDIK joint out of range!");
385 ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
386
387 if (is_setup) {
388 if (stack->skeleton) {
389 ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
390 ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
391 ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
392 ccdik_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
393 } else {
394 WARN_PRINT("Cannot verify the CCDIK joint " + itos(p_joint_idx) + " bone index for this modification...");
395 ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
396 }
397 } else {
398 ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
399 }
400
401 notify_property_list_changed();
402 }
403
404 int SkeletonModification2DCCDIK::get_ccdik_joint_bone_index(int p_joint_idx) const {
405 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), -1, "CCDIK joint out of range!");
406 return ccdik_data_chain[p_joint_idx].bone_idx;
407 }
408
409 void SkeletonModification2DCCDIK::set_ccdik_joint_rotate_from_joint(int p_joint_idx, bool p_rotate_from_joint) {
410 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
411 ccdik_data_chain.write[p_joint_idx].rotate_from_joint = p_rotate_from_joint;
412 }
413
414 bool SkeletonModification2DCCDIK::get_ccdik_joint_rotate_from_joint(int p_joint_idx) const {
415 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
416 return ccdik_data_chain[p_joint_idx].rotate_from_joint;
417 }
418
419 void SkeletonModification2DCCDIK::set_ccdik_joint_enable_constraint(int p_joint_idx, bool p_constraint) {
420 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
421 ccdik_data_chain.write[p_joint_idx].enable_constraint = p_constraint;
422 notify_property_list_changed();
423
424 #ifdef TOOLS_ENABLED
425 if (stack && is_setup) {
426 stack->set_editor_gizmos_dirty(true);
427 }
428 #endif // TOOLS_ENABLED
429 }
430
431 bool SkeletonModification2DCCDIK::get_ccdik_joint_enable_constraint(int p_joint_idx) const {
432 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
433 return ccdik_data_chain[p_joint_idx].enable_constraint;
434 }
435
436 void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_min(int p_joint_idx, float p_angle_min) {
437 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
438 ccdik_data_chain.write[p_joint_idx].constraint_angle_min = p_angle_min;
439
440 #ifdef TOOLS_ENABLED
441 if (stack && is_setup) {
442 stack->set_editor_gizmos_dirty(true);
443 }
444 #endif // TOOLS_ENABLED
445 }
446
447 float SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_min(int p_joint_idx) const {
448 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), 0.0, "CCDIK joint out of range!");
449 return ccdik_data_chain[p_joint_idx].constraint_angle_min;
450 }
451
452 void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_max(int p_joint_idx, float p_angle_max) {
453 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
454 ccdik_data_chain.write[p_joint_idx].constraint_angle_max = p_angle_max;
455
456 #ifdef TOOLS_ENABLED
457 if (stack && is_setup) {
458 stack->set_editor_gizmos_dirty(true);
459 }
460 #endif // TOOLS_ENABLED
461 }
462
463 float SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_max(int p_joint_idx) const {
464 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), 0.0, "CCDIK joint out of range!");
465 return ccdik_data_chain[p_joint_idx].constraint_angle_max;
466 }
467
468 void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_invert(int p_joint_idx, bool p_invert) {
469 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
470 ccdik_data_chain.write[p_joint_idx].constraint_angle_invert = p_invert;
471
472 #ifdef TOOLS_ENABLED
473 if (stack && is_setup) {
474 stack->set_editor_gizmos_dirty(true);
475 }
476 #endif // TOOLS_ENABLED
477 }
478
479 bool SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_invert(int p_joint_idx) const {
480 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
481 return ccdik_data_chain[p_joint_idx].constraint_angle_invert;
482 }
483
484 void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_in_localspace(int p_joint_idx, bool p_constraint_in_localspace) {
485 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
486 ccdik_data_chain.write[p_joint_idx].constraint_in_localspace = p_constraint_in_localspace;
487
488 #ifdef TOOLS_ENABLED
489 if (stack && is_setup) {
490 stack->set_editor_gizmos_dirty(true);
491 }
492 #endif // TOOLS_ENABLED
493 }
494
495 bool SkeletonModification2DCCDIK::get_ccdik_joint_constraint_in_localspace(int p_joint_idx) const {
496 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
497 return ccdik_data_chain[p_joint_idx].constraint_in_localspace;
498 }
499
500 void SkeletonModification2DCCDIK::set_ccdik_joint_editor_draw_gizmo(int p_joint_idx, bool p_draw_gizmo) {
501 ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
502 ccdik_data_chain.write[p_joint_idx].editor_draw_gizmo = p_draw_gizmo;
503
504 #ifdef TOOLS_ENABLED
505 if (stack && is_setup) {
506 stack->set_editor_gizmos_dirty(true);
507 }
508 #endif // TOOLS_ENABLED
509 }
510
511 bool SkeletonModification2DCCDIK::get_ccdik_joint_editor_draw_gizmo(int p_joint_idx) const {
512 ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
513 return ccdik_data_chain[p_joint_idx].editor_draw_gizmo;
514 }
515
516 void SkeletonModification2DCCDIK::_bind_methods() {
517 ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DCCDIK::set_target_node);
518 ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DCCDIK::get_target_node);
519 ClassDB::bind_method(D_METHOD("set_tip_node", "tip_nodepath"), &SkeletonModification2DCCDIK::set_tip_node);
520 ClassDB::bind_method(D_METHOD("get_tip_node"), &SkeletonModification2DCCDIK::get_tip_node);
521
522 ClassDB::bind_method(D_METHOD("set_ccdik_data_chain_length", "length"), &SkeletonModification2DCCDIK::set_ccdik_data_chain_length);
523 ClassDB::bind_method(D_METHOD("get_ccdik_data_chain_length"), &SkeletonModification2DCCDIK::get_ccdik_data_chain_length);
524
525 ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone2d_node", "joint_idx", "bone2d_nodepath"), &SkeletonModification2DCCDIK::set_ccdik_joint_bone2d_node);
526 ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone2d_node", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_bone2d_node);
527 ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DCCDIK::set_ccdik_joint_bone_index);
528 ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone_index", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_bone_index);
529 ClassDB::bind_method(D_METHOD("set_ccdik_joint_rotate_from_joint", "joint_idx", "rotate_from_joint"), &SkeletonModification2DCCDIK::set_ccdik_joint_rotate_from_joint);
530 ClassDB::bind_method(D_METHOD("get_ccdik_joint_rotate_from_joint", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_rotate_from_joint);
531 ClassDB::bind_method(D_METHOD("set_ccdik_joint_enable_constraint", "joint_idx", "enable_constraint"), &SkeletonModification2DCCDIK::set_ccdik_joint_enable_constraint);
532 ClassDB::bind_method(D_METHOD("get_ccdik_joint_enable_constraint", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_enable_constraint);
533 ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_min", "joint_idx", "angle_min"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_min);
534 ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_min", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_min);
535 ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_max", "joint_idx", "angle_max"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_max);
536 ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_max", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_max);
537 ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_invert", "joint_idx", "invert"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_invert);
538 ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_invert", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_invert);
539
540 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
541 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "tip_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_tip_node", "get_tip_node");
542 ADD_PROPERTY(PropertyInfo(Variant::INT, "ccdik_data_chain_length", PROPERTY_HINT_RANGE, "0, 100, 1"), "set_ccdik_data_chain_length", "get_ccdik_data_chain_length");
543 }
544
545 SkeletonModification2DCCDIK::SkeletonModification2DCCDIK() {
546 stack = nullptr;
547 is_setup = false;
548 enabled = true;
549 editor_draw_gizmo = true;
550 }
551
552 SkeletonModification2DCCDIK::~SkeletonModification2DCCDIK() {
553 }