animation_player.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /**************************************************************************/
  2. /* animation_player.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_player.h"
  31. #include "animation_player.compat.inc"
  32. #include "core/config/engine.h"
  33. #include "scene/scene_string_names.h"
  34. bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
  35. String name = p_name;
  36. if (name.begins_with("playback/play")) { // For backward compatibility.
  37. set_current_animation(p_value);
  38. } else if (name.begins_with("next/")) {
  39. String which = name.get_slicec('/', 1);
  40. animation_set_next(which, p_value);
  41. } else if (p_name == SceneStringNames::get_singleton()->blend_times) {
  42. Array array = p_value;
  43. int len = array.size();
  44. ERR_FAIL_COND_V(len % 3, false);
  45. for (int i = 0; i < len / 3; i++) {
  46. StringName from = array[i * 3 + 0];
  47. StringName to = array[i * 3 + 1];
  48. float time = array[i * 3 + 2];
  49. set_blend_time(from, to, time);
  50. }
  51. #ifndef DISABLE_DEPRECATED
  52. } else if (p_name == "method_call_mode") {
  53. set_callback_mode_method(static_cast<AnimationCallbackModeMethod>((int)p_value));
  54. } else if (p_name == "playback_process_mode") {
  55. set_callback_mode_process(static_cast<AnimationCallbackModeProcess>((int)p_value));
  56. } else if (p_name == "playback_active") {
  57. set_active(p_value);
  58. #endif // DISABLE_DEPRECATED
  59. } else {
  60. return false;
  61. }
  62. return true;
  63. }
  64. bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
  65. String name = p_name;
  66. if (name == "playback/play") { // For backward compatibility.
  67. r_ret = get_current_animation();
  68. } else if (name.begins_with("next/")) {
  69. String which = name.get_slicec('/', 1);
  70. r_ret = animation_get_next(which);
  71. } else if (name == "blend_times") {
  72. Vector<BlendKey> keys;
  73. for (const KeyValue<BlendKey, double> &E : blend_times) {
  74. keys.ordered_insert(E.key);
  75. }
  76. Array array;
  77. for (int i = 0; i < keys.size(); i++) {
  78. array.push_back(keys[i].from);
  79. array.push_back(keys[i].to);
  80. array.push_back(blend_times.get(keys[i]));
  81. }
  82. r_ret = array;
  83. #ifndef DISABLE_DEPRECATED
  84. } else if (name == "method_call_mode") {
  85. r_ret = get_callback_mode_method();
  86. } else if (name == "playback_process_mode") {
  87. r_ret = get_callback_mode_process();
  88. } else if (name == "playback_active") {
  89. r_ret = is_active();
  90. #endif // DISABLE_DEPRECATED
  91. } else {
  92. return false;
  93. }
  94. return true;
  95. }
  96. void AnimationPlayer::_validate_property(PropertyInfo &p_property) const {
  97. AnimationMixer::_validate_property(p_property);
  98. if (p_property.name == "current_animation") {
  99. List<String> names;
  100. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  101. names.push_back(E.key);
  102. }
  103. names.push_front("[stop]");
  104. String hint;
  105. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  106. if (E != names.front()) {
  107. hint += ",";
  108. }
  109. hint += E->get();
  110. }
  111. p_property.hint_string = hint;
  112. }
  113. }
  114. void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  115. List<PropertyInfo> anim_names;
  116. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  117. HashMap<StringName, StringName>::ConstIterator F = animation_next_set.find(E.key);
  118. if (F && F->value != StringName()) {
  119. anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  120. }
  121. }
  122. for (const PropertyInfo &E : anim_names) {
  123. p_list->push_back(E);
  124. }
  125. p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  126. }
  127. void AnimationPlayer::_notification(int p_what) {
  128. switch (p_what) {
  129. case NOTIFICATION_READY: {
  130. if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
  131. set_active(true);
  132. play(autoplay);
  133. }
  134. } break;
  135. }
  136. }
  137. void AnimationPlayer::_process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started, bool p_is_current) {
  138. double speed = speed_scale * cd.speed_scale;
  139. bool backwards = signbit(speed); // Negative zero means playing backwards too.
  140. double delta = p_started ? 0 : p_delta * speed;
  141. double next_pos = cd.pos + delta;
  142. real_t len = cd.from->animation->get_length();
  143. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  144. switch (cd.from->animation->get_loop_mode()) {
  145. case Animation::LOOP_NONE: {
  146. if (next_pos < 0) {
  147. next_pos = 0;
  148. } else if (next_pos > len) {
  149. next_pos = len;
  150. }
  151. delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here).
  152. } break;
  153. case Animation::LOOP_LINEAR: {
  154. if (next_pos < 0 && cd.pos >= 0) {
  155. looped_flag = Animation::LOOPED_FLAG_START;
  156. }
  157. if (next_pos > len && cd.pos <= len) {
  158. looped_flag = Animation::LOOPED_FLAG_END;
  159. }
  160. next_pos = Math::fposmod(next_pos, (double)len);
  161. } break;
  162. case Animation::LOOP_PINGPONG: {
  163. if (next_pos < 0 && cd.pos >= 0) {
  164. cd.speed_scale *= -1.0;
  165. looped_flag = Animation::LOOPED_FLAG_START;
  166. }
  167. if (next_pos > len && cd.pos <= len) {
  168. cd.speed_scale *= -1.0;
  169. looped_flag = Animation::LOOPED_FLAG_END;
  170. }
  171. next_pos = Math::pingpong(next_pos, (double)len);
  172. } break;
  173. default:
  174. break;
  175. }
  176. double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.
  177. cd.pos = next_pos;
  178. // End detection.
  179. if (p_is_current) {
  180. if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {
  181. if (!backwards && prev_pos <= len && next_pos == len) {
  182. // Playback finished.
  183. end_reached = true;
  184. end_notify = prev_pos < len; // Notify only if not already at the end.
  185. p_blend = 1.0;
  186. }
  187. if (backwards && prev_pos >= 0 && next_pos == 0) {
  188. // Playback finished.
  189. end_reached = true;
  190. end_notify = prev_pos > 0; // Notify only if not already at the beginning.
  191. p_blend = 1.0;
  192. }
  193. }
  194. }
  195. PlaybackInfo pi;
  196. if (p_started) {
  197. pi.time = prev_pos;
  198. pi.delta = 0;
  199. pi.seeked = true;
  200. } else {
  201. pi.time = next_pos;
  202. pi.delta = delta;
  203. pi.seeked = p_seeked;
  204. }
  205. pi.is_external_seeking = false;
  206. pi.looped_flag = looped_flag;
  207. pi.weight = p_blend;
  208. make_animation_instance(cd.from->name, pi);
  209. }
  210. void AnimationPlayer::_blend_playback_data(double p_delta, bool p_started) {
  211. Playback &c = playback;
  212. bool seeked = c.seeked; // The animation may be changed during process, so it is safer that the state is changed before process.
  213. if (p_delta != 0) {
  214. c.seeked = false;
  215. }
  216. // First, calc all blends weight.
  217. float blend = 1.0;
  218. for (List<Blend>::Element *E = c.blend.front(); E; E = E->next()) {
  219. Blend &b = E->get();
  220. blend = MAX(0, blend - b.blend_left);
  221. b.blend_left = MAX(0, b.blend_left - Math::absf(speed_scale * p_delta) / b.blend_time);
  222. }
  223. // Second, process current animation to check if the animation end reached.
  224. _process_playback_data(c.current, p_delta, blend, seeked, p_started, true);
  225. // Finally, if not end the animation, do blending.
  226. if (end_reached) {
  227. playback.blend.clear();
  228. return;
  229. }
  230. List<List<Blend>::Element *> to_erase;
  231. for (List<Blend>::Element *E = c.blend.front(); E; E = E->next()) {
  232. Blend &b = E->get();
  233. if (b.blend_left <= 0) {
  234. to_erase.push_back(E);
  235. b.blend_left = CMP_EPSILON; // May want to play last frame.
  236. }
  237. // Note: There may be issues if an animation event triggers an animation change while this blend is active,
  238. // so it is best to use "deferred" calls instead of "immediate" for animation events that can trigger new animations.
  239. _process_playback_data(b.data, p_delta, b.blend_left, false, false);
  240. }
  241. for (List<Blend>::Element *&E : to_erase) {
  242. c.blend.erase(E);
  243. }
  244. }
  245. bool AnimationPlayer::_blend_pre_process(double p_delta, int p_track_count, const HashMap<NodePath, int> &p_track_map) {
  246. if (!playback.current.from) {
  247. _set_process(false);
  248. return false;
  249. }
  250. tmp_from = playback.current.from->animation->get_instance_id();
  251. end_reached = false;
  252. end_notify = false;
  253. bool started = playback.started; // The animation may be changed during process, so it is safer that the state is changed before process.
  254. if (playback.started) {
  255. playback.started = false;
  256. }
  257. AnimationData *prev_from = playback.current.from;
  258. _blend_playback_data(p_delta, started);
  259. if (prev_from != playback.current.from) {
  260. return false; // Animation has been changed in the process (may be caused by method track), abort process.
  261. }
  262. return true;
  263. }
  264. void AnimationPlayer::_blend_post_process() {
  265. if (end_reached) {
  266. // If the method track changes current animation, the animation is not finished.
  267. if (tmp_from == playback.current.from->animation->get_instance_id()) {
  268. if (playback_queue.size()) {
  269. String old = playback.assigned;
  270. play(playback_queue.front()->get());
  271. String new_name = playback.assigned;
  272. playback_queue.pop_front();
  273. if (end_notify) {
  274. emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
  275. }
  276. } else {
  277. _clear_caches();
  278. playing = false;
  279. _set_process(false);
  280. if (end_notify) {
  281. emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned);
  282. if (movie_quit_on_finish && OS::get_singleton()->has_feature("movie")) {
  283. print_line(vformat("Movie Maker mode is enabled. Quitting on animation finish as requested by: %s", get_path()));
  284. get_tree()->quit();
  285. }
  286. }
  287. }
  288. }
  289. end_reached = false;
  290. end_notify = false;
  291. }
  292. tmp_from = ObjectID();
  293. }
  294. void AnimationPlayer::queue(const StringName &p_name) {
  295. if (!is_playing()) {
  296. play(p_name);
  297. } else {
  298. playback_queue.push_back(p_name);
  299. }
  300. }
  301. Vector<String> AnimationPlayer::get_queue() {
  302. Vector<String> ret;
  303. for (const StringName &E : playback_queue) {
  304. ret.push_back(E);
  305. }
  306. return ret;
  307. }
  308. void AnimationPlayer::clear_queue() {
  309. playback_queue.clear();
  310. }
  311. void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_blend) {
  312. play(p_name, p_custom_blend, -1, true);
  313. }
  314. void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
  315. StringName name = p_name;
  316. if (String(name) == "") {
  317. name = playback.assigned;
  318. }
  319. ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
  320. Playback &c = playback;
  321. if (c.current.from) {
  322. double blend_time = 0.0;
  323. // Find if it can blend.
  324. BlendKey bk;
  325. bk.from = c.current.from->name;
  326. bk.to = name;
  327. if (p_custom_blend >= 0) {
  328. blend_time = p_custom_blend;
  329. } else if (blend_times.has(bk)) {
  330. blend_time = blend_times[bk];
  331. } else {
  332. bk.from = "*";
  333. if (blend_times.has(bk)) {
  334. blend_time = blend_times[bk];
  335. } else {
  336. bk.from = c.current.from->name;
  337. bk.to = "*";
  338. if (blend_times.has(bk)) {
  339. blend_time = blend_times[bk];
  340. }
  341. }
  342. }
  343. if (p_custom_blend < 0 && blend_time == 0 && default_blend_time) {
  344. blend_time = default_blend_time;
  345. }
  346. if (blend_time > 0) {
  347. Blend b;
  348. b.data = c.current;
  349. b.blend_left = 1.0;
  350. b.blend_time = blend_time;
  351. c.blend.push_back(b);
  352. } else {
  353. c.blend.clear();
  354. }
  355. }
  356. if (get_current_animation() != p_name) {
  357. _clear_caches();
  358. }
  359. c.current.from = &animation_set[name];
  360. c.current.speed_scale = p_custom_scale;
  361. if (!end_reached) {
  362. playback_queue.clear();
  363. }
  364. if (c.assigned != name) { // Reset.
  365. c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0;
  366. c.assigned = name;
  367. emit_signal(SNAME("current_animation_changed"), c.assigned);
  368. } else {
  369. if (p_from_end && c.current.pos == 0) {
  370. // Animation reset but played backwards, set position to the end.
  371. c.current.pos = c.current.from->animation->get_length();
  372. } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) {
  373. // Animation resumed but already ended, set position to the beginning.
  374. c.current.pos = 0;
  375. } else if (playing) {
  376. return;
  377. }
  378. }
  379. c.seeked = false;
  380. c.started = true;
  381. _set_process(true); // Always process when starting an animation.
  382. playing = true;
  383. emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned);
  384. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  385. return; // No next in this case.
  386. }
  387. StringName next = animation_get_next(p_name);
  388. if (next != StringName() && animation_set.has(next)) {
  389. queue(next);
  390. }
  391. }
  392. bool AnimationPlayer::is_playing() const {
  393. return playing;
  394. }
  395. void AnimationPlayer::set_current_animation(const String &p_animation) {
  396. if (p_animation == "[stop]" || p_animation.is_empty()) {
  397. stop();
  398. } else if (!is_playing()) {
  399. play(p_animation);
  400. } else if (playback.assigned != p_animation) {
  401. float speed = playback.current.speed_scale;
  402. play(p_animation, -1.0, speed, signbit(speed));
  403. } else {
  404. // Same animation, do not replay from start.
  405. }
  406. }
  407. String AnimationPlayer::get_current_animation() const {
  408. return (is_playing() ? playback.assigned : "");
  409. }
  410. void AnimationPlayer::set_assigned_animation(const String &p_animation) {
  411. if (is_playing()) {
  412. float speed = playback.current.speed_scale;
  413. play(p_animation, -1.0, speed, signbit(speed));
  414. } else {
  415. ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
  416. playback.current.pos = 0;
  417. playback.current.from = &animation_set[p_animation];
  418. playback.assigned = p_animation;
  419. emit_signal(SNAME("current_animation_changed"), playback.assigned);
  420. }
  421. }
  422. String AnimationPlayer::get_assigned_animation() const {
  423. return playback.assigned;
  424. }
  425. void AnimationPlayer::pause() {
  426. _stop_internal(false, false);
  427. }
  428. void AnimationPlayer::stop(bool p_keep_state) {
  429. _stop_internal(true, p_keep_state);
  430. }
  431. void AnimationPlayer::set_speed_scale(float p_speed) {
  432. speed_scale = p_speed;
  433. }
  434. float AnimationPlayer::get_speed_scale() const {
  435. return speed_scale;
  436. }
  437. float AnimationPlayer::get_playing_speed() const {
  438. if (!playing) {
  439. return 0;
  440. }
  441. return speed_scale * playback.current.speed_scale;
  442. }
  443. void AnimationPlayer::seek(double p_time, bool p_update, bool p_update_only) {
  444. if (!active) {
  445. return;
  446. }
  447. playback.current.pos = p_time;
  448. if (!playback.current.from) {
  449. if (playback.assigned) {
  450. ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
  451. playback.current.from = &animation_set[playback.assigned];
  452. }
  453. if (!playback.current.from) {
  454. return; // There is no animation.
  455. }
  456. }
  457. playback.seeked = true;
  458. if (p_update) {
  459. _process_animation(0, p_update_only);
  460. }
  461. }
  462. bool AnimationPlayer::is_valid() const {
  463. return (playback.current.from);
  464. }
  465. double AnimationPlayer::get_current_animation_position() const {
  466. ERR_FAIL_NULL_V_MSG(playback.current.from, 0, "AnimationPlayer has no current animation.");
  467. return playback.current.pos;
  468. }
  469. double AnimationPlayer::get_current_animation_length() const {
  470. ERR_FAIL_NULL_V_MSG(playback.current.from, 0, "AnimationPlayer has no current animation.");
  471. return playback.current.from->animation->get_length();
  472. }
  473. void AnimationPlayer::set_autoplay(const String &p_name) {
  474. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  475. WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
  476. }
  477. autoplay = p_name;
  478. }
  479. String AnimationPlayer::get_autoplay() const {
  480. return autoplay;
  481. }
  482. void AnimationPlayer::set_movie_quit_on_finish_enabled(bool p_enabled) {
  483. movie_quit_on_finish = p_enabled;
  484. }
  485. bool AnimationPlayer::is_movie_quit_on_finish_enabled() const {
  486. return movie_quit_on_finish;
  487. }
  488. void AnimationPlayer::_stop_internal(bool p_reset, bool p_keep_state) {
  489. _clear_caches();
  490. Playback &c = playback;
  491. // c.blend.clear();
  492. if (p_reset) {
  493. c.blend.clear();
  494. if (p_keep_state) {
  495. c.current.pos = 0;
  496. } else {
  497. is_stopping = true;
  498. seek(0, true, true);
  499. is_stopping = false;
  500. }
  501. c.current.from = nullptr;
  502. c.current.speed_scale = 1;
  503. emit_signal(SNAME("current_animation_changed"), "");
  504. }
  505. _set_process(false);
  506. playback_queue.clear();
  507. playing = false;
  508. }
  509. void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
  510. ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
  511. animation_next_set[p_animation] = p_next;
  512. }
  513. StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
  514. if (!animation_next_set.has(p_animation)) {
  515. return StringName();
  516. }
  517. return animation_next_set[p_animation];
  518. }
  519. void AnimationPlayer::set_default_blend_time(double p_default) {
  520. default_blend_time = p_default;
  521. }
  522. double AnimationPlayer::get_default_blend_time() const {
  523. return default_blend_time;
  524. }
  525. void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time) {
  526. ERR_FAIL_COND_MSG(!animation_set.has(p_animation1), vformat("Animation not found: %s.", p_animation1));
  527. ERR_FAIL_COND_MSG(!animation_set.has(p_animation2), vformat("Animation not found: %s.", p_animation2));
  528. ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");
  529. BlendKey bk;
  530. bk.from = p_animation1;
  531. bk.to = p_animation2;
  532. if (p_time == 0) {
  533. blend_times.erase(bk);
  534. } else {
  535. blend_times[bk] = p_time;
  536. }
  537. }
  538. double AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
  539. BlendKey bk;
  540. bk.from = p_animation1;
  541. bk.to = p_animation2;
  542. if (blend_times.has(bk)) {
  543. return blend_times[bk];
  544. } else {
  545. return 0;
  546. }
  547. }
  548. void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  549. String pf = p_function;
  550. if (p_idx == 0 && (p_function == "play" || p_function == "play_backwards" || p_function == "has_animation" || p_function == "queue")) {
  551. List<StringName> al;
  552. get_animation_list(&al);
  553. for (const StringName &name : al) {
  554. r_options->push_back(String(name).quote());
  555. }
  556. }
  557. Node::get_argument_options(p_function, p_idx, r_options);
  558. }
  559. void AnimationPlayer::_animation_removed(const StringName &p_name, const StringName &p_library) {
  560. AnimationMixer::_animation_removed(p_name, p_library);
  561. StringName name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
  562. if (!animation_set.has(name)) {
  563. return; // No need to update because not the one from the library being used.
  564. }
  565. _animation_set_cache_update();
  566. // Erase blends if needed
  567. List<BlendKey> to_erase;
  568. for (const KeyValue<BlendKey, double> &E : blend_times) {
  569. BlendKey bk = E.key;
  570. if (bk.from == name || bk.to == name) {
  571. to_erase.push_back(bk);
  572. }
  573. }
  574. while (to_erase.size()) {
  575. blend_times.erase(to_erase.front()->get());
  576. to_erase.pop_front();
  577. }
  578. }
  579. void AnimationPlayer::_rename_animation(const StringName &p_from_name, const StringName &p_to_name) {
  580. AnimationMixer::_rename_animation(p_from_name, p_to_name);
  581. // Rename autoplay or blends if needed.
  582. List<BlendKey> to_erase;
  583. HashMap<BlendKey, double, BlendKey> to_insert;
  584. for (const KeyValue<BlendKey, double> &E : blend_times) {
  585. BlendKey bk = E.key;
  586. BlendKey new_bk = bk;
  587. bool erase = false;
  588. if (bk.from == p_from_name) {
  589. new_bk.from = p_to_name;
  590. erase = true;
  591. }
  592. if (bk.to == p_from_name) {
  593. new_bk.to = p_to_name;
  594. erase = true;
  595. }
  596. if (erase) {
  597. to_erase.push_back(bk);
  598. to_insert[new_bk] = E.value;
  599. }
  600. }
  601. while (to_erase.size()) {
  602. blend_times.erase(to_erase.front()->get());
  603. to_erase.pop_front();
  604. }
  605. while (to_insert.size()) {
  606. blend_times[to_insert.begin()->key] = to_insert.begin()->value;
  607. to_insert.remove(to_insert.begin());
  608. }
  609. if (autoplay == p_from_name) {
  610. autoplay = p_to_name;
  611. }
  612. }
  613. void AnimationPlayer::_bind_methods() {
  614. ClassDB::bind_method(D_METHOD("animation_set_next", "animation_from", "animation_to"), &AnimationPlayer::animation_set_next);
  615. ClassDB::bind_method(D_METHOD("animation_get_next", "animation_from"), &AnimationPlayer::animation_get_next);
  616. ClassDB::bind_method(D_METHOD("set_blend_time", "animation_from", "animation_to", "sec"), &AnimationPlayer::set_blend_time);
  617. ClassDB::bind_method(D_METHOD("get_blend_time", "animation_from", "animation_to"), &AnimationPlayer::get_blend_time);
  618. ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
  619. ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
  620. ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
  621. ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1));
  622. ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);
  623. ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));
  624. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
  625. ClassDB::bind_method(D_METHOD("set_current_animation", "animation"), &AnimationPlayer::set_current_animation);
  626. ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
  627. ClassDB::bind_method(D_METHOD("set_assigned_animation", "animation"), &AnimationPlayer::set_assigned_animation);
  628. ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
  629. ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
  630. ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
  631. ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
  632. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
  633. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
  634. ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
  635. ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
  636. ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
  637. ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
  638. ClassDB::bind_method(D_METHOD("find_animation_library", "animation"), &AnimationPlayer::find_animation_library);
  639. ClassDB::bind_method(D_METHOD("set_movie_quit_on_finish_enabled", "enabled"), &AnimationPlayer::set_movie_quit_on_finish_enabled);
  640. ClassDB::bind_method(D_METHOD("is_movie_quit_on_finish_enabled"), &AnimationPlayer::is_movie_quit_on_finish_enabled);
  641. ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
  642. ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
  643. ClassDB::bind_method(D_METHOD("seek", "seconds", "update", "update_only"), &AnimationPlayer::seek, DEFVAL(false), DEFVAL(false));
  644. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR), "set_current_animation", "get_current_animation");
  645. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");
  646. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
  647. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");
  648. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
  649. ADD_GROUP("Playback Options", "playback_");
  650. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:s"), "set_default_blend_time", "get_default_blend_time");
  651. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-4,4,0.001,or_less,or_greater"), "set_speed_scale", "get_speed_scale");
  652. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");
  653. ADD_SIGNAL(MethodInfo(SNAME("current_animation_changed"), PropertyInfo(Variant::STRING, "name")));
  654. ADD_SIGNAL(MethodInfo(SNAME("animation_changed"), PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
  655. }
  656. AnimationPlayer::AnimationPlayer() {
  657. }
  658. AnimationPlayer::~AnimationPlayer() {
  659. }