skeleton_modification_2d_ccdik.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*************************************************************************/
  2. /* skeleton_modification_2d_ccdik.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_ccdik.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_settings.h"
  34. #endif // TOOLS_ENABLED
  35. bool SkeletonModification2DCCDIK::_set(const StringName &p_path, const Variant &p_value) {
  36. String path = p_path;
  37. if (path.begins_with("joint_data/")) {
  38. int which = path.get_slicec('/', 1).to_int();
  39. String what = path.get_slicec('/', 2);
  40. ERR_FAIL_INDEX_V(which, ccdik_data_chain.size(), false);
  41. if (what == "bone2d_node") {
  42. set_ccdik_joint_bone2d_node(which, p_value);
  43. } else if (what == "bone_index") {
  44. set_ccdik_joint_bone_index(which, p_value);
  45. } else if (what == "rotate_from_joint") {
  46. set_ccdik_joint_rotate_from_joint(which, p_value);
  47. } else if (what == "enable_constraint") {
  48. set_ccdik_joint_enable_constraint(which, p_value);
  49. } else if (what == "constraint_angle_min") {
  50. set_ccdik_joint_constraint_angle_min(which, Math::deg_to_rad(float(p_value)));
  51. } else if (what == "constraint_angle_max") {
  52. set_ccdik_joint_constraint_angle_max(which, Math::deg_to_rad(float(p_value)));
  53. } else if (what == "constraint_angle_invert") {
  54. set_ccdik_joint_constraint_angle_invert(which, p_value);
  55. } else if (what == "constraint_in_localspace") {
  56. set_ccdik_joint_constraint_in_localspace(which, p_value);
  57. }
  58. #ifdef TOOLS_ENABLED
  59. if (what.begins_with("editor_draw_gizmo")) {
  60. set_ccdik_joint_editor_draw_gizmo(which, p_value);
  61. }
  62. #endif // TOOLS_ENABLED
  63. return true;
  64. }
  65. #ifdef TOOLS_ENABLED
  66. if (path.begins_with("editor/draw_gizmo")) {
  67. set_editor_draw_gizmo(p_value);
  68. }
  69. #endif // TOOLS_ENABLED
  70. return true;
  71. }
  72. bool SkeletonModification2DCCDIK::_get(const StringName &p_path, Variant &r_ret) const {
  73. String path = p_path;
  74. if (path.begins_with("joint_data/")) {
  75. int which = path.get_slicec('/', 1).to_int();
  76. String what = path.get_slicec('/', 2);
  77. ERR_FAIL_INDEX_V(which, ccdik_data_chain.size(), false);
  78. if (what == "bone2d_node") {
  79. r_ret = get_ccdik_joint_bone2d_node(which);
  80. } else if (what == "bone_index") {
  81. r_ret = get_ccdik_joint_bone_index(which);
  82. } else if (what == "rotate_from_joint") {
  83. r_ret = get_ccdik_joint_rotate_from_joint(which);
  84. } else if (what == "enable_constraint") {
  85. r_ret = get_ccdik_joint_enable_constraint(which);
  86. } else if (what == "constraint_angle_min") {
  87. r_ret = Math::rad_to_deg(get_ccdik_joint_constraint_angle_min(which));
  88. } else if (what == "constraint_angle_max") {
  89. r_ret = Math::rad_to_deg(get_ccdik_joint_constraint_angle_max(which));
  90. } else if (what == "constraint_angle_invert") {
  91. r_ret = get_ccdik_joint_constraint_angle_invert(which);
  92. } else if (what == "constraint_in_localspace") {
  93. r_ret = get_ccdik_joint_constraint_in_localspace(which);
  94. }
  95. #ifdef TOOLS_ENABLED
  96. if (what.begins_with("editor_draw_gizmo")) {
  97. r_ret = get_ccdik_joint_editor_draw_gizmo(which);
  98. }
  99. #endif // TOOLS_ENABLED
  100. return true;
  101. }
  102. #ifdef TOOLS_ENABLED
  103. if (path.begins_with("editor/draw_gizmo")) {
  104. r_ret = get_editor_draw_gizmo();
  105. }
  106. #endif // TOOLS_ENABLED
  107. return true;
  108. }
  109. void SkeletonModification2DCCDIK::_get_property_list(List<PropertyInfo> *p_list) const {
  110. for (int i = 0; i < ccdik_data_chain.size(); i++) {
  111. String base_string = "joint_data/" + itos(i) + "/";
  112. p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  113. p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));
  114. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "rotate_from_joint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  115. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "enable_constraint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  116. if (ccdik_data_chain[i].enable_constraint) {
  117. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "constraint_angle_min", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
  118. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "constraint_angle_max", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
  119. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "constraint_angle_invert", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  120. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "constraint_in_localspace", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  121. }
  122. #ifdef TOOLS_ENABLED
  123. if (Engine::get_singleton()->is_editor_hint()) {
  124. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "editor_draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  125. }
  126. #endif // TOOLS_ENABLED
  127. }
  128. #ifdef TOOLS_ENABLED
  129. if (Engine::get_singleton()->is_editor_hint()) {
  130. p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  131. }
  132. #endif // TOOLS_ENABLED
  133. }
  134. void SkeletonModification2DCCDIK::_execute(float p_delta) {
  135. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  136. "Modification is not setup and therefore cannot execute!");
  137. if (!enabled) {
  138. return;
  139. }
  140. if (target_node_cache.is_null()) {
  141. WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
  142. update_target_cache();
  143. return;
  144. }
  145. if (tip_node_cache.is_null()) {
  146. WARN_PRINT_ONCE("Tip cache is out of date. Attempting to update...");
  147. update_tip_cache();
  148. return;
  149. }
  150. Node2D *target = Object::cast_to<Node2D>(ObjectDB::get_instance(target_node_cache));
  151. if (!target || !target->is_inside_tree()) {
  152. ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
  153. return;
  154. }
  155. Node2D *tip = Object::cast_to<Node2D>(ObjectDB::get_instance(tip_node_cache));
  156. if (!tip || !tip->is_inside_tree()) {
  157. ERR_PRINT_ONCE("Tip node is not in the scene tree. Cannot execute modification!");
  158. return;
  159. }
  160. for (int i = 0; i < ccdik_data_chain.size(); i++) {
  161. _execute_ccdik_joint(i, target, tip);
  162. }
  163. }
  164. void SkeletonModification2DCCDIK::_execute_ccdik_joint(int p_joint_idx, Node2D *p_target, Node2D *p_tip) {
  165. CCDIK_Joint_Data2D ccdik_data = ccdik_data_chain[p_joint_idx];
  166. if (ccdik_data.bone_idx < 0 || ccdik_data.bone_idx > stack->skeleton->get_bone_count()) {
  167. ERR_PRINT_ONCE("2D CCDIK joint: bone index not found!");
  168. return;
  169. }
  170. Bone2D *operation_bone = stack->skeleton->get_bone(ccdik_data.bone_idx);
  171. Transform2D operation_transform = operation_bone->get_global_transform();
  172. if (ccdik_data.rotate_from_joint) {
  173. // To rotate from the joint, simply look at the target!
  174. operation_transform.set_rotation(
  175. operation_transform.looking_at(p_target->get_global_position()).get_rotation() - operation_bone->get_bone_angle());
  176. } else {
  177. // How to rotate from the tip: get the difference of rotation needed from the tip to the target, from the perspective of the joint.
  178. // Because we are only using the offset, we do not need to account for the bone angle of the Bone2D node.
  179. float joint_to_tip = p_tip->get_global_position().angle_to_point(operation_transform.get_origin());
  180. float joint_to_target = p_target->get_global_position().angle_to_point(operation_transform.get_origin());
  181. operation_transform.set_rotation(
  182. operation_transform.get_rotation() + (joint_to_target - joint_to_tip));
  183. }
  184. // Reset scale
  185. operation_transform.set_scale(operation_bone->get_global_scale());
  186. // Apply constraints in globalspace:
  187. if (ccdik_data.enable_constraint && !ccdik_data.constraint_in_localspace) {
  188. operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), ccdik_data.constraint_angle_min, ccdik_data.constraint_angle_max, ccdik_data.constraint_angle_invert));
  189. }
  190. // Convert from a global transform to a delta and then apply the delta to the local transform.
  191. operation_bone->set_global_transform(operation_transform);
  192. operation_transform = operation_bone->get_transform();
  193. // Apply constraints in localspace:
  194. if (ccdik_data.enable_constraint && ccdik_data.constraint_in_localspace) {
  195. operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), ccdik_data.constraint_angle_min, ccdik_data.constraint_angle_max, ccdik_data.constraint_angle_invert));
  196. }
  197. // Set the local pose override, and to make sure child bones are also updated, set the transform of the bone.
  198. stack->skeleton->set_bone_local_pose_override(ccdik_data.bone_idx, operation_transform, stack->strength, true);
  199. operation_bone->set_transform(operation_transform);
  200. operation_bone->notification(operation_bone->NOTIFICATION_TRANSFORM_CHANGED);
  201. }
  202. void SkeletonModification2DCCDIK::_setup_modification(SkeletonModificationStack2D *p_stack) {
  203. stack = p_stack;
  204. if (stack != nullptr) {
  205. is_setup = true;
  206. update_target_cache();
  207. update_tip_cache();
  208. }
  209. }
  210. void SkeletonModification2DCCDIK::_draw_editor_gizmo() {
  211. if (!enabled || !is_setup) {
  212. return;
  213. }
  214. for (int i = 0; i < ccdik_data_chain.size(); i++) {
  215. if (!ccdik_data_chain[i].editor_draw_gizmo) {
  216. continue;
  217. }
  218. Bone2D *operation_bone = stack->skeleton->get_bone(ccdik_data_chain[i].bone_idx);
  219. editor_draw_angle_constraints(operation_bone, ccdik_data_chain[i].constraint_angle_min, ccdik_data_chain[i].constraint_angle_max,
  220. ccdik_data_chain[i].enable_constraint, ccdik_data_chain[i].constraint_in_localspace, ccdik_data_chain[i].constraint_angle_invert);
  221. }
  222. }
  223. void SkeletonModification2DCCDIK::update_target_cache() {
  224. if (!is_setup || !stack) {
  225. ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
  226. return;
  227. }
  228. target_node_cache = ObjectID();
  229. if (stack->skeleton) {
  230. if (stack->skeleton->is_inside_tree()) {
  231. if (stack->skeleton->has_node(target_node)) {
  232. Node *node = stack->skeleton->get_node(target_node);
  233. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  234. "Cannot update target cache: node is this modification's skeleton or cannot be found!");
  235. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  236. "Cannot update target cache: node is not in the scene tree!");
  237. target_node_cache = node->get_instance_id();
  238. }
  239. }
  240. }
  241. }
  242. void SkeletonModification2DCCDIK::update_tip_cache() {
  243. if (!is_setup || !stack) {
  244. ERR_PRINT_ONCE("Cannot update tip cache: modification is not properly setup!");
  245. return;
  246. }
  247. tip_node_cache = ObjectID();
  248. if (stack->skeleton) {
  249. if (stack->skeleton->is_inside_tree()) {
  250. if (stack->skeleton->has_node(tip_node)) {
  251. Node *node = stack->skeleton->get_node(tip_node);
  252. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  253. "Cannot update tip cache: node is this modification's skeleton or cannot be found!");
  254. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  255. "Cannot update tip cache: node is not in the scene tree!");
  256. tip_node_cache = node->get_instance_id();
  257. }
  258. }
  259. }
  260. }
  261. void SkeletonModification2DCCDIK::ccdik_joint_update_bone2d_cache(int p_joint_idx) {
  262. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");
  263. if (!is_setup || !stack) {
  264. ERR_PRINT_ONCE("Cannot update CCDIK Bone2D cache: modification is not properly setup!");
  265. return;
  266. }
  267. ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();
  268. if (stack->skeleton) {
  269. if (stack->skeleton->is_inside_tree()) {
  270. if (stack->skeleton->has_node(ccdik_data_chain[p_joint_idx].bone2d_node)) {
  271. Node *node = stack->skeleton->get_node(ccdik_data_chain[p_joint_idx].bone2d_node);
  272. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  273. "Cannot update CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");
  274. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  275. "Cannot update CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: node is not in the scene tree!");
  276. ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();
  277. Bone2D *bone = Object::cast_to<Bone2D>(node);
  278. if (bone) {
  279. ccdik_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();
  280. } else {
  281. ERR_FAIL_MSG("CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
  282. }
  283. }
  284. }
  285. }
  286. }
  287. void SkeletonModification2DCCDIK::set_target_node(const NodePath &p_target_node) {
  288. target_node = p_target_node;
  289. update_target_cache();
  290. }
  291. NodePath SkeletonModification2DCCDIK::get_target_node() const {
  292. return target_node;
  293. }
  294. void SkeletonModification2DCCDIK::set_tip_node(const NodePath &p_tip_node) {
  295. tip_node = p_tip_node;
  296. update_tip_cache();
  297. }
  298. NodePath SkeletonModification2DCCDIK::get_tip_node() const {
  299. return tip_node;
  300. }
  301. void SkeletonModification2DCCDIK::set_ccdik_data_chain_length(int p_length) {
  302. ccdik_data_chain.resize(p_length);
  303. notify_property_list_changed();
  304. }
  305. int SkeletonModification2DCCDIK::get_ccdik_data_chain_length() {
  306. return ccdik_data_chain.size();
  307. }
  308. void SkeletonModification2DCCDIK::set_ccdik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {
  309. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  310. ccdik_data_chain.write[p_joint_idx].bone2d_node = p_target_node;
  311. ccdik_joint_update_bone2d_cache(p_joint_idx);
  312. notify_property_list_changed();
  313. }
  314. NodePath SkeletonModification2DCCDIK::get_ccdik_joint_bone2d_node(int p_joint_idx) const {
  315. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), NodePath(), "CCDIK joint out of range!");
  316. return ccdik_data_chain[p_joint_idx].bone2d_node;
  317. }
  318. void SkeletonModification2DCCDIK::set_ccdik_joint_bone_index(int p_joint_idx, int p_bone_idx) {
  319. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCCDIK joint out of range!");
  320. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  321. if (is_setup) {
  322. if (stack->skeleton) {
  323. ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
  324. ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  325. ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
  326. ccdik_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
  327. } else {
  328. WARN_PRINT("Cannot verify the CCDIK joint " + itos(p_joint_idx) + " bone index for this modification...");
  329. ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  330. }
  331. } else {
  332. WARN_PRINT("Cannot verify the CCDIK joint " + itos(p_joint_idx) + " bone index for this modification...");
  333. ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  334. }
  335. notify_property_list_changed();
  336. }
  337. int SkeletonModification2DCCDIK::get_ccdik_joint_bone_index(int p_joint_idx) const {
  338. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), -1, "CCDIK joint out of range!");
  339. return ccdik_data_chain[p_joint_idx].bone_idx;
  340. }
  341. void SkeletonModification2DCCDIK::set_ccdik_joint_rotate_from_joint(int p_joint_idx, bool p_rotate_from_joint) {
  342. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  343. ccdik_data_chain.write[p_joint_idx].rotate_from_joint = p_rotate_from_joint;
  344. }
  345. bool SkeletonModification2DCCDIK::get_ccdik_joint_rotate_from_joint(int p_joint_idx) const {
  346. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  347. return ccdik_data_chain[p_joint_idx].rotate_from_joint;
  348. }
  349. void SkeletonModification2DCCDIK::set_ccdik_joint_enable_constraint(int p_joint_idx, bool p_constraint) {
  350. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  351. ccdik_data_chain.write[p_joint_idx].enable_constraint = p_constraint;
  352. notify_property_list_changed();
  353. #ifdef TOOLS_ENABLED
  354. if (stack && is_setup) {
  355. stack->set_editor_gizmos_dirty(true);
  356. }
  357. #endif // TOOLS_ENABLED
  358. }
  359. bool SkeletonModification2DCCDIK::get_ccdik_joint_enable_constraint(int p_joint_idx) const {
  360. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  361. return ccdik_data_chain[p_joint_idx].enable_constraint;
  362. }
  363. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_min(int p_joint_idx, float p_angle_min) {
  364. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  365. ccdik_data_chain.write[p_joint_idx].constraint_angle_min = p_angle_min;
  366. #ifdef TOOLS_ENABLED
  367. if (stack && is_setup) {
  368. stack->set_editor_gizmos_dirty(true);
  369. }
  370. #endif // TOOLS_ENABLED
  371. }
  372. float SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_min(int p_joint_idx) const {
  373. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), 0.0, "CCDIK joint out of range!");
  374. return ccdik_data_chain[p_joint_idx].constraint_angle_min;
  375. }
  376. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_max(int p_joint_idx, float p_angle_max) {
  377. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  378. ccdik_data_chain.write[p_joint_idx].constraint_angle_max = p_angle_max;
  379. #ifdef TOOLS_ENABLED
  380. if (stack && is_setup) {
  381. stack->set_editor_gizmos_dirty(true);
  382. }
  383. #endif // TOOLS_ENABLED
  384. }
  385. float SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_max(int p_joint_idx) const {
  386. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), 0.0, "CCDIK joint out of range!");
  387. return ccdik_data_chain[p_joint_idx].constraint_angle_max;
  388. }
  389. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_invert(int p_joint_idx, bool p_invert) {
  390. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  391. ccdik_data_chain.write[p_joint_idx].constraint_angle_invert = p_invert;
  392. #ifdef TOOLS_ENABLED
  393. if (stack && is_setup) {
  394. stack->set_editor_gizmos_dirty(true);
  395. }
  396. #endif // TOOLS_ENABLED
  397. }
  398. bool SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_invert(int p_joint_idx) const {
  399. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  400. return ccdik_data_chain[p_joint_idx].constraint_angle_invert;
  401. }
  402. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_in_localspace(int p_joint_idx, bool p_constraint_in_localspace) {
  403. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  404. ccdik_data_chain.write[p_joint_idx].constraint_in_localspace = p_constraint_in_localspace;
  405. #ifdef TOOLS_ENABLED
  406. if (stack && is_setup) {
  407. stack->set_editor_gizmos_dirty(true);
  408. }
  409. #endif // TOOLS_ENABLED
  410. }
  411. bool SkeletonModification2DCCDIK::get_ccdik_joint_constraint_in_localspace(int p_joint_idx) const {
  412. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  413. return ccdik_data_chain[p_joint_idx].constraint_in_localspace;
  414. }
  415. void SkeletonModification2DCCDIK::set_ccdik_joint_editor_draw_gizmo(int p_joint_idx, bool p_draw_gizmo) {
  416. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  417. ccdik_data_chain.write[p_joint_idx].editor_draw_gizmo = p_draw_gizmo;
  418. #ifdef TOOLS_ENABLED
  419. if (stack && is_setup) {
  420. stack->set_editor_gizmos_dirty(true);
  421. }
  422. #endif // TOOLS_ENABLED
  423. }
  424. bool SkeletonModification2DCCDIK::get_ccdik_joint_editor_draw_gizmo(int p_joint_idx) const {
  425. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  426. return ccdik_data_chain[p_joint_idx].editor_draw_gizmo;
  427. }
  428. void SkeletonModification2DCCDIK::_bind_methods() {
  429. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DCCDIK::set_target_node);
  430. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DCCDIK::get_target_node);
  431. ClassDB::bind_method(D_METHOD("set_tip_node", "tip_nodepath"), &SkeletonModification2DCCDIK::set_tip_node);
  432. ClassDB::bind_method(D_METHOD("get_tip_node"), &SkeletonModification2DCCDIK::get_tip_node);
  433. ClassDB::bind_method(D_METHOD("set_ccdik_data_chain_length", "length"), &SkeletonModification2DCCDIK::set_ccdik_data_chain_length);
  434. ClassDB::bind_method(D_METHOD("get_ccdik_data_chain_length"), &SkeletonModification2DCCDIK::get_ccdik_data_chain_length);
  435. ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone2d_node", "joint_idx", "bone2d_nodepath"), &SkeletonModification2DCCDIK::set_ccdik_joint_bone2d_node);
  436. ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone2d_node", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_bone2d_node);
  437. ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DCCDIK::set_ccdik_joint_bone_index);
  438. ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone_index", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_bone_index);
  439. ClassDB::bind_method(D_METHOD("set_ccdik_joint_rotate_from_joint", "joint_idx", "rotate_from_joint"), &SkeletonModification2DCCDIK::set_ccdik_joint_rotate_from_joint);
  440. ClassDB::bind_method(D_METHOD("get_ccdik_joint_rotate_from_joint", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_rotate_from_joint);
  441. ClassDB::bind_method(D_METHOD("set_ccdik_joint_enable_constraint", "joint_idx", "enable_constraint"), &SkeletonModification2DCCDIK::set_ccdik_joint_enable_constraint);
  442. ClassDB::bind_method(D_METHOD("get_ccdik_joint_enable_constraint", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_enable_constraint);
  443. ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_min", "joint_idx", "angle_min"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_min);
  444. ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_min", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_min);
  445. ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_max", "joint_idx", "angle_max"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_max);
  446. ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_max", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_max);
  447. ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_invert", "joint_idx", "invert"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_invert);
  448. ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_invert", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_invert);
  449. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
  450. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "tip_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_tip_node", "get_tip_node");
  451. ADD_PROPERTY(PropertyInfo(Variant::INT, "ccdik_data_chain_length", PROPERTY_HINT_RANGE, "0, 100, 1"), "set_ccdik_data_chain_length", "get_ccdik_data_chain_length");
  452. }
  453. SkeletonModification2DCCDIK::SkeletonModification2DCCDIK() {
  454. stack = nullptr;
  455. is_setup = false;
  456. enabled = true;
  457. editor_draw_gizmo = true;
  458. }
  459. SkeletonModification2DCCDIK::~SkeletonModification2DCCDIK() {
  460. }