animation_player.cpp 49 KB

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