skeleton_modification_2d_jiggle.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /**************************************************************************/
  2. /* skeleton_modification_2d_jiggle.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. #include "skeleton_modification_2d_jiggle.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. #include "scene/resources/world_2d.h"
  33. bool SkeletonModification2DJiggle::_set(const StringName &p_path, const Variant &p_value) {
  34. String path = p_path;
  35. if (path.begins_with("joint_data/")) {
  36. int which = path.get_slicec('/', 1).to_int();
  37. String what = path.get_slicec('/', 2);
  38. ERR_FAIL_INDEX_V(which, jiggle_data_chain.size(), false);
  39. if (what == "bone2d_node") {
  40. set_jiggle_joint_bone2d_node(which, p_value);
  41. } else if (what == "bone_index") {
  42. set_jiggle_joint_bone_index(which, p_value);
  43. } else if (what == "override_defaults") {
  44. set_jiggle_joint_override(which, p_value);
  45. } else if (what == "stiffness") {
  46. set_jiggle_joint_stiffness(which, p_value);
  47. } else if (what == "mass") {
  48. set_jiggle_joint_mass(which, p_value);
  49. } else if (what == "damping") {
  50. set_jiggle_joint_damping(which, p_value);
  51. } else if (what == "use_gravity") {
  52. set_jiggle_joint_use_gravity(which, p_value);
  53. } else if (what == "gravity") {
  54. set_jiggle_joint_gravity(which, p_value);
  55. } else {
  56. return false;
  57. }
  58. } else if (path == "use_colliders") {
  59. set_use_colliders(p_value);
  60. } else if (path == "collision_mask") {
  61. set_collision_mask(p_value);
  62. } else {
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool SkeletonModification2DJiggle::_get(const StringName &p_path, Variant &r_ret) const {
  68. String path = p_path;
  69. if (path.begins_with("joint_data/")) {
  70. int which = path.get_slicec('/', 1).to_int();
  71. String what = path.get_slicec('/', 2);
  72. ERR_FAIL_INDEX_V(which, jiggle_data_chain.size(), false);
  73. if (what == "bone2d_node") {
  74. r_ret = get_jiggle_joint_bone2d_node(which);
  75. } else if (what == "bone_index") {
  76. r_ret = get_jiggle_joint_bone_index(which);
  77. } else if (what == "override_defaults") {
  78. r_ret = get_jiggle_joint_override(which);
  79. } else if (what == "stiffness") {
  80. r_ret = get_jiggle_joint_stiffness(which);
  81. } else if (what == "mass") {
  82. r_ret = get_jiggle_joint_mass(which);
  83. } else if (what == "damping") {
  84. r_ret = get_jiggle_joint_damping(which);
  85. } else if (what == "use_gravity") {
  86. r_ret = get_jiggle_joint_use_gravity(which);
  87. } else if (what == "gravity") {
  88. r_ret = get_jiggle_joint_gravity(which);
  89. } else {
  90. return false;
  91. }
  92. } else if (path == "use_colliders") {
  93. r_ret = get_use_colliders();
  94. } else if (path == "collision_mask") {
  95. r_ret = get_collision_mask();
  96. } else {
  97. return false;
  98. }
  99. return true;
  100. }
  101. void SkeletonModification2DJiggle::_get_property_list(List<PropertyInfo> *p_list) const {
  102. p_list->push_back(PropertyInfo(Variant::BOOL, "use_colliders", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  103. if (use_colliders) {
  104. p_list->push_back(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS, "", PROPERTY_USAGE_DEFAULT));
  105. }
  106. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  107. String base_string = "joint_data/" + itos(i) + "/";
  108. p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  109. p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));
  110. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "override_defaults", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  111. if (jiggle_data_chain[i].override_defaults) {
  112. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "stiffness", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
  113. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "mass", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
  114. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01", PROPERTY_USAGE_DEFAULT));
  115. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  116. if (jiggle_data_chain[i].use_gravity) {
  117. p_list->push_back(PropertyInfo(Variant::VECTOR2, base_string + "gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  118. }
  119. }
  120. }
  121. }
  122. void SkeletonModification2DJiggle::_execute(float p_delta) {
  123. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  124. "Modification is not setup and therefore cannot execute!");
  125. if (!enabled) {
  126. return;
  127. }
  128. if (target_node_cache.is_null()) {
  129. WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
  130. update_target_cache();
  131. return;
  132. }
  133. Node2D *target = Object::cast_to<Node2D>(ObjectDB::get_instance(target_node_cache));
  134. if (!target || !target->is_inside_tree()) {
  135. ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
  136. return;
  137. }
  138. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  139. _execute_jiggle_joint(i, target, p_delta);
  140. }
  141. }
  142. void SkeletonModification2DJiggle::_execute_jiggle_joint(int p_joint_idx, Node2D *p_target, float p_delta) {
  143. // Adopted from: https://wiki.unity3d.com/index.php/JiggleBone
  144. // With modifications by TwistedTwigleg.
  145. if (jiggle_data_chain[p_joint_idx].bone_idx <= -1 || jiggle_data_chain[p_joint_idx].bone_idx > stack->skeleton->get_bone_count()) {
  146. ERR_PRINT_ONCE("Jiggle joint " + itos(p_joint_idx) + " bone index is invalid. Cannot execute modification on joint...");
  147. return;
  148. }
  149. if (jiggle_data_chain[p_joint_idx].bone2d_node_cache.is_null() && !jiggle_data_chain[p_joint_idx].bone2d_node.is_empty()) {
  150. WARN_PRINT_ONCE("Bone2D cache for joint " + itos(p_joint_idx) + " is out of date. Updating...");
  151. jiggle_joint_update_bone2d_cache(p_joint_idx);
  152. }
  153. Bone2D *operation_bone = stack->skeleton->get_bone(jiggle_data_chain[p_joint_idx].bone_idx);
  154. if (!operation_bone) {
  155. ERR_PRINT_ONCE("Jiggle joint " + itos(p_joint_idx) + " does not have a Bone2D node or it cannot be found!");
  156. return;
  157. }
  158. Transform2D operation_bone_trans = operation_bone->get_global_transform();
  159. Vector2 target_position = p_target->get_global_position();
  160. jiggle_data_chain.write[p_joint_idx].force = (target_position - jiggle_data_chain[p_joint_idx].dynamic_position) * jiggle_data_chain[p_joint_idx].stiffness * p_delta;
  161. if (jiggle_data_chain[p_joint_idx].use_gravity) {
  162. jiggle_data_chain.write[p_joint_idx].force += jiggle_data_chain[p_joint_idx].gravity * p_delta;
  163. }
  164. jiggle_data_chain.write[p_joint_idx].acceleration = jiggle_data_chain[p_joint_idx].force / jiggle_data_chain[p_joint_idx].mass;
  165. jiggle_data_chain.write[p_joint_idx].velocity += jiggle_data_chain[p_joint_idx].acceleration * (1 - jiggle_data_chain[p_joint_idx].damping);
  166. jiggle_data_chain.write[p_joint_idx].dynamic_position += jiggle_data_chain[p_joint_idx].velocity + jiggle_data_chain[p_joint_idx].force;
  167. jiggle_data_chain.write[p_joint_idx].dynamic_position += operation_bone_trans.get_origin() - jiggle_data_chain[p_joint_idx].last_position;
  168. jiggle_data_chain.write[p_joint_idx].last_position = operation_bone_trans.get_origin();
  169. // Collision detection/response
  170. if (use_colliders) {
  171. if (execution_mode == SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process) {
  172. Ref<World2D> world_2d = stack->skeleton->get_world_2d();
  173. ERR_FAIL_COND(world_2d.is_null());
  174. PhysicsDirectSpaceState2D *space_state = PhysicsServer2D::get_singleton()->space_get_direct_state(world_2d->get_space());
  175. PhysicsDirectSpaceState2D::RayResult ray_result;
  176. PhysicsDirectSpaceState2D::RayParameters ray_params;
  177. ray_params.from = operation_bone_trans.get_origin();
  178. ray_params.to = jiggle_data_chain[p_joint_idx].dynamic_position;
  179. ray_params.collision_mask = collision_mask;
  180. // Add exception support?
  181. bool ray_hit = space_state->intersect_ray(ray_params, ray_result);
  182. if (ray_hit) {
  183. jiggle_data_chain.write[p_joint_idx].dynamic_position = jiggle_data_chain[p_joint_idx].last_noncollision_position;
  184. jiggle_data_chain.write[p_joint_idx].acceleration = Vector2(0, 0);
  185. jiggle_data_chain.write[p_joint_idx].velocity = Vector2(0, 0);
  186. } else {
  187. jiggle_data_chain.write[p_joint_idx].last_noncollision_position = jiggle_data_chain[p_joint_idx].dynamic_position;
  188. }
  189. } else {
  190. WARN_PRINT_ONCE("Jiggle 2D modifier: You cannot detect colliders without the stack mode being set to _physics_process!");
  191. }
  192. }
  193. // Rotate the bone using the dynamic position!
  194. operation_bone_trans = operation_bone_trans.looking_at(jiggle_data_chain[p_joint_idx].dynamic_position);
  195. operation_bone_trans.set_rotation(operation_bone_trans.get_rotation() - operation_bone->get_bone_angle());
  196. // Reset scale
  197. operation_bone_trans.set_scale(operation_bone->get_global_scale());
  198. operation_bone->set_global_transform(operation_bone_trans);
  199. stack->skeleton->set_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx, operation_bone->get_transform(), stack->strength, true);
  200. }
  201. void SkeletonModification2DJiggle::_update_jiggle_joint_data() {
  202. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  203. if (!jiggle_data_chain[i].override_defaults) {
  204. set_jiggle_joint_stiffness(i, stiffness);
  205. set_jiggle_joint_mass(i, mass);
  206. set_jiggle_joint_damping(i, damping);
  207. set_jiggle_joint_use_gravity(i, use_gravity);
  208. set_jiggle_joint_gravity(i, gravity);
  209. }
  210. }
  211. }
  212. void SkeletonModification2DJiggle::_setup_modification(SkeletonModificationStack2D *p_stack) {
  213. stack = p_stack;
  214. if (stack) {
  215. is_setup = true;
  216. if (stack->skeleton) {
  217. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  218. int bone_idx = jiggle_data_chain[i].bone_idx;
  219. if (bone_idx > 0 && bone_idx < stack->skeleton->get_bone_count()) {
  220. Bone2D *bone2d_node = stack->skeleton->get_bone(bone_idx);
  221. jiggle_data_chain.write[i].dynamic_position = bone2d_node->get_global_position();
  222. }
  223. jiggle_joint_update_bone2d_cache(i);
  224. }
  225. }
  226. update_target_cache();
  227. }
  228. }
  229. void SkeletonModification2DJiggle::update_target_cache() {
  230. if (!is_setup || !stack) {
  231. if (is_setup) {
  232. ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
  233. }
  234. return;
  235. }
  236. target_node_cache = ObjectID();
  237. if (stack->skeleton) {
  238. if (stack->skeleton->is_inside_tree()) {
  239. if (stack->skeleton->has_node(target_node)) {
  240. Node *node = stack->skeleton->get_node(target_node);
  241. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  242. "Cannot update target cache: node is this modification's skeleton or cannot be found!");
  243. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  244. "Cannot update target cache: node is not in scene tree!");
  245. target_node_cache = node->get_instance_id();
  246. }
  247. }
  248. }
  249. }
  250. void SkeletonModification2DJiggle::jiggle_joint_update_bone2d_cache(int p_joint_idx) {
  251. ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");
  252. if (!is_setup || !stack) {
  253. if (is_setup) {
  254. ERR_PRINT_ONCE("Cannot update Jiggle " + itos(p_joint_idx) + " Bone2D cache: modification is not properly setup!");
  255. }
  256. return;
  257. }
  258. jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();
  259. if (stack->skeleton) {
  260. if (stack->skeleton->is_inside_tree()) {
  261. if (stack->skeleton->has_node(jiggle_data_chain[p_joint_idx].bone2d_node)) {
  262. Node *node = stack->skeleton->get_node(jiggle_data_chain[p_joint_idx].bone2d_node);
  263. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  264. "Cannot update Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");
  265. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  266. "Cannot update Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: node is not in scene tree!");
  267. jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();
  268. Bone2D *bone = Object::cast_to<Bone2D>(node);
  269. if (bone) {
  270. jiggle_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();
  271. } else {
  272. ERR_FAIL_MSG("Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
  273. }
  274. }
  275. }
  276. }
  277. }
  278. void SkeletonModification2DJiggle::set_target_node(const NodePath &p_target_node) {
  279. target_node = p_target_node;
  280. update_target_cache();
  281. }
  282. NodePath SkeletonModification2DJiggle::get_target_node() const {
  283. return target_node;
  284. }
  285. void SkeletonModification2DJiggle::set_stiffness(float p_stiffness) {
  286. ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
  287. stiffness = p_stiffness;
  288. _update_jiggle_joint_data();
  289. }
  290. float SkeletonModification2DJiggle::get_stiffness() const {
  291. return stiffness;
  292. }
  293. void SkeletonModification2DJiggle::set_mass(float p_mass) {
  294. ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
  295. mass = p_mass;
  296. _update_jiggle_joint_data();
  297. }
  298. float SkeletonModification2DJiggle::get_mass() const {
  299. return mass;
  300. }
  301. void SkeletonModification2DJiggle::set_damping(float p_damping) {
  302. ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
  303. ERR_FAIL_COND_MSG(p_damping > 1, "Damping cannot be more than one!");
  304. damping = p_damping;
  305. _update_jiggle_joint_data();
  306. }
  307. float SkeletonModification2DJiggle::get_damping() const {
  308. return damping;
  309. }
  310. void SkeletonModification2DJiggle::set_use_gravity(bool p_use_gravity) {
  311. use_gravity = p_use_gravity;
  312. _update_jiggle_joint_data();
  313. }
  314. bool SkeletonModification2DJiggle::get_use_gravity() const {
  315. return use_gravity;
  316. }
  317. void SkeletonModification2DJiggle::set_gravity(Vector2 p_gravity) {
  318. gravity = p_gravity;
  319. _update_jiggle_joint_data();
  320. }
  321. Vector2 SkeletonModification2DJiggle::get_gravity() const {
  322. return gravity;
  323. }
  324. void SkeletonModification2DJiggle::set_use_colliders(bool p_use_colliders) {
  325. use_colliders = p_use_colliders;
  326. notify_property_list_changed();
  327. }
  328. bool SkeletonModification2DJiggle::get_use_colliders() const {
  329. return use_colliders;
  330. }
  331. void SkeletonModification2DJiggle::set_collision_mask(int p_mask) {
  332. collision_mask = p_mask;
  333. }
  334. int SkeletonModification2DJiggle::get_collision_mask() const {
  335. return collision_mask;
  336. }
  337. // Jiggle joint data functions
  338. int SkeletonModification2DJiggle::get_jiggle_data_chain_length() {
  339. return jiggle_data_chain.size();
  340. }
  341. void SkeletonModification2DJiggle::set_jiggle_data_chain_length(int p_length) {
  342. ERR_FAIL_COND(p_length < 0);
  343. jiggle_data_chain.resize(p_length);
  344. notify_property_list_changed();
  345. }
  346. void SkeletonModification2DJiggle::set_jiggle_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {
  347. ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Jiggle joint out of range!");
  348. jiggle_data_chain.write[p_joint_idx].bone2d_node = p_target_node;
  349. jiggle_joint_update_bone2d_cache(p_joint_idx);
  350. notify_property_list_changed();
  351. }
  352. NodePath SkeletonModification2DJiggle::get_jiggle_joint_bone2d_node(int p_joint_idx) const {
  353. ERR_FAIL_INDEX_V_MSG(p_joint_idx, jiggle_data_chain.size(), NodePath(), "Jiggle joint out of range!");
  354. return jiggle_data_chain[p_joint_idx].bone2d_node;
  355. }
  356. void SkeletonModification2DJiggle::set_jiggle_joint_bone_index(int p_joint_idx, int p_bone_idx) {
  357. ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Jiggle joint out of range!");
  358. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  359. if (is_setup) {
  360. if (stack->skeleton) {
  361. ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
  362. jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  363. jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
  364. jiggle_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
  365. } else {
  366. WARN_PRINT("Cannot verify the Jiggle joint " + itos(p_joint_idx) + " bone index for this modification...");
  367. jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  368. }
  369. } else {
  370. jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  371. }
  372. notify_property_list_changed();
  373. }
  374. int SkeletonModification2DJiggle::get_jiggle_joint_bone_index(int p_joint_idx) const {
  375. ERR_FAIL_INDEX_V_MSG(p_joint_idx, jiggle_data_chain.size(), -1, "Jiggle joint out of range!");
  376. return jiggle_data_chain[p_joint_idx].bone_idx;
  377. }
  378. void SkeletonModification2DJiggle::set_jiggle_joint_override(int p_joint_idx, bool p_override) {
  379. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  380. jiggle_data_chain.write[p_joint_idx].override_defaults = p_override;
  381. _update_jiggle_joint_data();
  382. notify_property_list_changed();
  383. }
  384. bool SkeletonModification2DJiggle::get_jiggle_joint_override(int p_joint_idx) const {
  385. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), false);
  386. return jiggle_data_chain[p_joint_idx].override_defaults;
  387. }
  388. void SkeletonModification2DJiggle::set_jiggle_joint_stiffness(int p_joint_idx, float p_stiffness) {
  389. ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
  390. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  391. jiggle_data_chain.write[p_joint_idx].stiffness = p_stiffness;
  392. }
  393. float SkeletonModification2DJiggle::get_jiggle_joint_stiffness(int p_joint_idx) const {
  394. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
  395. return jiggle_data_chain[p_joint_idx].stiffness;
  396. }
  397. void SkeletonModification2DJiggle::set_jiggle_joint_mass(int p_joint_idx, float p_mass) {
  398. ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
  399. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  400. jiggle_data_chain.write[p_joint_idx].mass = p_mass;
  401. }
  402. float SkeletonModification2DJiggle::get_jiggle_joint_mass(int p_joint_idx) const {
  403. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
  404. return jiggle_data_chain[p_joint_idx].mass;
  405. }
  406. void SkeletonModification2DJiggle::set_jiggle_joint_damping(int p_joint_idx, float p_damping) {
  407. ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
  408. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  409. jiggle_data_chain.write[p_joint_idx].damping = p_damping;
  410. }
  411. float SkeletonModification2DJiggle::get_jiggle_joint_damping(int p_joint_idx) const {
  412. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
  413. return jiggle_data_chain[p_joint_idx].damping;
  414. }
  415. void SkeletonModification2DJiggle::set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity) {
  416. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  417. jiggle_data_chain.write[p_joint_idx].use_gravity = p_use_gravity;
  418. notify_property_list_changed();
  419. }
  420. bool SkeletonModification2DJiggle::get_jiggle_joint_use_gravity(int p_joint_idx) const {
  421. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), false);
  422. return jiggle_data_chain[p_joint_idx].use_gravity;
  423. }
  424. void SkeletonModification2DJiggle::set_jiggle_joint_gravity(int p_joint_idx, Vector2 p_gravity) {
  425. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  426. jiggle_data_chain.write[p_joint_idx].gravity = p_gravity;
  427. }
  428. Vector2 SkeletonModification2DJiggle::get_jiggle_joint_gravity(int p_joint_idx) const {
  429. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), Vector2(0, 0));
  430. return jiggle_data_chain[p_joint_idx].gravity;
  431. }
  432. void SkeletonModification2DJiggle::_bind_methods() {
  433. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DJiggle::set_target_node);
  434. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DJiggle::get_target_node);
  435. ClassDB::bind_method(D_METHOD("set_jiggle_data_chain_length", "length"), &SkeletonModification2DJiggle::set_jiggle_data_chain_length);
  436. ClassDB::bind_method(D_METHOD("get_jiggle_data_chain_length"), &SkeletonModification2DJiggle::get_jiggle_data_chain_length);
  437. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &SkeletonModification2DJiggle::set_stiffness);
  438. ClassDB::bind_method(D_METHOD("get_stiffness"), &SkeletonModification2DJiggle::get_stiffness);
  439. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &SkeletonModification2DJiggle::set_mass);
  440. ClassDB::bind_method(D_METHOD("get_mass"), &SkeletonModification2DJiggle::get_mass);
  441. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &SkeletonModification2DJiggle::set_damping);
  442. ClassDB::bind_method(D_METHOD("get_damping"), &SkeletonModification2DJiggle::get_damping);
  443. ClassDB::bind_method(D_METHOD("set_use_gravity", "use_gravity"), &SkeletonModification2DJiggle::set_use_gravity);
  444. ClassDB::bind_method(D_METHOD("get_use_gravity"), &SkeletonModification2DJiggle::get_use_gravity);
  445. ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &SkeletonModification2DJiggle::set_gravity);
  446. ClassDB::bind_method(D_METHOD("get_gravity"), &SkeletonModification2DJiggle::get_gravity);
  447. ClassDB::bind_method(D_METHOD("set_use_colliders", "use_colliders"), &SkeletonModification2DJiggle::set_use_colliders);
  448. ClassDB::bind_method(D_METHOD("get_use_colliders"), &SkeletonModification2DJiggle::get_use_colliders);
  449. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &SkeletonModification2DJiggle::set_collision_mask);
  450. ClassDB::bind_method(D_METHOD("get_collision_mask"), &SkeletonModification2DJiggle::get_collision_mask);
  451. // Jiggle joint data functions
  452. ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone2d_node", "joint_idx", "bone2d_node"), &SkeletonModification2DJiggle::set_jiggle_joint_bone2d_node);
  453. ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone2d_node", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_bone2d_node);
  454. ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DJiggle::set_jiggle_joint_bone_index);
  455. ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_index", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_bone_index);
  456. ClassDB::bind_method(D_METHOD("set_jiggle_joint_override", "joint_idx", "override"), &SkeletonModification2DJiggle::set_jiggle_joint_override);
  457. ClassDB::bind_method(D_METHOD("get_jiggle_joint_override", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_override);
  458. ClassDB::bind_method(D_METHOD("set_jiggle_joint_stiffness", "joint_idx", "stiffness"), &SkeletonModification2DJiggle::set_jiggle_joint_stiffness);
  459. ClassDB::bind_method(D_METHOD("get_jiggle_joint_stiffness", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_stiffness);
  460. ClassDB::bind_method(D_METHOD("set_jiggle_joint_mass", "joint_idx", "mass"), &SkeletonModification2DJiggle::set_jiggle_joint_mass);
  461. ClassDB::bind_method(D_METHOD("get_jiggle_joint_mass", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_mass);
  462. ClassDB::bind_method(D_METHOD("set_jiggle_joint_damping", "joint_idx", "damping"), &SkeletonModification2DJiggle::set_jiggle_joint_damping);
  463. ClassDB::bind_method(D_METHOD("get_jiggle_joint_damping", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_damping);
  464. ClassDB::bind_method(D_METHOD("set_jiggle_joint_use_gravity", "joint_idx", "use_gravity"), &SkeletonModification2DJiggle::set_jiggle_joint_use_gravity);
  465. ClassDB::bind_method(D_METHOD("get_jiggle_joint_use_gravity", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_use_gravity);
  466. ClassDB::bind_method(D_METHOD("set_jiggle_joint_gravity", "joint_idx", "gravity"), &SkeletonModification2DJiggle::set_jiggle_joint_gravity);
  467. ClassDB::bind_method(D_METHOD("get_jiggle_joint_gravity", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_gravity);
  468. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
  469. ADD_PROPERTY(PropertyInfo(Variant::INT, "jiggle_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_jiggle_data_chain_length", "get_jiggle_data_chain_length");
  470. ADD_GROUP("Default Joint Settings", "");
  471. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness"), "set_stiffness", "get_stiffness");
  472. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
  473. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01"), "set_damping", "get_damping");
  474. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_gravity"), "set_use_gravity", "get_use_gravity");
  475. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity"), "set_gravity", "get_gravity");
  476. ADD_GROUP("", "");
  477. }
  478. SkeletonModification2DJiggle::SkeletonModification2DJiggle() {
  479. stack = nullptr;
  480. is_setup = false;
  481. jiggle_data_chain = Vector<Jiggle_Joint_Data2D>();
  482. stiffness = 3;
  483. mass = 0.75;
  484. damping = 0.75;
  485. use_gravity = false;
  486. gravity = Vector2(0, 6.0);
  487. enabled = true;
  488. editor_draw_gizmo = false; // Nothing to really show in a gizmo right now.
  489. }
  490. SkeletonModification2DJiggle::~SkeletonModification2DJiggle() {
  491. }