skeleton_2d.cpp 8.8 KB

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