animation_cache.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*************************************************************************/
  2. /* animation_cache.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "animation_cache.h"
  30. void AnimationCache::_node_exit_scene(Node *p_node) {
  31. //it is one shot, so it disconnects upon arrival
  32. ERR_FAIL_COND(!connected_nodes.has(p_node));
  33. connected_nodes.erase(p_node);
  34. for(int i=0;i<path_cache.size();i++) {
  35. if (path_cache[i].node!=p_node)
  36. continue;
  37. path_cache[i].valid=false; //invalidate path cache
  38. }
  39. }
  40. void AnimationCache::_animation_changed() {
  41. _clear_cache();
  42. }
  43. void AnimationCache::_clear_cache() {
  44. while(connected_nodes.size()) {
  45. connected_nodes.front()->get()->disconnect("exit_scene",this,"_node_exit_scene");
  46. connected_nodes.erase(connected_nodes.front());
  47. }
  48. path_cache.clear();;
  49. cache_valid=false;
  50. cache_dirty=true;
  51. }
  52. void AnimationCache::_update_cache() {
  53. cache_valid=false;
  54. ERR_FAIL_COND(!root);
  55. ERR_FAIL_COND(!root->is_inside_scene());
  56. ERR_FAIL_COND(animation.is_null());
  57. for(int i=0;i<animation->get_track_count();i++) {
  58. NodePath np = animation->track_get_path(i);
  59. Node *node = root->get_node(np);
  60. if (!node) {
  61. path_cache.push_back(Path());
  62. ERR_EXPLAIN("Invalid Track Path in Animation: "+np);
  63. ERR_CONTINUE(!node);
  64. }
  65. Path path;
  66. Ref<Resource> res;
  67. if (np.get_subname_count()) {
  68. if (animation->track_get_type(i)==Animation::TYPE_TRANSFORM) {
  69. path_cache.push_back(Path());
  70. ERR_EXPLAIN("Transform tracks can't have a subpath: "+np);
  71. ERR_CONTINUE(animation->track_get_type(i)==Animation::TYPE_TRANSFORM);
  72. }
  73. RES res;
  74. for(int j=0;j<np.get_subname_count();j++) {
  75. res = j==0 ? node->get(np.get_subname(j)) : res->get(np.get_subname(j));
  76. if (res.is_null())
  77. break;
  78. }
  79. if (res.is_null()) {
  80. path_cache.push_back(Path());
  81. ERR_EXPLAIN("Invalid Track SubPath in Animation: "+np);
  82. ERR_CONTINUE(res.is_null());
  83. }
  84. path.resource=res;
  85. path.object=res.ptr();
  86. } else {
  87. if (animation->track_get_type(i)==Animation::TYPE_TRANSFORM) {
  88. StringName property = np.get_property();
  89. String ps = property;
  90. Spatial *sp = node->cast_to<Spatial>();
  91. if (!sp) {
  92. path_cache.push_back(Path());
  93. ERR_EXPLAIN("Transform track not of type Spatial: "+np);
  94. ERR_CONTINUE(!sp);
  95. }
  96. if (ps!="") {
  97. Skeleton *sk = node->cast_to<Skeleton>();
  98. if (!sk) {
  99. path_cache.push_back(Path());
  100. ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton!: "+np);
  101. ERR_CONTINUE(!sk);
  102. }
  103. int idx = sk->find_bone(ps);
  104. if (idx==-1) {
  105. path_cache.push_back(Path());
  106. ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton Bone!: "+np);
  107. ERR_CONTINUE(idx==-1);
  108. }
  109. path.bone_idx=idx;
  110. path.skeleton=sk;
  111. }
  112. path.spatial=sp;
  113. }
  114. path.node=node;
  115. path.object=node;
  116. }
  117. if (animation->track_get_type(i)==Animation::TYPE_VALUE) {
  118. if (np.get_property().operator String()=="") {
  119. path_cache.push_back(Path());
  120. ERR_EXPLAIN("Value Track lacks property: "+np);
  121. ERR_CONTINUE(np.get_property().operator String()=="");
  122. }
  123. path.property=np.get_property();
  124. } else if (animation->track_get_type(i)==Animation::TYPE_METHOD) {
  125. if (np.get_property().operator String()!="") {
  126. path_cache.push_back(Path());
  127. ERR_EXPLAIN("Method Track has property: "+np);
  128. ERR_CONTINUE(np.get_property().operator String()!="");
  129. }
  130. }
  131. path.valid=true;
  132. path_cache.push_back(path);
  133. if (!connected_nodes.has(path.node)) {
  134. connected_nodes.insert(path.node);
  135. path.node->connect("exit_scene",this,"_node_exit_scene",Node::make_binds(path.node),CONNECT_ONESHOT);
  136. }
  137. }
  138. cache_dirty=false;
  139. cache_valid=true;
  140. }
  141. void AnimationCache::set_track_transform(int p_idx,const Transform& p_transform) {
  142. if (cache_dirty)
  143. _update_cache();
  144. ERR_FAIL_COND(!cache_valid);
  145. ERR_FAIL_INDEX(p_idx,path_cache.size());
  146. Path &p = path_cache[p_idx];
  147. if (!p.valid)
  148. return;
  149. ERR_FAIL_COND(!p.node);
  150. ERR_FAIL_COND(!p.spatial);
  151. if (p.skeleton) {
  152. p.skeleton->set_bone_pose(p.bone_idx,p_transform);
  153. } else {
  154. p.spatial->set_transform(p_transform);
  155. }
  156. }
  157. void AnimationCache::set_track_value(int p_idx,const Variant& p_value) {
  158. if (cache_dirty)
  159. _update_cache();
  160. ERR_FAIL_COND(!cache_valid);
  161. ERR_FAIL_INDEX(p_idx,path_cache.size());
  162. Path &p = path_cache[p_idx];
  163. if (!p.valid)
  164. return;
  165. ERR_FAIL_COND(!p.object);
  166. p.object->set(p.property,p_value);
  167. }
  168. void AnimationCache::call_track(int p_idx,const StringName& p_method,const Variant** p_args,int p_argcount,Variant::CallError &r_error) {
  169. if (cache_dirty)
  170. _update_cache();
  171. ERR_FAIL_COND(!cache_valid);
  172. ERR_FAIL_INDEX(p_idx,path_cache.size());
  173. Path &p = path_cache[p_idx];
  174. if (!p.valid)
  175. return;
  176. ERR_FAIL_COND(!p.object);
  177. p.object->call(p_method,p_args,p_argcount,r_error);
  178. }
  179. void AnimationCache::set_all(float p_time, float p_delta) {
  180. if (cache_dirty)
  181. _update_cache();
  182. ERR_FAIL_COND(!cache_valid);
  183. int tc = animation->get_track_count();
  184. for(int i=0;i<tc;i++) {
  185. switch(animation->track_get_type(i)) {
  186. case Animation::TYPE_TRANSFORM: {
  187. Vector3 loc,scale;
  188. Quat rot;
  189. animation->transform_track_interpolate(i,p_time,&loc,&rot,&scale);
  190. Transform tr( Matrix3(rot), loc );
  191. tr.basis.scale(scale);
  192. set_track_transform(i,tr);
  193. } break;
  194. case Animation::TYPE_VALUE: {
  195. if (animation->value_track_is_continuous(i)) {
  196. Variant v = animation->value_track_interpolate(i,p_time);
  197. set_track_value(i,v);
  198. } else {
  199. List<int> indices;
  200. animation->value_track_get_key_indices(i,p_time,p_delta,&indices);
  201. for(List<int>::Element *E=indices.front();E;E=E->next()) {
  202. Variant v = animation->track_get_key_value(i,E->get());
  203. set_track_value(i,v);
  204. }
  205. }
  206. } break;
  207. case Animation::TYPE_METHOD: {
  208. List<int> indices;
  209. animation->method_track_get_key_indices(i,p_time,p_delta,&indices);
  210. for(List<int>::Element *E=indices.front();E;E=E->next()) {
  211. Vector<Variant> args = animation->method_track_get_params(i,E->get());
  212. StringName name = animation->method_track_get_name(i,E->get());
  213. Variant::CallError err;
  214. if (!args.size()) {
  215. call_track(i,name,NULL,0,err);
  216. } else {
  217. Vector<Variant*> argptrs;
  218. argptrs.resize(args.size());
  219. for(int j=0;j<args.size();j++) {
  220. argptrs[j]=&args[j];
  221. }
  222. call_track(i,name,(const Variant**)&argptrs[0],args.size(),err);
  223. }
  224. }
  225. } break;
  226. default: {}
  227. }
  228. }
  229. }
  230. void AnimationCache::set_animation(const Ref<Animation>& p_animation) {
  231. _clear_cache();
  232. if (animation.is_valid())
  233. animation->disconnect("changed",this,"_animation_changed");
  234. animation=p_animation;
  235. if (animation.is_valid())
  236. animation->connect("changed",this,"_animation_changed");
  237. }
  238. void AnimationCache::_bind_methods() {
  239. ObjectTypeDB::bind_method(_MD("_node_exit_scene"),&AnimationCache::_node_exit_scene);
  240. ObjectTypeDB::bind_method(_MD("_animation_changed"),&AnimationCache::_animation_changed);
  241. }
  242. void AnimationCache::set_root(Node* p_root) {
  243. _clear_cache();
  244. root=p_root;
  245. }
  246. AnimationCache::AnimationCache() {
  247. root=NULL;
  248. cache_dirty=true;
  249. cache_valid=false;
  250. }