animation_player.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  1. /**************************************************************************/
  2. /* animation_player.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/engine.h"
  32. #include "core/object/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_node.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "scene/2d/skeleton_2d.h"
  39. void AnimatedValuesBackup::update_skeletons() {
  40. for (int i = 0; i < entries.size(); i++) {
  41. if (entries[i].bone_idx != -1) {
  42. // 3D bone
  43. Object::cast_to<Skeleton3D>(entries[i].object)->notification(Skeleton3D::NOTIFICATION_UPDATE_SKELETON);
  44. } else {
  45. Bone2D *bone = Object::cast_to<Bone2D>(entries[i].object);
  46. if (bone && bone->skeleton) {
  47. // 2D bone
  48. bone->skeleton->_update_transform();
  49. }
  50. }
  51. }
  52. }
  53. void AnimatedValuesBackup::restore() const {
  54. for (int i = 0; i < entries.size(); i++) {
  55. const AnimatedValuesBackup::Entry *entry = &entries[i];
  56. if (entry->bone_idx == -1) {
  57. entry->object->set_indexed(entry->subpath, entry->value);
  58. } else {
  59. Array arr = entry->value;
  60. if (arr.size() == 3) {
  61. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_position(entry->bone_idx, arr[0]);
  62. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_rotation(entry->bone_idx, arr[1]);
  63. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_scale(entry->bone_idx, arr[2]);
  64. }
  65. }
  66. }
  67. }
  68. void AnimatedValuesBackup::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("restore"), &AnimatedValuesBackup::restore);
  70. }
  71. #endif
  72. bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
  73. String name = p_name;
  74. if (name.begins_with("playback/play")) { // bw compatibility
  75. set_current_animation(p_value);
  76. } else if (name.begins_with("anims/")) {
  77. // Backwards compatibility with 3.x, add them to "default" library.
  78. String which = name.get_slicec('/', 1);
  79. Ref<Animation> anim = p_value;
  80. Ref<AnimationLibrary> al;
  81. if (!has_animation_library(StringName())) {
  82. al.instantiate();
  83. add_animation_library(StringName(), al);
  84. } else {
  85. al = get_animation_library(StringName());
  86. }
  87. al->add_animation(which, anim);
  88. } else if (name.begins_with("libraries")) {
  89. Dictionary d = p_value;
  90. while (animation_libraries.size()) {
  91. remove_animation_library(animation_libraries[0].name);
  92. }
  93. List<Variant> keys;
  94. d.get_key_list(&keys);
  95. for (const Variant &K : keys) {
  96. StringName lib_name = K;
  97. Ref<AnimationLibrary> lib = d[lib_name];
  98. add_animation_library(lib_name, lib);
  99. }
  100. emit_signal("animation_libraries_updated");
  101. } else if (name.begins_with("next/")) {
  102. String which = name.get_slicec('/', 1);
  103. animation_set_next(which, p_value);
  104. } else if (p_name == SceneStringNames::get_singleton()->blend_times) {
  105. Array array = p_value;
  106. int len = array.size();
  107. ERR_FAIL_COND_V(len % 3, false);
  108. for (int i = 0; i < len / 3; i++) {
  109. StringName from = array[i * 3 + 0];
  110. StringName to = array[i * 3 + 1];
  111. float time = array[i * 3 + 2];
  112. set_blend_time(from, to, time);
  113. }
  114. } else {
  115. return false;
  116. }
  117. return true;
  118. }
  119. bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
  120. String name = p_name;
  121. if (name == "playback/play") { // bw compatibility
  122. r_ret = get_current_animation();
  123. } else if (name.begins_with("libraries")) {
  124. Dictionary d;
  125. for (const AnimationLibraryData &lib : animation_libraries) {
  126. d[lib.name] = lib.library;
  127. }
  128. r_ret = d;
  129. } else if (name.begins_with("next/")) {
  130. String which = name.get_slicec('/', 1);
  131. r_ret = animation_get_next(which);
  132. } else if (name == "blend_times") {
  133. Vector<BlendKey> keys;
  134. for (const KeyValue<BlendKey, double> &E : blend_times) {
  135. keys.ordered_insert(E.key);
  136. }
  137. Array array;
  138. for (int i = 0; i < keys.size(); i++) {
  139. array.push_back(keys[i].from);
  140. array.push_back(keys[i].to);
  141. array.push_back(blend_times.get(keys[i]));
  142. }
  143. r_ret = array;
  144. } else {
  145. return false;
  146. }
  147. return true;
  148. }
  149. void AnimationPlayer::_validate_property(PropertyInfo &p_property) const {
  150. if (p_property.name == "current_animation") {
  151. List<String> names;
  152. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  153. names.push_back(E.key);
  154. }
  155. names.sort();
  156. names.push_front("[stop]");
  157. String hint;
  158. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  159. if (E != names.front()) {
  160. hint += ",";
  161. }
  162. hint += E->get();
  163. }
  164. p_property.hint_string = hint;
  165. }
  166. }
  167. void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  168. List<PropertyInfo> anim_names;
  169. anim_names.push_back(PropertyInfo(Variant::DICTIONARY, PNAME("libraries")));
  170. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  171. if (E.value.next != StringName()) {
  172. anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  173. }
  174. }
  175. anim_names.sort();
  176. for (const PropertyInfo &E : anim_names) {
  177. p_list->push_back(E);
  178. }
  179. p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  180. }
  181. void AnimationPlayer::advance(double p_time) {
  182. _animation_process(p_time);
  183. }
  184. void AnimationPlayer::_notification(int p_what) {
  185. switch (p_what) {
  186. case NOTIFICATION_ENTER_TREE: {
  187. if (!processing) {
  188. //make sure that a previous process state was not saved
  189. //only process if "processing" is set
  190. set_physics_process_internal(false);
  191. set_process_internal(false);
  192. }
  193. clear_caches();
  194. } break;
  195. case NOTIFICATION_READY: {
  196. if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
  197. play(autoplay);
  198. _animation_process(0);
  199. }
  200. } break;
  201. case NOTIFICATION_INTERNAL_PROCESS: {
  202. if (process_callback == ANIMATION_PROCESS_PHYSICS) {
  203. break;
  204. }
  205. if (processing) {
  206. _animation_process(get_process_delta_time());
  207. }
  208. } break;
  209. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  210. if (process_callback == ANIMATION_PROCESS_IDLE) {
  211. break;
  212. }
  213. if (processing) {
  214. _animation_process(get_physics_process_delta_time());
  215. }
  216. } break;
  217. case NOTIFICATION_EXIT_TREE: {
  218. clear_caches();
  219. } break;
  220. }
  221. }
  222. void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim, Node *p_root_override) {
  223. // Already cached?
  224. if (p_anim->node_cache.size() == p_anim->animation->get_track_count()) {
  225. return;
  226. }
  227. Node *parent = p_root_override ? p_root_override : get_node_or_null(root);
  228. ERR_FAIL_NULL(parent);
  229. Animation *a = p_anim->animation.operator->();
  230. p_anim->node_cache.resize(a->get_track_count());
  231. setup_pass++;
  232. for (int i = 0; i < a->get_track_count(); i++) {
  233. p_anim->node_cache.write[i] = nullptr;
  234. if (!a->track_is_enabled(i)) {
  235. continue;
  236. }
  237. Ref<Resource> resource;
  238. Vector<StringName> leftover_path;
  239. Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path);
  240. 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
  241. ObjectID id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id();
  242. int bone_idx = -1;
  243. int blend_shape_idx = -1;
  244. #ifndef _3D_DISABLED
  245. if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton3D>(child)) {
  246. Skeleton3D *sk = Object::cast_to<Skeleton3D>(child);
  247. bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0));
  248. if (bone_idx == -1) {
  249. continue;
  250. }
  251. }
  252. if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
  253. MeshInstance3D *mi_3d = Object::cast_to<MeshInstance3D>(child);
  254. if (!mi_3d) {
  255. continue;
  256. }
  257. if (a->track_get_path(i).get_subname_count() != 1) {
  258. continue;
  259. }
  260. blend_shape_idx = mi_3d->find_blend_shape_by_name(a->track_get_path(i).get_subname(0));
  261. if (blend_shape_idx == -1) {
  262. continue;
  263. }
  264. }
  265. #endif // _3D_DISABLED
  266. if (!child->is_connected("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed))) {
  267. child->connect("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed).bind(child), CONNECT_ONE_SHOT);
  268. }
  269. TrackNodeCacheKey key;
  270. key.id = id;
  271. key.bone_idx = bone_idx;
  272. key.blend_shape_idx = blend_shape_idx;
  273. if (!node_cache_map.has(key)) {
  274. node_cache_map[key] = TrackNodeCache();
  275. }
  276. TrackNodeCache *node_cache = &node_cache_map[key];
  277. p_anim->node_cache.write[i] = node_cache;
  278. node_cache->path = a->track_get_path(i);
  279. node_cache->node = child;
  280. node_cache->resource = resource;
  281. node_cache->node_2d = Object::cast_to<Node2D>(child);
  282. #ifndef _3D_DISABLED
  283. if (a->track_get_type(i) == Animation::TYPE_POSITION_3D || a->track_get_type(i) == Animation::TYPE_ROTATION_3D || a->track_get_type(i) == Animation::TYPE_SCALE_3D) {
  284. // special cases and caches for transform tracks
  285. if (node_cache->last_setup_pass != setup_pass) {
  286. node_cache->loc_used = false;
  287. node_cache->rot_used = false;
  288. node_cache->scale_used = false;
  289. }
  290. // cache node_3d
  291. node_cache->node_3d = Object::cast_to<Node3D>(child);
  292. // cache skeleton
  293. node_cache->skeleton = Object::cast_to<Skeleton3D>(child);
  294. if (node_cache->skeleton) {
  295. if (a->track_get_path(i).get_subname_count() == 1) {
  296. StringName bone_name = a->track_get_path(i).get_subname(0);
  297. node_cache->bone_idx = node_cache->skeleton->find_bone(bone_name);
  298. if (node_cache->bone_idx < 0) {
  299. // broken track (nonexistent bone)
  300. node_cache->skeleton = nullptr;
  301. node_cache->node_3d = nullptr;
  302. ERR_CONTINUE(node_cache->bone_idx < 0);
  303. }
  304. Transform3D rest = node_cache->skeleton->get_bone_rest(bone_idx);
  305. node_cache->init_loc = rest.origin;
  306. node_cache->init_rot = rest.basis.get_rotation_quaternion();
  307. node_cache->init_scale = rest.basis.get_scale();
  308. } else {
  309. // Not a skeleton, the node can be accessed with the node_3d member.
  310. node_cache->skeleton = nullptr;
  311. }
  312. }
  313. switch (a->track_get_type(i)) {
  314. case Animation::TYPE_POSITION_3D: {
  315. node_cache->loc_used = true;
  316. } break;
  317. case Animation::TYPE_ROTATION_3D: {
  318. node_cache->rot_used = true;
  319. } break;
  320. case Animation::TYPE_SCALE_3D: {
  321. node_cache->scale_used = true;
  322. } break;
  323. default: {
  324. }
  325. }
  326. }
  327. if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
  328. // special cases and caches for transform tracks
  329. node_cache->node_blend_shape = Object::cast_to<MeshInstance3D>(child);
  330. node_cache->blend_shape_idx = blend_shape_idx;
  331. }
  332. #endif // _3D_DISABLED
  333. if (a->track_get_type(i) == Animation::TYPE_VALUE) {
  334. if (!node_cache->property_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  335. TrackNodeCache::PropertyAnim pa;
  336. pa.subpath = leftover_path;
  337. pa.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  338. pa.special = SP_NONE;
  339. pa.owner = p_anim->node_cache[i];
  340. if (false && node_cache->node_2d) {
  341. if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_pos) {
  342. pa.special = SP_NODE2D_POS;
  343. } else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_rot) {
  344. pa.special = SP_NODE2D_ROT;
  345. } else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_scale) {
  346. pa.special = SP_NODE2D_SCALE;
  347. }
  348. }
  349. node_cache->property_anim[a->track_get_path(i).get_concatenated_subnames()] = pa;
  350. }
  351. }
  352. if (a->track_get_type(i) == Animation::TYPE_BEZIER && leftover_path.size()) {
  353. if (!node_cache->bezier_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  354. TrackNodeCache::BezierAnim ba;
  355. ba.bezier_property = leftover_path;
  356. ba.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  357. ba.owner = p_anim->node_cache[i];
  358. node_cache->bezier_anim[a->track_get_path(i).get_concatenated_subnames()] = ba;
  359. }
  360. }
  361. if (a->track_get_type(i) == Animation::TYPE_AUDIO) {
  362. if (!node_cache->audio_anim.has(a->track_get_path(i).get_concatenated_names())) {
  363. TrackNodeCache::AudioAnim aa;
  364. aa.object = (Object *)child;
  365. aa.audio_stream.instantiate();
  366. aa.audio_stream->set_polyphony(audio_max_polyphony);
  367. node_cache->audio_anim[a->track_get_path(i).get_concatenated_names()] = aa;
  368. }
  369. }
  370. node_cache->last_setup_pass = setup_pass;
  371. }
  372. }
  373. static void _call_object(Object *p_object, const StringName &p_method, const Vector<Variant> &p_params, bool p_deferred) {
  374. // Separate function to use alloca() more efficiently
  375. const Variant **argptrs = (const Variant **)alloca(sizeof(const Variant **) * p_params.size());
  376. const Variant *args = p_params.ptr();
  377. uint32_t argcount = p_params.size();
  378. for (uint32_t i = 0; i < argcount; i++) {
  379. argptrs[i] = &args[i];
  380. }
  381. if (p_deferred) {
  382. MessageQueue::get_singleton()->push_callp(p_object, p_method, argptrs, argcount);
  383. } else {
  384. Callable::CallError ce;
  385. p_object->callp(p_method, argptrs, argcount, ce);
  386. }
  387. }
  388. Variant AnimationPlayer::post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx) {
  389. Variant res;
  390. if (GDVIRTUAL_CALL(_post_process_key_value, p_anim, p_track, p_value, const_cast<Object *>(p_object), p_object_idx, res)) {
  391. return res;
  392. }
  393. return _post_process_key_value(p_anim, p_track, p_value, p_object, p_object_idx);
  394. }
  395. Variant AnimationPlayer::_post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx) {
  396. switch (p_anim->track_get_type(p_track)) {
  397. #ifndef _3D_DISABLED
  398. case Animation::TYPE_POSITION_3D: {
  399. if (p_object_idx >= 0) {
  400. const Skeleton3D *skel = Object::cast_to<Skeleton3D>(p_object);
  401. return Vector3(p_value) * skel->get_motion_scale();
  402. }
  403. return p_value;
  404. } break;
  405. #endif // _3D_DISABLED
  406. default: {
  407. } break;
  408. }
  409. return p_value;
  410. }
  411. void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, double p_prev_time, double p_time, double p_delta, float p_interp, bool p_is_current, bool p_seeked, bool p_started, Animation::LoopedFlag p_looped_flag) {
  412. _ensure_node_caches(p_anim);
  413. ERR_FAIL_COND(p_anim->node_cache.size() != p_anim->animation->get_track_count());
  414. Animation *a = p_anim->animation.operator->();
  415. #ifdef TOOLS_ENABLED
  416. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  417. #endif // TOOLS_ENABLED
  418. bool backward = signbit(p_delta);
  419. for (int i = 0; i < a->get_track_count(); i++) {
  420. // If an animation changes this animation (or it animates itself)
  421. // we need to recreate our animation cache
  422. if (p_anim->node_cache.size() != a->get_track_count()) {
  423. _ensure_node_caches(p_anim);
  424. }
  425. TrackNodeCache *nc = p_anim->node_cache[i];
  426. if (!nc) {
  427. continue; // no node cache for this track, skip it
  428. }
  429. if (!a->track_is_enabled(i)) {
  430. continue; // do nothing if the track is disabled
  431. }
  432. if (a->track_get_key_count(i) == 0) {
  433. continue; // do nothing if track is empty
  434. }
  435. switch (a->track_get_type(i)) {
  436. case Animation::TYPE_POSITION_3D: {
  437. #ifndef _3D_DISABLED
  438. if (!nc->node_3d) {
  439. continue;
  440. }
  441. Vector3 loc;
  442. Error err = a->try_position_track_interpolate(i, p_time, &loc);
  443. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  444. if (err != OK) {
  445. continue;
  446. }
  447. loc = post_process_key_value(a, i, loc, nc->node_3d, nc->bone_idx);
  448. if (nc->accum_pass != accum_pass) {
  449. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  450. cache_update[cache_update_size++] = nc;
  451. nc->accum_pass = accum_pass;
  452. nc->loc_accum = loc;
  453. nc->rot_accum = nc->init_rot;
  454. nc->scale_accum = nc->init_scale;
  455. } else {
  456. nc->loc_accum = nc->loc_accum.lerp(loc, p_interp);
  457. }
  458. #endif // _3D_DISABLED
  459. } break;
  460. case Animation::TYPE_ROTATION_3D: {
  461. #ifndef _3D_DISABLED
  462. if (!nc->node_3d) {
  463. continue;
  464. }
  465. Quaternion rot;
  466. Error err = a->try_rotation_track_interpolate(i, p_time, &rot);
  467. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  468. if (err != OK) {
  469. continue;
  470. }
  471. rot = post_process_key_value(a, i, rot, nc->node_3d, nc->bone_idx);
  472. if (nc->accum_pass != accum_pass) {
  473. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  474. cache_update[cache_update_size++] = nc;
  475. nc->accum_pass = accum_pass;
  476. nc->loc_accum = nc->init_loc;
  477. nc->rot_accum = rot;
  478. nc->scale_accum = nc->init_scale;
  479. } else {
  480. nc->rot_accum = nc->rot_accum.slerp(rot, p_interp);
  481. }
  482. #endif // _3D_DISABLED
  483. } break;
  484. case Animation::TYPE_SCALE_3D: {
  485. #ifndef _3D_DISABLED
  486. if (!nc->node_3d) {
  487. continue;
  488. }
  489. Vector3 scale;
  490. Error err = a->try_scale_track_interpolate(i, p_time, &scale);
  491. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  492. if (err != OK) {
  493. continue;
  494. }
  495. scale = post_process_key_value(a, i, scale, nc->node_3d, nc->bone_idx);
  496. if (nc->accum_pass != accum_pass) {
  497. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  498. cache_update[cache_update_size++] = nc;
  499. nc->accum_pass = accum_pass;
  500. nc->loc_accum = nc->init_loc;
  501. nc->rot_accum = nc->init_rot;
  502. nc->scale_accum = scale;
  503. } else {
  504. nc->scale_accum = nc->scale_accum.lerp(scale, p_interp);
  505. }
  506. #endif // _3D_DISABLED
  507. } break;
  508. case Animation::TYPE_BLEND_SHAPE: {
  509. #ifndef _3D_DISABLED
  510. if (!nc->node_blend_shape) {
  511. continue;
  512. }
  513. float blend;
  514. Error err = a->try_blend_shape_track_interpolate(i, p_time, &blend);
  515. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  516. if (err != OK) {
  517. continue;
  518. }
  519. blend = post_process_key_value(a, i, blend, nc->node_blend_shape, nc->blend_shape_idx);
  520. if (nc->accum_pass != accum_pass) {
  521. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  522. nc->accum_pass = accum_pass;
  523. cache_update[cache_update_size++] = nc;
  524. nc->blend_shape_accum = blend;
  525. } else {
  526. nc->blend_shape_accum = Math::lerp(nc->blend_shape_accum, blend, p_interp);
  527. }
  528. #endif // _3D_DISABLED
  529. } break;
  530. case Animation::TYPE_VALUE: {
  531. if (!nc->node) {
  532. continue;
  533. }
  534. //StringName property=a->track_get_path(i).get_property();
  535. HashMap<StringName, TrackNodeCache::PropertyAnim>::Iterator E = nc->property_anim.find(a->track_get_path(i).get_concatenated_subnames());
  536. ERR_CONTINUE(!E); //should it continue, or create a new one?
  537. TrackNodeCache::PropertyAnim *pa = &E->value;
  538. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  539. if (update_mode == Animation::UPDATE_CAPTURE) {
  540. if (p_started || pa->capture == Variant()) {
  541. pa->capture = pa->object->get_indexed(pa->subpath);
  542. }
  543. int key_count = a->track_get_key_count(i);
  544. if (key_count == 0) {
  545. continue; //eeh not worth it
  546. }
  547. double first_key_time = a->track_get_key_time(i, 0);
  548. double transition = 1.0;
  549. int first_key = 0;
  550. if (first_key_time == 0.0) {
  551. //ignore, use for transition
  552. if (key_count == 1) {
  553. continue; //with one key we can't do anything
  554. }
  555. transition = (double)a->track_get_key_transition(i, 0);
  556. first_key_time = a->track_get_key_time(i, 1);
  557. first_key = 1;
  558. }
  559. if (p_time < first_key_time) {
  560. double c = Math::ease(p_time / first_key_time, transition);
  561. Variant first_value = a->track_get_key_value(i, first_key);
  562. first_value = post_process_key_value(a, i, first_value, nc->node);
  563. Variant interp_value = Animation::interpolate_variant(pa->capture, first_value, c);
  564. if (pa->accum_pass != accum_pass) {
  565. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  566. cache_update_prop[cache_update_prop_size++] = pa;
  567. pa->value_accum = interp_value;
  568. pa->accum_pass = accum_pass;
  569. } else {
  570. pa->value_accum = Animation::interpolate_variant(pa->value_accum, interp_value, p_interp);
  571. }
  572. continue; //handled
  573. }
  574. }
  575. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) {
  576. Variant value = a->value_track_interpolate(i, p_time);
  577. if (value == Variant()) {
  578. continue;
  579. }
  580. value = post_process_key_value(a, i, value, nc->node);
  581. if (pa->accum_pass != accum_pass) {
  582. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  583. cache_update_prop[cache_update_prop_size++] = pa;
  584. pa->value_accum = value;
  585. pa->accum_pass = accum_pass;
  586. } else {
  587. pa->value_accum = Animation::interpolate_variant(pa->value_accum, value, p_interp);
  588. }
  589. } else {
  590. List<int> indices;
  591. if (p_seeked) {
  592. int found_key = a->track_find_key(i, p_time);
  593. if (found_key >= 0) {
  594. indices.push_back(found_key);
  595. }
  596. } else {
  597. if (p_started) {
  598. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  599. if (first_key >= 0) {
  600. indices.push_back(first_key);
  601. }
  602. }
  603. a->track_get_key_indices_in_range(i, p_time, p_delta, &indices, p_looped_flag);
  604. }
  605. for (int &F : indices) {
  606. Variant value = a->track_get_key_value(i, F);
  607. value = post_process_key_value(a, i, value, nc->node);
  608. switch (pa->special) {
  609. case SP_NONE: {
  610. bool valid;
  611. pa->object->set_indexed(pa->subpath, value, &valid); //you are not speshul
  612. #ifdef DEBUG_ENABLED
  613. if (!valid) {
  614. ERR_PRINT("Failed setting track value '" + String(pa->owner->path) + "'. Check if the property exists or the type of key is valid. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  615. }
  616. #endif
  617. } break;
  618. case SP_NODE2D_POS: {
  619. #ifdef DEBUG_ENABLED
  620. if (value.get_type() != Variant::VECTOR2) {
  621. ERR_PRINT("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() + "'.");
  622. }
  623. #endif
  624. static_cast<Node2D *>(pa->object)->set_position(value);
  625. } break;
  626. case SP_NODE2D_ROT: {
  627. #ifdef DEBUG_ENABLED
  628. if (value.is_num()) {
  629. ERR_PRINT("Rotation key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not numerical. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  630. }
  631. #endif
  632. static_cast<Node2D *>(pa->object)->set_rotation((double)value);
  633. } break;
  634. case SP_NODE2D_SCALE: {
  635. #ifdef DEBUG_ENABLED
  636. if (value.get_type() != Variant::VECTOR2) {
  637. ERR_PRINT("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() + "'.");
  638. }
  639. #endif
  640. static_cast<Node2D *>(pa->object)->set_scale(value);
  641. } break;
  642. }
  643. }
  644. }
  645. } break;
  646. case Animation::TYPE_METHOD: {
  647. #ifdef TOOLS_ENABLED
  648. if (!can_call) {
  649. continue;
  650. }
  651. #endif // TOOLS_ENABLED
  652. if (!p_is_current || !nc->node || is_stopping) {
  653. continue;
  654. }
  655. List<int> indices;
  656. if (p_seeked) {
  657. int found_key = a->track_find_key(i, p_time, Animation::FIND_MODE_EXACT);
  658. if (found_key >= 0) {
  659. indices.push_back(found_key);
  660. }
  661. } else {
  662. if (p_started) {
  663. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  664. if (first_key >= 0) {
  665. indices.push_back(first_key);
  666. }
  667. }
  668. a->track_get_key_indices_in_range(i, p_time, p_delta, &indices, p_looped_flag);
  669. }
  670. for (int &E : indices) {
  671. StringName method = a->method_track_get_name(i, E);
  672. Vector<Variant> params = a->method_track_get_params(i, E);
  673. #ifdef DEBUG_ENABLED
  674. if (!nc->node->has_method(method)) {
  675. ERR_PRINT("Invalid method call '" + method + "'. '" + a->get_name() + "' at node '" + get_path() + "'.");
  676. }
  677. #endif
  678. _call_object(nc->node, method, params, method_call_mode == ANIMATION_METHOD_CALL_DEFERRED);
  679. }
  680. } break;
  681. case Animation::TYPE_BEZIER: {
  682. if (!nc->node) {
  683. continue;
  684. }
  685. HashMap<StringName, TrackNodeCache::BezierAnim>::Iterator E = nc->bezier_anim.find(a->track_get_path(i).get_concatenated_subnames());
  686. ERR_CONTINUE(!E); //should it continue, or create a new one?
  687. TrackNodeCache::BezierAnim *ba = &E->value;
  688. real_t bezier = a->bezier_track_interpolate(i, p_time);
  689. bezier = post_process_key_value(a, i, bezier, nc->node);
  690. if (ba->accum_pass != accum_pass) {
  691. ERR_CONTINUE(cache_update_bezier_size >= NODE_CACHE_UPDATE_MAX);
  692. cache_update_bezier[cache_update_bezier_size++] = ba;
  693. ba->bezier_accum = bezier;
  694. ba->accum_pass = accum_pass;
  695. } else {
  696. ba->bezier_accum = Math::lerp(ba->bezier_accum, (float)bezier, p_interp);
  697. }
  698. } break;
  699. case Animation::TYPE_AUDIO: {
  700. if (!nc->node || is_stopping) {
  701. continue;
  702. }
  703. #ifdef TOOLS_ENABLED
  704. if (p_seeked && !can_call) {
  705. continue; // To avoid spamming the preview in editor.
  706. }
  707. #endif // TOOLS_ENABLED
  708. HashMap<StringName, TrackNodeCache::AudioAnim>::Iterator E = nc->audio_anim.find(a->track_get_path(i).get_concatenated_names());
  709. ERR_CONTINUE(!E); //should it continue, or create a new one?
  710. TrackNodeCache::AudioAnim *aa = &E->value;
  711. Node *asp = Object::cast_to<Node>(aa->object);
  712. if (!asp) {
  713. continue;
  714. }
  715. aa->length = a->get_length();
  716. aa->time = p_time;
  717. aa->loop = a->get_loop_mode() != Animation::LOOP_NONE;
  718. aa->backward = backward;
  719. if (aa->accum_pass != accum_pass) {
  720. ERR_CONTINUE(cache_update_audio_size >= NODE_CACHE_UPDATE_MAX);
  721. cache_update_audio[cache_update_audio_size++] = aa;
  722. aa->accum_pass = accum_pass;
  723. }
  724. HashMap<int, TrackNodeCache::PlayingAudioStreamInfo> &map = aa->playing_streams;
  725. // Find stream.
  726. int idx = -1;
  727. if (p_seeked || p_started) {
  728. idx = a->track_find_key(i, p_time);
  729. // Discard previous stream when seeking.
  730. if (map.has(idx)) {
  731. aa->audio_stream_playback->stop_stream(map[idx].index);
  732. map.erase(idx);
  733. }
  734. } else {
  735. List<int> to_play;
  736. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play, p_looped_flag);
  737. if (to_play.size()) {
  738. idx = to_play.back()->get();
  739. }
  740. }
  741. if (idx < 0) {
  742. continue;
  743. }
  744. // Play stream.
  745. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  746. if (stream.is_valid()) {
  747. double start_ofs = a->audio_track_get_key_start_offset(i, idx);
  748. double end_ofs = a->audio_track_get_key_end_offset(i, idx);
  749. double len = stream->get_length();
  750. if (p_seeked || p_started) {
  751. start_ofs += p_time - a->track_get_key_time(i, idx);
  752. }
  753. if (aa->object->call(SNAME("get_stream")) != aa->audio_stream) {
  754. aa->object->call(SNAME("set_stream"), aa->audio_stream);
  755. aa->audio_stream_playback.unref();
  756. if (!playing_audio_stream_players.has(asp)) {
  757. playing_audio_stream_players.push_back(asp);
  758. }
  759. }
  760. if (!aa->object->call(SNAME("is_playing"))) {
  761. aa->object->call(SNAME("play"));
  762. }
  763. if (!aa->object->call(SNAME("has_stream_playback"))) {
  764. aa->audio_stream_playback.unref();
  765. continue;
  766. }
  767. if (aa->audio_stream_playback.is_null()) {
  768. aa->audio_stream_playback = aa->object->call(SNAME("get_stream_playback"));
  769. }
  770. TrackNodeCache::PlayingAudioStreamInfo pasi;
  771. pasi.index = aa->audio_stream_playback->play_stream(stream, start_ofs);
  772. pasi.start = p_time;
  773. if (len && end_ofs > 0) { // Force an end at a time.
  774. pasi.len = len - start_ofs - end_ofs;
  775. } else {
  776. pasi.len = 0;
  777. }
  778. map[idx] = pasi;
  779. }
  780. } break;
  781. case Animation::TYPE_ANIMATION: {
  782. if (is_stopping) {
  783. continue;
  784. }
  785. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(nc->node);
  786. if (!player) {
  787. continue;
  788. }
  789. if (p_seeked) {
  790. //seek
  791. int idx = a->track_find_key(i, p_time);
  792. if (idx < 0) {
  793. continue;
  794. }
  795. double pos = a->track_get_key_time(i, idx);
  796. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  797. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  798. continue;
  799. }
  800. Ref<Animation> anim = player->get_animation(anim_name);
  801. double at_anim_pos = 0.0;
  802. switch (anim->get_loop_mode()) {
  803. case Animation::LOOP_NONE: {
  804. at_anim_pos = MIN((double)anim->get_length(), p_time - pos); //seek to end
  805. } break;
  806. case Animation::LOOP_LINEAR: {
  807. at_anim_pos = Math::fposmod(p_time - pos, (double)anim->get_length()); //seek to loop
  808. } break;
  809. case Animation::LOOP_PINGPONG: {
  810. at_anim_pos = Math::pingpong(p_time - pos, (double)anim->get_length());
  811. } break;
  812. default:
  813. break;
  814. }
  815. if (player->is_playing()) {
  816. player->seek(at_anim_pos);
  817. player->play(anim_name);
  818. nc->animation_playing = true;
  819. playing_caches.insert(nc);
  820. } else {
  821. player->set_assigned_animation(anim_name);
  822. player->seek(at_anim_pos, true);
  823. }
  824. } else {
  825. //find stuff to play
  826. List<int> to_play;
  827. if (p_started) {
  828. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  829. if (first_key >= 0) {
  830. to_play.push_back(first_key);
  831. }
  832. }
  833. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play, p_looped_flag);
  834. if (to_play.size()) {
  835. int idx = to_play.back()->get();
  836. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  837. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  838. if (playing_caches.has(nc)) {
  839. playing_caches.erase(nc);
  840. player->stop();
  841. nc->animation_playing = false;
  842. }
  843. } else {
  844. player->seek(0.0);
  845. player->play(anim_name);
  846. nc->animation_playing = true;
  847. playing_caches.insert(nc);
  848. }
  849. }
  850. }
  851. } break;
  852. }
  853. }
  854. }
  855. void AnimationPlayer::_animation_process_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started) {
  856. double delta = p_delta * speed_scale * cd.speed_scale;
  857. double next_pos = cd.pos + delta;
  858. bool backwards = signbit(delta); // Negative zero means playing backwards too.
  859. real_t len = cd.from->animation->get_length();
  860. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  861. switch (cd.from->animation->get_loop_mode()) {
  862. case Animation::LOOP_NONE: {
  863. if (next_pos < 0) {
  864. next_pos = 0;
  865. } else if (next_pos > len) {
  866. next_pos = len;
  867. }
  868. delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here).
  869. } break;
  870. case Animation::LOOP_LINEAR: {
  871. if (next_pos < 0 && cd.pos >= 0) {
  872. looped_flag = Animation::LOOPED_FLAG_START;
  873. }
  874. if (next_pos > len && cd.pos <= len) {
  875. looped_flag = Animation::LOOPED_FLAG_END;
  876. }
  877. next_pos = Math::fposmod(next_pos, (double)len);
  878. } break;
  879. case Animation::LOOP_PINGPONG: {
  880. if (next_pos < 0 && cd.pos >= 0) {
  881. cd.speed_scale *= -1.0;
  882. looped_flag = Animation::LOOPED_FLAG_START;
  883. }
  884. if (next_pos > len && cd.pos <= len) {
  885. cd.speed_scale *= -1.0;
  886. looped_flag = Animation::LOOPED_FLAG_END;
  887. }
  888. next_pos = Math::pingpong(next_pos, (double)len);
  889. } break;
  890. default:
  891. break;
  892. }
  893. double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.
  894. cd.pos = next_pos;
  895. AnimationData *prev_from = cd.from;
  896. _animation_process_animation(cd.from, prev_pos, cd.pos, delta, p_blend, &cd == &playback.current, p_seeked, p_started, looped_flag);
  897. // End detection.
  898. if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {
  899. if (prev_from != playback.current.from) {
  900. return; // Animation has been changed in the process (may be caused by method track), abort process.
  901. }
  902. if (!backwards && prev_pos <= len && next_pos == len) {
  903. // Playback finished.
  904. end_reached = true;
  905. end_notify = prev_pos < len; // Notify only if not already at the end.
  906. }
  907. if (backwards && prev_pos >= 0 && next_pos == 0) {
  908. // Playback finished.
  909. end_reached = true;
  910. end_notify = prev_pos > 0; // Notify only if not already at the beginning.
  911. }
  912. }
  913. }
  914. void AnimationPlayer::_animation_process2(double p_delta, bool p_started) {
  915. Playback &c = playback;
  916. accum_pass++;
  917. bool seeked = c.seeked; // The animation may be changed during process, so it is safer that the state is changed before process.
  918. if (p_delta != 0) {
  919. c.seeked = false;
  920. }
  921. float blend = 1.0; // First animation we play at 100% blend
  922. List<Blend>::Element *next = NULL;
  923. for (List<Blend>::Element *E = c.blend.front(); E; E = next) {
  924. Blend &b = E->get();
  925. // Note: There may be issues if an animation event triggers an animation change while this blend is active,
  926. // so it is best to use "deferred" calls instead of "immediate" for animation events that can trigger new animations.
  927. _animation_process_data(b.data, p_delta, blend, false, false);
  928. blend = 1.0 - b.blend_left / b.blend_time; // This is how much to blend the NEXT animation
  929. b.blend_left -= Math::absf(speed_scale * p_delta);
  930. next = E->next();
  931. if (b.blend_left < 0) {
  932. // If the blend of this has finished, we need to remove ALL the previous blends
  933. List<Blend>::Element *prev;
  934. while (E) {
  935. prev = E->prev();
  936. c.blend.erase(E);
  937. E = prev;
  938. }
  939. }
  940. }
  941. _animation_process_data(c.current, p_delta, blend, seeked, p_started);
  942. }
  943. void AnimationPlayer::_animation_update_transforms() {
  944. {
  945. Transform3D t;
  946. for (int i = 0; i < cache_update_size; i++) {
  947. TrackNodeCache *nc = cache_update[i];
  948. ERR_CONTINUE(nc->accum_pass != accum_pass);
  949. #ifndef _3D_DISABLED
  950. if (nc->skeleton && nc->bone_idx >= 0) {
  951. if (nc->loc_used) {
  952. nc->skeleton->set_bone_pose_position(nc->bone_idx, nc->loc_accum);
  953. }
  954. if (nc->rot_used) {
  955. nc->skeleton->set_bone_pose_rotation(nc->bone_idx, nc->rot_accum);
  956. }
  957. if (nc->scale_used) {
  958. nc->skeleton->set_bone_pose_scale(nc->bone_idx, nc->scale_accum);
  959. }
  960. } else if (nc->node_blend_shape) {
  961. nc->node_blend_shape->set_blend_shape_value(nc->blend_shape_idx, nc->blend_shape_accum);
  962. } else if (nc->node_3d) {
  963. if (nc->loc_used) {
  964. nc->node_3d->set_position(nc->loc_accum);
  965. }
  966. if (nc->rot_used) {
  967. nc->node_3d->set_rotation(nc->rot_accum.get_euler());
  968. }
  969. if (nc->scale_used) {
  970. nc->node_3d->set_scale(nc->scale_accum);
  971. }
  972. }
  973. #endif // _3D_DISABLED
  974. }
  975. }
  976. for (int i = 0; i < cache_update_prop_size; i++) {
  977. TrackNodeCache::PropertyAnim *pa = cache_update_prop[i];
  978. ERR_CONTINUE(pa->accum_pass != accum_pass);
  979. switch (pa->special) {
  980. case SP_NONE: {
  981. bool valid;
  982. pa->object->set_indexed(pa->subpath, pa->value_accum, &valid); //you are not speshul
  983. #ifdef DEBUG_ENABLED
  984. if (!valid) {
  985. // Get subpath as string for printing the error
  986. // Cannot use `String::join(Vector<String>)` because this is a vector of StringName
  987. String key_debug;
  988. if (pa->subpath.size() > 0) {
  989. key_debug = pa->subpath[0];
  990. for (int subpath_index = 1; subpath_index < pa->subpath.size(); ++subpath_index) {
  991. key_debug += ".";
  992. key_debug += pa->subpath[subpath_index];
  993. }
  994. }
  995. ERR_PRINT("Failed setting key '" + key_debug +
  996. "' at time " + rtos(playback.current.pos) +
  997. " in Animation '" + get_current_animation() +
  998. "' at Node '" + get_path() +
  999. "', Track '" + String(pa->owner->path) +
  1000. "'. Check if the property exists or the type of key is right for the property.");
  1001. }
  1002. #endif
  1003. } break;
  1004. case SP_NODE2D_POS: {
  1005. #ifdef DEBUG_ENABLED
  1006. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  1007. ERR_PRINT("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()");
  1008. }
  1009. #endif
  1010. static_cast<Node2D *>(pa->object)->set_position(pa->value_accum);
  1011. } break;
  1012. case SP_NODE2D_ROT: {
  1013. #ifdef DEBUG_ENABLED
  1014. if (pa->value_accum.is_num()) {
  1015. ERR_PRINT("Rotation key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not numerical");
  1016. }
  1017. #endif
  1018. static_cast<Node2D *>(pa->object)->set_rotation(Math::deg_to_rad((double)pa->value_accum));
  1019. } break;
  1020. case SP_NODE2D_SCALE: {
  1021. #ifdef DEBUG_ENABLED
  1022. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  1023. ERR_PRINT("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()");
  1024. }
  1025. #endif
  1026. static_cast<Node2D *>(pa->object)->set_scale(pa->value_accum);
  1027. } break;
  1028. }
  1029. }
  1030. for (int i = 0; i < cache_update_bezier_size; i++) {
  1031. TrackNodeCache::BezierAnim *ba = cache_update_bezier[i];
  1032. ERR_CONTINUE(ba->accum_pass != accum_pass);
  1033. ba->object->set_indexed(ba->bezier_property, ba->bezier_accum);
  1034. }
  1035. for (int i = 0; i < cache_update_audio_size; i++) {
  1036. TrackNodeCache::AudioAnim *aa = cache_update_audio[i];
  1037. ERR_CONTINUE(aa->accum_pass != accum_pass);
  1038. // Audio ending process.
  1039. LocalVector<int> erase_list;
  1040. for (const KeyValue<int, TrackNodeCache::PlayingAudioStreamInfo> &K : aa->playing_streams) {
  1041. TrackNodeCache::PlayingAudioStreamInfo pasi = K.value;
  1042. bool stop = false;
  1043. if (!aa->audio_stream_playback->is_stream_playing(pasi.index)) {
  1044. stop = true;
  1045. }
  1046. if (!aa->loop) {
  1047. if (!aa->backward) {
  1048. if (aa->time < pasi.start) {
  1049. stop = true;
  1050. }
  1051. } else {
  1052. if (aa->time > pasi.start) {
  1053. stop = true;
  1054. }
  1055. }
  1056. }
  1057. if (pasi.len > 0) {
  1058. double len = 0.0;
  1059. if (!aa->backward) {
  1060. len = pasi.start > aa->time ? (aa->length - pasi.start) + aa->time : aa->time - pasi.start;
  1061. } else {
  1062. len = pasi.start < aa->time ? (aa->length - aa->time) + pasi.start : pasi.start - aa->time;
  1063. }
  1064. if (len > pasi.len) {
  1065. stop = true;
  1066. }
  1067. }
  1068. if (stop) {
  1069. // Time to stop.
  1070. aa->audio_stream_playback->stop_stream(pasi.index);
  1071. erase_list.push_back(K.key);
  1072. }
  1073. }
  1074. for (uint32_t erase_idx = 0; erase_idx < erase_list.size(); erase_idx++) {
  1075. aa->playing_streams.erase(erase_list[erase_idx]);
  1076. }
  1077. }
  1078. }
  1079. void AnimationPlayer::_animation_process(double p_delta) {
  1080. if (playback.current.from) {
  1081. end_reached = false;
  1082. end_notify = false;
  1083. bool started = playback.started; // The animation may be changed during process, so it is safer that the state is changed before process.
  1084. if (playback.started) {
  1085. playback.started = false;
  1086. }
  1087. cache_update_size = 0;
  1088. cache_update_prop_size = 0;
  1089. cache_update_bezier_size = 0;
  1090. cache_update_audio_size = 0;
  1091. AnimationData *prev_from = playback.current.from;
  1092. _animation_process2(p_delta, started);
  1093. if (prev_from != playback.current.from) {
  1094. return; // Animation has been changed in the process (may be caused by method track), abort process.
  1095. }
  1096. _animation_update_transforms();
  1097. if (end_reached) {
  1098. _clear_audio_streams();
  1099. _stop_playing_caches(false);
  1100. if (queued.size()) {
  1101. String old = playback.assigned;
  1102. play(queued.front()->get());
  1103. String new_name = playback.assigned;
  1104. queued.pop_front();
  1105. if (end_notify) {
  1106. emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
  1107. }
  1108. } else {
  1109. playing = false;
  1110. _set_process(false);
  1111. if (end_notify) {
  1112. emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned);
  1113. if (movie_quit_on_finish && OS::get_singleton()->has_feature("movie")) {
  1114. print_line(vformat("Movie Maker mode is enabled. Quitting on animation finish as requested by: %s", get_path()));
  1115. get_tree()->quit();
  1116. }
  1117. }
  1118. }
  1119. end_reached = false;
  1120. }
  1121. } else {
  1122. _set_process(false);
  1123. }
  1124. }
  1125. void AnimationPlayer::_animation_set_cache_update() {
  1126. // Relatively fast function to update all animations.
  1127. animation_set_update_pass++;
  1128. bool clear_cache_needed = false;
  1129. // Update changed and add otherwise
  1130. for (const AnimationLibraryData &lib : animation_libraries) {
  1131. for (const KeyValue<StringName, Ref<Animation>> &K : lib.library->animations) {
  1132. StringName key = lib.name == StringName() ? K.key : StringName(String(lib.name) + "/" + String(K.key));
  1133. if (!animation_set.has(key)) {
  1134. AnimationData ad;
  1135. ad.animation = K.value;
  1136. ad.animation_library = lib.name;
  1137. ad.name = key;
  1138. ad.last_update = animation_set_update_pass;
  1139. animation_set.insert(ad.name, ad);
  1140. } else {
  1141. AnimationData &ad = animation_set[key];
  1142. if (ad.last_update != animation_set_update_pass) {
  1143. // Was not updated, update. If the animation is duplicated, the second one will be ignored.
  1144. if (ad.animation != K.value || ad.animation_library != lib.name) {
  1145. // Animation changed, update and clear caches.
  1146. clear_cache_needed = true;
  1147. ad.animation = K.value;
  1148. ad.animation_library = lib.name;
  1149. }
  1150. ad.last_update = animation_set_update_pass;
  1151. }
  1152. }
  1153. }
  1154. }
  1155. // Check removed
  1156. List<StringName> to_erase;
  1157. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1158. if (E.value.last_update != animation_set_update_pass) {
  1159. // Was not updated, must be erased
  1160. to_erase.push_back(E.key);
  1161. clear_cache_needed = true;
  1162. }
  1163. }
  1164. while (to_erase.size()) {
  1165. animation_set.erase(to_erase.front()->get());
  1166. to_erase.pop_front();
  1167. }
  1168. if (clear_cache_needed) {
  1169. // If something was modified or removed, caches need to be cleared
  1170. clear_caches();
  1171. }
  1172. emit_signal(SNAME("animation_list_changed"));
  1173. }
  1174. void AnimationPlayer::_animation_added(const StringName &p_name, const StringName &p_library) {
  1175. _animation_set_cache_update();
  1176. }
  1177. void AnimationPlayer::_animation_removed(const StringName &p_name, const StringName &p_library) {
  1178. StringName name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
  1179. if (!animation_set.has(name)) {
  1180. return; // No need to update because not the one from the library being used.
  1181. }
  1182. _animation_set_cache_update();
  1183. // Erase blends if needed
  1184. List<BlendKey> to_erase;
  1185. for (const KeyValue<BlendKey, double> &E : blend_times) {
  1186. BlendKey bk = E.key;
  1187. if (bk.from == name || bk.to == name) {
  1188. to_erase.push_back(bk);
  1189. }
  1190. }
  1191. while (to_erase.size()) {
  1192. blend_times.erase(to_erase.front()->get());
  1193. to_erase.pop_front();
  1194. }
  1195. }
  1196. void AnimationPlayer::_rename_animation(const StringName &p_from_name, const StringName &p_to_name) {
  1197. // Rename autoplay or blends if needed.
  1198. List<BlendKey> to_erase;
  1199. HashMap<BlendKey, double, BlendKey> to_insert;
  1200. for (const KeyValue<BlendKey, double> &E : blend_times) {
  1201. BlendKey bk = E.key;
  1202. BlendKey new_bk = bk;
  1203. bool erase = false;
  1204. if (bk.from == p_from_name) {
  1205. new_bk.from = p_to_name;
  1206. erase = true;
  1207. }
  1208. if (bk.to == p_from_name) {
  1209. new_bk.to = p_to_name;
  1210. erase = true;
  1211. }
  1212. if (erase) {
  1213. to_erase.push_back(bk);
  1214. to_insert[new_bk] = E.value;
  1215. }
  1216. }
  1217. while (to_erase.size()) {
  1218. blend_times.erase(to_erase.front()->get());
  1219. to_erase.pop_front();
  1220. }
  1221. while (to_insert.size()) {
  1222. blend_times[to_insert.begin()->key] = to_insert.begin()->value;
  1223. to_insert.remove(to_insert.begin());
  1224. }
  1225. if (autoplay == p_from_name) {
  1226. autoplay = p_to_name;
  1227. }
  1228. }
  1229. void AnimationPlayer::_animation_renamed(const StringName &p_name, const StringName &p_to_name, const StringName &p_library) {
  1230. StringName from_name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
  1231. StringName to_name = p_library == StringName() ? p_to_name : StringName(String(p_library) + "/" + String(p_to_name));
  1232. if (!animation_set.has(from_name)) {
  1233. return; // No need to update because not the one from the library being used.
  1234. }
  1235. _animation_set_cache_update();
  1236. _rename_animation(from_name, to_name);
  1237. }
  1238. Error AnimationPlayer::add_animation_library(const StringName &p_name, const Ref<AnimationLibrary> &p_animation_library) {
  1239. ERR_FAIL_COND_V(p_animation_library.is_null(), ERR_INVALID_PARAMETER);
  1240. #ifdef DEBUG_ENABLED
  1241. ERR_FAIL_COND_V_MSG(String(p_name).contains("/") || String(p_name).contains(":") || String(p_name).contains(",") || String(p_name).contains("["), ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + ".");
  1242. #endif
  1243. int insert_pos = 0;
  1244. for (const AnimationLibraryData &lib : animation_libraries) {
  1245. ERR_FAIL_COND_V_MSG(lib.name == p_name, ERR_ALREADY_EXISTS, "Can't add animation library twice with name: " + String(p_name));
  1246. ERR_FAIL_COND_V_MSG(lib.library == p_animation_library, ERR_ALREADY_EXISTS, "Can't add animation library twice (adding as '" + p_name.operator String() + "', exists as '" + lib.name.operator String() + "'.");
  1247. if (lib.name.operator String() >= p_name.operator String()) {
  1248. break;
  1249. }
  1250. insert_pos++;
  1251. }
  1252. AnimationLibraryData ald;
  1253. ald.name = p_name;
  1254. ald.library = p_animation_library;
  1255. animation_libraries.insert(insert_pos, ald);
  1256. ald.library->connect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_name));
  1257. ald.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed).bind(p_name));
  1258. ald.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_name));
  1259. ald.library->connect(SNAME("animation_changed"), callable_mp(this, &AnimationPlayer::_animation_changed));
  1260. _animation_set_cache_update();
  1261. notify_property_list_changed();
  1262. return OK;
  1263. }
  1264. void AnimationPlayer::remove_animation_library(const StringName &p_name) {
  1265. int at_pos = -1;
  1266. for (uint32_t i = 0; i < animation_libraries.size(); i++) {
  1267. if (animation_libraries[i].name == p_name) {
  1268. at_pos = i;
  1269. break;
  1270. }
  1271. }
  1272. ERR_FAIL_COND(at_pos == -1);
  1273. animation_libraries[at_pos].library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
  1274. animation_libraries[at_pos].library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
  1275. animation_libraries[at_pos].library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
  1276. animation_libraries[at_pos].library->disconnect(SNAME("animation_changed"), callable_mp(this, &AnimationPlayer::_animation_changed));
  1277. stop();
  1278. animation_libraries.remove_at(at_pos);
  1279. _animation_set_cache_update();
  1280. notify_property_list_changed();
  1281. }
  1282. void AnimationPlayer::rename_animation_library(const StringName &p_name, const StringName &p_new_name) {
  1283. if (p_name == p_new_name) {
  1284. return;
  1285. }
  1286. #ifdef DEBUG_ENABLED
  1287. ERR_FAIL_COND_MSG(String(p_new_name).contains("/") || String(p_new_name).contains(":") || String(p_new_name).contains(",") || String(p_new_name).contains("["), "Invalid animation library name: " + String(p_new_name) + ".");
  1288. #endif
  1289. bool found = false;
  1290. for (AnimationLibraryData &lib : animation_libraries) {
  1291. ERR_FAIL_COND_MSG(lib.name == p_new_name, "Can't rename animation library to another existing name: " + String(p_new_name));
  1292. if (lib.name == p_name) {
  1293. found = true;
  1294. lib.name = p_new_name;
  1295. // rename connections
  1296. lib.library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
  1297. lib.library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
  1298. lib.library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
  1299. lib.library->connect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_new_name));
  1300. lib.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed).bind(p_new_name));
  1301. lib.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_new_name));
  1302. for (const KeyValue<StringName, Ref<Animation>> &K : lib.library->animations) {
  1303. StringName old_name = p_name == StringName() ? K.key : StringName(String(p_name) + "/" + String(K.key));
  1304. StringName new_name = p_new_name == StringName() ? K.key : StringName(String(p_new_name) + "/" + String(K.key));
  1305. _rename_animation(old_name, new_name);
  1306. }
  1307. }
  1308. }
  1309. ERR_FAIL_COND(!found);
  1310. stop();
  1311. animation_libraries.sort(); // Must keep alphabetical order.
  1312. _animation_set_cache_update(); // Update cache.
  1313. notify_property_list_changed();
  1314. }
  1315. bool AnimationPlayer::has_animation_library(const StringName &p_name) const {
  1316. for (const AnimationLibraryData &lib : animation_libraries) {
  1317. if (lib.name == p_name) {
  1318. return true;
  1319. }
  1320. }
  1321. return false;
  1322. }
  1323. Ref<AnimationLibrary> AnimationPlayer::get_animation_library(const StringName &p_name) const {
  1324. for (const AnimationLibraryData &lib : animation_libraries) {
  1325. if (lib.name == p_name) {
  1326. return lib.library;
  1327. }
  1328. }
  1329. ERR_FAIL_V(Ref<AnimationLibrary>());
  1330. }
  1331. TypedArray<StringName> AnimationPlayer::_get_animation_library_list() const {
  1332. TypedArray<StringName> ret;
  1333. for (const AnimationLibraryData &lib : animation_libraries) {
  1334. ret.push_back(lib.name);
  1335. }
  1336. return ret;
  1337. }
  1338. void AnimationPlayer::get_animation_library_list(List<StringName> *p_libraries) const {
  1339. for (const AnimationLibraryData &lib : animation_libraries) {
  1340. p_libraries->push_back(lib.name);
  1341. }
  1342. }
  1343. bool AnimationPlayer::has_animation(const StringName &p_name) const {
  1344. return animation_set.has(p_name);
  1345. }
  1346. Ref<Animation> AnimationPlayer::get_animation(const StringName &p_name) const {
  1347. ERR_FAIL_COND_V_MSG(!animation_set.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\".", p_name));
  1348. const AnimationData &anim_data = animation_set[p_name];
  1349. return anim_data.animation;
  1350. }
  1351. void AnimationPlayer::get_animation_list(List<StringName> *p_animations) const {
  1352. List<String> anims;
  1353. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1354. anims.push_back(E.key);
  1355. }
  1356. anims.sort();
  1357. for (const String &E : anims) {
  1358. p_animations->push_back(E);
  1359. }
  1360. }
  1361. void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time) {
  1362. ERR_FAIL_COND_MSG(!animation_set.has(p_animation1), vformat("Animation not found: %s.", p_animation1));
  1363. ERR_FAIL_COND_MSG(!animation_set.has(p_animation2), vformat("Animation not found: %s.", p_animation2));
  1364. ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");
  1365. BlendKey bk;
  1366. bk.from = p_animation1;
  1367. bk.to = p_animation2;
  1368. if (p_time == 0) {
  1369. blend_times.erase(bk);
  1370. } else {
  1371. blend_times[bk] = p_time;
  1372. }
  1373. }
  1374. double AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
  1375. BlendKey bk;
  1376. bk.from = p_animation1;
  1377. bk.to = p_animation2;
  1378. if (blend_times.has(bk)) {
  1379. return blend_times[bk];
  1380. } else {
  1381. return 0;
  1382. }
  1383. }
  1384. void AnimationPlayer::queue(const StringName &p_name) {
  1385. if (!is_playing()) {
  1386. play(p_name);
  1387. } else {
  1388. queued.push_back(p_name);
  1389. }
  1390. }
  1391. Vector<String> AnimationPlayer::get_queue() {
  1392. Vector<String> ret;
  1393. for (const StringName &E : queued) {
  1394. ret.push_back(E);
  1395. }
  1396. return ret;
  1397. }
  1398. void AnimationPlayer::clear_queue() {
  1399. queued.clear();
  1400. }
  1401. void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_blend) {
  1402. play(p_name, p_custom_blend, -1, true);
  1403. }
  1404. void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
  1405. StringName name = p_name;
  1406. if (String(name) == "") {
  1407. name = playback.assigned;
  1408. }
  1409. ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
  1410. Playback &c = playback;
  1411. if (c.current.from) {
  1412. double blend_time = 0.0;
  1413. // find if it can blend
  1414. BlendKey bk;
  1415. bk.from = c.current.from->name;
  1416. bk.to = name;
  1417. if (p_custom_blend >= 0) {
  1418. blend_time = p_custom_blend;
  1419. } else if (blend_times.has(bk)) {
  1420. blend_time = blend_times[bk];
  1421. } else {
  1422. bk.from = "*";
  1423. if (blend_times.has(bk)) {
  1424. blend_time = blend_times[bk];
  1425. } else {
  1426. bk.from = c.current.from->name;
  1427. bk.to = "*";
  1428. if (blend_times.has(bk)) {
  1429. blend_time = blend_times[bk];
  1430. }
  1431. }
  1432. }
  1433. if (p_custom_blend < 0 && blend_time == 0 && default_blend_time) {
  1434. blend_time = default_blend_time;
  1435. }
  1436. if (blend_time > 0) {
  1437. Blend b;
  1438. b.data = c.current;
  1439. b.blend_time = b.blend_left = blend_time;
  1440. c.blend.push_back(b);
  1441. } else {
  1442. c.blend.clear();
  1443. }
  1444. }
  1445. if (get_current_animation() != p_name) {
  1446. _clear_audio_streams();
  1447. _stop_playing_caches(false);
  1448. }
  1449. c.current.from = &animation_set[name];
  1450. if (c.assigned != name) { // reset
  1451. c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0;
  1452. } else {
  1453. if (p_from_end && c.current.pos == 0) {
  1454. // Animation reset BUT played backwards, set position to the end
  1455. c.current.pos = c.current.from->animation->get_length();
  1456. } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) {
  1457. // Animation resumed but already ended, set position to the beginning
  1458. c.current.pos = 0;
  1459. }
  1460. }
  1461. c.current.speed_scale = p_custom_scale;
  1462. c.assigned = name;
  1463. c.seeked = false;
  1464. c.started = true;
  1465. if (!end_reached) {
  1466. queued.clear();
  1467. }
  1468. _set_process(true); // always process when starting an animation
  1469. playing = true;
  1470. emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned);
  1471. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  1472. return; // no next in this case
  1473. }
  1474. StringName next = animation_get_next(p_name);
  1475. if (next != StringName() && animation_set.has(next)) {
  1476. queue(next);
  1477. }
  1478. }
  1479. bool AnimationPlayer::is_playing() const {
  1480. return playing;
  1481. }
  1482. void AnimationPlayer::set_current_animation(const String &p_anim) {
  1483. if (p_anim == "[stop]" || p_anim.is_empty()) {
  1484. stop();
  1485. } else if (!is_playing()) {
  1486. play(p_anim);
  1487. } else if (playback.assigned != p_anim) {
  1488. float speed = playback.current.speed_scale;
  1489. play(p_anim, -1.0, speed, signbit(speed));
  1490. } else {
  1491. // Same animation, do not replay from start
  1492. }
  1493. }
  1494. String AnimationPlayer::get_current_animation() const {
  1495. return (is_playing() ? playback.assigned : "");
  1496. }
  1497. void AnimationPlayer::set_assigned_animation(const String &p_anim) {
  1498. if (is_playing()) {
  1499. float speed = playback.current.speed_scale;
  1500. play(p_anim, -1.0, speed, signbit(speed));
  1501. } else {
  1502. ERR_FAIL_COND_MSG(!animation_set.has(p_anim), vformat("Animation not found: %s.", p_anim));
  1503. playback.current.pos = 0;
  1504. playback.current.from = &animation_set[p_anim];
  1505. playback.assigned = p_anim;
  1506. }
  1507. }
  1508. String AnimationPlayer::get_assigned_animation() const {
  1509. return playback.assigned;
  1510. }
  1511. void AnimationPlayer::pause() {
  1512. _stop_internal(false, false);
  1513. }
  1514. void AnimationPlayer::stop(bool p_keep_state) {
  1515. _stop_internal(true, p_keep_state);
  1516. }
  1517. void AnimationPlayer::set_speed_scale(float p_speed) {
  1518. speed_scale = p_speed;
  1519. }
  1520. float AnimationPlayer::get_speed_scale() const {
  1521. return speed_scale;
  1522. }
  1523. float AnimationPlayer::get_playing_speed() const {
  1524. if (!playing) {
  1525. return 0;
  1526. }
  1527. return speed_scale * playback.current.speed_scale;
  1528. }
  1529. void AnimationPlayer::seek(double p_time, bool p_update) {
  1530. playback.current.pos = p_time;
  1531. if (!playback.current.from) {
  1532. if (playback.assigned) {
  1533. ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
  1534. playback.current.from = &animation_set[playback.assigned];
  1535. }
  1536. if (!playback.current.from) {
  1537. return; // There is no animation.
  1538. }
  1539. }
  1540. playback.seeked = true;
  1541. if (p_update) {
  1542. _animation_process(0);
  1543. }
  1544. }
  1545. void AnimationPlayer::seek_delta(double p_time, double p_delta) {
  1546. playback.current.pos = p_time - p_delta;
  1547. if (!playback.current.from) {
  1548. if (playback.assigned) {
  1549. ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
  1550. playback.current.from = &animation_set[playback.assigned];
  1551. }
  1552. if (!playback.current.from) {
  1553. return; // There is no animation.
  1554. }
  1555. }
  1556. if (speed_scale != 0.0) {
  1557. p_delta /= speed_scale;
  1558. }
  1559. _animation_process(p_delta);
  1560. }
  1561. bool AnimationPlayer::is_valid() const {
  1562. return (playback.current.from);
  1563. }
  1564. double AnimationPlayer::get_current_animation_position() const {
  1565. ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
  1566. return playback.current.pos;
  1567. }
  1568. double AnimationPlayer::get_current_animation_length() const {
  1569. ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
  1570. return playback.current.from->animation->get_length();
  1571. }
  1572. void AnimationPlayer::_animation_changed(const StringName &p_name) {
  1573. clear_caches();
  1574. if (is_playing()) {
  1575. playback.seeked = true; //need to restart stuff, like audio
  1576. }
  1577. }
  1578. void AnimationPlayer::_stop_playing_caches(bool p_reset) {
  1579. for (TrackNodeCache *E : playing_caches) {
  1580. if (E->node && E->audio_playing) {
  1581. E->node->call(SNAME("stop"));
  1582. }
  1583. if (E->node && E->animation_playing) {
  1584. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(E->node);
  1585. if (!player) {
  1586. continue;
  1587. }
  1588. if (p_reset) {
  1589. player->stop();
  1590. } else {
  1591. player->pause();
  1592. }
  1593. }
  1594. }
  1595. playing_caches.clear();
  1596. }
  1597. void AnimationPlayer::_node_removed(Node *p_node) {
  1598. clear_caches(); // nodes contained here are being removed, clear the caches
  1599. }
  1600. void AnimationPlayer::clear_caches() {
  1601. _clear_audio_streams();
  1602. _stop_playing_caches(true);
  1603. node_cache_map.clear();
  1604. for (KeyValue<StringName, AnimationData> &E : animation_set) {
  1605. E.value.node_cache.clear();
  1606. }
  1607. cache_update_size = 0;
  1608. cache_update_prop_size = 0;
  1609. cache_update_bezier_size = 0;
  1610. cache_update_audio_size = 0;
  1611. emit_signal(SNAME("caches_cleared"));
  1612. }
  1613. void AnimationPlayer::_clear_audio_streams() {
  1614. for (int i = 0; i < playing_audio_stream_players.size(); i++) {
  1615. playing_audio_stream_players[i]->call(SNAME("stop"));
  1616. playing_audio_stream_players[i]->call(SNAME("set_stream"), Ref<AudioStream>());
  1617. }
  1618. playing_audio_stream_players.clear();
  1619. }
  1620. void AnimationPlayer::set_active(bool p_active) {
  1621. if (active == p_active) {
  1622. return;
  1623. }
  1624. active = p_active;
  1625. _set_process(processing, true);
  1626. }
  1627. bool AnimationPlayer::is_active() const {
  1628. return active;
  1629. }
  1630. StringName AnimationPlayer::find_animation(const Ref<Animation> &p_animation) const {
  1631. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1632. if (E.value.animation == p_animation) {
  1633. return E.key;
  1634. }
  1635. }
  1636. return StringName();
  1637. }
  1638. StringName AnimationPlayer::find_animation_library(const Ref<Animation> &p_animation) const {
  1639. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1640. if (E.value.animation == p_animation) {
  1641. return E.value.animation_library;
  1642. }
  1643. }
  1644. return StringName();
  1645. }
  1646. void AnimationPlayer::set_autoplay(const String &p_name) {
  1647. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  1648. WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
  1649. }
  1650. autoplay = p_name;
  1651. }
  1652. String AnimationPlayer::get_autoplay() const {
  1653. return autoplay;
  1654. }
  1655. void AnimationPlayer::set_reset_on_save_enabled(bool p_enabled) {
  1656. reset_on_save = p_enabled;
  1657. }
  1658. bool AnimationPlayer::is_reset_on_save_enabled() const {
  1659. return reset_on_save;
  1660. }
  1661. void AnimationPlayer::set_process_callback(AnimationProcessCallback p_mode) {
  1662. if (process_callback == p_mode) {
  1663. return;
  1664. }
  1665. bool pr = processing;
  1666. if (pr) {
  1667. _set_process(false);
  1668. }
  1669. process_callback = p_mode;
  1670. if (pr) {
  1671. _set_process(true);
  1672. }
  1673. }
  1674. AnimationPlayer::AnimationProcessCallback AnimationPlayer::get_process_callback() const {
  1675. return process_callback;
  1676. }
  1677. void AnimationPlayer::set_method_call_mode(AnimationMethodCallMode p_mode) {
  1678. method_call_mode = p_mode;
  1679. }
  1680. AnimationPlayer::AnimationMethodCallMode AnimationPlayer::get_method_call_mode() const {
  1681. return method_call_mode;
  1682. }
  1683. void AnimationPlayer::set_audio_max_polyphony(int p_audio_max_polyphony) {
  1684. ERR_FAIL_COND(p_audio_max_polyphony < 0 || p_audio_max_polyphony > 128);
  1685. audio_max_polyphony = p_audio_max_polyphony;
  1686. }
  1687. int AnimationPlayer::get_audio_max_polyphony() const {
  1688. return audio_max_polyphony;
  1689. }
  1690. void AnimationPlayer::set_movie_quit_on_finish_enabled(bool p_enabled) {
  1691. movie_quit_on_finish = p_enabled;
  1692. }
  1693. bool AnimationPlayer::is_movie_quit_on_finish_enabled() const {
  1694. return movie_quit_on_finish;
  1695. }
  1696. void AnimationPlayer::_set_process(bool p_process, bool p_force) {
  1697. if (processing == p_process && !p_force) {
  1698. return;
  1699. }
  1700. switch (process_callback) {
  1701. case ANIMATION_PROCESS_PHYSICS:
  1702. set_physics_process_internal(p_process && active);
  1703. break;
  1704. case ANIMATION_PROCESS_IDLE:
  1705. set_process_internal(p_process && active);
  1706. break;
  1707. case ANIMATION_PROCESS_MANUAL:
  1708. break;
  1709. }
  1710. processing = p_process;
  1711. }
  1712. void AnimationPlayer::_stop_internal(bool p_reset, bool p_keep_state) {
  1713. _clear_audio_streams();
  1714. _stop_playing_caches(p_reset);
  1715. Playback &c = playback;
  1716. c.blend.clear();
  1717. if (p_reset) {
  1718. if (p_keep_state) {
  1719. c.current.pos = 0;
  1720. } else {
  1721. is_stopping = true;
  1722. seek(0, true);
  1723. is_stopping = false;
  1724. }
  1725. c.current.from = nullptr;
  1726. c.current.speed_scale = 1;
  1727. }
  1728. _set_process(false);
  1729. queued.clear();
  1730. playing = false;
  1731. }
  1732. void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
  1733. ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
  1734. animation_set[p_animation].next = p_next;
  1735. }
  1736. StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
  1737. if (!animation_set.has(p_animation)) {
  1738. return StringName();
  1739. }
  1740. return animation_set[p_animation].next;
  1741. }
  1742. void AnimationPlayer::set_default_blend_time(double p_default) {
  1743. default_blend_time = p_default;
  1744. }
  1745. double AnimationPlayer::get_default_blend_time() const {
  1746. return default_blend_time;
  1747. }
  1748. void AnimationPlayer::set_root(const NodePath &p_root) {
  1749. root = p_root;
  1750. clear_caches();
  1751. }
  1752. NodePath AnimationPlayer::get_root() const {
  1753. return root;
  1754. }
  1755. void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1756. String pf = p_function;
  1757. if (p_idx == 0 && (p_function == "play" || p_function == "play_backwards" || p_function == "has_animation" || p_function == "queue")) {
  1758. List<StringName> al;
  1759. get_animation_list(&al);
  1760. for (const StringName &name : al) {
  1761. r_options->push_back(String(name).quote());
  1762. }
  1763. }
  1764. Node::get_argument_options(p_function, p_idx, r_options);
  1765. }
  1766. #ifdef TOOLS_ENABLED
  1767. Ref<AnimatedValuesBackup> AnimationPlayer::backup_animated_values(Node *p_root_override) {
  1768. Ref<AnimatedValuesBackup> backup;
  1769. if (!playback.current.from) {
  1770. return backup;
  1771. }
  1772. _ensure_node_caches(playback.current.from, p_root_override);
  1773. backup.instantiate();
  1774. for (int i = 0; i < playback.current.from->node_cache.size(); i++) {
  1775. TrackNodeCache *nc = playback.current.from->node_cache[i];
  1776. if (!nc) {
  1777. continue;
  1778. }
  1779. if (nc->skeleton) {
  1780. if (nc->bone_idx == -1) {
  1781. continue;
  1782. }
  1783. AnimatedValuesBackup::Entry entry;
  1784. entry.object = nc->skeleton;
  1785. entry.bone_idx = nc->bone_idx;
  1786. Array arr;
  1787. arr.resize(3);
  1788. arr[0] = nc->skeleton->get_bone_pose_position(nc->bone_idx);
  1789. arr[1] = nc->skeleton->get_bone_pose_rotation(nc->bone_idx);
  1790. arr[2] = nc->skeleton->get_bone_pose_scale(nc->bone_idx);
  1791. entry.value = nc;
  1792. backup->entries.push_back(entry);
  1793. } else {
  1794. if (nc->node_3d) {
  1795. AnimatedValuesBackup::Entry entry;
  1796. entry.object = nc->node_3d;
  1797. entry.subpath.push_back("transform");
  1798. entry.value = nc->node_3d->get_transform();
  1799. entry.bone_idx = -1;
  1800. backup->entries.push_back(entry);
  1801. } else {
  1802. for (const KeyValue<StringName, TrackNodeCache::PropertyAnim> &E : nc->property_anim) {
  1803. AnimatedValuesBackup::Entry entry;
  1804. entry.object = E.value.object;
  1805. entry.subpath = E.value.subpath;
  1806. bool valid;
  1807. entry.value = E.value.object->get_indexed(E.value.subpath, &valid);
  1808. entry.bone_idx = -1;
  1809. if (valid) {
  1810. backup->entries.push_back(entry);
  1811. }
  1812. }
  1813. }
  1814. }
  1815. }
  1816. return backup;
  1817. }
  1818. Ref<AnimatedValuesBackup> AnimationPlayer::apply_reset(bool p_user_initiated) {
  1819. ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>());
  1820. Ref<Animation> reset_anim = animation_set[SceneStringNames::get_singleton()->RESET].animation;
  1821. ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>());
  1822. Node *root_node = get_node_or_null(root);
  1823. ERR_FAIL_NULL_V(root_node, Ref<AnimatedValuesBackup>());
  1824. AnimationPlayer *aux_player = memnew(AnimationPlayer);
  1825. EditorNode::get_singleton()->add_child(aux_player);
  1826. Ref<AnimationLibrary> al;
  1827. al.instantiate();
  1828. al->add_animation(SceneStringNames::get_singleton()->RESET, reset_anim);
  1829. aux_player->add_animation_library("", al);
  1830. aux_player->set_assigned_animation(SceneStringNames::get_singleton()->RESET);
  1831. // Forcing the use of the original root because the scene where original player belongs may be not the active one
  1832. Ref<AnimatedValuesBackup> old_values = aux_player->backup_animated_values(get_node(get_root()));
  1833. aux_player->seek(0.0f, true);
  1834. aux_player->queue_free();
  1835. if (p_user_initiated) {
  1836. Ref<AnimatedValuesBackup> new_values = aux_player->backup_animated_values();
  1837. old_values->restore();
  1838. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1839. ur->create_action(TTR("Animation Apply Reset"));
  1840. ur->add_do_method(new_values.ptr(), "restore");
  1841. ur->add_undo_method(old_values.ptr(), "restore");
  1842. ur->commit_action();
  1843. }
  1844. return old_values;
  1845. }
  1846. bool AnimationPlayer::can_apply_reset() const {
  1847. return has_animation(SceneStringNames::get_singleton()->RESET) && playback.assigned != SceneStringNames::get_singleton()->RESET;
  1848. }
  1849. #endif // TOOLS_ENABLED
  1850. void AnimationPlayer::_bind_methods() {
  1851. ClassDB::bind_method(D_METHOD("add_animation_library", "name", "library"), &AnimationPlayer::add_animation_library);
  1852. ClassDB::bind_method(D_METHOD("remove_animation_library", "name"), &AnimationPlayer::remove_animation_library);
  1853. ClassDB::bind_method(D_METHOD("rename_animation_library", "name", "newname"), &AnimationPlayer::rename_animation_library);
  1854. ClassDB::bind_method(D_METHOD("has_animation_library", "name"), &AnimationPlayer::has_animation_library);
  1855. ClassDB::bind_method(D_METHOD("get_animation_library", "name"), &AnimationPlayer::get_animation_library);
  1856. ClassDB::bind_method(D_METHOD("get_animation_library_list"), &AnimationPlayer::_get_animation_library_list);
  1857. ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationPlayer::has_animation);
  1858. ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationPlayer::get_animation);
  1859. ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationPlayer::_get_animation_list);
  1860. ClassDB::bind_method(D_METHOD("animation_set_next", "anim_from", "anim_to"), &AnimationPlayer::animation_set_next);
  1861. ClassDB::bind_method(D_METHOD("animation_get_next", "anim_from"), &AnimationPlayer::animation_get_next);
  1862. ClassDB::bind_method(D_METHOD("set_blend_time", "anim_from", "anim_to", "sec"), &AnimationPlayer::set_blend_time);
  1863. ClassDB::bind_method(D_METHOD("get_blend_time", "anim_from", "anim_to"), &AnimationPlayer::get_blend_time);
  1864. ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
  1865. ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
  1866. ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
  1867. ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1));
  1868. ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);
  1869. ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));
  1870. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
  1871. ClassDB::bind_method(D_METHOD("set_current_animation", "anim"), &AnimationPlayer::set_current_animation);
  1872. ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
  1873. ClassDB::bind_method(D_METHOD("set_assigned_animation", "anim"), &AnimationPlayer::set_assigned_animation);
  1874. ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
  1875. ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
  1876. ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
  1877. ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
  1878. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationPlayer::set_active);
  1879. ClassDB::bind_method(D_METHOD("is_active"), &AnimationPlayer::is_active);
  1880. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
  1881. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
  1882. ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
  1883. ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
  1884. ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
  1885. ClassDB::bind_method(D_METHOD("set_reset_on_save_enabled", "enabled"), &AnimationPlayer::set_reset_on_save_enabled);
  1886. ClassDB::bind_method(D_METHOD("is_reset_on_save_enabled"), &AnimationPlayer::is_reset_on_save_enabled);
  1887. ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::set_root);
  1888. ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::get_root);
  1889. ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
  1890. ClassDB::bind_method(D_METHOD("find_animation_library", "animation"), &AnimationPlayer::find_animation_library);
  1891. ClassDB::bind_method(D_METHOD("clear_caches"), &AnimationPlayer::clear_caches);
  1892. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationPlayer::set_process_callback);
  1893. ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationPlayer::get_process_callback);
  1894. ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &AnimationPlayer::set_method_call_mode);
  1895. ClassDB::bind_method(D_METHOD("get_method_call_mode"), &AnimationPlayer::get_method_call_mode);
  1896. ClassDB::bind_method(D_METHOD("set_audio_max_polyphony", "max_polyphony"), &AnimationPlayer::set_audio_max_polyphony);
  1897. ClassDB::bind_method(D_METHOD("get_audio_max_polyphony"), &AnimationPlayer::get_audio_max_polyphony);
  1898. ClassDB::bind_method(D_METHOD("set_movie_quit_on_finish_enabled", "enabled"), &AnimationPlayer::set_movie_quit_on_finish_enabled);
  1899. ClassDB::bind_method(D_METHOD("is_movie_quit_on_finish_enabled"), &AnimationPlayer::is_movie_quit_on_finish_enabled);
  1900. ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
  1901. ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
  1902. ClassDB::bind_method(D_METHOD("seek", "seconds", "update"), &AnimationPlayer::seek, DEFVAL(false));
  1903. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationPlayer::advance);
  1904. GDVIRTUAL_BIND(_post_process_key_value, "animation", "track", "value", "object", "object_idx");
  1905. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_node"), "set_root", "get_root");
  1906. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR), "set_current_animation", "get_current_animation");
  1907. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");
  1908. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
  1909. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_on_save", PROPERTY_HINT_NONE, ""), "set_reset_on_save_enabled", "is_reset_on_save_enabled");
  1910. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");
  1911. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
  1912. ADD_GROUP("Playback Options", "playback_");
  1913. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
  1914. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:s"), "set_default_blend_time", "get_default_blend_time");
  1915. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_active", "is_active");
  1916. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
  1917. ADD_PROPERTY(PropertyInfo(Variant::INT, "method_call_mode", PROPERTY_HINT_ENUM, "Deferred,Immediate"), "set_method_call_mode", "get_method_call_mode");
  1918. ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_max_polyphony", PROPERTY_HINT_RANGE, "1,127,1"), "set_audio_max_polyphony", "get_audio_max_polyphony");
  1919. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");
  1920. ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1921. ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
  1922. ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1923. ADD_SIGNAL(MethodInfo("animation_list_changed"));
  1924. ADD_SIGNAL(MethodInfo("animation_libraries_updated"));
  1925. ADD_SIGNAL(MethodInfo("caches_cleared"));
  1926. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1927. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1928. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1929. BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_DEFERRED);
  1930. BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_IMMEDIATE);
  1931. }
  1932. AnimationPlayer::AnimationPlayer() {
  1933. root = SceneStringNames::get_singleton()->path_pp;
  1934. }
  1935. AnimationPlayer::~AnimationPlayer() {
  1936. }