animation_player.cpp 48 KB

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