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