animation_player.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /*************************************************************************/
  2. /* animation_player.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_player.h"
  30. #include "message_queue.h"
  31. #include "scene/scene_string_names.h"
  32. bool AnimationPlayer::_set(const StringName& p_name, const Variant& p_value) {
  33. String name=p_name;
  34. if (name=="playback/speed" || name=="speed") { //bw compatibility
  35. set_speed(p_value);
  36. } else if (name=="playback/active") {
  37. set_active(p_value);
  38. } else if (name.begins_with("playback/play")) {
  39. String which=p_value;
  40. if (which=="[stop]")
  41. stop();
  42. else
  43. play(which);
  44. } else if (name.begins_with("anims/")) {
  45. String which=name.get_slice("/",1);
  46. add_animation(which,p_value);
  47. } else if (name.begins_with("next/")) {
  48. String which=name.get_slice("/",1);
  49. animation_set_next(which,p_value);
  50. } else if (name=="blend_times") {
  51. Array array=p_value;
  52. int len = array.size();
  53. ERR_FAIL_COND_V(len%3,false);
  54. for(int i=0;i<len/3;i++) {
  55. StringName from = array[i*3+0];
  56. StringName to = array[i*3+1];
  57. float time = array[i*3+2];
  58. set_blend_time(from,to,time);
  59. }
  60. } else if (name=="autoplay") {
  61. autoplay=p_value;
  62. } else
  63. return false;
  64. return true;
  65. }
  66. bool AnimationPlayer::_get(const StringName& p_name,Variant &r_ret) const {
  67. String name=p_name;
  68. if (name=="playback/speed") { //bw compatibility
  69. r_ret=speed_scale;
  70. } else if (name=="playback/active") {
  71. r_ret=is_active();
  72. } else if (name=="playback/play") {
  73. if (is_active() && is_playing())
  74. r_ret=playback.assigned;
  75. else
  76. r_ret="[stop]";
  77. } else if (name.begins_with("anims/")) {
  78. String which=name.get_slice("/",1);
  79. r_ret= get_animation(which).get_ref_ptr();
  80. } else if (name.begins_with("next/")) {
  81. String which=name.get_slice("/",1);
  82. r_ret= animation_get_next(which);
  83. } else if (name=="blend_times") {
  84. Array array;
  85. array.resize(blend_times.size()*3);
  86. int idx=0;
  87. for(Map<BlendKey, float >::Element *E=blend_times.front();E;E=E->next()) {
  88. array.set(idx*3+0,E->key().from);
  89. array.set(idx*3+1,E->key().to);
  90. array.set(idx*3+2,E->get());
  91. idx++;
  92. }
  93. r_ret=array;
  94. } else if (name=="autoplay") {
  95. r_ret=autoplay;
  96. } else
  97. return false;
  98. return true;
  99. }
  100. void AnimationPlayer::_get_property_list( List<PropertyInfo> *p_list) const {
  101. List<String> names;
  102. for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
  103. p_list->push_back( PropertyInfo( Variant::OBJECT, "anims/"+String(E->key()), PROPERTY_HINT_RESOURCE_TYPE, "Animation",PROPERTY_USAGE_NOEDITOR) );
  104. if (E->get().next!=StringName())
  105. p_list->push_back( PropertyInfo( Variant::STRING, "next/"+String(E->key()), PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR) );
  106. names.push_back(E->key());
  107. }
  108. {
  109. names.sort();
  110. names.push_front("[stop]");
  111. String hint;
  112. for(List<String>::Element *E=names.front();E;E=E->next()) {
  113. if (E!=names.front())
  114. hint+=",";
  115. hint+=E->get();
  116. }
  117. p_list->push_back( PropertyInfo( Variant::STRING, "playback/play", PROPERTY_HINT_ENUM, hint,PROPERTY_USAGE_EDITOR) );
  118. p_list->push_back( PropertyInfo( Variant::BOOL, "playback/active", PROPERTY_HINT_NONE,"" ) );
  119. p_list->push_back( PropertyInfo( Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01") );
  120. }
  121. p_list->push_back( PropertyInfo( Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR) );
  122. p_list->push_back( PropertyInfo( Variant::STRING, "autoplay", PROPERTY_HINT_NONE, "",PROPERTY_USAGE_NOEDITOR) );
  123. }
  124. void AnimationPlayer::_notification(int p_what) {
  125. switch(p_what) {
  126. case NOTIFICATION_ENTER_SCENE: {
  127. if (!processing) {
  128. //make sure that a previous process state was not saved
  129. //only process if "processing" is set
  130. set_fixed_process(false);
  131. set_process(false);
  132. }
  133. //_set_process(false);
  134. clear_caches();
  135. } break;
  136. case NOTIFICATION_READY: {
  137. if (!get_scene()->is_editor_hint() && animation_set.has(autoplay)) {
  138. play(autoplay);
  139. }
  140. } break;
  141. case NOTIFICATION_PROCESS: {
  142. if (animation_process_mode==ANIMATION_PROCESS_FIXED)
  143. break;
  144. if (processing)
  145. _animation_process( get_process_delta_time() );
  146. } break;
  147. case NOTIFICATION_FIXED_PROCESS: {
  148. if (animation_process_mode==ANIMATION_PROCESS_IDLE)
  149. break;
  150. if (processing)
  151. _animation_process( get_fixed_process_delta_time() );
  152. } break;
  153. case NOTIFICATION_EXIT_SCENE: {
  154. stop_all();
  155. clear_caches();
  156. } break;
  157. }
  158. }
  159. void AnimationPlayer::_generate_node_caches(AnimationData* p_anim) {
  160. Node *parent = get_node(root);
  161. ERR_FAIL_COND(!parent);
  162. Animation *a=p_anim->animation.operator->();
  163. p_anim->node_cache.resize( a->get_track_count() );
  164. for (int i=0;i<a->get_track_count();i++) {
  165. p_anim->node_cache[i]=NULL;
  166. RES resource;
  167. Node *child = parent->get_node_and_resource(a->track_get_path(i),resource);
  168. if (!child) {
  169. ERR_EXPLAIN("On Animation: '"+p_anim->name+"', couldn't resolve track: '"+String(a->track_get_path(i))+"'");
  170. }
  171. ERR_CONTINUE(!child); // couldn't find the child node
  172. uint32_t id=resource.is_valid()?resource->get_instance_ID():child->get_instance_ID();
  173. int bone_idx=-1;
  174. if (a->track_get_path(i).get_property() && child->cast_to<Skeleton>()) {
  175. bone_idx = child->cast_to<Skeleton>()->find_bone( a->track_get_path(i).get_property() );
  176. if (bone_idx==-1) {
  177. continue;
  178. }
  179. }
  180. {
  181. if (!child->is_connected("exit_scene",this,"_node_removed"))
  182. child->connect("exit_scene",this,"_node_removed",make_binds(child),CONNECT_ONESHOT);
  183. }
  184. TrackNodeCacheKey key;
  185. key.id=id;
  186. key.bone_idx=bone_idx;
  187. if (node_cache_map.has(key)) {
  188. p_anim->node_cache[i]=&node_cache_map[key];
  189. } else {
  190. node_cache_map[key]=TrackNodeCache();
  191. p_anim->node_cache[i]=&node_cache_map[key];
  192. p_anim->node_cache[i]->node=child;
  193. p_anim->node_cache[i]->resource=resource;
  194. p_anim->node_cache[i]->node_2d=child->cast_to<Node2D>();
  195. if (a->track_get_type(i)==Animation::TYPE_TRANSFORM) {
  196. // special cases and caches for transform tracks
  197. // cache spatial
  198. p_anim->node_cache[i]->spatial=child->cast_to<Spatial>();
  199. // cache skeleton
  200. p_anim->node_cache[i]->skeleton=child->cast_to<Skeleton>();
  201. if (p_anim->node_cache[i]->skeleton) {
  202. StringName bone_name=a->track_get_path(i).get_property();
  203. if (bone_name.operator String()!="") {
  204. p_anim->node_cache[i]->bone_idx=p_anim->node_cache[i]->skeleton->find_bone(bone_name);
  205. if (p_anim->node_cache[i]->bone_idx<0) {
  206. // broken track (unexisting bone)
  207. p_anim->node_cache[i]->skeleton=NULL;
  208. p_anim->node_cache[i]->spatial=NULL;
  209. printf("bone is %ls\n", String(bone_name).c_str());
  210. ERR_CONTINUE( p_anim->node_cache[i]->bone_idx<0 );
  211. } else {
  212. }
  213. } else {
  214. // no property, just use spatialnode
  215. p_anim->node_cache[i]->skeleton=NULL;
  216. }
  217. }
  218. }
  219. }
  220. if (a->track_get_type(i)==Animation::TYPE_VALUE) {
  221. StringName property = a->track_get_path(i).get_property();
  222. if (!p_anim->node_cache[i]->property_anim.has(property)) {
  223. TrackNodeCache::PropertyAnim pa;
  224. pa.prop=property;
  225. pa.object=resource.is_valid()?(Object*)resource.ptr():(Object*)child;
  226. pa.special=SP_NONE;
  227. if (false && p_anim->node_cache[i]->node_2d) {
  228. if (pa.prop==SceneStringNames::get_singleton()->transform_pos)
  229. pa.special=SP_NODE2D_POS;
  230. else if (pa.prop==SceneStringNames::get_singleton()->transform_rot)
  231. pa.special=SP_NODE2D_ROT;
  232. else if (pa.prop==SceneStringNames::get_singleton()->transform_scale)
  233. pa.special=SP_NODE2D_SCALE;
  234. }
  235. p_anim->node_cache[i]->property_anim[property]=pa;
  236. }
  237. }
  238. }
  239. }
  240. void AnimationPlayer::_animation_process_animation(AnimationData* p_anim,float p_time, float p_delta,float p_interp, bool p_allow_discrete) {
  241. if (p_anim->node_cache.size() != p_anim->animation->get_track_count()) {
  242. // animation hasn't been "node-cached"
  243. _generate_node_caches(p_anim);
  244. }
  245. ERR_FAIL_COND( p_anim->node_cache.size() != p_anim->animation->get_track_count() );
  246. Animation *a=p_anim->animation.operator->();
  247. bool can_call = is_inside_scene() && !get_scene()->is_editor_hint();
  248. for (int i=0;i<a->get_track_count();i++) {
  249. TrackNodeCache *nc=p_anim->node_cache[i];
  250. if (!nc) // no node cache for this track, skip it
  251. continue;
  252. if (a->track_get_key_count(i)==0)
  253. continue; // do nothing if track is empty
  254. switch(a->track_get_type(i)) {
  255. case Animation::TYPE_TRANSFORM: {
  256. if (!nc->spatial)
  257. continue;
  258. Vector3 loc;
  259. Quat rot;
  260. Vector3 scale;
  261. Error err = a->transform_track_interpolate(i,p_time,&loc,&rot,&scale);
  262. ERR_CONTINUE(err!=OK); //used for testing, should be removed
  263. if (err!=OK)
  264. continue;
  265. if (nc->accum_pass!=accum_pass) {
  266. ERR_CONTINUE( cache_update_size >= NODE_CACHE_UPDATE_MAX );
  267. cache_update[cache_update_size++]=nc;
  268. nc->accum_pass=accum_pass;
  269. nc->loc_accum=loc;
  270. nc->rot_accum=rot;
  271. nc->scale_accum=scale;
  272. } else {
  273. nc->loc_accum=nc->loc_accum.linear_interpolate(loc,p_interp);
  274. nc->rot_accum=nc->rot_accum.slerp(rot,p_interp);
  275. nc->scale_accum=nc->scale_accum.linear_interpolate(scale,p_interp);
  276. }
  277. } break;
  278. case Animation::TYPE_VALUE: {
  279. if (!nc->node)
  280. continue;
  281. //StringName property=a->track_get_path(i).get_property();
  282. Map<StringName,TrackNodeCache::PropertyAnim>::Element *E=nc->property_anim.find(a->track_get_path(i).get_property());
  283. ERR_CONTINUE(!E); //should it continue, or create a new one?
  284. TrackNodeCache::PropertyAnim *pa = &E->get();
  285. if (a->value_track_is_continuous(i) || p_delta==0) {
  286. Variant value=a->value_track_interpolate(i,p_time);
  287. if (p_delta==0 && value.get_type()==Variant::STRING)
  288. continue; // doing this with strings is messy, should find another way
  289. if (pa->accum_pass!=accum_pass) {
  290. ERR_CONTINUE( cache_update_prop_size >= NODE_CACHE_UPDATE_MAX );
  291. cache_update_prop[cache_update_prop_size++]=pa;
  292. pa->value_accum=value;
  293. pa->accum_pass=accum_pass;
  294. } else {
  295. Variant::interpolate(pa->value_accum,value,p_interp,pa->value_accum);
  296. }
  297. } else if (p_allow_discrete) {
  298. List<int> indices;
  299. a->value_track_get_key_indices(i,p_time,p_delta,&indices);
  300. for(List<int>::Element *F=indices.front();F;F=F->next()) {
  301. Variant value=a->track_get_key_value(i,F->get());
  302. switch(pa->special) {
  303. case SP_NONE: pa->object->set(pa->prop,value); break; //you are not speshul
  304. case SP_NODE2D_POS: static_cast<Node2D*>(pa->object)->set_pos(value); break;
  305. case SP_NODE2D_ROT: static_cast<Node2D*>(pa->object)->set_rot(Math::deg2rad(value)); break;
  306. case SP_NODE2D_SCALE: static_cast<Node2D*>(pa->object)->set_scale(value); break;
  307. }
  308. }
  309. }
  310. } break;
  311. case Animation::TYPE_METHOD: {
  312. if (!nc->node)
  313. continue;
  314. if (p_delta==0)
  315. continue;
  316. if (!p_allow_discrete)
  317. break;
  318. List<int> indices;
  319. a->method_track_get_key_indices(i,p_time,p_delta,&indices);
  320. for(List<int>::Element *E=indices.front();E;E=E->next()) {
  321. StringName method=a->method_track_get_name(i,E->get());
  322. Vector<Variant> params=a->method_track_get_params(i,E->get());
  323. int s=params.size();
  324. ERR_CONTINUE( s > VARIANT_ARG_MAX );
  325. if (can_call) {
  326. MessageQueue::get_singleton()->push_call(
  327. nc->node,
  328. method,
  329. s>=1 ? params[0] : Variant(),
  330. s>=2 ? params[1] : Variant(),
  331. s>=3 ? params[2] : Variant(),
  332. s>=4 ? params[3] : Variant(),
  333. s>=5 ? params[4] : Variant()
  334. );
  335. }
  336. }
  337. } break;
  338. }
  339. }
  340. }
  341. void AnimationPlayer::_animation_process_data(PlaybackData &cd,float p_delta,float p_blend) {
  342. float delta=p_delta*speed_scale*cd.speed_scale;
  343. bool backwards=delta<0;
  344. float next_pos=cd.pos+delta;
  345. float len=cd.from->animation->get_length();
  346. bool loop=cd.from->animation->has_loop();
  347. if (!loop) {
  348. if (next_pos<0)
  349. next_pos=0;
  350. else if (next_pos>len)
  351. next_pos=len;
  352. // fix delta
  353. delta=next_pos-cd.pos;
  354. if (&cd == &playback.current) {
  355. if (!backwards && cd.pos < len && next_pos==len /*&& playback.blend.empty()*/) {
  356. //playback finished
  357. end_notify=true;
  358. }
  359. if (backwards && cd.pos > 0 && next_pos==0 /*&& playback.blend.empty()*/) {
  360. //playback finished
  361. end_notify=true;
  362. }
  363. }
  364. } else {
  365. next_pos=Math::fposmod(next_pos,len);
  366. }
  367. cd.pos=next_pos;
  368. _animation_process_animation(cd.from,cd.pos,delta,p_blend,&cd == &playback.current);
  369. }
  370. void AnimationPlayer::_animation_process2(float p_delta) {
  371. Playback &c=playback;
  372. float prev_blend=1.0;
  373. accum_pass++;
  374. int pop_count=1;
  375. int pop=0; // if >0, then amount of elements to pop from the back
  376. for (List<Blend>::Element *E=c.blend.back();E;E=E->prev(),pop_count++) {
  377. Blend& b=E->get();
  378. _animation_process_data(b.data,p_delta,prev_blend);
  379. prev_blend=1.0-b.blend_left/b.blend_time;
  380. b.blend_left-=Math::absf(speed_scale*p_delta);
  381. if (b.blend_left<0) {
  382. pop=pop_count;
  383. }
  384. }
  385. while(pop--) {
  386. c.blend.pop_back();
  387. }
  388. _animation_process_data(c.current,p_delta,prev_blend);
  389. }
  390. void AnimationPlayer::_animation_update_transforms() {
  391. for (int i=0;i<cache_update_size;i++) {
  392. TrackNodeCache *nc=cache_update[i];
  393. ERR_CONTINUE( nc->accum_pass!=accum_pass );
  394. if (nc->spatial) {
  395. Transform t;
  396. t.origin=nc->loc_accum;
  397. t.basis=nc->rot_accum;
  398. t.basis.scale( nc->scale_accum );
  399. if (nc->skeleton && nc->bone_idx>=0) {
  400. nc->skeleton->set_bone_pose( nc->bone_idx, t );
  401. } else if (nc->spatial) {
  402. nc->spatial->set_transform(t);
  403. }
  404. }
  405. }
  406. cache_update_size=0;
  407. for (int i=0;i<cache_update_prop_size;i++) {
  408. TrackNodeCache::PropertyAnim *pa=cache_update_prop[i];
  409. ERR_CONTINUE( pa->accum_pass!=accum_pass );
  410. #if 1
  411. switch(pa->special) {
  412. case SP_NONE: pa->object->set(pa->prop,pa->value_accum); break; //you are not speshul
  413. case SP_NODE2D_POS: static_cast<Node2D*>(pa->object)->set_pos(pa->value_accum); break;
  414. case SP_NODE2D_ROT: static_cast<Node2D*>(pa->object)->set_rot(Math::deg2rad(pa->value_accum)); break;
  415. case SP_NODE2D_SCALE: static_cast<Node2D*>(pa->object)->set_scale(pa->value_accum); break;
  416. }
  417. #else
  418. pa->object->set(pa->prop,pa->value_accum);
  419. #endif
  420. }
  421. cache_update_prop_size=0;
  422. }
  423. void AnimationPlayer::_animation_process(float p_delta) {
  424. // bool any_active=false;
  425. if (playback.current.from) {
  426. end_notify=false;
  427. _animation_process2(p_delta);
  428. _animation_update_transforms();
  429. if (end_notify) {
  430. if (queued.size()) {
  431. String old = playback.assigned;
  432. play(queued.front()->get());
  433. String new_name = playback.assigned;
  434. queued.pop_front();
  435. end_notify=false;
  436. emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
  437. } else {
  438. //stop();
  439. playing = false;
  440. _set_process(false);
  441. end_notify=false;
  442. emit_signal(SceneStringNames::get_singleton()->finished);
  443. }
  444. }
  445. } else {
  446. _set_process(false);
  447. }
  448. }
  449. Error AnimationPlayer::add_animation(const StringName& p_name, const Ref<Animation>& p_animation) {
  450. ERR_EXPLAIN("Invalid animation name: "+String(p_name));
  451. ERR_FAIL_COND_V( String(p_name).find("/")!=-1 || String(p_name).find(":")!=-1 || String(p_name).find(",")!=-1 || String(p_name).find("[")!=-1, ERR_INVALID_PARAMETER );
  452. ERR_FAIL_COND_V( p_animation.is_null() , ERR_INVALID_PARAMETER );
  453. //print_line("Add anim: "+String(p_name)+" name: "+p_animation->get_name());
  454. if (animation_set.has(p_name)) {
  455. _unref_anim(animation_set[p_name].animation);
  456. animation_set[p_name].animation=p_animation;
  457. clear_caches();
  458. } else {
  459. AnimationData ad;
  460. ad.animation=p_animation;
  461. ad.name=p_name;
  462. animation_set[p_name]=ad;
  463. }
  464. _ref_anim(p_animation);
  465. _change_notify();
  466. return OK;
  467. }
  468. void AnimationPlayer::remove_animation(const StringName& p_name) {
  469. ERR_FAIL_COND(!animation_set.has(p_name) );
  470. stop_all();
  471. _unref_anim(animation_set[p_name].animation);
  472. animation_set.erase(p_name);
  473. clear_caches();
  474. _change_notify();
  475. }
  476. void AnimationPlayer::_ref_anim(const Ref<Animation>& p_anim) {
  477. if (used_anims.has(p_anim))
  478. used_anims[p_anim]++;
  479. else {
  480. used_anims[p_anim]=1;
  481. Ref<Animation>(p_anim)->connect("changed",this,"_animation_changed");
  482. }
  483. }
  484. void AnimationPlayer::_unref_anim(const Ref<Animation>& p_anim) {
  485. ERR_FAIL_COND(!used_anims.has(p_anim));
  486. int & n = used_anims[p_anim];
  487. n--;
  488. if (n==0) {
  489. Ref<Animation>(p_anim)->disconnect("changed",this,"_animation_changed");
  490. used_anims.erase(p_anim);
  491. }
  492. }
  493. void AnimationPlayer::rename_animation(const StringName& p_name,const StringName& p_new_name) {
  494. ERR_FAIL_COND(!animation_set.has(p_name) );
  495. ERR_FAIL_COND( String(p_new_name).find("/")!=-1 || String(p_new_name).find(":")!=-1 );
  496. ERR_FAIL_COND( animation_set.has(p_new_name) );
  497. //print_line("Rename anim: "+String(p_name)+" name: "+String(p_new_name));
  498. stop_all();
  499. AnimationData ad = animation_set[p_name];
  500. ad.name=p_new_name;
  501. animation_set.erase(p_name);
  502. animation_set[p_new_name]=ad;
  503. List<BlendKey> to_erase;
  504. Map<BlendKey,float> to_insert;
  505. for(Map<BlendKey, float >::Element *E=blend_times.front();E;E=E->next()) {
  506. BlendKey bk=E->key();
  507. BlendKey new_bk=bk;
  508. bool erase=false;
  509. if (bk.from==p_name) {
  510. new_bk.from=p_new_name;
  511. erase=true;
  512. }
  513. if (bk.to==p_name) {
  514. new_bk.to=p_new_name;
  515. erase=true;
  516. }
  517. if (erase) {
  518. to_erase.push_back(bk);
  519. to_insert[new_bk]=E->get();
  520. }
  521. }
  522. while(to_erase.size()) {
  523. blend_times.erase(to_erase.front()->get());
  524. to_erase.pop_front();;
  525. }
  526. while(to_insert.size()) {
  527. blend_times[to_insert.front()->key()]=to_insert.front()->get();
  528. to_insert.erase(to_insert.front());
  529. }
  530. if (autoplay==p_name)
  531. autoplay=p_new_name;
  532. clear_caches();
  533. _change_notify();
  534. }
  535. bool AnimationPlayer::has_animation(const StringName& p_name) const {
  536. return animation_set.has(p_name);
  537. }
  538. Ref<Animation> AnimationPlayer::get_animation(const StringName& p_name) const {
  539. ERR_FAIL_COND_V( !animation_set.has(p_name), Ref<Animation>() );
  540. const AnimationData& data = animation_set[p_name];
  541. return data.animation;
  542. }
  543. void AnimationPlayer::get_animation_list( List<StringName> * p_animations) const {
  544. List<String> anims;
  545. for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
  546. anims.push_back(E->key());
  547. }
  548. anims.sort();
  549. for(List<String>::Element *E=anims.front();E;E=E->next()) {
  550. p_animations->push_back(E->get());
  551. }
  552. }
  553. void AnimationPlayer::set_blend_time(const StringName& p_animation1, const StringName& p_animation2, float p_time) {
  554. ERR_FAIL_COND(p_time<0);
  555. BlendKey bk;
  556. bk.from=p_animation1;
  557. bk.to=p_animation2;
  558. if (p_time==0)
  559. blend_times.erase(bk);
  560. else
  561. blend_times[bk]=p_time;
  562. }
  563. float AnimationPlayer::get_blend_time( const StringName& p_animation1, const StringName& p_animation2) const {
  564. BlendKey bk;
  565. bk.from=p_animation1;
  566. bk.to=p_animation2;
  567. if (blend_times.has(bk))
  568. return blend_times[bk];
  569. else
  570. return 0;
  571. }
  572. void AnimationPlayer::queue(const StringName& p_name) {
  573. if (!is_playing())
  574. play(p_name);
  575. else
  576. queued.push_back(p_name);
  577. }
  578. void AnimationPlayer::clear_queue() {
  579. queued.clear();
  580. };
  581. void AnimationPlayer::play(const StringName& p_name, float p_custom_blend, float p_custom_scale,bool p_from_end) {
  582. //printf("animation is %ls\n", String(p_name).c_str());
  583. //ERR_FAIL_COND(!is_inside_scene());
  584. StringName name=p_name;
  585. if (String(name)=="")
  586. name=playback.assigned;
  587. if (!animation_set.has(name)) {
  588. ERR_EXPLAIN("Animation not found: "+name);
  589. ERR_FAIL();
  590. }
  591. Playback &c=playback;
  592. if (c.current.from) {
  593. float blend_time=0;
  594. // find if it can blend
  595. BlendKey bk;
  596. bk.from=c.current.from->name;
  597. bk.to=name;
  598. if (p_custom_blend>=0) {
  599. blend_time=p_custom_blend;
  600. } else if (blend_times.has(bk)) {
  601. blend_time=blend_times[bk];
  602. } else {
  603. bk.from="*";
  604. if (blend_times.has(bk)) {
  605. blend_time=blend_times[bk];
  606. } else {
  607. bk.from=c.current.from->name;
  608. bk.to="*";
  609. if (blend_times.has(bk)) {
  610. blend_time=blend_times[bk];
  611. }
  612. }
  613. }
  614. if (p_custom_blend<0 && blend_time==0 && default_blend_time)
  615. blend_time=default_blend_time;
  616. if (blend_time>0) {
  617. Blend b;
  618. b.data=c.current;
  619. b.blend_time=b.blend_left=blend_time;
  620. c.blend.push_back(b);
  621. }
  622. }
  623. c.current.from=&animation_set[name];
  624. c.current.pos=p_from_end ? c.current.from->animation->get_length() : 0;
  625. c.current.speed_scale=p_custom_scale;
  626. c.assigned=p_name;
  627. if (!end_notify)
  628. queued.clear();
  629. _set_process(true); // always process when starting an animation
  630. playing = true;
  631. if (is_inside_scene() && get_scene()->is_editor_hint())
  632. return; // no next in this case
  633. StringName next=animation_get_next(p_name);
  634. if (next!=StringName()) {
  635. queue(next);
  636. }
  637. }
  638. bool AnimationPlayer::is_playing() const {
  639. return playing;
  640. /*
  641. if (playback.current.from==NULL)
  642. return false;
  643. float len=playback.current.from->animation->get_length();
  644. float pos = playback.current.pos;
  645. bool loop=playback.current.from->animation->has_loop();
  646. if (!loop && pos >= len) {
  647. return false;
  648. };
  649. return true;
  650. */
  651. }
  652. void AnimationPlayer::set_current_animation(const String& p_anim) {
  653. if (is_playing()) {
  654. play(p_anim);
  655. } else {
  656. ERR_FAIL_COND(!animation_set.has(p_anim));
  657. playback.current.pos=0;
  658. playback.current.from=&animation_set[p_anim];
  659. playback.assigned=p_anim;
  660. }
  661. }
  662. String AnimationPlayer::get_current_animation() const {
  663. return (playback.assigned);
  664. }
  665. void AnimationPlayer::stop() {
  666. Playback &c=playback;
  667. c.blend.clear();
  668. c.current.from=NULL;
  669. _set_process(false);
  670. queued.clear();
  671. playing = false;
  672. }
  673. void AnimationPlayer::stop_all() {
  674. stop();
  675. _set_process(false); // always process when starting an animation
  676. }
  677. void AnimationPlayer::set_speed(float p_speed) {
  678. speed_scale=p_speed;
  679. }
  680. float AnimationPlayer::get_speed() const {
  681. return speed_scale;
  682. }
  683. void AnimationPlayer::seek(float p_time,bool p_update) {
  684. if (!playback.current.from) {
  685. if (playback.assigned)
  686. set_current_animation(playback.assigned);
  687. ERR_FAIL_COND(!playback.current.from);
  688. }
  689. playback.current.pos=p_time;
  690. if (p_update) {
  691. _animation_process(0);
  692. }
  693. }
  694. void AnimationPlayer::seek_delta(float p_time,float p_delta) {
  695. if (!playback.current.from) {
  696. if (playback.assigned)
  697. set_current_animation(playback.assigned);
  698. ERR_FAIL_COND(!playback.current.from);
  699. }
  700. playback.current.pos=p_time-p_delta;
  701. if (speed_scale!=0.0)
  702. p_delta/=speed_scale;
  703. _animation_process(p_delta);
  704. //playback.current.pos=p_time;
  705. }
  706. bool AnimationPlayer::is_valid() const {
  707. return (playback.current.from);
  708. }
  709. float AnimationPlayer::get_current_animation_pos() const {
  710. ERR_FAIL_COND_V(!playback.current.from,0);
  711. return playback.current.pos;
  712. }
  713. float AnimationPlayer::get_current_animation_length() const {
  714. ERR_FAIL_COND_V(!playback.current.from,0);
  715. return playback.current.from->animation->get_length();
  716. }
  717. void AnimationPlayer::_animation_changed() {
  718. clear_caches();
  719. }
  720. void AnimationPlayer::_node_removed(Node *p_node) {
  721. clear_caches(); // nodes contained here ar being removed, clear the caches
  722. }
  723. void AnimationPlayer::clear_caches() {
  724. node_cache_map.clear();
  725. for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
  726. E->get().node_cache.clear();
  727. }
  728. cache_update_size=0;
  729. cache_update_prop_size=0;
  730. }
  731. void AnimationPlayer::set_active(bool p_active) {
  732. if (active==p_active)
  733. return;
  734. active=p_active;
  735. _set_process(processing,true);
  736. }
  737. bool AnimationPlayer::is_active() const {
  738. return active;
  739. }
  740. StringName AnimationPlayer::find_animation(const Ref<Animation>& p_animation) const {
  741. for( Map<StringName, AnimationData>::Element *E=animation_set.front();E;E=E->next()) {
  742. if (E->get().animation==p_animation)
  743. return E->key();
  744. }
  745. return "";
  746. }
  747. void AnimationPlayer::set_autoplay(const String& p_name) {
  748. autoplay=p_name;
  749. }
  750. String AnimationPlayer::get_autoplay() const{
  751. return autoplay;
  752. }
  753. void AnimationPlayer::set_animation_process_mode(AnimationProcessMode p_mode) {
  754. if (animation_process_mode==p_mode)
  755. return;
  756. bool pr = processing;
  757. if (pr)
  758. _set_process(false);
  759. animation_process_mode=p_mode;
  760. if (pr)
  761. _set_process(true);
  762. }
  763. AnimationPlayer::AnimationProcessMode AnimationPlayer::get_animation_process_mode() const{
  764. return animation_process_mode;
  765. }
  766. void AnimationPlayer::_set_process(bool p_process,bool p_force) {
  767. if (processing==p_process && !p_force)
  768. return;
  769. switch(animation_process_mode) {
  770. case ANIMATION_PROCESS_FIXED: set_fixed_process(p_process && active); break;
  771. case ANIMATION_PROCESS_IDLE: set_process(p_process && active); break;
  772. }
  773. processing=p_process;
  774. }
  775. void AnimationPlayer::animation_set_next(const StringName& p_animation, const StringName& p_next) {
  776. ERR_FAIL_COND(!animation_set.has(p_animation));
  777. animation_set[p_animation].next=p_next;
  778. }
  779. StringName AnimationPlayer::animation_get_next(const StringName& p_animation) const{
  780. if (!animation_set.has(p_animation))
  781. return StringName();
  782. return animation_set[p_animation].next;
  783. }
  784. void AnimationPlayer::set_default_blend_time(float p_default) {
  785. default_blend_time=p_default;
  786. }
  787. float AnimationPlayer::get_default_blend_time() const {
  788. return default_blend_time;
  789. }
  790. void AnimationPlayer::set_root(const NodePath& p_root) {
  791. root=p_root;
  792. clear_caches();
  793. }
  794. NodePath AnimationPlayer::get_root() const {
  795. return root;
  796. }
  797. void AnimationPlayer::_bind_methods() {
  798. ObjectTypeDB::bind_method(_MD("_node_removed"),&AnimationPlayer::_node_removed);
  799. ObjectTypeDB::bind_method(_MD("_animation_changed"),&AnimationPlayer::_animation_changed);
  800. ObjectTypeDB::bind_method(_MD("add_animation","name","animation:Animation"),&AnimationPlayer::add_animation);
  801. ObjectTypeDB::bind_method(_MD("remove_animation","name"),&AnimationPlayer::remove_animation);
  802. ObjectTypeDB::bind_method(_MD("rename_animation","name","newname"),&AnimationPlayer::rename_animation);
  803. ObjectTypeDB::bind_method(_MD("has_animation","name"),&AnimationPlayer::has_animation);
  804. ObjectTypeDB::bind_method(_MD("get_animation:Animation","name"),&AnimationPlayer::get_animation);
  805. ObjectTypeDB::bind_method(_MD("get_animation_list"),&AnimationPlayer::_get_animation_list);
  806. ObjectTypeDB::bind_method(_MD("set_blend_time","anim_from","anim_to","sec"),&AnimationPlayer::set_blend_time);
  807. ObjectTypeDB::bind_method(_MD("get_blend_time","anim_from","anim_to"),&AnimationPlayer::get_blend_time);
  808. ObjectTypeDB::bind_method(_MD("set_default_blend_time","sec"),&AnimationPlayer::set_default_blend_time);
  809. ObjectTypeDB::bind_method(_MD("get_default_blend_time"),&AnimationPlayer::get_default_blend_time);
  810. ObjectTypeDB::bind_method(_MD("play","name","custom_blend","custom_speed","from_end"),&AnimationPlayer::play,DEFVAL(""),DEFVAL(-1),DEFVAL(1.0),DEFVAL(false));
  811. ObjectTypeDB::bind_method(_MD("stop"),&AnimationPlayer::stop);
  812. ObjectTypeDB::bind_method(_MD("stop_all"),&AnimationPlayer::stop_all);
  813. ObjectTypeDB::bind_method(_MD("is_playing"),&AnimationPlayer::is_playing);
  814. ObjectTypeDB::bind_method(_MD("set_current_animation","anim"),&AnimationPlayer::set_current_animation);
  815. ObjectTypeDB::bind_method(_MD("get_current_animation"),&AnimationPlayer::get_current_animation);
  816. ObjectTypeDB::bind_method(_MD("queue","name"),&AnimationPlayer::queue);
  817. ObjectTypeDB::bind_method(_MD("clear_queue"),&AnimationPlayer::clear_queue);
  818. ObjectTypeDB::bind_method(_MD("set_active","active"),&AnimationPlayer::set_active);
  819. ObjectTypeDB::bind_method(_MD("is_active"),&AnimationPlayer::is_active);
  820. ObjectTypeDB::bind_method(_MD("set_speed","speed"),&AnimationPlayer::set_speed);
  821. ObjectTypeDB::bind_method(_MD("get_speed"),&AnimationPlayer::get_speed);
  822. ObjectTypeDB::bind_method(_MD("set_autoplay","name"),&AnimationPlayer::set_autoplay);
  823. ObjectTypeDB::bind_method(_MD("get_autoplay"),&AnimationPlayer::get_autoplay);
  824. ObjectTypeDB::bind_method(_MD("set_root","path"),&AnimationPlayer::set_root);
  825. ObjectTypeDB::bind_method(_MD("get_root"),&AnimationPlayer::get_root);
  826. ObjectTypeDB::bind_method(_MD("seek","pos_sec","update"),&AnimationPlayer::seek,DEFVAL(false));
  827. ObjectTypeDB::bind_method(_MD("get_pos"),&AnimationPlayer::get_current_animation_pos);
  828. ObjectTypeDB::bind_method(_MD("find_animation","animation:Animation"),&AnimationPlayer::find_animation);
  829. ObjectTypeDB::bind_method(_MD("clear_caches"),&AnimationPlayer::clear_caches);
  830. ObjectTypeDB::bind_method(_MD("set_animation_process_mode","mode"),&AnimationPlayer::set_animation_process_mode);
  831. ObjectTypeDB::bind_method(_MD("get_animation_process_mode"),&AnimationPlayer::get_animation_process_mode);
  832. ObjectTypeDB::bind_method(_MD("get_current_animation_pos"),&AnimationPlayer::get_current_animation_pos);
  833. ObjectTypeDB::bind_method(_MD("get_current_animation_length"),&AnimationPlayer::get_current_animation_length);
  834. ADD_PROPERTY( PropertyInfo( Variant::INT, "playback/process_mode", PROPERTY_HINT_ENUM, "Fixed,Idle"), _SCS("set_animation_process_mode"), _SCS("get_animation_process_mode"));
  835. ADD_PROPERTY( PropertyInfo( Variant::REAL, "playback/default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01"), _SCS("set_default_blend_time"), _SCS("get_default_blend_time"));
  836. ADD_PROPERTY( PropertyInfo( Variant::NODE_PATH, "root/root"), _SCS("set_root"), _SCS("get_root"));
  837. ADD_SIGNAL( MethodInfo("finished") );
  838. ADD_SIGNAL( MethodInfo("animation_changed", PropertyInfo(Variant::STRING,"old_name"), PropertyInfo(Variant::STRING,"new_name")) );
  839. BIND_CONSTANT( ANIMATION_PROCESS_FIXED );
  840. BIND_CONSTANT( ANIMATION_PROCESS_IDLE );
  841. }
  842. AnimationPlayer::AnimationPlayer() {
  843. accum_pass=1;
  844. cache_update_size=0;
  845. cache_update_prop_size=0;
  846. speed_scale=1;
  847. end_notify=false;
  848. animation_process_mode=ANIMATION_PROCESS_IDLE;
  849. processing=false;
  850. default_blend_time=0;
  851. root=NodePath("..");
  852. playing = false;
  853. active=true;
  854. }
  855. AnimationPlayer::~AnimationPlayer()
  856. {
  857. }