animation_player.cpp 69 KB

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