skeleton_modification_2d_ccdik.cpp 25 KB

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