skeleton_modification_2d_physicalbones.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "skeleton_modification_2d_physicalbones.h"
  31. #include "scene/2d/physical_bone_2d.h"
  32. #include "scene/2d/skeleton_2d.h"
  33. bool SkeletonModification2DPhysicalBones::_set(const StringName &p_path, const Variant &p_value) {
  34. String path = p_path;
  35. #ifdef TOOLS_ENABLED
  36. // Exposes a way to fetch the PhysicalBone2D nodes from the Godot editor.
  37. if (is_setup) {
  38. if (Engine::get_singleton()->is_editor_hint()) {
  39. if (path.begins_with("fetch_bones")) {
  40. fetch_physical_bones();
  41. notify_property_list_changed();
  42. return true;
  43. }
  44. }
  45. }
  46. #endif //TOOLS_ENABLED
  47. if (path.begins_with("joint_")) {
  48. int which = path.get_slicec('_', 1).to_int();
  49. String what = path.get_slicec('_', 2);
  50. ERR_FAIL_INDEX_V(which, physical_bone_chain.size(), false);
  51. if (what == "nodepath") {
  52. set_physical_bone_node(which, p_value);
  53. }
  54. return true;
  55. }
  56. return true;
  57. }
  58. bool SkeletonModification2DPhysicalBones::_get(const StringName &p_path, Variant &r_ret) const {
  59. String path = p_path;
  60. #ifdef TOOLS_ENABLED
  61. if (Engine::get_singleton()->is_editor_hint()) {
  62. if (path.begins_with("fetch_bones")) {
  63. return true; // Do nothing!
  64. }
  65. }
  66. #endif //TOOLS_ENABLED
  67. if (path.begins_with("joint_")) {
  68. int which = path.get_slicec('_', 1).to_int();
  69. String what = path.get_slicec('_', 2);
  70. ERR_FAIL_INDEX_V(which, physical_bone_chain.size(), false);
  71. if (what == "nodepath") {
  72. r_ret = get_physical_bone_node(which);
  73. }
  74. return true;
  75. }
  76. return true;
  77. }
  78. void SkeletonModification2DPhysicalBones::_get_property_list(List<PropertyInfo> *p_list) const {
  79. #ifdef TOOLS_ENABLED
  80. if (Engine::get_singleton()->is_editor_hint()) {
  81. p_list->push_back(PropertyInfo(Variant::BOOL, "fetch_bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  82. }
  83. #endif //TOOLS_ENABLED
  84. for (int i = 0; i < physical_bone_chain.size(); i++) {
  85. String base_string = "joint_" + itos(i) + "_";
  86. p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicalBone2D", PROPERTY_USAGE_DEFAULT));
  87. }
  88. }
  89. void SkeletonModification2DPhysicalBones::_execute(float p_delta) {
  90. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  91. "Modification is not setup and therefore cannot execute!");
  92. if (!enabled) {
  93. return;
  94. }
  95. if (_simulation_state_dirty) {
  96. _update_simulation_state();
  97. }
  98. for (int i = 0; i < physical_bone_chain.size(); i++) {
  99. PhysicalBone_Data2D bone_data = physical_bone_chain[i];
  100. if (bone_data.physical_bone_node_cache.is_null()) {
  101. WARN_PRINT_ONCE("PhysicalBone2D cache " + itos(i) + " is out of date. Attempting to update...");
  102. _physical_bone_update_cache(i);
  103. continue;
  104. }
  105. PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(ObjectDB::get_instance(bone_data.physical_bone_node_cache));
  106. if (!physical_bone) {
  107. ERR_PRINT_ONCE("PhysicalBone2D not found at index " + itos(i) + "!");
  108. return;
  109. }
  110. if (physical_bone->get_bone2d_index() < 0 || physical_bone->get_bone2d_index() > stack->skeleton->get_bone_count()) {
  111. ERR_PRINT_ONCE("PhysicalBone2D at index " + itos(i) + " has invalid Bone2D!");
  112. return;
  113. }
  114. Bone2D *bone_2d = stack->skeleton->get_bone(physical_bone->get_bone2d_index());
  115. if (physical_bone->get_simulate_physics() && !physical_bone->get_follow_bone_when_simulating()) {
  116. bone_2d->set_global_transform(physical_bone->get_global_transform());
  117. stack->skeleton->set_bone_local_pose_override(physical_bone->get_bone2d_index(), bone_2d->get_transform(), stack->strength, true);
  118. }
  119. }
  120. }
  121. void SkeletonModification2DPhysicalBones::_setup_modification(SkeletonModificationStack2D *p_stack) {
  122. stack = p_stack;
  123. if (stack) {
  124. is_setup = true;
  125. if (stack->skeleton) {
  126. for (int i = 0; i < physical_bone_chain.size(); i++) {
  127. _physical_bone_update_cache(i);
  128. }
  129. }
  130. }
  131. }
  132. void SkeletonModification2DPhysicalBones::_physical_bone_update_cache(int p_joint_idx) {
  133. ERR_FAIL_INDEX_MSG(p_joint_idx, physical_bone_chain.size(), "Cannot update PhysicalBone2D cache: joint index out of range!");
  134. if (!is_setup || !stack) {
  135. if (!stack) {
  136. ERR_PRINT_ONCE("Cannot update PhysicalBone2D cache: modification is not properly setup!");
  137. }
  138. return;
  139. }
  140. physical_bone_chain.write[p_joint_idx].physical_bone_node_cache = ObjectID();
  141. if (stack->skeleton) {
  142. if (stack->skeleton->is_inside_tree()) {
  143. if (stack->skeleton->has_node(physical_bone_chain[p_joint_idx].physical_bone_node)) {
  144. Node *node = stack->skeleton->get_node(physical_bone_chain[p_joint_idx].physical_bone_node);
  145. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  146. "Cannot update Physical Bone2D " + itos(p_joint_idx) + " cache: node is this modification's skeleton or cannot be found!");
  147. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  148. "Cannot update Physical Bone2D " + itos(p_joint_idx) + " cache: node is not in scene tree!");
  149. physical_bone_chain.write[p_joint_idx].physical_bone_node_cache = node->get_instance_id();
  150. }
  151. }
  152. }
  153. }
  154. int SkeletonModification2DPhysicalBones::get_physical_bone_chain_length() {
  155. return physical_bone_chain.size();
  156. }
  157. void SkeletonModification2DPhysicalBones::set_physical_bone_chain_length(int p_length) {
  158. ERR_FAIL_COND(p_length < 0);
  159. physical_bone_chain.resize(p_length);
  160. notify_property_list_changed();
  161. }
  162. void SkeletonModification2DPhysicalBones::fetch_physical_bones() {
  163. ERR_FAIL_COND_MSG(!stack, "No modification stack found! Cannot fetch physical bones!");
  164. ERR_FAIL_COND_MSG(!stack->skeleton, "No skeleton found! Cannot fetch physical bones!");
  165. physical_bone_chain.clear();
  166. List<Node *> node_queue = List<Node *>();
  167. node_queue.push_back(stack->skeleton);
  168. while (node_queue.size() > 0) {
  169. Node *node_to_process = node_queue[0];
  170. node_queue.pop_front();
  171. if (node_to_process != nullptr) {
  172. PhysicalBone2D *potential_bone = Object::cast_to<PhysicalBone2D>(node_to_process);
  173. if (potential_bone) {
  174. PhysicalBone_Data2D new_data = PhysicalBone_Data2D();
  175. new_data.physical_bone_node = stack->skeleton->get_path_to(potential_bone);
  176. new_data.physical_bone_node_cache = potential_bone->get_instance_id();
  177. physical_bone_chain.push_back(new_data);
  178. }
  179. for (int i = 0; i < node_to_process->get_child_count(); i++) {
  180. node_queue.push_back(node_to_process->get_child(i));
  181. }
  182. }
  183. }
  184. }
  185. void SkeletonModification2DPhysicalBones::start_simulation(const TypedArray<StringName> &p_bones) {
  186. _simulation_state_dirty = true;
  187. _simulation_state_dirty_names = p_bones;
  188. _simulation_state_dirty_process = true;
  189. if (is_setup) {
  190. _update_simulation_state();
  191. }
  192. }
  193. void SkeletonModification2DPhysicalBones::stop_simulation(const TypedArray<StringName> &p_bones) {
  194. _simulation_state_dirty = true;
  195. _simulation_state_dirty_names = p_bones;
  196. _simulation_state_dirty_process = false;
  197. if (is_setup) {
  198. _update_simulation_state();
  199. }
  200. }
  201. void SkeletonModification2DPhysicalBones::_update_simulation_state() {
  202. if (!_simulation_state_dirty) {
  203. return;
  204. }
  205. _simulation_state_dirty = false;
  206. if (_simulation_state_dirty_names.size() <= 0) {
  207. for (int i = 0; i < physical_bone_chain.size(); i++) {
  208. PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(stack->skeleton->get_node(physical_bone_chain[i].physical_bone_node));
  209. if (!physical_bone) {
  210. continue;
  211. }
  212. physical_bone->set_simulate_physics(_simulation_state_dirty_process);
  213. }
  214. } else {
  215. for (int i = 0; i < physical_bone_chain.size(); i++) {
  216. PhysicalBone2D *physical_bone = Object::cast_to<PhysicalBone2D>(ObjectDB::get_instance(physical_bone_chain[i].physical_bone_node_cache));
  217. if (!physical_bone) {
  218. continue;
  219. }
  220. if (_simulation_state_dirty_names.has(physical_bone->get_name())) {
  221. physical_bone->set_simulate_physics(_simulation_state_dirty_process);
  222. }
  223. }
  224. }
  225. }
  226. void SkeletonModification2DPhysicalBones::set_physical_bone_node(int p_joint_idx, const NodePath &p_nodepath) {
  227. ERR_FAIL_INDEX_MSG(p_joint_idx, physical_bone_chain.size(), "Joint index out of range!");
  228. physical_bone_chain.write[p_joint_idx].physical_bone_node = p_nodepath;
  229. _physical_bone_update_cache(p_joint_idx);
  230. }
  231. NodePath SkeletonModification2DPhysicalBones::get_physical_bone_node(int p_joint_idx) const {
  232. ERR_FAIL_INDEX_V_MSG(p_joint_idx, physical_bone_chain.size(), NodePath(), "Joint index out of range!");
  233. return physical_bone_chain[p_joint_idx].physical_bone_node;
  234. }
  235. void SkeletonModification2DPhysicalBones::_bind_methods() {
  236. ClassDB::bind_method(D_METHOD("set_physical_bone_chain_length", "length"), &SkeletonModification2DPhysicalBones::set_physical_bone_chain_length);
  237. ClassDB::bind_method(D_METHOD("get_physical_bone_chain_length"), &SkeletonModification2DPhysicalBones::get_physical_bone_chain_length);
  238. ClassDB::bind_method(D_METHOD("set_physical_bone_node", "joint_idx", "physicalbone2d_node"), &SkeletonModification2DPhysicalBones::set_physical_bone_node);
  239. ClassDB::bind_method(D_METHOD("get_physical_bone_node", "joint_idx"), &SkeletonModification2DPhysicalBones::get_physical_bone_node);
  240. ClassDB::bind_method(D_METHOD("fetch_physical_bones"), &SkeletonModification2DPhysicalBones::fetch_physical_bones);
  241. ClassDB::bind_method(D_METHOD("start_simulation", "bones"), &SkeletonModification2DPhysicalBones::start_simulation, DEFVAL(Array()));
  242. ClassDB::bind_method(D_METHOD("stop_simulation", "bones"), &SkeletonModification2DPhysicalBones::stop_simulation, DEFVAL(Array()));
  243. 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");
  244. }
  245. SkeletonModification2DPhysicalBones::SkeletonModification2DPhysicalBones() {
  246. stack = nullptr;
  247. is_setup = false;
  248. physical_bone_chain = Vector<PhysicalBone_Data2D>();
  249. enabled = true;
  250. editor_draw_gizmo = false; // Nothing to really show in a gizmo right now.
  251. }
  252. SkeletonModification2DPhysicalBones::~SkeletonModification2DPhysicalBones() {
  253. }