| 1 | /**************************************************************************/ |
| 2 | /* skeleton_2d.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_2d.h" |
| 32 | |
| 33 | #include "core/math/transform_interpolator.h" |
| 34 | |
| 35 | #ifdef TOOLS_ENABLED |
| 36 | #include "editor/editor_data.h" |
| 37 | #include "editor/scene/canvas_item_editor_plugin.h" |
| 38 | #include "editor/settings/editor_settings.h" |
| 39 | #endif //TOOLS_ENABLED |
| 40 | |
| 41 | bool Bone2D::_set(const StringName &p_path, const Variant &p_value) { |
| 42 | if (p_path == SNAME("auto_calculate_length_and_angle")) { |
| 43 | set_autocalculate_length_and_angle(p_value); |
| 44 | } else if (p_path == SNAME("length")) { |
| 45 | set_length(p_value); |
| 46 | } else if (p_path == SNAME("bone_angle")) { |
| 47 | set_bone_angle(Math::deg_to_rad(real_t(p_value))); |
| 48 | } else if (p_path == SNAME("default_length")) { |
| 49 | set_length(p_value); |
| 50 | } |
| 51 | #ifdef TOOLS_ENABLED |
| 52 | else if (p_path == SNAME("editor_settings/show_bone_gizmo")) { |
| 53 | _editor_set_show_bone_gizmo(p_value); |
| 54 | } |
| 55 | #endif // TOOLS_ENABLED |
| 56 | else { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool Bone2D::_get(const StringName &p_path, Variant &r_ret) const { |
| 64 | if (p_path == SNAME("auto_calculate_length_and_angle")) { |
| 65 | r_ret = get_autocalculate_length_and_angle(); |
| 66 | } else if (p_path == SNAME("length")) { |
| 67 | r_ret = get_length(); |
| 68 | } else if (p_path == SNAME("bone_angle")) { |
| 69 | r_ret = Math::rad_to_deg(get_bone_angle()); |
| 70 | } else if (p_path == SNAME("default_length")) { |
| 71 | r_ret = get_length(); |
| 72 | } |
| 73 | #ifdef TOOLS_ENABLED |
| 74 | else if (p_path == SNAME("editor_settings/show_bone_gizmo")) { |
| 75 | r_ret = _editor_get_show_bone_gizmo(); |
| 76 | } |
| 77 | #endif // TOOLS_ENABLED |
| 78 | else { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void Bone2D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 86 | p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("auto_calculate_length_and_angle"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); |
| 87 | if (!autocalculate_length_and_angle) { |
| 88 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("length"), PROPERTY_HINT_RANGE, "1, 1024, 1", PROPERTY_USAGE_DEFAULT)); |
| 89 | p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("bone_angle"), PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT)); |
| 90 | } |
| 91 | |
| 92 | #ifdef TOOLS_ENABLED |
| 93 | p_list->push_back(PropertyInfo(Variant::BOOL, PNAME("editor_settings/show_bone_gizmo"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); |
| 94 | #endif // TOOLS_ENABLED |
| 95 | } |
| 96 | |
| 97 | void Bone2D::_notification(int p_what) { |
| 98 | switch (p_what) { |
| 99 | case NOTIFICATION_ENTER_TREE: { |
| 100 | Node *parent = get_parent(); |
| 101 | parent_bone = Object::cast_to<Bone2D>(parent); |
| 102 | skeleton = nullptr; |
| 103 | while (parent) { |
| 104 | skeleton = Object::cast_to<Skeleton2D>(parent); |
| 105 | if (skeleton) { |
| 106 | break; |
| 107 | } |
| 108 | if (!Object::cast_to<Bone2D>(parent)) { |
| 109 | break; //skeletons must be chained to Bone2Ds. |
| 110 | } |
| 111 | |
| 112 | parent = parent->get_parent(); |
| 113 | } |
| 114 | |
| 115 | if (skeleton) { |
| 116 | Skeleton2D::Bone bone; |
| 117 | bone.bone = this; |
| 118 | skeleton->bones.push_back(bone); |
| 119 | skeleton->_make_bone_setup_dirty(); |
| 120 | get_parent()->connect(SNAME("child_order_changed"), callable_mp(skeleton, &Skeleton2D::_make_bone_setup_dirty), CONNECT_REFERENCE_COUNTED); |
| 121 | } |
| 122 | |
| 123 | cache_transform = get_transform(); |
| 124 | copy_transform_to_cache = true; |
| 125 | |
| 126 | #ifdef TOOLS_ENABLED |
| 127 | // Only draw the gizmo in the editor! |
| 128 | if (Engine::get_singleton()->is_editor_hint() == false) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | queue_redraw(); |
| 133 | #endif // TOOLS_ENABLED |
| 134 | } break; |
| 135 | |
| 136 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
| 137 | if (skeleton) { |
| 138 | skeleton->_make_transform_dirty(); |
| 139 | } |
| 140 | if (copy_transform_to_cache) { |
| 141 | cache_transform = get_transform(); |
| 142 | } |
| 143 | #ifdef TOOLS_ENABLED |
| 144 | // Only draw the gizmo in the editor! |
| 145 | if (Engine::get_singleton()->is_editor_hint() == false) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | queue_redraw(); |
| 150 | |
| 151 | if (get_parent()) { |
| 152 | Bone2D *p_bone = Object::cast_to<Bone2D>(get_parent()); |
| 153 | if (p_bone) { |
| 154 | p_bone->queue_redraw(); |
| 155 | } |
| 156 | } |
| 157 | #endif // TOOLS_ENABLED |
| 158 | } break; |
| 159 | |
| 160 | case NOTIFICATION_EXIT_TREE: { |
| 161 | if (skeleton) { |
| 162 | for (uint32_t i = 0; i < skeleton->bones.size(); i++) { |
| 163 | if (skeleton->bones[i].bone == this) { |
| 164 | skeleton->bones.remove_at(i); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | skeleton->_make_bone_setup_dirty(); |
| 169 | get_parent()->disconnect(SNAME("child_order_changed"), callable_mp(skeleton, &Skeleton2D::_make_bone_setup_dirty)); |
| 170 | } |
| 171 | parent_bone = nullptr; |
| 172 | set_transform(cache_transform); |
| 173 | } break; |
| 174 | |
| 175 | case NOTIFICATION_READY: { |
| 176 | if (autocalculate_length_and_angle) { |
| 177 | calculate_length_and_rotation(); |
| 178 | } |
| 179 | } break; |
| 180 | |
| 181 | #ifdef TOOLS_ENABLED |
| 182 | case NOTIFICATION_EDITOR_PRE_SAVE: |
| 183 | case NOTIFICATION_EDITOR_POST_SAVE: { |
| 184 | Transform2D tmp_trans = get_transform(); |
| 185 | set_transform(cache_transform); |
| 186 | cache_transform = tmp_trans; |
| 187 | } break; |
| 188 | |
| 189 | // Bone2D Editor gizmo drawing. |
| 190 | // TODO: Bone2D gizmo drawing needs to be moved to an editor plugin. |
| 191 | case NOTIFICATION_DRAW: { |
| 192 | // Only draw the gizmo in the editor! |
| 193 | if (Engine::get_singleton()->is_editor_hint() == false) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | if (editor_gizmo_rid.is_null()) { |
| 198 | editor_gizmo_rid = RenderingServer::get_singleton()->canvas_item_create(); |
| 199 | RenderingServer::get_singleton()->canvas_item_set_parent(editor_gizmo_rid, get_canvas_item()); |
| 200 | RenderingServer::get_singleton()->canvas_item_set_z_as_relative_to_parent(editor_gizmo_rid, true); |
| 201 | RenderingServer::get_singleton()->canvas_item_set_z_index(editor_gizmo_rid, 10); |
| 202 | } |
| 203 | RenderingServer::get_singleton()->canvas_item_clear(editor_gizmo_rid); |
| 204 | |
| 205 | if (!_editor_show_bone_gizmo) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Undo scaling |
| 210 | Transform2D editor_gizmo_trans; |
| 211 | editor_gizmo_trans.set_scale(Vector2(1, 1) / get_global_scale()); |
| 212 | RenderingServer::get_singleton()->canvas_item_set_transform(editor_gizmo_rid, editor_gizmo_trans); |
| 213 | |
| 214 | Color bone_color1 = EDITOR_GET("editors/2d/bone_color1"); |
| 215 | Color bone_color2 = EDITOR_GET("editors/2d/bone_color2"); |
| 216 | Color bone_ik_color = EDITOR_GET("editors/2d/bone_ik_color"); |
| 217 | Color bone_outline_color = EDITOR_GET("editors/2d/bone_outline_color"); |
| 218 | Color bone_selected_color = EDITOR_GET("editors/2d/bone_selected_color"); |
| 219 | |
| 220 | bool Bone2D_found = false; |
| 221 | for (int i = 0; i < get_child_count(); i++) { |
| 222 | Bone2D *child_node = nullptr; |
| 223 | child_node = Object::cast_to<Bone2D>(get_child(i)); |
| 224 | if (!child_node) { |
| 225 | continue; |
| 226 | } |
| 227 | Bone2D_found = true; |
| 228 | |
| 229 | Vector<Vector2> bone_shape; |
| 230 | Vector<Vector2> bone_shape_outline; |
| 231 | |
| 232 | _editor_get_bone_shape(&bone_shape, &bone_shape_outline, child_node); |
| 233 | |
| 234 | Vector<Color> colors; |
| 235 | if (has_meta("_local_pose_override_enabled_")) { |
| 236 | colors.push_back(bone_ik_color); |
| 237 | colors.push_back(bone_ik_color); |
| 238 | colors.push_back(bone_ik_color); |
| 239 | colors.push_back(bone_ik_color); |
| 240 | } else { |
| 241 | colors.push_back(bone_color1); |
| 242 | colors.push_back(bone_color2); |
| 243 | colors.push_back(bone_color1); |
| 244 | colors.push_back(bone_color2); |
| 245 | } |
| 246 | |
| 247 | Vector<Color> outline_colors; |
| 248 | if (CanvasItemEditor::get_singleton()->editor_selection->is_selected(this)) { |
| 249 | outline_colors.push_back(bone_selected_color); |
| 250 | outline_colors.push_back(bone_selected_color); |
| 251 | outline_colors.push_back(bone_selected_color); |
| 252 | outline_colors.push_back(bone_selected_color); |
| 253 | outline_colors.push_back(bone_selected_color); |
| 254 | outline_colors.push_back(bone_selected_color); |
| 255 | } else { |
| 256 | outline_colors.push_back(bone_outline_color); |
| 257 | outline_colors.push_back(bone_outline_color); |
| 258 | outline_colors.push_back(bone_outline_color); |
| 259 | outline_colors.push_back(bone_outline_color); |
| 260 | outline_colors.push_back(bone_outline_color); |
| 261 | outline_colors.push_back(bone_outline_color); |
| 262 | } |
| 263 | |
| 264 | RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape_outline, outline_colors); |
| 265 | RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape, colors); |
| 266 | } |
| 267 | |
| 268 | if (!Bone2D_found) { |
| 269 | Vector<Vector2> bone_shape; |
| 270 | Vector<Vector2> bone_shape_outline; |
| 271 | |
| 272 | _editor_get_bone_shape(&bone_shape, &bone_shape_outline, nullptr); |
| 273 | |
| 274 | Vector<Color> colors; |
| 275 | if (has_meta("_local_pose_override_enabled_")) { |
| 276 | colors.push_back(bone_ik_color); |
| 277 | colors.push_back(bone_ik_color); |
| 278 | colors.push_back(bone_ik_color); |
| 279 | colors.push_back(bone_ik_color); |
| 280 | } else { |
| 281 | colors.push_back(bone_color1); |
| 282 | colors.push_back(bone_color2); |
| 283 | colors.push_back(bone_color1); |
| 284 | colors.push_back(bone_color2); |
| 285 | } |
| 286 | |
| 287 | Vector<Color> outline_colors; |
| 288 | if (CanvasItemEditor::get_singleton()->editor_selection->is_selected(this)) { |
| 289 | outline_colors.push_back(bone_selected_color); |
| 290 | outline_colors.push_back(bone_selected_color); |
| 291 | outline_colors.push_back(bone_selected_color); |
| 292 | outline_colors.push_back(bone_selected_color); |
| 293 | outline_colors.push_back(bone_selected_color); |
| 294 | outline_colors.push_back(bone_selected_color); |
| 295 | } else { |
| 296 | outline_colors.push_back(bone_outline_color); |
| 297 | outline_colors.push_back(bone_outline_color); |
| 298 | outline_colors.push_back(bone_outline_color); |
| 299 | outline_colors.push_back(bone_outline_color); |
| 300 | outline_colors.push_back(bone_outline_color); |
| 301 | outline_colors.push_back(bone_outline_color); |
| 302 | } |
| 303 | |
| 304 | RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape_outline, outline_colors); |
| 305 | RenderingServer::get_singleton()->canvas_item_add_polygon(editor_gizmo_rid, bone_shape, colors); |
| 306 | } |
| 307 | } break; |
| 308 | #endif // TOOLS_ENABLED |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | #ifdef TOOLS_ENABLED |
| 313 | bool Bone2D::_editor_get_bone_shape(Vector<Vector2> *p_shape, Vector<Vector2> *p_outline_shape, Bone2D *p_other_bone) { |
| 314 | float bone_width = EDITOR_GET("editors/2d/bone_width"); |
| 315 | float bone_outline_width = EDITOR_GET("editors/2d/bone_outline_size"); |
| 316 | |
| 317 | if (!is_inside_tree()) { |
| 318 | return false; //may have been removed |
| 319 | } |
| 320 | if (!p_other_bone && length <= 0) { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | Vector2 rel; |
| 325 | if (p_other_bone) { |
| 326 | rel = (p_other_bone->get_global_position() - get_global_position()); |
| 327 | rel = rel.rotated(-get_global_rotation()); // Undo Bone2D node's rotation so its drawn correctly regardless of the node's rotation |
| 328 | } else { |
| 329 | rel = Vector2(Math::cos(bone_angle), Math::sin(bone_angle)) * length * get_global_scale(); |
| 330 | } |
| 331 | |
| 332 | Vector2 relt = rel.rotated(Math::PI * 0.5).normalized() * bone_width; |
| 333 | Vector2 reln = rel.normalized(); |
| 334 | Vector2 reltn = relt.normalized(); |
| 335 | |
| 336 | if (p_shape) { |
| 337 | p_shape->clear(); |
| 338 | p_shape->push_back(Vector2(0, 0)); |
| 339 | p_shape->push_back(rel * 0.2 + relt); |
| 340 | p_shape->push_back(rel); |
| 341 | p_shape->push_back(rel * 0.2 - relt); |
| 342 | } |
| 343 | |
| 344 | if (p_outline_shape) { |
| 345 | p_outline_shape->clear(); |
| 346 | p_outline_shape->push_back((-reln - reltn) * bone_outline_width); |
| 347 | p_outline_shape->push_back((-reln + reltn) * bone_outline_width); |
| 348 | p_outline_shape->push_back(rel * 0.2 + relt + reltn * bone_outline_width); |
| 349 | p_outline_shape->push_back(rel + (reln + reltn) * bone_outline_width); |
| 350 | p_outline_shape->push_back(rel + (reln - reltn) * bone_outline_width); |
| 351 | p_outline_shape->push_back(rel * 0.2 - relt - reltn * bone_outline_width); |
| 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | void Bone2D::_editor_set_show_bone_gizmo(bool p_show_gizmo) { |
| 357 | _editor_show_bone_gizmo = p_show_gizmo; |
| 358 | queue_redraw(); |
| 359 | } |
| 360 | |
| 361 | bool Bone2D::_editor_get_show_bone_gizmo() const { |
| 362 | return _editor_show_bone_gizmo; |
| 363 | } |
| 364 | #endif // TOOLS_ENABLED |
| 365 | |
| 366 | void Bone2D::_bind_methods() { |
| 367 | ClassDB::bind_method(D_METHOD("set_rest", "rest"), &Bone2D::set_rest); |
| 368 | ClassDB::bind_method(D_METHOD("get_rest"), &Bone2D::get_rest); |
| 369 | ClassDB::bind_method(D_METHOD("apply_rest"), &Bone2D::apply_rest); |
| 370 | ClassDB::bind_method(D_METHOD("get_skeleton_rest"), &Bone2D::get_skeleton_rest); |
| 371 | ClassDB::bind_method(D_METHOD("get_index_in_skeleton"), &Bone2D::get_index_in_skeleton); |
| 372 | |
| 373 | ClassDB::bind_method(D_METHOD("set_autocalculate_length_and_angle", "auto_calculate"), &Bone2D::set_autocalculate_length_and_angle); |
| 374 | ClassDB::bind_method(D_METHOD("get_autocalculate_length_and_angle"), &Bone2D::get_autocalculate_length_and_angle); |
| 375 | ClassDB::bind_method(D_METHOD("set_length", "length"), &Bone2D::set_length); |
| 376 | ClassDB::bind_method(D_METHOD("get_length"), &Bone2D::get_length); |
| 377 | ClassDB::bind_method(D_METHOD("set_bone_angle", "angle"), &Bone2D::set_bone_angle); |
| 378 | ClassDB::bind_method(D_METHOD("get_bone_angle"), &Bone2D::get_bone_angle); |
| 379 | |
| 380 | ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "rest", PROPERTY_HINT_NONE, "suffix:px"), "set_rest", "get_rest"); |
| 381 | } |
| 382 | |
| 383 | void Bone2D::set_rest(const Transform2D &p_rest) { |
| 384 | rest = p_rest; |
| 385 | if (skeleton) { |
| 386 | skeleton->_make_bone_setup_dirty(); |
| 387 | } |
| 388 | |
| 389 | update_configuration_warnings(); |
| 390 | } |
| 391 | |
| 392 | Transform2D Bone2D::get_rest() const { |
| 393 | return rest; |
| 394 | } |
| 395 | |
| 396 | Transform2D Bone2D::get_skeleton_rest() const { |
| 397 | if (parent_bone) { |
| 398 | return parent_bone->get_skeleton_rest() * rest; |
| 399 | } else { |
| 400 | return rest; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void Bone2D::apply_rest() { |
| 405 | set_transform(rest); |
| 406 | } |
| 407 | |
| 408 | int Bone2D::get_index_in_skeleton() const { |
| 409 | ERR_FAIL_NULL_V(skeleton, -1); |
| 410 | skeleton->_update_bone_setup(); |
| 411 | return skeleton_index; |
| 412 | } |
| 413 | |
| 414 | PackedStringArray Bone2D::get_configuration_warnings() const { |
| 415 | PackedStringArray warnings = Node2D::get_configuration_warnings(); |
| 416 | if (!skeleton) { |
| 417 | if (parent_bone) { |
| 418 | warnings.push_back(RTR("This Bone2D chain should end at a Skeleton2D node.")); |
| 419 | } else { |
| 420 | warnings.push_back(RTR("A Bone2D only works with a Skeleton2D or another Bone2D as parent node.")); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | if (rest == Transform2D(0, 0, 0, 0, 0, 0)) { |
| 425 | warnings.push_back(RTR("This bone lacks a proper REST pose. Go to the Skeleton2D node and set one.")); |
| 426 | } |
| 427 | |
| 428 | return warnings; |
| 429 | } |
| 430 | |
| 431 | void Bone2D::calculate_length_and_rotation() { |
| 432 | // If there is at least a single child Bone2D node, we can calculate |
| 433 | // the length and direction. We will always just use the first Bone2D for this. |
| 434 | int child_count = get_child_count(); |
| 435 | Transform2D global_inv = get_global_transform().affine_inverse(); |
| 436 | |
| 437 | for (int i = 0; i < child_count; i++) { |
| 438 | Bone2D *child = Object::cast_to<Bone2D>(get_child(i)); |
| 439 | if (child) { |
| 440 | Vector2 child_local_pos = global_inv.xform(child->get_global_position()); |
| 441 | length = child_local_pos.length(); |
| 442 | bone_angle = child_local_pos.angle(); |
| 443 | return; // Finished! |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | WARN_PRINT("No Bone2D children of node " + get_name() + ". Cannot calculate bone length or angle reliably.\nUsing transform rotation for bone angle."); |
| 448 | bone_angle = get_transform().get_rotation(); |
| 449 | } |
| 450 | |
| 451 | void Bone2D::set_autocalculate_length_and_angle(bool p_autocalculate) { |
| 452 | autocalculate_length_and_angle = p_autocalculate; |
| 453 | if (autocalculate_length_and_angle) { |
| 454 | calculate_length_and_rotation(); |
| 455 | } |
| 456 | notify_property_list_changed(); |
| 457 | } |
| 458 | |
| 459 | bool Bone2D::get_autocalculate_length_and_angle() const { |
| 460 | return autocalculate_length_and_angle; |
| 461 | } |
| 462 | |
| 463 | void Bone2D::set_length(real_t p_length) { |
| 464 | length = p_length; |
| 465 | |
| 466 | #ifdef TOOLS_ENABLED |
| 467 | queue_redraw(); |
| 468 | #endif // TOOLS_ENABLED |
| 469 | } |
| 470 | |
| 471 | real_t Bone2D::get_length() const { |
| 472 | return length; |
| 473 | } |
| 474 | |
| 475 | void Bone2D::set_bone_angle(real_t p_angle) { |
| 476 | bone_angle = p_angle; |
| 477 | |
| 478 | #ifdef TOOLS_ENABLED |
| 479 | queue_redraw(); |
| 480 | #endif // TOOLS_ENABLED |
| 481 | } |
| 482 | |
| 483 | real_t Bone2D::get_bone_angle() const { |
| 484 | return bone_angle; |
| 485 | } |
| 486 | |
| 487 | Bone2D::Bone2D() { |
| 488 | skeleton = nullptr; |
| 489 | parent_bone = nullptr; |
| 490 | skeleton_index = -1; |
| 491 | length = 16; |
| 492 | bone_angle = 0; |
| 493 | autocalculate_length_and_angle = true; |
| 494 | set_notify_local_transform(true); |
| 495 | set_hide_clip_children(true); |
| 496 | //this is a clever hack so the bone knows no rest has been set yet, allowing to show an error. |
| 497 | for (int i = 0; i < 3; i++) { |
| 498 | rest[i] = Vector2(0, 0); |
| 499 | } |
| 500 | copy_transform_to_cache = true; |
| 501 | } |
| 502 | |
| 503 | Bone2D::~Bone2D() { |
| 504 | #ifdef TOOLS_ENABLED |
| 505 | if (!editor_gizmo_rid.is_null()) { |
| 506 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 507 | RenderingServer::get_singleton()->free_rid(editor_gizmo_rid); |
| 508 | } |
| 509 | #endif // TOOLS_ENABLED |
| 510 | } |
| 511 | |
| 512 | ////////////////////////////////////// |
| 513 | |
| 514 | bool Skeleton2D::_set(const StringName &p_path, const Variant &p_value) { |
| 515 | if (p_path == SNAME("modification_stack")) { |
| 516 | set_modification_stack(p_value); |
| 517 | return true; |
| 518 | } |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | bool Skeleton2D::_get(const StringName &p_path, Variant &r_ret) const { |
| 523 | if (p_path == SNAME("modification_stack")) { |
| 524 | r_ret = get_modification_stack(); |
| 525 | return true; |
| 526 | } |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | void Skeleton2D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 531 | p_list->push_back( |
| 532 | PropertyInfo(Variant::OBJECT, PNAME("modification_stack"), |
| 533 | PROPERTY_HINT_RESOURCE_TYPE, |
| 534 | "SkeletonModificationStack2D", |
| 535 | PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE)); |
| 536 | } |
| 537 | |
| 538 | void Skeleton2D::_make_bone_setup_dirty() { |
| 539 | if (bone_setup_dirty) { |
| 540 | return; |
| 541 | } |
| 542 | bone_setup_dirty = true; |
| 543 | if (is_inside_tree()) { |
| 544 | callable_mp(this, &Skeleton2D::_update_bone_setup).call_deferred(); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void Skeleton2D::_update_bone_setup() { |
| 549 | if (!bone_setup_dirty) { |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | bone_setup_dirty = false; |
| 554 | RS::get_singleton()->skeleton_allocate_data(skeleton, bones.size(), true); |
| 555 | |
| 556 | bones.sort(); //sorting so that they are always in the same order/index |
| 557 | |
| 558 | for (uint32_t i = 0; i < bones.size(); i++) { |
| 559 | bones[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose |
| 560 | bones[i].bone->skeleton_index = i; |
| 561 | Bone2D *parent_bone = Object::cast_to<Bone2D>(bones[i].bone->get_parent()); |
| 562 | if (parent_bone) { |
| 563 | bones[i].parent_index = parent_bone->skeleton_index; |
| 564 | } else { |
| 565 | bones[i].parent_index = -1; |
| 566 | } |
| 567 | |
| 568 | bones[i].local_pose_override = bones[i].bone->get_skeleton_rest(); |
| 569 | } |
| 570 | |
| 571 | transform_dirty = true; |
| 572 | _update_transform(); |
| 573 | emit_signal(SNAME("bone_setup_changed")); |
| 574 | } |
| 575 | |
| 576 | void Skeleton2D::_make_transform_dirty() { |
| 577 | if (transform_dirty) { |
| 578 | return; |
| 579 | } |
| 580 | transform_dirty = true; |
| 581 | if (is_inside_tree()) { |
| 582 | callable_mp(this, &Skeleton2D::_update_transform).call_deferred(); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void Skeleton2D::_update_transform() { |
| 587 | if (bone_setup_dirty) { |
| 588 | _update_bone_setup(); |
| 589 | return; //above will update transform anyway |
| 590 | } |
| 591 | if (!transform_dirty) { |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | transform_dirty = false; |
| 596 | |
| 597 | for (uint32_t i = 0; i < bones.size(); i++) { |
| 598 | ERR_CONTINUE(bones[i].parent_index >= (int)i); |
| 599 | if (bones[i].parent_index >= 0) { |
| 600 | bones[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform(); |
| 601 | } else { |
| 602 | bones[i].accum_transform = bones[i].bone->get_transform(); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | for (uint32_t i = 0; i < bones.size(); i++) { |
| 607 | Transform2D final_xform = bones[i].accum_transform * bones[i].rest_inverse; |
| 608 | RS::get_singleton()->skeleton_bone_set_transform_2d(skeleton, i, final_xform); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | int Skeleton2D::get_bone_count() const { |
| 613 | ERR_FAIL_COND_V(!is_inside_tree(), 0); |
| 614 | |
| 615 | if (bone_setup_dirty) { |
| 616 | // TODO: Is this necessary? It doesn't seem to change bones.size() |
| 617 | const_cast<Skeleton2D *>(this)->_update_bone_setup(); |
| 618 | } |
| 619 | |
| 620 | return bones.size(); |
| 621 | } |
| 622 | |
| 623 | Bone2D *Skeleton2D::get_bone(int p_idx) { |
| 624 | ERR_FAIL_COND_V(!is_inside_tree(), nullptr); |
| 625 | ERR_FAIL_INDEX_V(p_idx, (int)bones.size(), nullptr); |
| 626 | |
| 627 | return bones[p_idx].bone; |
| 628 | } |
| 629 | |
| 630 | void Skeleton2D::_update_process_mode() { |
| 631 | bool process = modification_stack.is_valid() && is_inside_tree(); |
| 632 | if (!process) { |
| 633 | // We might have another reason to process. |
| 634 | process = is_physics_interpolated_and_enabled() && is_visible_in_tree(); |
| 635 | } |
| 636 | |
| 637 | set_process_internal(process); |
| 638 | set_physics_process_internal(process); |
| 639 | } |
| 640 | |
| 641 | void Skeleton2D::_ensure_update_interpolation_data() { |
| 642 | uint64_t tick = Engine::get_singleton()->get_physics_frames(); |
| 643 | |
| 644 | if (_interpolation_data.last_update_physics_tick != tick) { |
| 645 | _interpolation_data.xform_prev = _interpolation_data.xform_curr; |
| 646 | _interpolation_data.last_update_physics_tick = tick; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | void Skeleton2D::_physics_interpolated_changed() { |
| 651 | _update_process_mode(); |
| 652 | } |
| 653 | |
| 654 | void Skeleton2D::_notification(int p_what) { |
| 655 | switch (p_what) { |
| 656 | case NOTIFICATION_READY: { |
| 657 | if (bone_setup_dirty) { |
| 658 | _update_bone_setup(); |
| 659 | } |
| 660 | if (transform_dirty) { |
| 661 | _update_transform(); |
| 662 | } |
| 663 | request_ready(); |
| 664 | } break; |
| 665 | |
| 666 | case NOTIFICATION_ENTER_TREE: { |
| 667 | _update_process_mode(); |
| 668 | |
| 669 | if (is_physics_interpolated_and_enabled()) { |
| 670 | _interpolation_data.xform_curr = get_global_transform(); |
| 671 | _interpolation_data.xform_prev = _interpolation_data.xform_curr; |
| 672 | } |
| 673 | } break; |
| 674 | |
| 675 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 676 | if (is_physics_interpolated_and_enabled()) { |
| 677 | _ensure_update_interpolation_data(); |
| 678 | if (Engine::get_singleton()->is_in_physics_frame()) { |
| 679 | _interpolation_data.xform_curr = get_global_transform(); |
| 680 | } |
| 681 | } else { |
| 682 | RS::get_singleton()->skeleton_set_base_transform_2d(skeleton, get_global_transform()); |
| 683 | } |
| 684 | } break; |
| 685 | |
| 686 | case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: { |
| 687 | _interpolation_data.xform_curr = get_global_transform(); |
| 688 | _interpolation_data.xform_prev = _interpolation_data.xform_curr; |
| 689 | } break; |
| 690 | |
| 691 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 692 | if (is_physics_interpolated_and_enabled()) { |
| 693 | Transform2D res; |
| 694 | TransformInterpolator::interpolate_transform_2d(_interpolation_data.xform_prev, _interpolation_data.xform_curr, res, Engine::get_singleton()->get_physics_interpolation_fraction()); |
| 695 | RS::get_singleton()->skeleton_set_base_transform_2d(skeleton, res); |
| 696 | } |
| 697 | if (modification_stack.is_valid()) { |
| 698 | execute_modifications(get_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process); |
| 699 | } |
| 700 | } break; |
| 701 | |
| 702 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { |
| 703 | if (is_physics_interpolated_and_enabled()) { |
| 704 | _ensure_update_interpolation_data(); |
| 705 | _interpolation_data.xform_curr = get_global_transform(); |
| 706 | } |
| 707 | if (modification_stack.is_valid()) { |
| 708 | execute_modifications(get_physics_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process); |
| 709 | } |
| 710 | } break; |
| 711 | |
| 712 | case NOTIFICATION_POST_ENTER_TREE: { |
| 713 | set_modification_stack(modification_stack); |
| 714 | } break; |
| 715 | |
| 716 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 717 | _update_process_mode(); |
| 718 | } break; |
| 719 | |
| 720 | #ifdef TOOLS_ENABLED |
| 721 | case NOTIFICATION_DRAW: { |
| 722 | if (Engine::get_singleton()->is_editor_hint()) { |
| 723 | if (modification_stack.is_valid()) { |
| 724 | modification_stack->draw_editor_gizmos(); |
| 725 | } |
| 726 | } |
| 727 | } break; |
| 728 | #endif // TOOLS_ENABLED |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | RID Skeleton2D::get_skeleton() const { |
| 733 | return skeleton; |
| 734 | } |
| 735 | |
| 736 | void Skeleton2D::set_bone_local_pose_override(int p_bone_idx, Transform2D p_override, real_t p_amount, bool p_persistent) { |
| 737 | ERR_FAIL_INDEX_MSG(p_bone_idx, (int)bones.size(), "Bone index is out of range!"); |
| 738 | bones[p_bone_idx].local_pose_override = p_override; |
| 739 | bones[p_bone_idx].local_pose_override_amount = p_amount; |
| 740 | bones[p_bone_idx].local_pose_override_persistent = p_persistent; |
| 741 | } |
| 742 | |
| 743 | Transform2D Skeleton2D::get_bone_local_pose_override(int p_bone_idx) { |
| 744 | ERR_FAIL_INDEX_V_MSG(p_bone_idx, (int)bones.size(), Transform2D(), "Bone index is out of range!"); |
| 745 | return bones[p_bone_idx].local_pose_override; |
| 746 | } |
| 747 | |
| 748 | void Skeleton2D::set_modification_stack(Ref<SkeletonModificationStack2D> p_stack) { |
| 749 | if (modification_stack.is_valid()) { |
| 750 | modification_stack->is_setup = false; |
| 751 | modification_stack->set_skeleton(nullptr); |
| 752 | } |
| 753 | modification_stack = p_stack; |
| 754 | if (modification_stack.is_valid() && is_inside_tree()) { |
| 755 | modification_stack->set_skeleton(this); |
| 756 | modification_stack->setup(); |
| 757 | |
| 758 | #ifdef TOOLS_ENABLED |
| 759 | modification_stack->set_editor_gizmos_dirty(true); |
| 760 | #endif // TOOLS_ENABLED |
| 761 | } |
| 762 | _update_process_mode(); |
| 763 | } |
| 764 | |
| 765 | Ref<SkeletonModificationStack2D> Skeleton2D::get_modification_stack() const { |
| 766 | return modification_stack; |
| 767 | } |
| 768 | |
| 769 | void Skeleton2D::execute_modifications(real_t p_delta, int p_execution_mode) { |
| 770 | if (modification_stack.is_null()) { |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | // Do not cache the transform changes caused by the modifications! |
| 775 | for (uint32_t i = 0; i < bones.size(); i++) { |
| 776 | bones[i].bone->copy_transform_to_cache = false; |
| 777 | } |
| 778 | |
| 779 | if (modification_stack->skeleton != this) { |
| 780 | modification_stack->set_skeleton(this); |
| 781 | } |
| 782 | |
| 783 | modification_stack->execute(p_delta, p_execution_mode); |
| 784 | |
| 785 | // Only apply the local pose override on _process. Otherwise, just calculate the local_pose_override and reset the transform. |
| 786 | if (p_execution_mode == SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process) { |
| 787 | for (uint32_t i = 0; i < bones.size(); i++) { |
| 788 | if (bones[i].local_pose_override_amount > 0) { |
| 789 | bones[i].bone->set_meta("_local_pose_override_enabled_", true); |
| 790 | |
| 791 | Transform2D final_trans = bones[i].bone->cache_transform; |
| 792 | final_trans = final_trans.interpolate_with(bones[i].local_pose_override, bones[i].local_pose_override_amount); |
| 793 | bones[i].bone->set_transform(final_trans); |
| 794 | bones[i].bone->propagate_call("force_update_transform"); |
| 795 | |
| 796 | if (bones[i].local_pose_override_persistent) { |
| 797 | bones[i].local_pose_override_amount = 0.0; |
| 798 | } |
| 799 | } else { |
| 800 | // TODO: see if there is a way to undo the override without having to resort to setting every bone's transform. |
| 801 | bones[i].bone->remove_meta("_local_pose_override_enabled_"); |
| 802 | bones[i].bone->set_transform(bones[i].bone->cache_transform); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // Cache any future transform changes |
| 808 | for (uint32_t i = 0; i < bones.size(); i++) { |
| 809 | bones[i].bone->copy_transform_to_cache = true; |
| 810 | } |
| 811 | |
| 812 | #ifdef TOOLS_ENABLED |
| 813 | modification_stack->set_editor_gizmos_dirty(true); |
| 814 | #endif // TOOLS_ENABLED |
| 815 | } |
| 816 | |
| 817 | void Skeleton2D::_bind_methods() { |
| 818 | ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton2D::get_bone_count); |
| 819 | ClassDB::bind_method(D_METHOD("get_bone", "idx"), &Skeleton2D::get_bone); |
| 820 | |
| 821 | ClassDB::bind_method(D_METHOD("get_skeleton"), &Skeleton2D::get_skeleton); |
| 822 | |
| 823 | ClassDB::bind_method(D_METHOD("set_modification_stack", "modification_stack"), &Skeleton2D::set_modification_stack); |
| 824 | ClassDB::bind_method(D_METHOD("get_modification_stack"), &Skeleton2D::get_modification_stack); |
| 825 | ClassDB::bind_method(D_METHOD("execute_modifications", "delta", "execution_mode"), &Skeleton2D::execute_modifications); |
| 826 | |
| 827 | ClassDB::bind_method(D_METHOD("set_bone_local_pose_override", "bone_idx", "override_pose", "strength", "persistent"), &Skeleton2D::set_bone_local_pose_override); |
| 828 | ClassDB::bind_method(D_METHOD("get_bone_local_pose_override", "bone_idx"), &Skeleton2D::get_bone_local_pose_override); |
| 829 | |
| 830 | ADD_SIGNAL(MethodInfo("bone_setup_changed")); |
| 831 | } |
| 832 | |
| 833 | Skeleton2D::Skeleton2D() { |
| 834 | skeleton = RS::get_singleton()->skeleton_create(); |
| 835 | set_notify_transform(true); |
| 836 | set_hide_clip_children(true); |
| 837 | } |
| 838 | |
| 839 | Skeleton2D::~Skeleton2D() { |
| 840 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 841 | RS::get_singleton()->free_rid(skeleton); |
| 842 | } |