| 1 | /**************************************************************************/ |
| 2 | /* skeleton_modification_2d_fabrik.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_fabrik.h" |
| 32 | #include "scene/2d/skeleton_2d.h" |
| 33 | |
| 34 | bool SkeletonModification2DFABRIK::_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, fabrik_data_chain.size(), false); |
| 41 | |
| 42 | if (what == "bone2d_node") { |
| 43 | set_fabrik_joint_bone2d_node(which, p_value); |
| 44 | } else if (what == "bone_index") { |
| 45 | set_fabrik_joint_bone_index(which, p_value); |
| 46 | } else if (what == "magnet_position") { |
| 47 | set_fabrik_joint_magnet_position(which, p_value); |
| 48 | } else if (what == "use_target_rotation") { |
| 49 | set_fabrik_joint_use_target_rotation(which, p_value); |
| 50 | } else { |
| 51 | return false; |
| 52 | } |
| 53 | } else { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool SkeletonModification2DFABRIK::_get(const StringName &p_path, Variant &r_ret) const { |
| 61 | String path = p_path; |
| 62 | |
| 63 | if (path.begins_with("joint_data/")) { |
| 64 | int which = path.get_slicec('/', 1).to_int(); |
| 65 | String what = path.get_slicec('/', 2); |
| 66 | ERR_FAIL_INDEX_V(which, fabrik_data_chain.size(), false); |
| 67 | |
| 68 | if (what == "bone2d_node") { |
| 69 | r_ret = get_fabrik_joint_bone2d_node(which); |
| 70 | } else if (what == "bone_index") { |
| 71 | r_ret = get_fabrik_joint_bone_index(which); |
| 72 | } else if (what == "magnet_position") { |
| 73 | r_ret = get_fabrik_joint_magnet_position(which); |
| 74 | } else if (what == "use_target_rotation") { |
| 75 | r_ret = get_fabrik_joint_use_target_rotation(which); |
| 76 | } else { |
| 77 | return false; |
| 78 | } |
| 79 | } else { |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void SkeletonModification2DFABRIK::_get_property_list(List<PropertyInfo> *p_list) const { |
| 86 | for (int i = 0; i < fabrik_data_chain.size(); i++) { |
| 87 | String base_string = "joint_data/" + itos(i) + "/"; |
| 88 | |
| 89 | p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); |
| 90 | p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT)); |
| 91 | |
| 92 | if (i > 0) { |
| 93 | p_list->push_back(PropertyInfo(Variant::VECTOR2, base_string + "magnet_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); |
| 94 | } |
| 95 | if (i == fabrik_data_chain.size() - 1) { |
| 96 | p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_target_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void SkeletonModification2DFABRIK::_execute(float p_delta) { |
| 102 | ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, |
| 103 | "Modification is not setup and therefore cannot execute!"); |
| 104 | if (!enabled) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (target_node_cache.is_null()) { |
| 109 | WARN_PRINT_ONCE("Target cache is out of date. Attempting to update..."); |
| 110 | update_target_cache(); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | if (fabrik_data_chain.size() <= 1) { |
| 115 | ERR_PRINT_ONCE("FABRIK requires at least two joints to operate! Cannot execute modification!"); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | Node2D *target = ObjectDB::get_instance<Node2D>(target_node_cache); |
| 120 | if (!target || !target->is_inside_tree()) { |
| 121 | ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!"); |
| 122 | return; |
| 123 | } |
| 124 | target_global_pose = target->get_global_transform(); |
| 125 | |
| 126 | if (fabrik_data_chain[0].bone2d_node_cache.is_null() && !fabrik_data_chain[0].bone2d_node.is_empty()) { |
| 127 | fabrik_joint_update_bone2d_cache(0); |
| 128 | WARN_PRINT("Bone2D cache for origin joint is out of date. Updating..."); |
| 129 | } |
| 130 | |
| 131 | Bone2D *origin_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[0].bone2d_node_cache); |
| 132 | if (!origin_bone2d_node || !origin_bone2d_node->is_inside_tree()) { |
| 133 | ERR_PRINT_ONCE("Origin joint's Bone2D node is not in the scene tree. Cannot execute modification!"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | origin_global_pose = origin_bone2d_node->get_global_transform(); |
| 138 | |
| 139 | if (fabrik_transform_chain.size() != fabrik_data_chain.size()) { |
| 140 | fabrik_transform_chain.resize(fabrik_data_chain.size()); |
| 141 | } |
| 142 | |
| 143 | for (int i = 0; i < fabrik_data_chain.size(); i++) { |
| 144 | // Update the transform chain |
| 145 | if (fabrik_data_chain[i].bone2d_node_cache.is_null() && !fabrik_data_chain[i].bone2d_node.is_empty()) { |
| 146 | WARN_PRINT_ONCE("Bone2D cache for joint " + itos(i) + " is out of date.. Attempting to update..."); |
| 147 | fabrik_joint_update_bone2d_cache(i); |
| 148 | } |
| 149 | Bone2D *joint_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache); |
| 150 | if (!joint_bone2d_node) { |
| 151 | ERR_PRINT_ONCE("FABRIK Joint " + itos(i) + " does not have a Bone2D node set! Cannot execute modification!"); |
| 152 | return; |
| 153 | } |
| 154 | fabrik_transform_chain.write[i] = joint_bone2d_node->get_global_transform(); |
| 155 | } |
| 156 | |
| 157 | Bone2D *final_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[fabrik_data_chain.size() - 1].bone2d_node_cache); |
| 158 | float final_bone2d_angle = final_bone2d_node->get_global_rotation(); |
| 159 | if (fabrik_data_chain[fabrik_data_chain.size() - 1].use_target_rotation) { |
| 160 | final_bone2d_angle = target_global_pose.get_rotation(); |
| 161 | } |
| 162 | Vector2 final_bone2d_direction = Vector2(Math::cos(final_bone2d_angle), Math::sin(final_bone2d_angle)); |
| 163 | float final_bone2d_length = final_bone2d_node->get_length() * MIN(final_bone2d_node->get_global_scale().x, final_bone2d_node->get_global_scale().y); |
| 164 | float target_distance = (final_bone2d_node->get_global_position() + (final_bone2d_direction * final_bone2d_length)).distance_to(target->get_global_position()); |
| 165 | chain_iterations = 0; |
| 166 | |
| 167 | while (target_distance > chain_tolarance) { |
| 168 | chain_backwards(); |
| 169 | chain_forwards(); |
| 170 | |
| 171 | final_bone2d_angle = final_bone2d_node->get_global_rotation(); |
| 172 | if (fabrik_data_chain[fabrik_data_chain.size() - 1].use_target_rotation) { |
| 173 | final_bone2d_angle = target_global_pose.get_rotation(); |
| 174 | } |
| 175 | final_bone2d_direction = Vector2(Math::cos(final_bone2d_angle), Math::sin(final_bone2d_angle)); |
| 176 | target_distance = (final_bone2d_node->get_global_position() + (final_bone2d_direction * final_bone2d_length)).distance_to(target->get_global_position()); |
| 177 | |
| 178 | chain_iterations += 1; |
| 179 | if (chain_iterations >= chain_max_iterations) { |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Apply all of the saved transforms to the Bone2D nodes |
| 185 | for (int i = 0; i < fabrik_data_chain.size(); i++) { |
| 186 | Bone2D *joint_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache); |
| 187 | if (!joint_bone2d_node) { |
| 188 | ERR_PRINT_ONCE("FABRIK Joint " + itos(i) + " does not have a Bone2D node set!"); |
| 189 | continue; |
| 190 | } |
| 191 | Transform2D chain_trans = fabrik_transform_chain[i]; |
| 192 | |
| 193 | // Apply rotation |
| 194 | if (i + 1 < fabrik_data_chain.size()) { |
| 195 | chain_trans = chain_trans.looking_at(fabrik_transform_chain[i + 1].get_origin()); |
| 196 | } else { |
| 197 | if (fabrik_data_chain[i].use_target_rotation) { |
| 198 | chain_trans.set_rotation(target_global_pose.get_rotation()); |
| 199 | } else { |
| 200 | chain_trans = chain_trans.looking_at(target_global_pose.get_origin()); |
| 201 | } |
| 202 | } |
| 203 | // Adjust for the bone angle |
| 204 | chain_trans.set_rotation(chain_trans.get_rotation() - joint_bone2d_node->get_bone_angle()); |
| 205 | |
| 206 | // Reset scale |
| 207 | chain_trans.set_scale(joint_bone2d_node->get_global_scale()); |
| 208 | |
| 209 | // Apply to the bone, and to the override |
| 210 | joint_bone2d_node->set_global_transform(chain_trans); |
| 211 | stack->skeleton->set_bone_local_pose_override(fabrik_data_chain[i].bone_idx, joint_bone2d_node->get_transform(), stack->strength, true); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void SkeletonModification2DFABRIK::chain_backwards() { |
| 216 | int final_joint_index = fabrik_data_chain.size() - 1; |
| 217 | Bone2D *final_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[final_joint_index].bone2d_node_cache); |
| 218 | Transform2D final_bone2d_trans = fabrik_transform_chain[final_joint_index]; |
| 219 | |
| 220 | // Apply magnet position |
| 221 | if (final_joint_index != 0) { |
| 222 | final_bone2d_trans.set_origin(final_bone2d_trans.get_origin() + fabrik_data_chain[final_joint_index].magnet_position); |
| 223 | } |
| 224 | |
| 225 | // Set the rotation of the tip bone |
| 226 | final_bone2d_trans = final_bone2d_trans.looking_at(target_global_pose.get_origin()); |
| 227 | |
| 228 | // Set the position of the tip bone |
| 229 | float final_bone2d_angle = final_bone2d_trans.get_rotation(); |
| 230 | if (fabrik_data_chain[final_joint_index].use_target_rotation) { |
| 231 | final_bone2d_angle = target_global_pose.get_rotation(); |
| 232 | } |
| 233 | Vector2 final_bone2d_direction = Vector2(Math::cos(final_bone2d_angle), Math::sin(final_bone2d_angle)); |
| 234 | float final_bone2d_length = final_bone2d_node->get_length() * MIN(final_bone2d_node->get_global_scale().x, final_bone2d_node->get_global_scale().y); |
| 235 | final_bone2d_trans.set_origin(target_global_pose.get_origin() - (final_bone2d_direction * final_bone2d_length)); |
| 236 | |
| 237 | // Save the transform |
| 238 | fabrik_transform_chain.write[final_joint_index] = final_bone2d_trans; |
| 239 | |
| 240 | int i = final_joint_index; |
| 241 | while (i >= 1) { |
| 242 | Transform2D previous_pose = fabrik_transform_chain[i]; |
| 243 | i -= 1; |
| 244 | Bone2D *current_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache); |
| 245 | Transform2D current_pose = fabrik_transform_chain[i]; |
| 246 | |
| 247 | // Apply magnet position |
| 248 | if (i != 0) { |
| 249 | current_pose.set_origin(current_pose.get_origin() + fabrik_data_chain[i].magnet_position); |
| 250 | } |
| 251 | |
| 252 | float current_bone2d_node_length = current_bone2d_node->get_length() * MIN(current_bone2d_node->get_global_scale().x, current_bone2d_node->get_global_scale().y); |
| 253 | float length = current_bone2d_node_length / (current_pose.get_origin().distance_to(previous_pose.get_origin())); |
| 254 | Vector2 finish_position = previous_pose.get_origin().lerp(current_pose.get_origin(), length); |
| 255 | current_pose.set_origin(finish_position); |
| 256 | |
| 257 | // Save the transform |
| 258 | fabrik_transform_chain.write[i] = current_pose; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void SkeletonModification2DFABRIK::chain_forwards() { |
| 263 | Transform2D origin_bone2d_trans = fabrik_transform_chain[0]; |
| 264 | origin_bone2d_trans.set_origin(origin_global_pose.get_origin()); |
| 265 | // Save the position |
| 266 | fabrik_transform_chain.write[0] = origin_bone2d_trans; |
| 267 | |
| 268 | for (int i = 0; i < fabrik_data_chain.size() - 1; i++) { |
| 269 | Bone2D *current_bone2d_node = ObjectDB::get_instance<Bone2D>(fabrik_data_chain[i].bone2d_node_cache); |
| 270 | Transform2D current_pose = fabrik_transform_chain[i]; |
| 271 | Transform2D next_pose = fabrik_transform_chain[i + 1]; |
| 272 | |
| 273 | float current_bone2d_node_length = current_bone2d_node->get_length() * MIN(current_bone2d_node->get_global_scale().x, current_bone2d_node->get_global_scale().y); |
| 274 | float length = current_bone2d_node_length / (next_pose.get_origin().distance_to(current_pose.get_origin())); |
| 275 | Vector2 finish_position = current_pose.get_origin().lerp(next_pose.get_origin(), length); |
| 276 | current_pose.set_origin(finish_position); |
| 277 | |
| 278 | // Apply to the bone |
| 279 | fabrik_transform_chain.write[i + 1] = current_pose; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void SkeletonModification2DFABRIK::_setup_modification(SkeletonModificationStack2D *p_stack) { |
| 284 | stack = p_stack; |
| 285 | |
| 286 | if (stack != nullptr) { |
| 287 | is_setup = true; |
| 288 | |
| 289 | if (stack->skeleton) { |
| 290 | for (int i = 0; i < fabrik_data_chain.size(); i++) { |
| 291 | fabrik_joint_update_bone2d_cache(i); |
| 292 | } |
| 293 | } |
| 294 | update_target_cache(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void SkeletonModification2DFABRIK::update_target_cache() { |
| 299 | if (!is_setup || !stack) { |
| 300 | if (is_setup) { |
| 301 | ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!"); |
| 302 | } |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | target_node_cache = ObjectID(); |
| 307 | if (stack->skeleton) { |
| 308 | if (stack->skeleton->is_inside_tree()) { |
| 309 | if (stack->skeleton->has_node(target_node)) { |
| 310 | Node *node = stack->skeleton->get_node(target_node); |
| 311 | ERR_FAIL_COND_MSG(!node || stack->skeleton == node, |
| 312 | "Cannot update target cache: node is this modification's skeleton or cannot be found!"); |
| 313 | ERR_FAIL_COND_MSG(!node->is_inside_tree(), |
| 314 | "Cannot update target cache: node is not in scene tree!"); |
| 315 | target_node_cache = node->get_instance_id(); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void SkeletonModification2DFABRIK::fabrik_joint_update_bone2d_cache(int p_joint_idx) { |
| 322 | ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "Cannot update bone2d cache: joint index out of range!"); |
| 323 | if (!is_setup || !stack) { |
| 324 | if (is_setup) { |
| 325 | ERR_PRINT_ONCE("Cannot update FABRIK Bone2D cache: modification is not properly setup!"); |
| 326 | } |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | fabrik_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID(); |
| 331 | if (stack->skeleton) { |
| 332 | if (stack->skeleton->is_inside_tree()) { |
| 333 | if (stack->skeleton->has_node(fabrik_data_chain[p_joint_idx].bone2d_node)) { |
| 334 | Node *node = stack->skeleton->get_node(fabrik_data_chain[p_joint_idx].bone2d_node); |
| 335 | ERR_FAIL_COND_MSG(!node || stack->skeleton == node, |
| 336 | "Cannot update FABRIK joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!"); |
| 337 | ERR_FAIL_COND_MSG(!node->is_inside_tree(), |
| 338 | "Cannot update FABRIK joint " + itos(p_joint_idx) + " Bone2D cache: node is not in scene tree!"); |
| 339 | fabrik_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id(); |
| 340 | |
| 341 | Bone2D *bone = Object::cast_to<Bone2D>(node); |
| 342 | if (bone) { |
| 343 | fabrik_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton(); |
| 344 | } else { |
| 345 | ERR_FAIL_MSG("FABRIK joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!"); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void SkeletonModification2DFABRIK::set_target_node(const NodePath &p_target_node) { |
| 353 | target_node = p_target_node; |
| 354 | update_target_cache(); |
| 355 | } |
| 356 | |
| 357 | NodePath SkeletonModification2DFABRIK::get_target_node() const { |
| 358 | return target_node; |
| 359 | } |
| 360 | |
| 361 | void SkeletonModification2DFABRIK::set_fabrik_data_chain_length(int p_length) { |
| 362 | fabrik_data_chain.resize(p_length); |
| 363 | notify_property_list_changed(); |
| 364 | } |
| 365 | |
| 366 | int SkeletonModification2DFABRIK::get_fabrik_data_chain_length() { |
| 367 | return fabrik_data_chain.size(); |
| 368 | } |
| 369 | |
| 370 | void SkeletonModification2DFABRIK::set_fabrik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) { |
| 371 | ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!"); |
| 372 | fabrik_data_chain.write[p_joint_idx].bone2d_node = p_target_node; |
| 373 | fabrik_joint_update_bone2d_cache(p_joint_idx); |
| 374 | |
| 375 | notify_property_list_changed(); |
| 376 | } |
| 377 | |
| 378 | NodePath SkeletonModification2DFABRIK::get_fabrik_joint_bone2d_node(int p_joint_idx) const { |
| 379 | ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), NodePath(), "FABRIK joint out of range!"); |
| 380 | return fabrik_data_chain[p_joint_idx].bone2d_node; |
| 381 | } |
| 382 | |
| 383 | void SkeletonModification2DFABRIK::set_fabrik_joint_bone_index(int p_joint_idx, int p_bone_idx) { |
| 384 | ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK 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 | fabrik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx; |
| 391 | fabrik_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id(); |
| 392 | fabrik_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 FABRIK joint " + itos(p_joint_idx) + " bone index for this modification..."); |
| 395 | fabrik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx; |
| 396 | } |
| 397 | } else { |
| 398 | fabrik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx; |
| 399 | } |
| 400 | |
| 401 | notify_property_list_changed(); |
| 402 | } |
| 403 | |
| 404 | int SkeletonModification2DFABRIK::get_fabrik_joint_bone_index(int p_joint_idx) const { |
| 405 | ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), -1, "FABRIK joint out of range!"); |
| 406 | return fabrik_data_chain[p_joint_idx].bone_idx; |
| 407 | } |
| 408 | |
| 409 | void SkeletonModification2DFABRIK::set_fabrik_joint_magnet_position(int p_joint_idx, Vector2 p_magnet_position) { |
| 410 | ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!"); |
| 411 | fabrik_data_chain.write[p_joint_idx].magnet_position = p_magnet_position; |
| 412 | } |
| 413 | |
| 414 | Vector2 SkeletonModification2DFABRIK::get_fabrik_joint_magnet_position(int p_joint_idx) const { |
| 415 | ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), Vector2(), "FABRIK joint out of range!"); |
| 416 | return fabrik_data_chain[p_joint_idx].magnet_position; |
| 417 | } |
| 418 | |
| 419 | void SkeletonModification2DFABRIK::set_fabrik_joint_use_target_rotation(int p_joint_idx, bool p_use_target_rotation) { |
| 420 | ERR_FAIL_INDEX_MSG(p_joint_idx, fabrik_data_chain.size(), "FABRIK joint out of range!"); |
| 421 | fabrik_data_chain.write[p_joint_idx].use_target_rotation = p_use_target_rotation; |
| 422 | } |
| 423 | |
| 424 | bool SkeletonModification2DFABRIK::get_fabrik_joint_use_target_rotation(int p_joint_idx) const { |
| 425 | ERR_FAIL_INDEX_V_MSG(p_joint_idx, fabrik_data_chain.size(), false, "FABRIK joint out of range!"); |
| 426 | return fabrik_data_chain[p_joint_idx].use_target_rotation; |
| 427 | } |
| 428 | |
| 429 | void SkeletonModification2DFABRIK::_bind_methods() { |
| 430 | ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DFABRIK::set_target_node); |
| 431 | ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DFABRIK::get_target_node); |
| 432 | |
| 433 | ClassDB::bind_method(D_METHOD("set_fabrik_data_chain_length", "length"), &SkeletonModification2DFABRIK::set_fabrik_data_chain_length); |
| 434 | ClassDB::bind_method(D_METHOD("get_fabrik_data_chain_length"), &SkeletonModification2DFABRIK::get_fabrik_data_chain_length); |
| 435 | |
| 436 | ClassDB::bind_method(D_METHOD("set_fabrik_joint_bone2d_node", "joint_idx", "bone2d_nodepath"), &SkeletonModification2DFABRIK::set_fabrik_joint_bone2d_node); |
| 437 | ClassDB::bind_method(D_METHOD("get_fabrik_joint_bone2d_node", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_bone2d_node); |
| 438 | ClassDB::bind_method(D_METHOD("set_fabrik_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DFABRIK::set_fabrik_joint_bone_index); |
| 439 | ClassDB::bind_method(D_METHOD("get_fabrik_joint_bone_index", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_bone_index); |
| 440 | ClassDB::bind_method(D_METHOD("set_fabrik_joint_magnet_position", "joint_idx", "magnet_position"), &SkeletonModification2DFABRIK::set_fabrik_joint_magnet_position); |
| 441 | ClassDB::bind_method(D_METHOD("get_fabrik_joint_magnet_position", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_magnet_position); |
| 442 | ClassDB::bind_method(D_METHOD("set_fabrik_joint_use_target_rotation", "joint_idx", "use_target_rotation"), &SkeletonModification2DFABRIK::set_fabrik_joint_use_target_rotation); |
| 443 | ClassDB::bind_method(D_METHOD("get_fabrik_joint_use_target_rotation", "joint_idx"), &SkeletonModification2DFABRIK::get_fabrik_joint_use_target_rotation); |
| 444 | |
| 445 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node"); |
| 446 | ADD_PROPERTY(PropertyInfo(Variant::INT, "fabrik_data_chain_length", PROPERTY_HINT_RANGE, "0, 100, 1"), "set_fabrik_data_chain_length", "get_fabrik_data_chain_length"); |
| 447 | } |
| 448 | |
| 449 | SkeletonModification2DFABRIK::SkeletonModification2DFABRIK() { |
| 450 | stack = nullptr; |
| 451 | is_setup = false; |
| 452 | enabled = true; |
| 453 | editor_draw_gizmo = false; |
| 454 | } |
| 455 | |
| 456 | SkeletonModification2DFABRIK::~SkeletonModification2DFABRIK() { |
| 457 | } |