animation_player.cpp 51 KB

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