skeleton_modification_2d_ccdik.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
  236. return;
  237. }
  238. target_node_cache = ObjectID();
  239. if (stack->skeleton) {
  240. if (stack->skeleton->is_inside_tree()) {
  241. if (stack->skeleton->has_node(target_node)) {
  242. Node *node = stack->skeleton->get_node(target_node);
  243. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  244. "Cannot update target cache: node is this modification's skeleton or cannot be found!");
  245. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  246. "Cannot update target cache: node is not in the scene tree!");
  247. target_node_cache = node->get_instance_id();
  248. }
  249. }
  250. }
  251. }
  252. void SkeletonModification2DCCDIK::update_tip_cache() {
  253. if (!is_setup || !stack) {
  254. ERR_PRINT_ONCE("Cannot update tip cache: modification is not properly setup!");
  255. return;
  256. }
  257. tip_node_cache = ObjectID();
  258. if (stack->skeleton) {
  259. if (stack->skeleton->is_inside_tree()) {
  260. if (stack->skeleton->has_node(tip_node)) {
  261. Node *node = stack->skeleton->get_node(tip_node);
  262. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  263. "Cannot update tip cache: node is this modification's skeleton or cannot be found!");
  264. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  265. "Cannot update tip cache: node is not in the scene tree!");
  266. tip_node_cache = node->get_instance_id();
  267. }
  268. }
  269. }
  270. }
  271. void SkeletonModification2DCCDIK::ccdik_joint_update_bone2d_cache(int p_joint_idx) {
  272. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");
  273. if (!is_setup || !stack) {
  274. ERR_PRINT_ONCE("Cannot update CCDIK Bone2D cache: modification is not properly setup!");
  275. return;
  276. }
  277. ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();
  278. if (stack->skeleton) {
  279. if (stack->skeleton->is_inside_tree()) {
  280. if (stack->skeleton->has_node(ccdik_data_chain[p_joint_idx].bone2d_node)) {
  281. Node *node = stack->skeleton->get_node(ccdik_data_chain[p_joint_idx].bone2d_node);
  282. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  283. "Cannot update CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");
  284. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  285. "Cannot update CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: node is not in the scene tree!");
  286. ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();
  287. Bone2D *bone = Object::cast_to<Bone2D>(node);
  288. if (bone) {
  289. ccdik_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();
  290. } else {
  291. ERR_FAIL_MSG("CCDIK joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
  292. }
  293. }
  294. }
  295. }
  296. }
  297. void SkeletonModification2DCCDIK::set_target_node(const NodePath &p_target_node) {
  298. target_node = p_target_node;
  299. update_target_cache();
  300. }
  301. NodePath SkeletonModification2DCCDIK::get_target_node() const {
  302. return target_node;
  303. }
  304. void SkeletonModification2DCCDIK::set_tip_node(const NodePath &p_tip_node) {
  305. tip_node = p_tip_node;
  306. update_tip_cache();
  307. }
  308. NodePath SkeletonModification2DCCDIK::get_tip_node() const {
  309. return tip_node;
  310. }
  311. void SkeletonModification2DCCDIK::set_ccdik_data_chain_length(int p_length) {
  312. ccdik_data_chain.resize(p_length);
  313. notify_property_list_changed();
  314. }
  315. int SkeletonModification2DCCDIK::get_ccdik_data_chain_length() {
  316. return ccdik_data_chain.size();
  317. }
  318. void SkeletonModification2DCCDIK::set_ccdik_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {
  319. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  320. ccdik_data_chain.write[p_joint_idx].bone2d_node = p_target_node;
  321. ccdik_joint_update_bone2d_cache(p_joint_idx);
  322. notify_property_list_changed();
  323. }
  324. NodePath SkeletonModification2DCCDIK::get_ccdik_joint_bone2d_node(int p_joint_idx) const {
  325. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), NodePath(), "CCDIK joint out of range!");
  326. return ccdik_data_chain[p_joint_idx].bone2d_node;
  327. }
  328. void SkeletonModification2DCCDIK::set_ccdik_joint_bone_index(int p_joint_idx, int p_bone_idx) {
  329. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCCDIK joint out of range!");
  330. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  331. if (is_setup) {
  332. if (stack->skeleton) {
  333. ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
  334. ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  335. ccdik_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
  336. ccdik_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
  337. } else {
  338. WARN_PRINT("Cannot verify the CCDIK joint " + itos(p_joint_idx) + " bone index for this modification...");
  339. ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  340. }
  341. } else {
  342. WARN_PRINT("Cannot verify the CCDIK joint " + itos(p_joint_idx) + " bone index for this modification...");
  343. ccdik_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  344. }
  345. notify_property_list_changed();
  346. }
  347. int SkeletonModification2DCCDIK::get_ccdik_joint_bone_index(int p_joint_idx) const {
  348. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), -1, "CCDIK joint out of range!");
  349. return ccdik_data_chain[p_joint_idx].bone_idx;
  350. }
  351. void SkeletonModification2DCCDIK::set_ccdik_joint_rotate_from_joint(int p_joint_idx, bool p_rotate_from_joint) {
  352. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  353. ccdik_data_chain.write[p_joint_idx].rotate_from_joint = p_rotate_from_joint;
  354. }
  355. bool SkeletonModification2DCCDIK::get_ccdik_joint_rotate_from_joint(int p_joint_idx) const {
  356. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  357. return ccdik_data_chain[p_joint_idx].rotate_from_joint;
  358. }
  359. void SkeletonModification2DCCDIK::set_ccdik_joint_enable_constraint(int p_joint_idx, bool p_constraint) {
  360. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  361. ccdik_data_chain.write[p_joint_idx].enable_constraint = p_constraint;
  362. notify_property_list_changed();
  363. #ifdef TOOLS_ENABLED
  364. if (stack && is_setup) {
  365. stack->set_editor_gizmos_dirty(true);
  366. }
  367. #endif // TOOLS_ENABLED
  368. }
  369. bool SkeletonModification2DCCDIK::get_ccdik_joint_enable_constraint(int p_joint_idx) const {
  370. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  371. return ccdik_data_chain[p_joint_idx].enable_constraint;
  372. }
  373. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_min(int p_joint_idx, float p_angle_min) {
  374. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  375. ccdik_data_chain.write[p_joint_idx].constraint_angle_min = p_angle_min;
  376. #ifdef TOOLS_ENABLED
  377. if (stack && is_setup) {
  378. stack->set_editor_gizmos_dirty(true);
  379. }
  380. #endif // TOOLS_ENABLED
  381. }
  382. float SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_min(int p_joint_idx) const {
  383. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), 0.0, "CCDIK joint out of range!");
  384. return ccdik_data_chain[p_joint_idx].constraint_angle_min;
  385. }
  386. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_max(int p_joint_idx, float p_angle_max) {
  387. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  388. ccdik_data_chain.write[p_joint_idx].constraint_angle_max = p_angle_max;
  389. #ifdef TOOLS_ENABLED
  390. if (stack && is_setup) {
  391. stack->set_editor_gizmos_dirty(true);
  392. }
  393. #endif // TOOLS_ENABLED
  394. }
  395. float SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_max(int p_joint_idx) const {
  396. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), 0.0, "CCDIK joint out of range!");
  397. return ccdik_data_chain[p_joint_idx].constraint_angle_max;
  398. }
  399. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_invert(int p_joint_idx, bool p_invert) {
  400. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  401. ccdik_data_chain.write[p_joint_idx].constraint_angle_invert = p_invert;
  402. #ifdef TOOLS_ENABLED
  403. if (stack && is_setup) {
  404. stack->set_editor_gizmos_dirty(true);
  405. }
  406. #endif // TOOLS_ENABLED
  407. }
  408. bool SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_invert(int p_joint_idx) const {
  409. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  410. return ccdik_data_chain[p_joint_idx].constraint_angle_invert;
  411. }
  412. void SkeletonModification2DCCDIK::set_ccdik_joint_constraint_in_localspace(int p_joint_idx, bool p_constraint_in_localspace) {
  413. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  414. ccdik_data_chain.write[p_joint_idx].constraint_in_localspace = p_constraint_in_localspace;
  415. #ifdef TOOLS_ENABLED
  416. if (stack && is_setup) {
  417. stack->set_editor_gizmos_dirty(true);
  418. }
  419. #endif // TOOLS_ENABLED
  420. }
  421. bool SkeletonModification2DCCDIK::get_ccdik_joint_constraint_in_localspace(int p_joint_idx) const {
  422. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  423. return ccdik_data_chain[p_joint_idx].constraint_in_localspace;
  424. }
  425. void SkeletonModification2DCCDIK::set_ccdik_joint_editor_draw_gizmo(int p_joint_idx, bool p_draw_gizmo) {
  426. ERR_FAIL_INDEX_MSG(p_joint_idx, ccdik_data_chain.size(), "CCDIK joint out of range!");
  427. ccdik_data_chain.write[p_joint_idx].editor_draw_gizmo = p_draw_gizmo;
  428. #ifdef TOOLS_ENABLED
  429. if (stack && is_setup) {
  430. stack->set_editor_gizmos_dirty(true);
  431. }
  432. #endif // TOOLS_ENABLED
  433. }
  434. bool SkeletonModification2DCCDIK::get_ccdik_joint_editor_draw_gizmo(int p_joint_idx) const {
  435. ERR_FAIL_INDEX_V_MSG(p_joint_idx, ccdik_data_chain.size(), false, "CCDIK joint out of range!");
  436. return ccdik_data_chain[p_joint_idx].editor_draw_gizmo;
  437. }
  438. void SkeletonModification2DCCDIK::_bind_methods() {
  439. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DCCDIK::set_target_node);
  440. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DCCDIK::get_target_node);
  441. ClassDB::bind_method(D_METHOD("set_tip_node", "tip_nodepath"), &SkeletonModification2DCCDIK::set_tip_node);
  442. ClassDB::bind_method(D_METHOD("get_tip_node"), &SkeletonModification2DCCDIK::get_tip_node);
  443. ClassDB::bind_method(D_METHOD("set_ccdik_data_chain_length", "length"), &SkeletonModification2DCCDIK::set_ccdik_data_chain_length);
  444. ClassDB::bind_method(D_METHOD("get_ccdik_data_chain_length"), &SkeletonModification2DCCDIK::get_ccdik_data_chain_length);
  445. ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone2d_node", "joint_idx", "bone2d_nodepath"), &SkeletonModification2DCCDIK::set_ccdik_joint_bone2d_node);
  446. ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone2d_node", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_bone2d_node);
  447. ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DCCDIK::set_ccdik_joint_bone_index);
  448. ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone_index", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_bone_index);
  449. ClassDB::bind_method(D_METHOD("set_ccdik_joint_rotate_from_joint", "joint_idx", "rotate_from_joint"), &SkeletonModification2DCCDIK::set_ccdik_joint_rotate_from_joint);
  450. ClassDB::bind_method(D_METHOD("get_ccdik_joint_rotate_from_joint", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_rotate_from_joint);
  451. ClassDB::bind_method(D_METHOD("set_ccdik_joint_enable_constraint", "joint_idx", "enable_constraint"), &SkeletonModification2DCCDIK::set_ccdik_joint_enable_constraint);
  452. ClassDB::bind_method(D_METHOD("get_ccdik_joint_enable_constraint", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_enable_constraint);
  453. ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_min", "joint_idx", "angle_min"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_min);
  454. ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_min", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_min);
  455. ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_max", "joint_idx", "angle_max"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_max);
  456. ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_max", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_max);
  457. ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_invert", "joint_idx", "invert"), &SkeletonModification2DCCDIK::set_ccdik_joint_constraint_angle_invert);
  458. ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_invert", "joint_idx"), &SkeletonModification2DCCDIK::get_ccdik_joint_constraint_angle_invert);
  459. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
  460. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "tip_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_tip_node", "get_tip_node");
  461. 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");
  462. }
  463. SkeletonModification2DCCDIK::SkeletonModification2DCCDIK() {
  464. stack = nullptr;
  465. is_setup = false;
  466. enabled = true;
  467. editor_draw_gizmo = true;
  468. }
  469. SkeletonModification2DCCDIK::~SkeletonModification2DCCDIK() {
  470. }