animation_player.cpp 51 KB

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