Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_2d_physicalbones.cpp
9904 views
/**************************************************************************/1/* skeleton_modification_2d_physicalbones.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "skeleton_modification_2d_physicalbones.h"31#include "scene/2d/physics/physical_bone_2d.h"32#include "scene/2d/skeleton_2d.h"3334bool SkeletonModification2DPhysicalBones::_set(const StringName &p_path, const Variant &p_value) {35String path = p_path;3637#ifdef TOOLS_ENABLED38// Exposes a way to fetch the PhysicalBone2D nodes from the Godot editor.39if (is_setup) {40if (Engine::get_singleton()->is_editor_hint()) {41if (path.begins_with("fetch_bones")) {42fetch_physical_bones();43notify_property_list_changed();44return true;45}46}47}48#endif //TOOLS_ENABLED4950if (path.begins_with("joint_")) {51int which = path.get_slicec('_', 1).to_int();52String what = path.get_slicec('_', 2);53ERR_FAIL_INDEX_V(which, physical_bone_chain.size(), false);5455if (what == "nodepath") {56set_physical_bone_node(which, p_value);57return true;58}59}60return false;61}6263bool SkeletonModification2DPhysicalBones::_get(const StringName &p_path, Variant &r_ret) const {64String path = p_path;6566#ifdef TOOLS_ENABLED67if (Engine::get_singleton()->is_editor_hint()) {68if (path.begins_with("fetch_bones")) {69// Do nothing!70return false;71}72}73#endif //TOOLS_ENABLED7475if (path.begins_with("joint_")) {76int which = path.get_slicec('_', 1).to_int();77String what = path.get_slicec('_', 2);78ERR_FAIL_INDEX_V(which, physical_bone_chain.size(), false);7980if (what == "nodepath") {81r_ret = get_physical_bone_node(which);82return true;83}84}85return false;86}8788void SkeletonModification2DPhysicalBones::_get_property_list(List<PropertyInfo> *p_list) const {89#ifdef TOOLS_ENABLED90if (Engine::get_singleton()->is_editor_hint()) {91p_list->push_back(PropertyInfo(Variant::BOOL, "fetch_bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));92}93#endif //TOOLS_ENABLED9495for (int i = 0; i < physical_bone_chain.size(); i++) {96String base_string = "joint_" + itos(i) + "_";9798p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicalBone2D", PROPERTY_USAGE_DEFAULT));99}100}101102void SkeletonModification2DPhysicalBones::_execute(float p_delta) {103ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,104"Modification is not setup and therefore cannot execute!");105if (!enabled) {106return;107}108109if (_simulation_state_dirty) {110_update_simulation_state();111}112113for (int i = 0; i < physical_bone_chain.size(); i++) {114PhysicalBone_Data2D bone_data = physical_bone_chain[i];115if (bone_data.physical_bone_node_cache.is_null()) {116WARN_PRINT_ONCE("PhysicalBone2D cache " + itos(i) + " is out of date. Attempting to update...");117_physical_bone_update_cache(i);118continue;119}120121PhysicalBone2D *physical_bone = ObjectDB::get_instance<PhysicalBone2D>(bone_data.physical_bone_node_cache);122if (!physical_bone) {123ERR_PRINT_ONCE("PhysicalBone2D not found at index " + itos(i) + "!");124return;125}126if (physical_bone->get_bone2d_index() < 0 || physical_bone->get_bone2d_index() > stack->skeleton->get_bone_count()) {127ERR_PRINT_ONCE("PhysicalBone2D at index " + itos(i) + " has invalid Bone2D!");128return;129}130Bone2D *bone_2d = stack->skeleton->get_bone(physical_bone->get_bone2d_index());131132if (physical_bone->get_simulate_physics() && !physical_bone->get_follow_bone_when_simulating()) {133bone_2d->set_global_transform(physical_bone->get_global_transform());134stack->skeleton->set_bone_local_pose_override(physical_bone->get_bone2d_index(), bone_2d->get_transform(), stack->strength, true);135}136}137}138139void SkeletonModification2DPhysicalBones::_setup_modification(SkeletonModificationStack2D *p_stack) {140stack = p_stack;141142if (stack) {143is_setup = true;144145if (stack->skeleton) {146for (int i = 0; i < physical_bone_chain.size(); i++) {147_physical_bone_update_cache(i);148}149}150}151}152153void SkeletonModification2DPhysicalBones::_physical_bone_update_cache(int p_joint_idx) {154ERR_FAIL_INDEX_MSG(p_joint_idx, physical_bone_chain.size(), "Cannot update PhysicalBone2D cache: joint index out of range!");155if (!is_setup || !stack) {156if (is_setup) {157ERR_PRINT_ONCE("Cannot update PhysicalBone2D cache: modification is not properly setup!");158}159return;160}161162physical_bone_chain.write[p_joint_idx].physical_bone_node_cache = ObjectID();163if (stack->skeleton) {164if (stack->skeleton->is_inside_tree()) {165if (stack->skeleton->has_node(physical_bone_chain[p_joint_idx].physical_bone_node)) {166Node *node = stack->skeleton->get_node(physical_bone_chain[p_joint_idx].physical_bone_node);167ERR_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!");169ERR_FAIL_COND_MSG(!node->is_inside_tree(),170"Cannot update Physical Bone2D " + itos(p_joint_idx) + " cache: node is not in scene tree!");171physical_bone_chain.write[p_joint_idx].physical_bone_node_cache = node->get_instance_id();172}173}174}175}176177int SkeletonModification2DPhysicalBones::get_physical_bone_chain_length() {178return physical_bone_chain.size();179}180181void SkeletonModification2DPhysicalBones::set_physical_bone_chain_length(int p_length) {182ERR_FAIL_COND(p_length < 0);183physical_bone_chain.resize(p_length);184notify_property_list_changed();185}186187void SkeletonModification2DPhysicalBones::fetch_physical_bones() {188ERR_FAIL_NULL_MSG(stack, "No modification stack found! Cannot fetch physical bones!");189ERR_FAIL_NULL_MSG(stack->skeleton, "No skeleton found! Cannot fetch physical bones!");190191physical_bone_chain.clear();192193List<Node *> node_queue = List<Node *>();194node_queue.push_back(stack->skeleton);195196while (node_queue.size() > 0) {197Node *node_to_process = node_queue.front()->get();198node_queue.pop_front();199200if (node_to_process != nullptr) {201PhysicalBone2D *potential_bone = Object::cast_to<PhysicalBone2D>(node_to_process);202if (potential_bone) {203PhysicalBone_Data2D new_data = PhysicalBone_Data2D();204new_data.physical_bone_node = stack->skeleton->get_path_to(potential_bone);205new_data.physical_bone_node_cache = potential_bone->get_instance_id();206physical_bone_chain.push_back(new_data);207}208for (int i = 0; i < node_to_process->get_child_count(); i++) {209node_queue.push_back(node_to_process->get_child(i));210}211}212}213}214215void 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;219220if (is_setup) {221_update_simulation_state();222}223}224225void 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;229230if (is_setup) {231_update_simulation_state();232}233}234235void SkeletonModification2DPhysicalBones::_update_simulation_state() {236if (!_simulation_state_dirty) {237return;238}239_simulation_state_dirty = false;240241if (_simulation_state_dirty_names.is_empty()) {242for (int i = 0; i < physical_bone_chain.size(); i++) {243PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(stack->skeleton->get_node(physical_bone_chain[i].physical_bone_node));244if (!physical_bone) {245continue;246}247248physical_bone->set_simulate_physics(_simulation_state_dirty_process);249}250} else {251for (int i = 0; i < physical_bone_chain.size(); i++) {252PhysicalBone2D *physical_bone = ObjectDB::get_instance<PhysicalBone2D>(physical_bone_chain[i].physical_bone_node_cache);253if (!physical_bone) {254continue;255}256if (_simulation_state_dirty_names.has(physical_bone->get_name())) {257physical_bone->set_simulate_physics(_simulation_state_dirty_process);258}259}260}261}262263void SkeletonModification2DPhysicalBones::set_physical_bone_node(int p_joint_idx, const NodePath &p_nodepath) {264ERR_FAIL_INDEX_MSG(p_joint_idx, physical_bone_chain.size(), "Joint index out of range!");265physical_bone_chain.write[p_joint_idx].physical_bone_node = p_nodepath;266_physical_bone_update_cache(p_joint_idx);267}268269NodePath SkeletonModification2DPhysicalBones::get_physical_bone_node(int p_joint_idx) const {270ERR_FAIL_INDEX_V_MSG(p_joint_idx, physical_bone_chain.size(), NodePath(), "Joint index out of range!");271return physical_bone_chain[p_joint_idx].physical_bone_node;272}273274void SkeletonModification2DPhysicalBones::_bind_methods() {275ClassDB::bind_method(D_METHOD("set_physical_bone_chain_length", "length"), &SkeletonModification2DPhysicalBones::set_physical_bone_chain_length);276ClassDB::bind_method(D_METHOD("get_physical_bone_chain_length"), &SkeletonModification2DPhysicalBones::get_physical_bone_chain_length);277278ClassDB::bind_method(D_METHOD("set_physical_bone_node", "joint_idx", "physicalbone2d_node"), &SkeletonModification2DPhysicalBones::set_physical_bone_node);279ClassDB::bind_method(D_METHOD("get_physical_bone_node", "joint_idx"), &SkeletonModification2DPhysicalBones::get_physical_bone_node);280281ClassDB::bind_method(D_METHOD("fetch_physical_bones"), &SkeletonModification2DPhysicalBones::fetch_physical_bones);282ClassDB::bind_method(D_METHOD("start_simulation", "bones"), &SkeletonModification2DPhysicalBones::start_simulation, DEFVAL(Array()));283ClassDB::bind_method(D_METHOD("stop_simulation", "bones"), &SkeletonModification2DPhysicalBones::stop_simulation, DEFVAL(Array()));284285ADD_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}287288SkeletonModification2DPhysicalBones::SkeletonModification2DPhysicalBones() {289stack = nullptr;290is_setup = false;291physical_bone_chain = Vector<PhysicalBone_Data2D>();292enabled = true;293editor_draw_gizmo = false; // Nothing to really show in a gizmo right now.294}295296SkeletonModification2DPhysicalBones::~SkeletonModification2DPhysicalBones() {297}298299300