skeleton_2d.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*************************************************************************/
  2. /* skeleton_2d.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_2d.h"
  31. void Bone2D::_notification(int p_what) {
  32. if (p_what == NOTIFICATION_ENTER_TREE) {
  33. Node *parent = get_parent();
  34. parent_bone = Object::cast_to<Bone2D>(parent);
  35. skeleton = nullptr;
  36. while (parent) {
  37. skeleton = Object::cast_to<Skeleton2D>(parent);
  38. if (skeleton) {
  39. break;
  40. }
  41. if (!Object::cast_to<Bone2D>(parent)) {
  42. break; //skeletons must be chained to Bone2Ds.
  43. }
  44. parent = parent->get_parent();
  45. }
  46. if (skeleton) {
  47. Skeleton2D::Bone bone;
  48. bone.bone = this;
  49. skeleton->bones.push_back(bone);
  50. skeleton->_make_bone_setup_dirty();
  51. }
  52. }
  53. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  54. if (skeleton) {
  55. skeleton->_make_transform_dirty();
  56. }
  57. }
  58. if (p_what == NOTIFICATION_MOVED_IN_PARENT) {
  59. if (skeleton) {
  60. skeleton->_make_bone_setup_dirty();
  61. }
  62. }
  63. if (p_what == NOTIFICATION_EXIT_TREE) {
  64. if (skeleton) {
  65. for (int i = 0; i < skeleton->bones.size(); i++) {
  66. if (skeleton->bones[i].bone == this) {
  67. skeleton->bones.remove(i);
  68. break;
  69. }
  70. }
  71. skeleton->_make_bone_setup_dirty();
  72. skeleton = nullptr;
  73. }
  74. parent_bone = nullptr;
  75. }
  76. }
  77. void Bone2D::_bind_methods() {
  78. ClassDB::bind_method(D_METHOD("set_rest", "rest"), &Bone2D::set_rest);
  79. ClassDB::bind_method(D_METHOD("get_rest"), &Bone2D::get_rest);
  80. ClassDB::bind_method(D_METHOD("apply_rest"), &Bone2D::apply_rest);
  81. ClassDB::bind_method(D_METHOD("get_skeleton_rest"), &Bone2D::get_skeleton_rest);
  82. ClassDB::bind_method(D_METHOD("get_index_in_skeleton"), &Bone2D::get_index_in_skeleton);
  83. ClassDB::bind_method(D_METHOD("set_default_length", "default_length"), &Bone2D::set_default_length);
  84. ClassDB::bind_method(D_METHOD("get_default_length"), &Bone2D::get_default_length);
  85. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "rest"), "set_rest", "get_rest");
  86. ADD_PROPERTY(PropertyInfo(Variant::REAL, "default_length", PROPERTY_HINT_RANGE, "1,1024,1"), "set_default_length", "get_default_length");
  87. }
  88. void Bone2D::set_rest(const Transform2D &p_rest) {
  89. rest = p_rest;
  90. if (skeleton) {
  91. skeleton->_make_bone_setup_dirty();
  92. }
  93. update_configuration_warning();
  94. }
  95. Transform2D Bone2D::get_rest() const {
  96. return rest;
  97. }
  98. Transform2D Bone2D::get_skeleton_rest() const {
  99. if (parent_bone) {
  100. return parent_bone->get_skeleton_rest() * rest;
  101. } else {
  102. return rest;
  103. }
  104. }
  105. void Bone2D::apply_rest() {
  106. set_transform(rest);
  107. }
  108. void Bone2D::set_default_length(float p_length) {
  109. default_length = p_length;
  110. }
  111. float Bone2D::get_default_length() const {
  112. return default_length;
  113. }
  114. int Bone2D::get_index_in_skeleton() const {
  115. ERR_FAIL_COND_V(!skeleton, -1);
  116. skeleton->_update_bone_setup();
  117. return skeleton_index;
  118. }
  119. String Bone2D::get_configuration_warning() const {
  120. String warning = Node2D::get_configuration_warning();
  121. if (!skeleton) {
  122. if (warning != String()) {
  123. warning += "\n\n";
  124. }
  125. if (parent_bone) {
  126. warning += TTR("This Bone2D chain should end at a Skeleton2D node.");
  127. } else {
  128. warning += TTR("A Bone2D only works with a Skeleton2D or another Bone2D as parent node.");
  129. }
  130. }
  131. if (rest == Transform2D(0, 0, 0, 0, 0, 0)) {
  132. if (warning != String()) {
  133. warning += "\n\n";
  134. }
  135. warning += TTR("This bone lacks a proper REST pose. Go to the Skeleton2D node and set one.");
  136. }
  137. return warning;
  138. }
  139. Bone2D::Bone2D() {
  140. skeleton = nullptr;
  141. parent_bone = nullptr;
  142. skeleton_index = -1;
  143. default_length = 16;
  144. set_notify_local_transform(true);
  145. //this is a clever hack so the bone knows no rest has been set yet, allowing to show an error.
  146. for (int i = 0; i < 3; i++) {
  147. rest[i] = Vector2(0, 0);
  148. }
  149. }
  150. //////////////////////////////////////
  151. void Skeleton2D::_make_bone_setup_dirty() {
  152. if (bone_setup_dirty) {
  153. return;
  154. }
  155. bone_setup_dirty = true;
  156. if (is_inside_tree()) {
  157. call_deferred("_update_bone_setup");
  158. }
  159. }
  160. void Skeleton2D::_update_bone_setup() {
  161. if (!bone_setup_dirty) {
  162. return;
  163. }
  164. bone_setup_dirty = false;
  165. VS::get_singleton()->skeleton_allocate(skeleton, bones.size(), true);
  166. bones.sort(); //sorty so they are always in the same order/index
  167. for (int i = 0; i < bones.size(); i++) {
  168. bones.write[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose
  169. bones.write[i].bone->skeleton_index = i;
  170. Bone2D *parent_bone = Object::cast_to<Bone2D>(bones[i].bone->get_parent());
  171. if (parent_bone) {
  172. bones.write[i].parent_index = parent_bone->skeleton_index;
  173. } else {
  174. bones.write[i].parent_index = -1;
  175. }
  176. }
  177. transform_dirty = true;
  178. _update_transform();
  179. emit_signal("bone_setup_changed");
  180. }
  181. void Skeleton2D::_make_transform_dirty() {
  182. if (transform_dirty) {
  183. return;
  184. }
  185. transform_dirty = true;
  186. if (is_inside_tree()) {
  187. call_deferred("_update_transform");
  188. }
  189. }
  190. void Skeleton2D::_update_transform() {
  191. if (bone_setup_dirty) {
  192. _update_bone_setup();
  193. return; //above will update transform anyway
  194. }
  195. if (!transform_dirty) {
  196. return;
  197. }
  198. transform_dirty = false;
  199. for (int i = 0; i < bones.size(); i++) {
  200. ERR_CONTINUE(bones[i].parent_index >= i);
  201. if (bones[i].parent_index >= 0) {
  202. bones.write[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform();
  203. } else {
  204. bones.write[i].accum_transform = bones[i].bone->get_transform();
  205. }
  206. }
  207. for (int i = 0; i < bones.size(); i++) {
  208. Transform2D final_xform = bones[i].accum_transform * bones[i].rest_inverse;
  209. VS::get_singleton()->skeleton_bone_set_transform_2d(skeleton, i, final_xform);
  210. }
  211. }
  212. int Skeleton2D::get_bone_count() const {
  213. ERR_FAIL_COND_V(!is_inside_tree(), 0);
  214. if (bone_setup_dirty) {
  215. const_cast<Skeleton2D *>(this)->_update_bone_setup();
  216. }
  217. return bones.size();
  218. }
  219. Bone2D *Skeleton2D::get_bone(int p_idx) {
  220. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  221. ERR_FAIL_INDEX_V(p_idx, bones.size(), nullptr);
  222. return bones[p_idx].bone;
  223. }
  224. void Skeleton2D::_notification(int p_what) {
  225. if (p_what == NOTIFICATION_READY) {
  226. if (bone_setup_dirty) {
  227. _update_bone_setup();
  228. }
  229. if (transform_dirty) {
  230. _update_transform();
  231. }
  232. request_ready();
  233. }
  234. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  235. VS::get_singleton()->skeleton_set_base_transform_2d(skeleton, get_global_transform());
  236. }
  237. }
  238. RID Skeleton2D::get_skeleton() const {
  239. return skeleton;
  240. }
  241. void Skeleton2D::_bind_methods() {
  242. ClassDB::bind_method(D_METHOD("_update_bone_setup"), &Skeleton2D::_update_bone_setup);
  243. ClassDB::bind_method(D_METHOD("_update_transform"), &Skeleton2D::_update_transform);
  244. ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton2D::get_bone_count);
  245. ClassDB::bind_method(D_METHOD("get_bone", "idx"), &Skeleton2D::get_bone);
  246. ClassDB::bind_method(D_METHOD("get_skeleton"), &Skeleton2D::get_skeleton);
  247. ADD_SIGNAL(MethodInfo("bone_setup_changed"));
  248. }
  249. Skeleton2D::Skeleton2D() {
  250. bone_setup_dirty = true;
  251. transform_dirty = true;
  252. skeleton = VS::get_singleton()->skeleton_create();
  253. set_notify_transform(true);
  254. }
  255. Skeleton2D::~Skeleton2D() {
  256. VS::get_singleton()->free(skeleton);
  257. }