animation_player.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  1. /*************************************************************************/
  2. /* animation_player.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 "animation_player.h"
  31. #include "core/engine.h"
  32. #include "core/message_queue.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/audio/audio_stream.h"
  35. #ifdef TOOLS_ENABLED
  36. void AnimatedValuesBackup::update_skeletons() {
  37. for (int i = 0; i < entries.size(); i++) {
  38. if (entries[i].bone_idx != -1) {
  39. Object::cast_to<Skeleton>(entries[i].object)->notification(Skeleton::NOTIFICATION_UPDATE_SKELETON);
  40. }
  41. }
  42. }
  43. #endif
  44. bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
  45. String name = p_name;
  46. if (name.begins_with("playback/play")) { // bw compatibility
  47. set_current_animation(p_value);
  48. } else if (name.begins_with("anims/")) {
  49. String which = name.get_slicec('/', 1);
  50. add_animation(which, p_value);
  51. } else if (name.begins_with("next/")) {
  52. String which = name.get_slicec('/', 1);
  53. animation_set_next(which, p_value);
  54. } else if (p_name == SceneStringNames::get_singleton()->blend_times) {
  55. Array array = p_value;
  56. int len = array.size();
  57. ERR_FAIL_COND_V(len % 3, false);
  58. for (int i = 0; i < len / 3; i++) {
  59. StringName from = array[i * 3 + 0];
  60. StringName to = array[i * 3 + 1];
  61. float time = array[i * 3 + 2];
  62. set_blend_time(from, to, time);
  63. }
  64. } else
  65. return false;
  66. return true;
  67. }
  68. bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
  69. String name = p_name;
  70. if (name == "playback/play") { // bw compatibility
  71. r_ret = get_current_animation();
  72. } else if (name.begins_with("anims/")) {
  73. String which = name.get_slicec('/', 1);
  74. r_ret = get_animation(which).get_ref_ptr();
  75. } else if (name.begins_with("next/")) {
  76. String which = name.get_slicec('/', 1);
  77. r_ret = animation_get_next(which);
  78. } else if (name == "blend_times") {
  79. Vector<BlendKey> keys;
  80. for (Map<BlendKey, float>::Element *E = blend_times.front(); E; E = E->next()) {
  81. keys.ordered_insert(E->key());
  82. }
  83. Array array;
  84. for (int i = 0; i < keys.size(); i++) {
  85. array.push_back(keys[i].from);
  86. array.push_back(keys[i].to);
  87. array.push_back(blend_times[keys[i]]);
  88. }
  89. r_ret = array;
  90. } else
  91. return false;
  92. return true;
  93. }
  94. void AnimationPlayer::_validate_property(PropertyInfo &property) const {
  95. if (property.name == "current_animation") {
  96. List<String> names;
  97. for (Map<StringName, AnimationData>::Element *E = animation_set.front(); E; E = E->next()) {
  98. names.push_back(E->key());
  99. }
  100. names.sort();
  101. names.push_front("[stop]");
  102. String hint;
  103. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  104. if (E != names.front())
  105. hint += ",";
  106. hint += E->get();
  107. }
  108. property.hint_string = hint;
  109. }
  110. }
  111. void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  112. List<PropertyInfo> anim_names;
  113. for (Map<StringName, AnimationData>::Element *E = animation_set.front(); E; E = E->next()) {
  114. anim_names.push_back(PropertyInfo(Variant::OBJECT, "anims/" + String(E->key()), PROPERTY_HINT_RESOURCE_TYPE, "Animation", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  115. if (E->get().next != StringName())
  116. anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E->key()), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  117. }
  118. anim_names.sort();
  119. for (List<PropertyInfo>::Element *E = anim_names.front(); E; E = E->next()) {
  120. p_list->push_back(E->get());
  121. }
  122. p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  123. }
  124. void AnimationPlayer::advance(float p_time) {
  125. _animation_process(p_time);
  126. }
  127. void AnimationPlayer::_notification(int p_what) {
  128. switch (p_what) {
  129. case NOTIFICATION_ENTER_TREE: {
  130. if (!processing) {
  131. //make sure that a previous process state was not saved
  132. //only process if "processing" is set
  133. set_physics_process_internal(false);
  134. set_process_internal(false);
  135. }
  136. //_set_process(false);
  137. clear_caches();
  138. } break;
  139. case NOTIFICATION_READY: {
  140. if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
  141. play(autoplay);
  142. _animation_process(0);
  143. }
  144. } break;
  145. case NOTIFICATION_INTERNAL_PROCESS: {
  146. if (animation_process_mode == ANIMATION_PROCESS_PHYSICS)
  147. break;
  148. if (processing)
  149. _animation_process(get_process_delta_time());
  150. } break;
  151. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  152. if (animation_process_mode == ANIMATION_PROCESS_IDLE)
  153. break;
  154. if (processing)
  155. _animation_process(get_physics_process_delta_time());
  156. } break;
  157. case NOTIFICATION_EXIT_TREE: {
  158. clear_caches();
  159. } break;
  160. }
  161. }
  162. void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) {
  163. // Already cached?
  164. if (p_anim->node_cache.size() == p_anim->animation->get_track_count())
  165. return;
  166. Node *parent = get_node(root);
  167. ERR_FAIL_COND(!parent);
  168. Animation *a = p_anim->animation.operator->();
  169. p_anim->node_cache.resize(a->get_track_count());
  170. for (int i = 0; i < a->get_track_count(); i++) {
  171. p_anim->node_cache.write[i] = NULL;
  172. RES resource;
  173. Vector<StringName> leftover_path;
  174. Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path);
  175. if (!child) {
  176. ERR_EXPLAIN("On Animation: '" + p_anim->name + "', couldn't resolve track: '" + String(a->track_get_path(i)) + "'");
  177. }
  178. ERR_CONTINUE(!child); // couldn't find the child node
  179. uint32_t id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id();
  180. int bone_idx = -1;
  181. if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton>(child)) {
  182. Skeleton *sk = Object::cast_to<Skeleton>(child);
  183. bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0));
  184. if (bone_idx == -1 || sk->is_bone_ignore_animation(bone_idx)) {
  185. continue;
  186. }
  187. }
  188. {
  189. if (!child->is_connected("tree_exiting", this, "_node_removed"))
  190. child->connect("tree_exiting", this, "_node_removed", make_binds(child), CONNECT_ONESHOT);
  191. }
  192. TrackNodeCacheKey key;
  193. key.id = id;
  194. key.bone_idx = bone_idx;
  195. if (!node_cache_map.has(key))
  196. node_cache_map[key] = TrackNodeCache();
  197. p_anim->node_cache.write[i] = &node_cache_map[key];
  198. p_anim->node_cache[i]->path = a->track_get_path(i);
  199. p_anim->node_cache[i]->node = child;
  200. p_anim->node_cache[i]->resource = resource;
  201. p_anim->node_cache[i]->node_2d = Object::cast_to<Node2D>(child);
  202. if (a->track_get_type(i) == Animation::TYPE_TRANSFORM) {
  203. // special cases and caches for transform tracks
  204. // cache spatial
  205. p_anim->node_cache[i]->spatial = Object::cast_to<Spatial>(child);
  206. // cache skeleton
  207. p_anim->node_cache[i]->skeleton = Object::cast_to<Skeleton>(child);
  208. if (p_anim->node_cache[i]->skeleton) {
  209. if (a->track_get_path(i).get_subname_count() == 1) {
  210. StringName bone_name = a->track_get_path(i).get_subname(0);
  211. p_anim->node_cache[i]->bone_idx = p_anim->node_cache[i]->skeleton->find_bone(bone_name);
  212. if (p_anim->node_cache[i]->bone_idx < 0) {
  213. // broken track (nonexistent bone)
  214. p_anim->node_cache[i]->skeleton = NULL;
  215. p_anim->node_cache[i]->spatial = NULL;
  216. ERR_CONTINUE(p_anim->node_cache[i]->bone_idx < 0);
  217. }
  218. } else {
  219. // no property, just use spatialnode
  220. p_anim->node_cache[i]->skeleton = NULL;
  221. }
  222. }
  223. }
  224. if (a->track_get_type(i) == Animation::TYPE_VALUE) {
  225. if (!p_anim->node_cache[i]->property_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  226. TrackNodeCache::PropertyAnim pa;
  227. pa.subpath = leftover_path;
  228. pa.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  229. pa.special = SP_NONE;
  230. pa.owner = p_anim->node_cache[i];
  231. if (false && p_anim->node_cache[i]->node_2d) {
  232. if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_pos)
  233. pa.special = SP_NODE2D_POS;
  234. else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_rot)
  235. pa.special = SP_NODE2D_ROT;
  236. else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_scale)
  237. pa.special = SP_NODE2D_SCALE;
  238. }
  239. p_anim->node_cache[i]->property_anim[a->track_get_path(i).get_concatenated_subnames()] = pa;
  240. }
  241. }
  242. if (a->track_get_type(i) == Animation::TYPE_BEZIER && leftover_path.size()) {
  243. if (!p_anim->node_cache[i]->bezier_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  244. TrackNodeCache::BezierAnim ba;
  245. ba.bezier_property = leftover_path;
  246. ba.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  247. ba.owner = p_anim->node_cache[i];
  248. p_anim->node_cache[i]->bezier_anim[a->track_get_path(i).get_concatenated_subnames()] = ba;
  249. }
  250. }
  251. }
  252. }
  253. void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, float p_time, float p_delta, float p_interp, bool p_is_current, bool p_seeked, bool p_started) {
  254. _ensure_node_caches(p_anim);
  255. ERR_FAIL_COND(p_anim->node_cache.size() != p_anim->animation->get_track_count());
  256. Animation *a = p_anim->animation.operator->();
  257. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  258. for (int i = 0; i < a->get_track_count(); i++) {
  259. // If an animation changes this animation (or it animates itself)
  260. // we need to recreate our animation cache
  261. if (p_anim->node_cache.size() != a->get_track_count()) {
  262. _ensure_node_caches(p_anim);
  263. }
  264. TrackNodeCache *nc = p_anim->node_cache[i];
  265. if (!nc)
  266. continue; // no node cache for this track, skip it
  267. if (!a->track_is_enabled(i))
  268. continue; // do nothing if the track is disabled
  269. if (a->track_get_key_count(i) == 0)
  270. continue; // do nothing if track is empty
  271. switch (a->track_get_type(i)) {
  272. case Animation::TYPE_TRANSFORM: {
  273. if (!nc->spatial)
  274. continue;
  275. Vector3 loc;
  276. Quat rot;
  277. Vector3 scale;
  278. Error err = a->transform_track_interpolate(i, p_time, &loc, &rot, &scale);
  279. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  280. if (err != OK)
  281. continue;
  282. if (nc->accum_pass != accum_pass) {
  283. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  284. cache_update[cache_update_size++] = nc;
  285. nc->accum_pass = accum_pass;
  286. nc->loc_accum = loc;
  287. nc->rot_accum = rot;
  288. nc->scale_accum = scale;
  289. } else {
  290. nc->loc_accum = nc->loc_accum.linear_interpolate(loc, p_interp);
  291. nc->rot_accum = nc->rot_accum.slerp(rot, p_interp);
  292. nc->scale_accum = nc->scale_accum.linear_interpolate(scale, p_interp);
  293. }
  294. } break;
  295. case Animation::TYPE_VALUE: {
  296. if (!nc->node)
  297. continue;
  298. //StringName property=a->track_get_path(i).get_property();
  299. Map<StringName, TrackNodeCache::PropertyAnim>::Element *E = nc->property_anim.find(a->track_get_path(i).get_concatenated_subnames());
  300. ERR_CONTINUE(!E); //should it continue, or create a new one?
  301. TrackNodeCache::PropertyAnim *pa = &E->get();
  302. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  303. if (update_mode == Animation::UPDATE_CAPTURE) {
  304. if (p_started) {
  305. pa->capture = pa->object->get_indexed(pa->subpath);
  306. }
  307. int key_count = a->track_get_key_count(i);
  308. if (key_count == 0)
  309. continue; //eeh not worth it
  310. float first_key_time = a->track_get_key_time(i, 0);
  311. float transition = 1.0;
  312. int first_key = 0;
  313. if (first_key_time == 0.0) {
  314. //ignore, use for transition
  315. if (key_count == 1)
  316. continue; //with one key we can't do anything
  317. transition = a->track_get_key_transition(i, 0);
  318. first_key_time = a->track_get_key_time(i, 1);
  319. first_key = 1;
  320. }
  321. if (p_time < first_key_time) {
  322. float c = Math::ease(p_time / first_key_time, transition);
  323. Variant first_value = a->track_get_key_value(i, first_key);
  324. Variant interp_value;
  325. Variant::interpolate(pa->capture, first_value, c, interp_value);
  326. if (pa->accum_pass != accum_pass) {
  327. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  328. cache_update_prop[cache_update_prop_size++] = pa;
  329. pa->value_accum = interp_value;
  330. pa->accum_pass = accum_pass;
  331. } else {
  332. Variant::interpolate(pa->value_accum, interp_value, p_interp, pa->value_accum);
  333. }
  334. continue; //handled
  335. }
  336. }
  337. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE || (p_delta == 0 && update_mode == Animation::UPDATE_DISCRETE)) { //delta == 0 means seek
  338. Variant value = a->value_track_interpolate(i, p_time);
  339. if (value == Variant())
  340. continue;
  341. //thanks to trigger mode, this should be solved now..
  342. /*
  343. if (p_delta==0 && value.get_type()==Variant::STRING)
  344. continue; // doing this with strings is messy, should find another way
  345. */
  346. if (pa->accum_pass != accum_pass) {
  347. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  348. cache_update_prop[cache_update_prop_size++] = pa;
  349. pa->value_accum = value;
  350. pa->accum_pass = accum_pass;
  351. } else {
  352. Variant::interpolate(pa->value_accum, value, p_interp, pa->value_accum);
  353. }
  354. } else if (p_is_current && p_delta != 0) {
  355. List<int> indices;
  356. a->value_track_get_key_indices(i, p_time, p_delta, &indices);
  357. for (List<int>::Element *F = indices.front(); F; F = F->next()) {
  358. Variant value = a->track_get_key_value(i, F->get());
  359. switch (pa->special) {
  360. case SP_NONE: {
  361. bool valid;
  362. pa->object->set_indexed(pa->subpath, value, &valid); //you are not speshul
  363. #ifdef DEBUG_ENABLED
  364. if (!valid) {
  365. ERR_PRINTS("Failed setting track value '" + String(pa->owner->path) + "'. Check if property exists or the type of key is valid. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  366. }
  367. #endif
  368. } break;
  369. case SP_NODE2D_POS: {
  370. #ifdef DEBUG_ENABLED
  371. if (value.get_type() != Variant::VECTOR2) {
  372. ERR_PRINTS("Position key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not of type Vector2(). Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  373. }
  374. #endif
  375. static_cast<Node2D *>(pa->object)->set_position(value);
  376. } break;
  377. case SP_NODE2D_ROT: {
  378. #ifdef DEBUG_ENABLED
  379. if (value.is_num()) {
  380. ERR_PRINTS("Rotation key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not numerical. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  381. }
  382. #endif
  383. static_cast<Node2D *>(pa->object)->set_rotation(Math::deg2rad((double)value));
  384. } break;
  385. case SP_NODE2D_SCALE: {
  386. #ifdef DEBUG_ENABLED
  387. if (value.get_type() != Variant::VECTOR2) {
  388. ERR_PRINTS("Scale key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not of type Vector2()." + a->get_name() + "' at node '" + get_path() + "'.");
  389. }
  390. #endif
  391. static_cast<Node2D *>(pa->object)->set_scale(value);
  392. } break;
  393. }
  394. }
  395. }
  396. } break;
  397. case Animation::TYPE_METHOD: {
  398. if (!nc->node)
  399. continue;
  400. if (p_delta == 0) {
  401. continue;
  402. }
  403. if (!p_is_current)
  404. break;
  405. List<int> indices;
  406. a->method_track_get_key_indices(i, p_time, p_delta, &indices);
  407. for (List<int>::Element *E = indices.front(); E; E = E->next()) {
  408. StringName method = a->method_track_get_name(i, E->get());
  409. Vector<Variant> params = a->method_track_get_params(i, E->get());
  410. int s = params.size();
  411. ERR_CONTINUE(s > VARIANT_ARG_MAX);
  412. #ifdef DEBUG_ENABLED
  413. if (!nc->node->has_method(method)) {
  414. ERR_PRINTS("Invalid method call '" + method + "'. '" + a->get_name() + "' at node '" + get_path() + "'.");
  415. }
  416. #endif
  417. if (can_call) {
  418. MessageQueue::get_singleton()->push_call(
  419. nc->node,
  420. method,
  421. s >= 1 ? params[0] : Variant(),
  422. s >= 2 ? params[1] : Variant(),
  423. s >= 3 ? params[2] : Variant(),
  424. s >= 4 ? params[3] : Variant(),
  425. s >= 5 ? params[4] : Variant());
  426. }
  427. }
  428. } break;
  429. case Animation::TYPE_BEZIER: {
  430. if (!nc->node)
  431. continue;
  432. Map<StringName, TrackNodeCache::BezierAnim>::Element *E = nc->bezier_anim.find(a->track_get_path(i).get_concatenated_subnames());
  433. ERR_CONTINUE(!E); //should it continue, or create a new one?
  434. TrackNodeCache::BezierAnim *ba = &E->get();
  435. float bezier = a->bezier_track_interpolate(i, p_time);
  436. if (ba->accum_pass != accum_pass) {
  437. ERR_CONTINUE(cache_update_bezier_size >= NODE_CACHE_UPDATE_MAX);
  438. cache_update_bezier[cache_update_bezier_size++] = ba;
  439. ba->bezier_accum = bezier;
  440. ba->accum_pass = accum_pass;
  441. } else {
  442. ba->bezier_accum = Math::lerp(ba->bezier_accum, bezier, p_interp);
  443. }
  444. } break;
  445. case Animation::TYPE_AUDIO: {
  446. if (!nc->node)
  447. continue;
  448. if (p_delta == 0) {
  449. continue;
  450. }
  451. if (p_seeked) {
  452. //find whathever should be playing
  453. int idx = a->track_find_key(i, p_time);
  454. if (idx < 0)
  455. continue;
  456. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  457. if (!stream.is_valid()) {
  458. nc->node->call("stop");
  459. nc->audio_playing = false;
  460. playing_caches.erase(nc);
  461. } else {
  462. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  463. start_ofs += p_time - a->track_get_key_time(i, idx);
  464. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  465. float len = stream->get_length();
  466. if (start_ofs > len - end_ofs) {
  467. nc->node->call("stop");
  468. nc->audio_playing = false;
  469. playing_caches.erase(nc);
  470. continue;
  471. }
  472. nc->node->call("set_stream", stream);
  473. nc->node->call("play", start_ofs);
  474. nc->audio_playing = true;
  475. playing_caches.insert(nc);
  476. if (len && end_ofs > 0) { //force a end at a time
  477. nc->audio_len = len - start_ofs - end_ofs;
  478. } else {
  479. nc->audio_len = 0;
  480. }
  481. nc->audio_start = p_time;
  482. }
  483. } else {
  484. //find stuff to play
  485. List<int> to_play;
  486. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play);
  487. if (to_play.size()) {
  488. int idx = to_play.back()->get();
  489. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  490. if (!stream.is_valid()) {
  491. nc->node->call("stop");
  492. nc->audio_playing = false;
  493. playing_caches.erase(nc);
  494. } else {
  495. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  496. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  497. float len = stream->get_length();
  498. nc->node->call("set_stream", stream);
  499. nc->node->call("play", start_ofs);
  500. nc->audio_playing = true;
  501. playing_caches.insert(nc);
  502. if (len && end_ofs > 0) { //force a end at a time
  503. nc->audio_len = len - start_ofs - end_ofs;
  504. } else {
  505. nc->audio_len = 0;
  506. }
  507. nc->audio_start = p_time;
  508. }
  509. } else if (nc->audio_playing) {
  510. bool loop = a->has_loop();
  511. bool stop = false;
  512. if (!loop && p_time < nc->audio_start) {
  513. stop = true;
  514. } else if (nc->audio_len > 0) {
  515. float len = nc->audio_start > p_time ? (a->get_length() - nc->audio_start) + p_time : p_time - nc->audio_start;
  516. if (len > nc->audio_len) {
  517. stop = true;
  518. }
  519. }
  520. if (stop) {
  521. //time to stop
  522. nc->node->call("stop");
  523. nc->audio_playing = false;
  524. playing_caches.erase(nc);
  525. }
  526. }
  527. }
  528. } break;
  529. case Animation::TYPE_ANIMATION: {
  530. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(nc->node);
  531. if (!player)
  532. continue;
  533. if (p_delta == 0 || p_seeked) {
  534. //seek
  535. int idx = a->track_find_key(i, p_time);
  536. if (idx < 0)
  537. continue;
  538. float pos = a->track_get_key_time(i, idx);
  539. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  540. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name))
  541. continue;
  542. Ref<Animation> anim = player->get_animation(anim_name);
  543. float at_anim_pos;
  544. if (anim->has_loop()) {
  545. at_anim_pos = Math::fposmod(p_time - pos, anim->get_length()); //seek to loop
  546. } else {
  547. at_anim_pos = MAX(anim->get_length(), p_time - pos); //seek to end
  548. }
  549. if (player->is_playing() || p_seeked) {
  550. player->play(anim_name);
  551. player->seek(at_anim_pos);
  552. nc->animation_playing = true;
  553. playing_caches.insert(nc);
  554. } else {
  555. player->set_assigned_animation(anim_name);
  556. player->seek(at_anim_pos, true);
  557. }
  558. } else {
  559. //find stuff to play
  560. List<int> to_play;
  561. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play);
  562. if (to_play.size()) {
  563. int idx = to_play.back()->get();
  564. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  565. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  566. if (playing_caches.has(nc)) {
  567. playing_caches.erase(nc);
  568. player->stop();
  569. nc->animation_playing = false;
  570. }
  571. } else {
  572. player->play(anim_name);
  573. nc->animation_playing = true;
  574. playing_caches.insert(nc);
  575. }
  576. }
  577. }
  578. } break;
  579. }
  580. }
  581. }
  582. void AnimationPlayer::_animation_process_data(PlaybackData &cd, float p_delta, float p_blend, bool p_seeked, bool p_started) {
  583. float delta = p_delta * speed_scale * cd.speed_scale;
  584. float next_pos = cd.pos + delta;
  585. float len = cd.from->animation->get_length();
  586. bool loop = cd.from->animation->has_loop();
  587. if (!loop) {
  588. if (next_pos < 0)
  589. next_pos = 0;
  590. else if (next_pos > len)
  591. next_pos = len;
  592. // fix delta
  593. delta = next_pos - cd.pos;
  594. if (&cd == &playback.current) {
  595. bool backwards = delta < 0;
  596. if (!backwards && cd.pos <= len && next_pos == len /*&& playback.blend.empty()*/) {
  597. //playback finished
  598. end_reached = true;
  599. end_notify = cd.pos < len; // Notify only if not already at the end
  600. }
  601. if (backwards && cd.pos >= 0 && next_pos == 0 /*&& playback.blend.empty()*/) {
  602. //playback finished
  603. end_reached = true;
  604. end_notify = cd.pos > 0; // Notify only if not already at the beginning
  605. }
  606. }
  607. } else {
  608. float looped_next_pos = Math::fposmod(next_pos, len);
  609. if (looped_next_pos == 0 && next_pos != 0) {
  610. // Loop multiples of the length to it, rather than 0
  611. // so state at time=length is previewable in the editor
  612. next_pos = len;
  613. } else {
  614. next_pos = looped_next_pos;
  615. }
  616. }
  617. cd.pos = next_pos;
  618. _animation_process_animation(cd.from, cd.pos, delta, p_blend, &cd == &playback.current, p_seeked, p_started);
  619. }
  620. void AnimationPlayer::_animation_process2(float p_delta, bool p_started) {
  621. Playback &c = playback;
  622. accum_pass++;
  623. _animation_process_data(c.current, p_delta, 1.0f, c.seeked && p_delta != 0, p_started);
  624. if (p_delta != 0) {
  625. c.seeked = false;
  626. }
  627. List<Blend>::Element *prev = NULL;
  628. for (List<Blend>::Element *E = c.blend.back(); E; E = prev) {
  629. Blend &b = E->get();
  630. float blend = b.blend_left / b.blend_time;
  631. _animation_process_data(b.data, p_delta, blend, false, false);
  632. b.blend_left -= Math::absf(speed_scale * p_delta);
  633. prev = E->prev();
  634. if (b.blend_left < 0) {
  635. c.blend.erase(E);
  636. }
  637. }
  638. }
  639. void AnimationPlayer::_animation_update_transforms() {
  640. {
  641. Transform t;
  642. for (int i = 0; i < cache_update_size; i++) {
  643. TrackNodeCache *nc = cache_update[i];
  644. ERR_CONTINUE(nc->accum_pass != accum_pass);
  645. t.origin = nc->loc_accum;
  646. t.basis.set_quat_scale(nc->rot_accum, nc->scale_accum);
  647. if (nc->skeleton && nc->bone_idx >= 0) {
  648. nc->skeleton->set_bone_pose(nc->bone_idx, t);
  649. } else if (nc->spatial) {
  650. nc->spatial->set_transform(t);
  651. }
  652. }
  653. }
  654. cache_update_size = 0;
  655. for (int i = 0; i < cache_update_prop_size; i++) {
  656. TrackNodeCache::PropertyAnim *pa = cache_update_prop[i];
  657. ERR_CONTINUE(pa->accum_pass != accum_pass);
  658. switch (pa->special) {
  659. case SP_NONE: {
  660. bool valid;
  661. pa->object->set_indexed(pa->subpath, pa->value_accum, &valid); //you are not speshul
  662. #ifdef DEBUG_ENABLED
  663. if (!valid) {
  664. ERR_PRINTS("Failed setting key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "'. Check if property exists or the type of key is right for the property");
  665. }
  666. #endif
  667. } break;
  668. case SP_NODE2D_POS: {
  669. #ifdef DEBUG_ENABLED
  670. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  671. ERR_PRINTS("Position key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not of type Vector2()");
  672. }
  673. #endif
  674. static_cast<Node2D *>(pa->object)->set_position(pa->value_accum);
  675. } break;
  676. case SP_NODE2D_ROT: {
  677. #ifdef DEBUG_ENABLED
  678. if (pa->value_accum.is_num()) {
  679. ERR_PRINTS("Rotation key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not numerical");
  680. }
  681. #endif
  682. static_cast<Node2D *>(pa->object)->set_rotation(Math::deg2rad((double)pa->value_accum));
  683. } break;
  684. case SP_NODE2D_SCALE: {
  685. #ifdef DEBUG_ENABLED
  686. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  687. ERR_PRINTS("Scale key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not of type Vector2()");
  688. }
  689. #endif
  690. static_cast<Node2D *>(pa->object)->set_scale(pa->value_accum);
  691. } break;
  692. }
  693. }
  694. cache_update_prop_size = 0;
  695. for (int i = 0; i < cache_update_bezier_size; i++) {
  696. TrackNodeCache::BezierAnim *ba = cache_update_bezier[i];
  697. ERR_CONTINUE(ba->accum_pass != accum_pass);
  698. ba->object->set_indexed(ba->bezier_property, ba->bezier_accum);
  699. }
  700. cache_update_bezier_size = 0;
  701. }
  702. void AnimationPlayer::_animation_process(float p_delta) {
  703. if (playback.current.from) {
  704. end_reached = false;
  705. end_notify = false;
  706. _animation_process2(p_delta, playback.started);
  707. if (playback.started) {
  708. playback.started = false;
  709. }
  710. _animation_update_transforms();
  711. if (end_reached) {
  712. if (queued.size()) {
  713. String old = playback.assigned;
  714. play(queued.front()->get());
  715. String new_name = playback.assigned;
  716. queued.pop_front();
  717. if (end_notify)
  718. emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
  719. } else {
  720. //stop();
  721. playing = false;
  722. _set_process(false);
  723. if (end_notify)
  724. emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned);
  725. }
  726. end_reached = false;
  727. }
  728. } else {
  729. _set_process(false);
  730. }
  731. }
  732. Error AnimationPlayer::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) {
  733. #ifdef DEBUG_ENABLED
  734. ERR_EXPLAIN("Invalid animation name: " + String(p_name));
  735. 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);
  736. #endif
  737. ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER);
  738. if (animation_set.has(p_name)) {
  739. _unref_anim(animation_set[p_name].animation);
  740. animation_set[p_name].animation = p_animation;
  741. clear_caches();
  742. } else {
  743. AnimationData ad;
  744. ad.animation = p_animation;
  745. ad.name = p_name;
  746. animation_set[p_name] = ad;
  747. }
  748. _ref_anim(p_animation);
  749. _change_notify();
  750. return OK;
  751. }
  752. void AnimationPlayer::remove_animation(const StringName &p_name) {
  753. ERR_FAIL_COND(!animation_set.has(p_name));
  754. stop();
  755. _unref_anim(animation_set[p_name].animation);
  756. animation_set.erase(p_name);
  757. clear_caches();
  758. _change_notify();
  759. }
  760. void AnimationPlayer::_ref_anim(const Ref<Animation> &p_anim) {
  761. Ref<Animation>(p_anim)->connect(SceneStringNames::get_singleton()->tracks_changed, this, "_animation_changed", varray(), CONNECT_REFERENCE_COUNTED);
  762. }
  763. void AnimationPlayer::_unref_anim(const Ref<Animation> &p_anim) {
  764. Ref<Animation>(p_anim)->disconnect(SceneStringNames::get_singleton()->tracks_changed, this, "_animation_changed");
  765. }
  766. void AnimationPlayer::rename_animation(const StringName &p_name, const StringName &p_new_name) {
  767. ERR_FAIL_COND(!animation_set.has(p_name));
  768. ERR_FAIL_COND(String(p_new_name).find("/") != -1 || String(p_new_name).find(":") != -1);
  769. ERR_FAIL_COND(animation_set.has(p_new_name));
  770. stop();
  771. AnimationData ad = animation_set[p_name];
  772. ad.name = p_new_name;
  773. animation_set.erase(p_name);
  774. animation_set[p_new_name] = ad;
  775. List<BlendKey> to_erase;
  776. Map<BlendKey, float> to_insert;
  777. for (Map<BlendKey, float>::Element *E = blend_times.front(); E; E = E->next()) {
  778. BlendKey bk = E->key();
  779. BlendKey new_bk = bk;
  780. bool erase = false;
  781. if (bk.from == p_name) {
  782. new_bk.from = p_new_name;
  783. erase = true;
  784. }
  785. if (bk.to == p_name) {
  786. new_bk.to = p_new_name;
  787. erase = true;
  788. }
  789. if (erase) {
  790. to_erase.push_back(bk);
  791. to_insert[new_bk] = E->get();
  792. }
  793. }
  794. while (to_erase.size()) {
  795. blend_times.erase(to_erase.front()->get());
  796. to_erase.pop_front();
  797. }
  798. while (to_insert.size()) {
  799. blend_times[to_insert.front()->key()] = to_insert.front()->get();
  800. to_insert.erase(to_insert.front());
  801. }
  802. if (autoplay == p_name)
  803. autoplay = p_new_name;
  804. clear_caches();
  805. _change_notify();
  806. }
  807. bool AnimationPlayer::has_animation(const StringName &p_name) const {
  808. return animation_set.has(p_name);
  809. }
  810. Ref<Animation> AnimationPlayer::get_animation(const StringName &p_name) const {
  811. ERR_FAIL_COND_V(!animation_set.has(p_name), Ref<Animation>());
  812. const AnimationData &data = animation_set[p_name];
  813. return data.animation;
  814. }
  815. void AnimationPlayer::get_animation_list(List<StringName> *p_animations) const {
  816. List<String> anims;
  817. for (Map<StringName, AnimationData>::Element *E = animation_set.front(); E; E = E->next()) {
  818. anims.push_back(E->key());
  819. }
  820. anims.sort();
  821. for (List<String>::Element *E = anims.front(); E; E = E->next()) {
  822. p_animations->push_back(E->get());
  823. }
  824. }
  825. void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time) {
  826. ERR_FAIL_COND(p_time < 0);
  827. BlendKey bk;
  828. bk.from = p_animation1;
  829. bk.to = p_animation2;
  830. if (p_time == 0)
  831. blend_times.erase(bk);
  832. else
  833. blend_times[bk] = p_time;
  834. }
  835. float AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
  836. BlendKey bk;
  837. bk.from = p_animation1;
  838. bk.to = p_animation2;
  839. if (blend_times.has(bk))
  840. return blend_times[bk];
  841. else
  842. return 0;
  843. }
  844. void AnimationPlayer::queue(const StringName &p_name) {
  845. if (!is_playing())
  846. play(p_name);
  847. else
  848. queued.push_back(p_name);
  849. }
  850. PoolVector<String> AnimationPlayer::get_queue() {
  851. PoolVector<String> ret;
  852. for (List<StringName>::Element *E = queued.front(); E; E = E->next()) {
  853. ret.push_back(E->get());
  854. }
  855. return ret;
  856. }
  857. void AnimationPlayer::clear_queue() {
  858. queued.clear();
  859. }
  860. void AnimationPlayer::play_backwards(const StringName &p_name, float p_custom_blend) {
  861. play(p_name, p_custom_blend, -1, true);
  862. }
  863. void AnimationPlayer::play(const StringName &p_name, float p_custom_blend, float p_custom_scale, bool p_from_end) {
  864. StringName name = p_name;
  865. if (String(name) == "")
  866. name = playback.assigned;
  867. if (!animation_set.has(name)) {
  868. ERR_EXPLAIN("Animation not found: " + name);
  869. ERR_FAIL();
  870. }
  871. Playback &c = playback;
  872. if (c.current.from) {
  873. float blend_time = 0;
  874. // find if it can blend
  875. BlendKey bk;
  876. bk.from = c.current.from->name;
  877. bk.to = name;
  878. if (p_custom_blend >= 0) {
  879. blend_time = p_custom_blend;
  880. } else if (blend_times.has(bk)) {
  881. blend_time = blend_times[bk];
  882. } else {
  883. bk.from = "*";
  884. if (blend_times.has(bk)) {
  885. blend_time = blend_times[bk];
  886. } else {
  887. bk.from = c.current.from->name;
  888. bk.to = "*";
  889. if (blend_times.has(bk)) {
  890. blend_time = blend_times[bk];
  891. }
  892. }
  893. }
  894. if (p_custom_blend < 0 && blend_time == 0 && default_blend_time)
  895. blend_time = default_blend_time;
  896. if (blend_time > 0) {
  897. Blend b;
  898. b.data = c.current;
  899. b.blend_time = b.blend_left = blend_time;
  900. c.blend.push_back(b);
  901. }
  902. }
  903. _stop_playing_caches();
  904. c.current.from = &animation_set[name];
  905. if (c.assigned != name) { // reset
  906. c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0;
  907. } else {
  908. if (p_from_end && c.current.pos == 0) {
  909. // Animation reset BUT played backwards, set position to the end
  910. c.current.pos = c.current.from->animation->get_length();
  911. } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) {
  912. // Animation resumed but already ended, set position to the beggining
  913. c.current.pos = 0;
  914. }
  915. }
  916. c.current.speed_scale = p_custom_scale;
  917. c.assigned = name;
  918. c.seeked = false;
  919. c.started = true;
  920. if (!end_reached)
  921. queued.clear();
  922. _set_process(true); // always process when starting an animation
  923. playing = true;
  924. emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned);
  925. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
  926. return; // no next in this case
  927. StringName next = animation_get_next(p_name);
  928. if (next != StringName() && animation_set.has(next)) {
  929. queue(next);
  930. }
  931. }
  932. bool AnimationPlayer::is_playing() const {
  933. return playing;
  934. /*
  935. if (playback.current.from==NULL)
  936. return false;
  937. float len=playback.current.from->animation->get_length();
  938. float pos = playback.current.pos;
  939. bool loop=playback.current.from->animation->has_loop();
  940. if (!loop && pos >= len) {
  941. return false;
  942. };
  943. return true;
  944. */
  945. }
  946. void AnimationPlayer::set_current_animation(const String &p_anim) {
  947. if (p_anim == "[stop]" || p_anim == "") {
  948. stop();
  949. } else if (!is_playing() || playback.assigned != p_anim) {
  950. play(p_anim);
  951. } else {
  952. // Same animation, do not replay from start
  953. }
  954. }
  955. String AnimationPlayer::get_current_animation() const {
  956. return (is_playing() ? playback.assigned : "");
  957. }
  958. void AnimationPlayer::set_assigned_animation(const String &p_anim) {
  959. if (is_playing()) {
  960. play(p_anim);
  961. } else {
  962. ERR_FAIL_COND(!animation_set.has(p_anim));
  963. playback.current.pos = 0;
  964. playback.current.from = &animation_set[p_anim];
  965. playback.assigned = p_anim;
  966. }
  967. }
  968. String AnimationPlayer::get_assigned_animation() const {
  969. return playback.assigned;
  970. }
  971. void AnimationPlayer::stop(bool p_reset) {
  972. _stop_playing_caches();
  973. Playback &c = playback;
  974. c.blend.clear();
  975. if (p_reset) {
  976. c.current.from = NULL;
  977. c.current.speed_scale = 1;
  978. c.current.pos = 0;
  979. }
  980. _set_process(false);
  981. queued.clear();
  982. playing = false;
  983. }
  984. void AnimationPlayer::set_speed_scale(float p_speed) {
  985. speed_scale = p_speed;
  986. }
  987. float AnimationPlayer::get_speed_scale() const {
  988. return speed_scale;
  989. }
  990. float AnimationPlayer::get_playing_speed() const {
  991. if (!playing) {
  992. return 0;
  993. }
  994. return speed_scale * playback.current.speed_scale;
  995. }
  996. void AnimationPlayer::seek(float p_time, bool p_update) {
  997. if (!playback.current.from) {
  998. if (playback.assigned) {
  999. ERR_FAIL_COND(!animation_set.has(playback.assigned));
  1000. playback.current.from = &animation_set[playback.assigned];
  1001. }
  1002. ERR_FAIL_COND(!playback.current.from);
  1003. }
  1004. playback.current.pos = p_time;
  1005. playback.seeked = true;
  1006. if (p_update) {
  1007. _animation_process(0);
  1008. }
  1009. }
  1010. void AnimationPlayer::seek_delta(float p_time, float p_delta) {
  1011. if (!playback.current.from) {
  1012. if (playback.assigned) {
  1013. ERR_FAIL_COND(!animation_set.has(playback.assigned));
  1014. playback.current.from = &animation_set[playback.assigned];
  1015. }
  1016. ERR_FAIL_COND(!playback.current.from);
  1017. }
  1018. playback.current.pos = p_time - p_delta;
  1019. if (speed_scale != 0.0)
  1020. p_delta /= speed_scale;
  1021. _animation_process(p_delta);
  1022. //playback.current.pos=p_time;
  1023. }
  1024. bool AnimationPlayer::is_valid() const {
  1025. return (playback.current.from);
  1026. }
  1027. float AnimationPlayer::get_current_animation_position() const {
  1028. ERR_FAIL_COND_V(!playback.current.from, 0);
  1029. return playback.current.pos;
  1030. }
  1031. float AnimationPlayer::get_current_animation_length() const {
  1032. ERR_FAIL_COND_V(!playback.current.from, 0);
  1033. return playback.current.from->animation->get_length();
  1034. }
  1035. void AnimationPlayer::_animation_changed() {
  1036. clear_caches();
  1037. emit_signal("caches_cleared");
  1038. if (is_playing()) {
  1039. playback.seeked = true; //need to restart stuff, like audio
  1040. }
  1041. }
  1042. void AnimationPlayer::_stop_playing_caches() {
  1043. for (Set<TrackNodeCache *>::Element *E = playing_caches.front(); E; E = E->next()) {
  1044. if (E->get()->node && E->get()->audio_playing) {
  1045. E->get()->node->call("stop");
  1046. }
  1047. if (E->get()->node && E->get()->animation_playing) {
  1048. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(E->get()->node);
  1049. if (!player)
  1050. continue;
  1051. player->stop();
  1052. }
  1053. }
  1054. playing_caches.clear();
  1055. }
  1056. void AnimationPlayer::_node_removed(Node *p_node) {
  1057. clear_caches(); // nodes contained here ar being removed, clear the caches
  1058. }
  1059. void AnimationPlayer::clear_caches() {
  1060. _stop_playing_caches();
  1061. node_cache_map.clear();
  1062. for (Map<StringName, AnimationData>::Element *E = animation_set.front(); E; E = E->next()) {
  1063. E->get().node_cache.clear();
  1064. }
  1065. cache_update_size = 0;
  1066. cache_update_prop_size = 0;
  1067. cache_update_bezier_size = 0;
  1068. }
  1069. void AnimationPlayer::set_active(bool p_active) {
  1070. if (active == p_active)
  1071. return;
  1072. active = p_active;
  1073. _set_process(processing, true);
  1074. }
  1075. bool AnimationPlayer::is_active() const {
  1076. return active;
  1077. }
  1078. StringName AnimationPlayer::find_animation(const Ref<Animation> &p_animation) const {
  1079. for (Map<StringName, AnimationData>::Element *E = animation_set.front(); E; E = E->next()) {
  1080. if (E->get().animation == p_animation)
  1081. return E->key();
  1082. }
  1083. return "";
  1084. }
  1085. void AnimationPlayer::set_autoplay(const String &p_name) {
  1086. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint())
  1087. WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
  1088. autoplay = p_name;
  1089. }
  1090. String AnimationPlayer::get_autoplay() const {
  1091. return autoplay;
  1092. }
  1093. void AnimationPlayer::set_animation_process_mode(AnimationProcessMode p_mode) {
  1094. if (animation_process_mode == p_mode)
  1095. return;
  1096. bool pr = processing;
  1097. if (pr)
  1098. _set_process(false);
  1099. animation_process_mode = p_mode;
  1100. if (pr)
  1101. _set_process(true);
  1102. }
  1103. AnimationPlayer::AnimationProcessMode AnimationPlayer::get_animation_process_mode() const {
  1104. return animation_process_mode;
  1105. }
  1106. void AnimationPlayer::_set_process(bool p_process, bool p_force) {
  1107. if (processing == p_process && !p_force)
  1108. return;
  1109. switch (animation_process_mode) {
  1110. case ANIMATION_PROCESS_PHYSICS: set_physics_process_internal(p_process && active); break;
  1111. case ANIMATION_PROCESS_IDLE: set_process_internal(p_process && active); break;
  1112. case ANIMATION_PROCESS_MANUAL: break;
  1113. }
  1114. processing = p_process;
  1115. }
  1116. void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
  1117. ERR_FAIL_COND(!animation_set.has(p_animation));
  1118. animation_set[p_animation].next = p_next;
  1119. }
  1120. StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
  1121. if (!animation_set.has(p_animation))
  1122. return StringName();
  1123. return animation_set[p_animation].next;
  1124. }
  1125. void AnimationPlayer::set_default_blend_time(float p_default) {
  1126. default_blend_time = p_default;
  1127. }
  1128. float AnimationPlayer::get_default_blend_time() const {
  1129. return default_blend_time;
  1130. }
  1131. void AnimationPlayer::set_root(const NodePath &p_root) {
  1132. root = p_root;
  1133. clear_caches();
  1134. }
  1135. NodePath AnimationPlayer::get_root() const {
  1136. return root;
  1137. }
  1138. void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1139. String pf = p_function;
  1140. if (p_function == "play" || p_function == "play_backwards" || p_function == "remove_animation" || p_function == "has_animation" || p_function == "queue") {
  1141. List<StringName> al;
  1142. get_animation_list(&al);
  1143. for (List<StringName>::Element *E = al.front(); E; E = E->next()) {
  1144. r_options->push_back("\"" + String(E->get()) + "\"");
  1145. }
  1146. }
  1147. Node::get_argument_options(p_function, p_idx, r_options);
  1148. }
  1149. #ifdef TOOLS_ENABLED
  1150. AnimatedValuesBackup AnimationPlayer::backup_animated_values() {
  1151. if (!playback.current.from)
  1152. return AnimatedValuesBackup();
  1153. _ensure_node_caches(playback.current.from);
  1154. AnimatedValuesBackup backup;
  1155. for (int i = 0; i < playback.current.from->node_cache.size(); i++) {
  1156. TrackNodeCache *nc = playback.current.from->node_cache[i];
  1157. if (!nc)
  1158. continue;
  1159. if (nc->skeleton) {
  1160. if (nc->bone_idx == -1)
  1161. continue;
  1162. AnimatedValuesBackup::Entry entry;
  1163. entry.object = nc->skeleton;
  1164. entry.bone_idx = nc->bone_idx;
  1165. entry.value = nc->skeleton->get_bone_pose(nc->bone_idx);
  1166. backup.entries.push_back(entry);
  1167. } else {
  1168. if (nc->spatial) {
  1169. AnimatedValuesBackup::Entry entry;
  1170. entry.object = nc->spatial;
  1171. entry.subpath.push_back("transform");
  1172. entry.value = nc->spatial->get_transform();
  1173. entry.bone_idx = -1;
  1174. backup.entries.push_back(entry);
  1175. } else {
  1176. for (Map<StringName, TrackNodeCache::PropertyAnim>::Element *E = nc->property_anim.front(); E; E = E->next()) {
  1177. AnimatedValuesBackup::Entry entry;
  1178. entry.object = E->value().object;
  1179. entry.subpath = E->value().subpath;
  1180. bool valid;
  1181. entry.value = E->value().object->get_indexed(E->value().subpath, &valid);
  1182. entry.bone_idx = -1;
  1183. if (valid)
  1184. backup.entries.push_back(entry);
  1185. }
  1186. }
  1187. }
  1188. }
  1189. return backup;
  1190. }
  1191. void AnimationPlayer::restore_animated_values(const AnimatedValuesBackup &p_backup) {
  1192. for (int i = 0; i < p_backup.entries.size(); i++) {
  1193. const AnimatedValuesBackup::Entry *entry = &p_backup.entries[i];
  1194. if (entry->bone_idx == -1) {
  1195. entry->object->set_indexed(entry->subpath, entry->value);
  1196. } else {
  1197. Object::cast_to<Skeleton>(entry->object)->set_bone_pose(entry->bone_idx, entry->value);
  1198. }
  1199. }
  1200. }
  1201. #endif
  1202. void AnimationPlayer::_bind_methods() {
  1203. ClassDB::bind_method(D_METHOD("_node_removed"), &AnimationPlayer::_node_removed);
  1204. ClassDB::bind_method(D_METHOD("_animation_changed"), &AnimationPlayer::_animation_changed);
  1205. ClassDB::bind_method(D_METHOD("add_animation", "name", "animation"), &AnimationPlayer::add_animation);
  1206. ClassDB::bind_method(D_METHOD("remove_animation", "name"), &AnimationPlayer::remove_animation);
  1207. ClassDB::bind_method(D_METHOD("rename_animation", "name", "newname"), &AnimationPlayer::rename_animation);
  1208. ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationPlayer::has_animation);
  1209. ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationPlayer::get_animation);
  1210. ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationPlayer::_get_animation_list);
  1211. ClassDB::bind_method(D_METHOD("animation_set_next", "anim_from", "anim_to"), &AnimationPlayer::animation_set_next);
  1212. ClassDB::bind_method(D_METHOD("animation_get_next", "anim_from"), &AnimationPlayer::animation_get_next);
  1213. ClassDB::bind_method(D_METHOD("set_blend_time", "anim_from", "anim_to", "sec"), &AnimationPlayer::set_blend_time);
  1214. ClassDB::bind_method(D_METHOD("get_blend_time", "anim_from", "anim_to"), &AnimationPlayer::get_blend_time);
  1215. ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
  1216. ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
  1217. ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
  1218. ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1));
  1219. ClassDB::bind_method(D_METHOD("stop", "reset"), &AnimationPlayer::stop, DEFVAL(true));
  1220. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
  1221. ClassDB::bind_method(D_METHOD("set_current_animation", "anim"), &AnimationPlayer::set_current_animation);
  1222. ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
  1223. ClassDB::bind_method(D_METHOD("set_assigned_animation", "anim"), &AnimationPlayer::set_assigned_animation);
  1224. ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
  1225. ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
  1226. ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
  1227. ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
  1228. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationPlayer::set_active);
  1229. ClassDB::bind_method(D_METHOD("is_active"), &AnimationPlayer::is_active);
  1230. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
  1231. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
  1232. ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
  1233. ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
  1234. ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
  1235. ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::set_root);
  1236. ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::get_root);
  1237. ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
  1238. ClassDB::bind_method(D_METHOD("clear_caches"), &AnimationPlayer::clear_caches);
  1239. ClassDB::bind_method(D_METHOD("set_animation_process_mode", "mode"), &AnimationPlayer::set_animation_process_mode);
  1240. ClassDB::bind_method(D_METHOD("get_animation_process_mode"), &AnimationPlayer::get_animation_process_mode);
  1241. ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
  1242. ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
  1243. ClassDB::bind_method(D_METHOD("seek", "seconds", "update"), &AnimationPlayer::seek, DEFVAL(false));
  1244. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationPlayer::advance);
  1245. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_node"), "set_root", "get_root");
  1246. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ANIMATE_AS_TRIGGER), "set_current_animation", "get_current_animation");
  1247. ADD_PROPERTY(PropertyInfo(Variant::STRING, "assigned_animation", PROPERTY_HINT_NONE, "", 0), "set_assigned_animation", "get_assigned_animation");
  1248. ADD_PROPERTY(PropertyInfo(Variant::STRING, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_autoplay", "get_autoplay");
  1249. ADD_PROPERTY(PropertyInfo(Variant::REAL, "current_animation_length", PROPERTY_HINT_NONE, "", 0), "", "get_current_animation_length");
  1250. ADD_PROPERTY(PropertyInfo(Variant::REAL, "current_animation_position", PROPERTY_HINT_NONE, "", 0), "", "get_current_animation_position");
  1251. ADD_GROUP("Playback Options", "playback_");
  1252. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_animation_process_mode", "get_animation_process_mode");
  1253. ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_default_blend_time", "get_default_blend_time");
  1254. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", 0), "set_active", "is_active");
  1255. ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
  1256. ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING, "anim_name")));
  1257. ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name")));
  1258. ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING, "anim_name")));
  1259. ADD_SIGNAL(MethodInfo("caches_cleared"));
  1260. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1261. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1262. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1263. }
  1264. AnimationPlayer::AnimationPlayer() {
  1265. accum_pass = 1;
  1266. cache_update_size = 0;
  1267. cache_update_prop_size = 0;
  1268. cache_update_bezier_size = 0;
  1269. speed_scale = 1;
  1270. end_reached = false;
  1271. end_notify = false;
  1272. animation_process_mode = ANIMATION_PROCESS_IDLE;
  1273. processing = false;
  1274. default_blend_time = 0;
  1275. root = SceneStringNames::get_singleton()->path_pp;
  1276. playing = false;
  1277. active = true;
  1278. playback.seeked = false;
  1279. playback.started = false;
  1280. }
  1281. AnimationPlayer::~AnimationPlayer() {
  1282. }