Path: blob/master/scene/animation/animation_player.cpp
9902 views
/**************************************************************************/1/* animation_player.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "animation_player.h"31#include "animation_player.compat.inc"3233#include "core/config/engine.h"3435bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {36String name = p_name;37if (name.begins_with("playback/play")) { // For backward compatibility.38set_current_animation(p_value);39} else if (name.begins_with("next/")) {40String which = name.get_slicec('/', 1);41animation_set_next(which, p_value);42} else if (p_name == SceneStringName(blend_times)) {43Array array = p_value;44int len = array.size();45ERR_FAIL_COND_V(len % 3, false);4647for (int i = 0; i < len / 3; i++) {48StringName from = array[i * 3 + 0];49StringName to = array[i * 3 + 1];50float time = array[i * 3 + 2];51set_blend_time(from, to, time);52}53#ifndef DISABLE_DEPRECATED54} else if (p_name == "method_call_mode") {55set_callback_mode_method(static_cast<AnimationCallbackModeMethod>((int)p_value));56} else if (p_name == "playback_process_mode") {57set_callback_mode_process(static_cast<AnimationCallbackModeProcess>((int)p_value));58} else if (p_name == "playback_active") {59set_active(p_value);60#endif // DISABLE_DEPRECATED61} else {62return false;63}64return true;65}6667bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {68String name = p_name;6970if (name == "playback/play") { // For backward compatibility.7172r_ret = get_current_animation();7374} else if (name.begins_with("next/")) {75String which = name.get_slicec('/', 1);76r_ret = animation_get_next(which);7778} else if (p_name == SceneStringName(blend_times)) {79Vector<BlendKey> keys;80for (const KeyValue<BlendKey, double> &E : blend_times) {81keys.ordered_insert(E.key);82}8384Array array;85for (int i = 0; i < keys.size(); i++) {86array.push_back(keys[i].from);87array.push_back(keys[i].to);88array.push_back(blend_times.get(keys[i]));89}9091r_ret = array;92#ifndef DISABLE_DEPRECATED93} else if (name == "method_call_mode") {94r_ret = get_callback_mode_method();95} else if (name == "playback_process_mode") {96r_ret = get_callback_mode_process();97} else if (name == "playback_active") {98r_ret = is_active();99#endif // DISABLE_DEPRECATED100} else {101return false;102}103104return true;105}106107void AnimationPlayer::_validate_property(PropertyInfo &p_property) const {108if (Engine::get_singleton()->is_editor_hint() && p_property.name == "current_animation") {109List<String> names;110111for (const KeyValue<StringName, AnimationData> &E : animation_set) {112names.push_back(E.key);113}114names.push_front("[stop]");115String hint;116for (List<String>::Element *E = names.front(); E; E = E->next()) {117if (E != names.front()) {118hint += ",";119}120hint += E->get();121}122123p_property.hint_string = hint;124} else if (!auto_capture && p_property.name.begins_with("playback_auto_capture_")) {125p_property.usage = PROPERTY_USAGE_NONE;126}127}128129void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {130List<PropertyInfo> anim_names;131132for (const KeyValue<StringName, AnimationData> &E : animation_set) {133AHashMap<StringName, StringName>::ConstIterator F = animation_next_set.find(E.key);134if (F && F->value != StringName()) {135anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));136}137}138139for (const PropertyInfo &E : anim_names) {140p_list->push_back(E);141}142143p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));144}145146void AnimationPlayer::_notification(int p_what) {147switch (p_what) {148case NOTIFICATION_READY: {149if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {150set_active(active);151play(autoplay);152_check_immediately_after_start();153}154} break;155}156}157158void AnimationPlayer::_process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_internal_seeked, bool p_started, bool p_is_current) {159double speed = speed_scale * cd.speed_scale;160bool backwards = std::signbit(speed); // Negative zero means playing backwards too.161double delta = p_started ? 0 : p_delta * speed;162double next_pos = cd.pos + delta;163164double start = cd.get_start_time();165double end = cd.get_end_time();166167Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;168169switch (cd.from->animation->get_loop_mode()) {170case Animation::LOOP_NONE: {171if (Animation::is_less_approx(next_pos, start)) {172next_pos = start;173} else if (Animation::is_greater_approx(next_pos, end)) {174next_pos = end;175}176delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here).177} break;178179case Animation::LOOP_LINEAR: {180if (Animation::is_less_approx(next_pos, start) && Animation::is_greater_or_equal_approx(cd.pos, start)) {181looped_flag = Animation::LOOPED_FLAG_START;182}183if (Animation::is_greater_approx(next_pos, end) && Animation::is_less_or_equal_approx(cd.pos, end)) {184looped_flag = Animation::LOOPED_FLAG_END;185}186next_pos = Math::fposmod(next_pos - start, end - start) + start;187} break;188189case Animation::LOOP_PINGPONG: {190if (Animation::is_less_approx(next_pos, start) && Animation::is_greater_or_equal_approx(cd.pos, start)) {191cd.speed_scale *= -1.0;192looped_flag = Animation::LOOPED_FLAG_START;193}194if (Animation::is_greater_approx(next_pos, end) && Animation::is_less_or_equal_approx(cd.pos, end)) {195cd.speed_scale *= -1.0;196looped_flag = Animation::LOOPED_FLAG_END;197}198next_pos = Math::pingpong(next_pos - start, end - start) + start;199} break;200201default:202break;203}204205double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.206207// End detection.208if (p_is_current) {209if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {210if (!backwards && Animation::is_less_or_equal_approx(prev_pos, end) && Math::is_equal_approx(next_pos, end)) {211// Playback finished.212next_pos = end; // Snap to the edge.213end_reached = true;214end_notify = Animation::is_less_approx(prev_pos, end); // Notify only if not already at the end.215p_blend = 1.0;216}217if (backwards && Animation::is_greater_or_equal_approx(prev_pos, start) && Math::is_equal_approx(next_pos, start)) {218// Playback finished.219next_pos = start; // Snap to the edge.220end_reached = true;221end_notify = Animation::is_greater_approx(prev_pos, start); // Notify only if not already at the beginning.222p_blend = 1.0;223}224}225}226227cd.pos = next_pos;228229PlaybackInfo pi;230if (p_started) {231pi.time = prev_pos;232pi.delta = 0;233pi.start = start;234pi.end = end;235pi.seeked = true;236} else {237pi.time = next_pos;238pi.delta = delta;239pi.start = start;240pi.end = end;241pi.seeked = p_seeked;242}243if (Math::is_zero_approx(pi.delta) && backwards) {244pi.delta = -0.0; // Sign is needed to handle converted Continuous track from Discrete track correctly.245}246// Immediately after playback, discrete keys should be retrieved with EXACT mode since behind keys must be ignored at that time.247pi.is_external_seeking = !p_internal_seeked && !p_started;248pi.looped_flag = looped_flag;249pi.weight = p_blend;250make_animation_instance(cd.from->name, pi);251}252253float AnimationPlayer::get_current_blend_amount() {254Playback &c = playback;255float blend = 1.0;256for (const Blend &b : c.blend) {257blend = blend - b.blend_left;258}259return MAX(0, blend);260}261262void AnimationPlayer::_blend_playback_data(double p_delta, bool p_started) {263Playback &c = playback;264265bool seeked = c.seeked; // The animation may be changed during process, so it is safer that the state is changed before process.266bool internal_seeked = c.internal_seeked;267268if (!Math::is_zero_approx(p_delta)) {269c.seeked = false;270c.internal_seeked = false;271}272273// Second, process current animation to check if the animation end reached.274_process_playback_data(c.current, p_delta, get_current_blend_amount(), seeked, internal_seeked, p_started, true);275276// Finally, if not end the animation, do blending.277if (end_reached) {278playback.blend.clear();279return;280}281List<List<Blend>::Element *> to_erase;282for (List<Blend>::Element *E = c.blend.front(); E; E = E->next()) {283Blend &b = E->get();284b.blend_left = MAX(0, b.blend_left - Math::abs(speed_scale * p_delta) / b.blend_time);285if (Animation::is_less_or_equal_approx(b.blend_left, 0)) {286to_erase.push_back(E);287b.blend_left = CMP_EPSILON; // May want to play last frame.288}289// Note: There may be issues if an animation event triggers an animation change while this blend is active,290// so it is best to use "deferred" calls instead of "immediate" for animation events that can trigger new animations.291_process_playback_data(b.data, p_delta, b.blend_left, false, false, false);292}293for (List<Blend>::Element *&E : to_erase) {294c.blend.erase(E);295}296}297298bool AnimationPlayer::_blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) {299if (!playback.current.from) {300_set_process(false);301return false;302}303304tmp_from = playback.current.from->animation->get_instance_id();305end_reached = false;306end_notify = false;307308bool started = playback.started; // The animation may be changed during process, so it is safer that the state is changed before process.309if (playback.started) {310playback.started = false;311}312313AnimationData *prev_from = playback.current.from;314_blend_playback_data(p_delta, started);315316if (prev_from != playback.current.from) {317return false; // Animation has been changed in the process (may be caused by method track), abort process.318}319320return true;321}322323void AnimationPlayer::_blend_capture(double p_delta) {324blend_capture(p_delta * Math::abs(speed_scale));325}326327void AnimationPlayer::_blend_post_process() {328if (end_reached) {329// If the method track changes current animation, the animation is not finished.330if (tmp_from == playback.current.from->animation->get_instance_id()) {331if (playback_queue.size()) {332String old = playback.assigned;333play(playback_queue.front()->get());334String new_name = playback.assigned;335playback_queue.pop_front();336if (end_notify) {337emit_signal(SceneStringName(animation_changed), old, new_name);338}339} else {340_clear_caches();341playing = false;342_set_process(false);343if (end_notify) {344emit_signal(SceneStringName(animation_finished), playback.assigned);345emit_signal(SNAME("current_animation_changed"), "");346if (movie_quit_on_finish && OS::get_singleton()->has_feature("movie")) {347print_line(vformat("Movie Maker mode is enabled. Quitting on animation finish as requested by: %s", get_path()));348get_tree()->quit();349}350}351}352}353end_reached = false;354end_notify = false;355}356tmp_from = ObjectID();357}358359void AnimationPlayer::queue(const StringName &p_name) {360if (!is_playing()) {361play(p_name);362} else {363playback_queue.push_back(p_name);364}365}366367Vector<String> AnimationPlayer::get_queue() {368Vector<String> ret;369for (const StringName &E : playback_queue) {370ret.push_back(E);371}372373return ret;374}375376void AnimationPlayer::clear_queue() {377playback_queue.clear();378}379380void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_blend) {381play(p_name, p_custom_blend, -1, true);382}383384void AnimationPlayer::play_section_with_markers_backwards(const StringName &p_name, const StringName &p_start_marker, const StringName &p_end_marker, double p_custom_blend) {385play_section_with_markers(p_name, p_start_marker, p_end_marker, p_custom_blend, -1, true);386}387388void AnimationPlayer::play_section_backwards(const StringName &p_name, double p_start_time, double p_end_time, double p_custom_blend) {389play_section(p_name, p_start_time, p_end_time, p_custom_blend, -1, true);390}391392void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {393if (auto_capture) {394play_with_capture(p_name, auto_capture_duration, p_custom_blend, p_custom_scale, p_from_end, auto_capture_transition_type, auto_capture_ease_type);395} else {396_play(p_name, p_custom_blend, p_custom_scale, p_from_end);397}398}399400void AnimationPlayer::_play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {401play_section_with_markers(p_name, StringName(), StringName(), p_custom_blend, p_custom_scale, p_from_end);402}403404void AnimationPlayer::play_section_with_markers(const StringName &p_name, const StringName &p_start_marker, const StringName &p_end_marker, double p_custom_blend, float p_custom_scale, bool p_from_end) {405StringName name = p_name;406407if (name == StringName()) {408name = playback.assigned;409}410411ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));412413Ref<Animation> animation = animation_set[name].animation;414415ERR_FAIL_COND_MSG(p_start_marker == p_end_marker && p_start_marker, vformat("Start marker and end marker cannot be the same marker: %s.", p_start_marker));416ERR_FAIL_COND_MSG(p_start_marker && !animation->has_marker(p_start_marker), vformat("Marker %s not found in animation: %s.", p_start_marker, name));417ERR_FAIL_COND_MSG(p_end_marker && !animation->has_marker(p_end_marker), vformat("Marker %s not found in animation: %s.", p_end_marker, name));418419double start_time = p_start_marker ? animation->get_marker_time(p_start_marker) : -1;420double end_time = p_end_marker ? animation->get_marker_time(p_end_marker) : -1;421422ERR_FAIL_COND_MSG(p_start_marker && p_end_marker && Animation::is_greater_approx(start_time, end_time), vformat("End marker %s is placed earlier than start marker %s in animation: %s.", p_end_marker, p_start_marker, name));423424if (p_start_marker && Animation::is_less_approx(start_time, 0)) {425WARN_PRINT_ED(vformat("Negative time start marker: %s is invalid in the section, so the start of the animation: %s is used instead.", p_start_marker, playback.current.from->animation->get_name()));426}427if (p_end_marker && Animation::is_less_approx(end_time, 0)) {428WARN_PRINT_ED(vformat("Negative time end marker: %s is invalid in the section, so the end of the animation: %s is used instead.", p_end_marker, playback.current.from->animation->get_name()));429}430431play_section(name, start_time, end_time, p_custom_blend, p_custom_scale, p_from_end);432}433434void AnimationPlayer::play_section(const StringName &p_name, double p_start_time, double p_end_time, double p_custom_blend, float p_custom_scale, bool p_from_end) {435StringName name = p_name;436437if (name == StringName()) {438name = playback.assigned;439}440441ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));442ERR_FAIL_COND_MSG(p_start_time >= 0 && p_end_time >= 0 && Math::is_equal_approx(p_start_time, p_end_time), "Start time and end time must not equal to each other.");443ERR_FAIL_COND_MSG(p_start_time >= 0 && p_end_time >= 0 && Animation::is_greater_approx(p_start_time, p_end_time), vformat("Start time %f is greater than end time %f.", p_start_time, p_end_time));444445Playback &c = playback;446447if (c.current.from) {448double blend_time = 0.0;449// Find if it can blend.450BlendKey bk;451bk.from = c.current.from->name;452bk.to = name;453454if (Animation::is_greater_or_equal_approx(p_custom_blend, 0)) {455blend_time = p_custom_blend;456} else if (blend_times.has(bk)) {457blend_time = blend_times[bk];458} else {459bk.from = "*";460if (blend_times.has(bk)) {461blend_time = blend_times[bk];462} else {463bk.from = c.current.from->name;464bk.to = "*";465466if (blend_times.has(bk)) {467blend_time = blend_times[bk];468}469}470}471472if (Animation::is_less_approx(p_custom_blend, 0) && Math::is_zero_approx(blend_time) && default_blend_time) {473blend_time = default_blend_time;474}475if (Animation::is_greater_approx(blend_time, 0)) {476Blend b;477b.data = c.current;478b.blend_left = get_current_blend_amount();479b.blend_time = blend_time;480c.blend.push_back(b);481} else {482c.blend.clear();483}484}485486if (get_current_animation() != p_name) {487_clear_playing_caches();488}489490c.current.from = &animation_set[name];491c.current.speed_scale = p_custom_scale;492c.current.start_time = p_start_time;493c.current.end_time = p_end_time;494495double start = playback.current.get_start_time();496double end = playback.current.get_end_time();497498if (!end_reached) {499playback_queue.clear();500}501502if (c.assigned != name) { // Reset.503c.current.pos = p_from_end ? end : start;504c.assigned = name;505emit_signal(SNAME("current_animation_changed"), c.assigned);506} else {507if (p_from_end && Math::is_equal_approx(c.current.pos, start)) {508// Animation reset but played backwards, set position to the end.509seek_internal(end, true, true, true);510} else if (!p_from_end && Math::is_equal_approx(c.current.pos, end)) {511// Animation resumed but already ended, set position to the beginning.512seek_internal(start, true, true, true);513} else if (playing) {514return;515}516}517518c.seeked = false;519c.started = true;520521_set_process(true); // Always process when starting an animation.522playing = true;523524emit_signal(SceneStringName(animation_started), c.assigned);525526if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {527return; // No next in this case.528}529530StringName next = animation_get_next(p_name);531if (next != StringName() && animation_set.has(next)) {532queue(next);533}534}535536void AnimationPlayer::_capture(const StringName &p_name, bool p_from_end, double p_duration, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {537StringName name = p_name;538if (name == StringName()) {539name = playback.assigned;540}541542Ref<Animation> anim = get_animation(name);543if (anim.is_null() || !anim->is_capture_included()) {544return;545}546if (std::signbit(p_duration)) {547double max_dur = 0;548double current_pos = playback.current.pos;549if (playback.assigned != name) {550current_pos = p_from_end ? anim->get_length() : 0;551}552for (int i = 0; i < anim->get_track_count(); i++) {553if (anim->track_get_type(i) != Animation::TYPE_VALUE) {554continue;555}556if (anim->value_track_get_update_mode(i) != Animation::UPDATE_CAPTURE) {557continue;558}559if (anim->track_get_key_count(i) == 0) {560continue;561}562max_dur = MAX(max_dur, p_from_end ? current_pos - anim->track_get_key_time(i, anim->track_get_key_count(i) - 1) : anim->track_get_key_time(i, 0) - current_pos);563}564p_duration = max_dur;565}566if (Math::is_zero_approx(p_duration)) {567return;568}569capture(name, p_duration, p_trans_type, p_ease_type);570}571572void AnimationPlayer::play_with_capture(const StringName &p_name, double p_duration, double p_custom_blend, float p_custom_scale, bool p_from_end, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {573_capture(p_name, p_from_end, p_duration, p_trans_type, p_ease_type);574_play(p_name, p_custom_blend, p_custom_scale, p_from_end);575}576577bool AnimationPlayer::is_playing() const {578return playing;579}580581void AnimationPlayer::set_current_animation(const String &p_animation) {582if (p_animation == "[stop]" || p_animation.is_empty()) {583stop();584} else if (!is_playing()) {585play(p_animation);586} else if (playback.assigned != p_animation) {587float speed = playback.current.speed_scale;588play(p_animation, -1.0, speed, std::signbit(speed));589} else {590// Same animation, do not replay from start.591}592}593594String AnimationPlayer::get_current_animation() const {595return (is_playing() ? playback.assigned : "");596}597598void AnimationPlayer::set_assigned_animation(const String &p_animation) {599if (is_playing()) {600float speed = playback.current.speed_scale;601play(p_animation, -1.0, speed, std::signbit(speed));602} else {603ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));604playback.current.pos = 0;605playback.current.from = &animation_set[p_animation];606playback.current.start_time = -1;607playback.current.end_time = -1;608playback.assigned = p_animation;609emit_signal(SNAME("current_animation_changed"), playback.assigned);610}611}612613String AnimationPlayer::get_assigned_animation() const {614return playback.assigned;615}616617void AnimationPlayer::pause() {618_stop_internal(false, false);619}620621void AnimationPlayer::stop(bool p_keep_state) {622_stop_internal(true, p_keep_state);623}624625void AnimationPlayer::set_speed_scale(float p_speed) {626speed_scale = p_speed;627}628629float AnimationPlayer::get_speed_scale() const {630return speed_scale;631}632633float AnimationPlayer::get_playing_speed() const {634if (!playing) {635return 0;636}637return speed_scale * playback.current.speed_scale;638}639640void AnimationPlayer::seek_internal(double p_time, bool p_update, bool p_update_only, bool p_is_internal_seek) {641if (!active) {642return;643}644645bool is_backward = Animation::is_less_approx(p_time, playback.current.pos);646647_check_immediately_after_start();648649playback.current.pos = p_time;650if (!playback.current.from) {651if (playback.assigned) {652ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));653playback.current.from = &animation_set[playback.assigned];654}655if (!playback.current.from) {656return; // There is no animation.657}658}659660double start = playback.current.get_start_time();661double end = playback.current.get_end_time();662663// Clamp the seek position.664p_time = CLAMP(p_time, start, end);665666playback.seeked = true;667playback.internal_seeked = p_is_internal_seek;668669if (p_update) {670_process_animation(is_backward ? -0.0 : 0.0, p_update_only);671playback.seeked = false; // If animation was proceeded here, no more seek in internal process.672}673}674675void AnimationPlayer::seek(double p_time, bool p_update, bool p_update_only) {676seek_internal(p_time, p_update, p_update_only);677}678679void AnimationPlayer::advance(double p_time) {680_check_immediately_after_start();681AnimationMixer::advance(p_time);682}683684void AnimationPlayer::_check_immediately_after_start() {685if (playback.started) {686_process_animation(0); // Force process current key for Discrete/Method/Audio/AnimationPlayback. Then, started flag is cleared.687}688}689690bool AnimationPlayer::is_valid() const {691return (playback.current.from);692}693694double AnimationPlayer::get_current_animation_position() const {695ERR_FAIL_NULL_V_MSG(playback.current.from, 0, "AnimationPlayer has no current animation.");696return playback.current.pos;697}698699double AnimationPlayer::get_current_animation_length() const {700ERR_FAIL_NULL_V_MSG(playback.current.from, 0, "AnimationPlayer has no current animation.");701return playback.current.from->animation->get_length();702}703704void AnimationPlayer::set_section_with_markers(const StringName &p_start_marker, const StringName &p_end_marker) {705ERR_FAIL_NULL_MSG(playback.current.from, "AnimationPlayer has no current animation.");706ERR_FAIL_COND_MSG(p_start_marker == p_end_marker && p_start_marker, vformat("Start marker and end marker cannot be the same marker: %s.", p_start_marker));707ERR_FAIL_COND_MSG(p_start_marker && !playback.current.from->animation->has_marker(p_start_marker), vformat("Marker %s not found in animation: %s.", p_start_marker, playback.current.from->animation->get_name()));708ERR_FAIL_COND_MSG(p_end_marker && !playback.current.from->animation->has_marker(p_end_marker), vformat("Marker %s not found in animation: %s.", p_end_marker, playback.current.from->animation->get_name()));709double start_time = p_start_marker ? playback.current.from->animation->get_marker_time(p_start_marker) : -1;710double end_time = p_end_marker ? playback.current.from->animation->get_marker_time(p_end_marker) : -1;711if (p_start_marker && Animation::is_less_approx(start_time, 0)) {712WARN_PRINT_ONCE_ED(vformat("Marker %s time must be positive in animation: %s.", p_start_marker, playback.current.from->animation->get_name()));713}714if (p_end_marker && Animation::is_less_approx(end_time, 0)) {715WARN_PRINT_ONCE_ED(vformat("Marker %s time must be positive in animation: %s.", p_end_marker, playback.current.from->animation->get_name()));716}717set_section(start_time, end_time);718}719720void AnimationPlayer::set_section(double p_start_time, double p_end_time) {721ERR_FAIL_NULL_MSG(playback.current.from, "AnimationPlayer has no current animation.");722ERR_FAIL_COND_MSG(Animation::is_greater_or_equal_approx(p_start_time, 0) && Animation::is_greater_or_equal_approx(p_end_time, 0) && Animation::is_greater_or_equal_approx(p_start_time, p_end_time), vformat("Start time %f is greater than end time %f.", p_start_time, p_end_time));723playback.current.start_time = p_start_time;724playback.current.end_time = p_end_time;725playback.current.pos = CLAMP(playback.current.pos, playback.current.get_start_time(), playback.current.get_end_time());726}727728void AnimationPlayer::reset_section() {729playback.current.start_time = -1;730playback.current.end_time = -1;731}732733double AnimationPlayer::get_section_start_time() const {734ERR_FAIL_NULL_V_MSG(playback.current.from, playback.current.start_time, "AnimationPlayer has no current animation.");735return playback.current.get_start_time();736}737738double AnimationPlayer::get_section_end_time() const {739ERR_FAIL_NULL_V_MSG(playback.current.from, playback.current.end_time, "AnimationPlayer has no current animation.");740return playback.current.get_end_time();741}742743bool AnimationPlayer::has_section() const {744return Animation::is_greater_or_equal_approx(playback.current.start_time, 0) || Animation::is_greater_or_equal_approx(playback.current.end_time, 0);745}746747void AnimationPlayer::set_autoplay(const String &p_name) {748if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {749WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");750}751752autoplay = p_name;753}754755String AnimationPlayer::get_autoplay() const {756return autoplay;757}758759void AnimationPlayer::set_movie_quit_on_finish_enabled(bool p_enabled) {760movie_quit_on_finish = p_enabled;761}762763bool AnimationPlayer::is_movie_quit_on_finish_enabled() const {764return movie_quit_on_finish;765}766767void AnimationPlayer::_stop_internal(bool p_reset, bool p_keep_state) {768_clear_caches();769Playback &c = playback;770// c.blend.clear();771double start = c.current.from ? playback.current.get_start_time() : 0;772if (p_reset) {773c.blend.clear();774if (p_keep_state) {775c.current.pos = start;776} else {777is_stopping = true;778seek_internal(start, true, true, true);779is_stopping = false;780}781c.current.from = nullptr;782c.current.speed_scale = 1;783emit_signal(SNAME("current_animation_changed"), "");784}785_set_process(false);786playback_queue.clear();787playing = false;788}789790void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {791ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));792animation_next_set[p_animation] = p_next;793}794795StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {796if (!animation_next_set.has(p_animation)) {797return StringName();798}799return animation_next_set[p_animation];800}801802void AnimationPlayer::set_default_blend_time(double p_default) {803default_blend_time = p_default;804}805806double AnimationPlayer::get_default_blend_time() const {807return default_blend_time;808}809810void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time) {811ERR_FAIL_COND_MSG(!animation_set.has(p_animation1), vformat("Animation not found: %s.", p_animation1));812ERR_FAIL_COND_MSG(!animation_set.has(p_animation2), vformat("Animation not found: %s.", p_animation2));813ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");814815BlendKey bk;816bk.from = p_animation1;817bk.to = p_animation2;818if (Math::is_zero_approx(p_time)) {819blend_times.erase(bk);820} else {821blend_times[bk] = p_time;822}823}824825double AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {826BlendKey bk;827bk.from = p_animation1;828bk.to = p_animation2;829830if (blend_times.has(bk)) {831return blend_times[bk];832} else {833return 0;834}835}836837void AnimationPlayer::set_auto_capture(bool p_auto_capture) {838auto_capture = p_auto_capture;839notify_property_list_changed();840}841842bool AnimationPlayer::is_auto_capture() const {843return auto_capture;844}845846void AnimationPlayer::set_auto_capture_duration(double p_auto_capture_duration) {847auto_capture_duration = p_auto_capture_duration;848}849850double AnimationPlayer::get_auto_capture_duration() const {851return auto_capture_duration;852}853854void AnimationPlayer::set_auto_capture_transition_type(Tween::TransitionType p_auto_capture_transition_type) {855auto_capture_transition_type = p_auto_capture_transition_type;856}857858Tween::TransitionType AnimationPlayer::get_auto_capture_transition_type() const {859return auto_capture_transition_type;860}861862void AnimationPlayer::set_auto_capture_ease_type(Tween::EaseType p_auto_capture_ease_type) {863auto_capture_ease_type = p_auto_capture_ease_type;864}865866Tween::EaseType AnimationPlayer::get_auto_capture_ease_type() const {867return auto_capture_ease_type;868}869870#ifdef TOOLS_ENABLED871void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {872const String pf = p_function;873if (p_idx == 0 && (pf == "play" || pf == "play_backwards" || pf == "has_animation" || pf == "queue")) {874List<StringName> al;875get_animation_list(&al);876for (const StringName &name : al) {877r_options->push_back(String(name).quote());878}879}880AnimationMixer::get_argument_options(p_function, p_idx, r_options);881}882#endif883884void AnimationPlayer::_animation_removed(const StringName &p_name, const StringName &p_library) {885AnimationMixer::_animation_removed(p_name, p_library);886887StringName name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));888889if (!animation_set.has(name)) {890return; // No need to update because not the one from the library being used.891}892893_animation_set_cache_update();894895// Erase blends if needed896List<BlendKey> to_erase;897for (const KeyValue<BlendKey, double> &E : blend_times) {898BlendKey bk = E.key;899if (bk.from == name || bk.to == name) {900to_erase.push_back(bk);901}902}903904while (to_erase.size()) {905blend_times.erase(to_erase.front()->get());906to_erase.pop_front();907}908}909910void AnimationPlayer::_rename_animation(const StringName &p_from_name, const StringName &p_to_name) {911AnimationMixer::_rename_animation(p_from_name, p_to_name);912913// Rename autoplay or blends if needed.914List<BlendKey> to_erase;915HashMap<BlendKey, double, BlendKey> to_insert;916for (const KeyValue<BlendKey, double> &E : blend_times) {917BlendKey bk = E.key;918BlendKey new_bk = bk;919bool erase = false;920if (bk.from == p_from_name) {921new_bk.from = p_to_name;922erase = true;923}924if (bk.to == p_from_name) {925new_bk.to = p_to_name;926erase = true;927}928929if (erase) {930to_erase.push_back(bk);931to_insert[new_bk] = E.value;932}933}934935while (to_erase.size()) {936blend_times.erase(to_erase.front()->get());937to_erase.pop_front();938}939940while (to_insert.size()) {941blend_times[to_insert.begin()->key] = to_insert.begin()->value;942to_insert.remove(to_insert.begin());943}944945if (autoplay == p_from_name) {946autoplay = p_to_name;947}948}949950void AnimationPlayer::_bind_methods() {951ClassDB::bind_method(D_METHOD("animation_set_next", "animation_from", "animation_to"), &AnimationPlayer::animation_set_next);952ClassDB::bind_method(D_METHOD("animation_get_next", "animation_from"), &AnimationPlayer::animation_get_next);953954ClassDB::bind_method(D_METHOD("set_blend_time", "animation_from", "animation_to", "sec"), &AnimationPlayer::set_blend_time);955ClassDB::bind_method(D_METHOD("get_blend_time", "animation_from", "animation_to"), &AnimationPlayer::get_blend_time);956957ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);958ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);959960ClassDB::bind_method(D_METHOD("set_auto_capture", "auto_capture"), &AnimationPlayer::set_auto_capture);961ClassDB::bind_method(D_METHOD("is_auto_capture"), &AnimationPlayer::is_auto_capture);962ClassDB::bind_method(D_METHOD("set_auto_capture_duration", "auto_capture_duration"), &AnimationPlayer::set_auto_capture_duration);963ClassDB::bind_method(D_METHOD("get_auto_capture_duration"), &AnimationPlayer::get_auto_capture_duration);964ClassDB::bind_method(D_METHOD("set_auto_capture_transition_type", "auto_capture_transition_type"), &AnimationPlayer::set_auto_capture_transition_type);965ClassDB::bind_method(D_METHOD("get_auto_capture_transition_type"), &AnimationPlayer::get_auto_capture_transition_type);966ClassDB::bind_method(D_METHOD("set_auto_capture_ease_type", "auto_capture_ease_type"), &AnimationPlayer::set_auto_capture_ease_type);967ClassDB::bind_method(D_METHOD("get_auto_capture_ease_type"), &AnimationPlayer::get_auto_capture_ease_type);968969ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));970ClassDB::bind_method(D_METHOD("play_section_with_markers", "name", "start_marker", "end_marker", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play_section_with_markers, DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));971ClassDB::bind_method(D_METHOD("play_section", "name", "start_time", "end_time", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play_section, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));972ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(StringName()), DEFVAL(-1));973ClassDB::bind_method(D_METHOD("play_section_with_markers_backwards", "name", "start_marker", "end_marker", "custom_blend"), &AnimationPlayer::play_section_with_markers_backwards, DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(-1));974ClassDB::bind_method(D_METHOD("play_section_backwards", "name", "start_time", "end_time", "custom_blend"), &AnimationPlayer::play_section_backwards, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1));975ClassDB::bind_method(D_METHOD("play_with_capture", "name", "duration", "custom_blend", "custom_speed", "from_end", "trans_type", "ease_type"), &AnimationPlayer::play_with_capture, DEFVAL(StringName()), DEFVAL(-1.0), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false), DEFVAL(Tween::TRANS_LINEAR), DEFVAL(Tween::EASE_IN));976ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);977ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));978ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);979980ClassDB::bind_method(D_METHOD("set_current_animation", "animation"), &AnimationPlayer::set_current_animation);981ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);982ClassDB::bind_method(D_METHOD("set_assigned_animation", "animation"), &AnimationPlayer::set_assigned_animation);983ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);984ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);985ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);986ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);987988ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);989ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);990ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);991992ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);993ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);994995ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);996ClassDB::bind_method(D_METHOD("find_animation_library", "animation"), &AnimationPlayer::find_animation_library);997998ClassDB::bind_method(D_METHOD("set_movie_quit_on_finish_enabled", "enabled"), &AnimationPlayer::set_movie_quit_on_finish_enabled);999ClassDB::bind_method(D_METHOD("is_movie_quit_on_finish_enabled"), &AnimationPlayer::is_movie_quit_on_finish_enabled);10001001ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);1002ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);10031004ClassDB::bind_method(D_METHOD("set_section_with_markers", "start_marker", "end_marker"), &AnimationPlayer::set_section_with_markers, DEFVAL(StringName()), DEFVAL(StringName()));1005ClassDB::bind_method(D_METHOD("set_section", "start_time", "end_time"), &AnimationPlayer::set_section, DEFVAL(-1), DEFVAL(-1));1006ClassDB::bind_method(D_METHOD("reset_section"), &AnimationPlayer::reset_section);10071008ClassDB::bind_method(D_METHOD("get_section_start_time"), &AnimationPlayer::get_section_start_time);1009ClassDB::bind_method(D_METHOD("get_section_end_time"), &AnimationPlayer::get_section_end_time);1010ClassDB::bind_method(D_METHOD("has_section"), &AnimationPlayer::has_section);10111012ClassDB::bind_method(D_METHOD("seek", "seconds", "update", "update_only"), &AnimationPlayer::seek, DEFVAL(false), DEFVAL(false));10131014ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR), "set_current_animation", "get_current_animation");1015ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");1016ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");1017ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");1018ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");10191020ADD_GROUP("Playback Options", "playback_");1021ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_auto_capture"), "set_auto_capture", "is_auto_capture");1022ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_auto_capture_duration", PROPERTY_HINT_NONE, "suffix:s"), "set_auto_capture_duration", "get_auto_capture_duration");1023ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_auto_capture_transition_type", PROPERTY_HINT_ENUM, "Linear,Sine,Quint,Quart,Quad,Expo,Elastic,Cubic,Circ,Bounce,Back,Spring"), "set_auto_capture_transition_type", "get_auto_capture_transition_type");1024ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_auto_capture_ease_type", PROPERTY_HINT_ENUM, "In,Out,InOut,OutIn"), "set_auto_capture_ease_type", "get_auto_capture_ease_type");1025ADD_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");10261027ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-4,4,0.001,or_less,or_greater"), "set_speed_scale", "get_speed_scale");1028ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");10291030ADD_SIGNAL(MethodInfo(SNAME("current_animation_changed"), PropertyInfo(Variant::STRING, "name")));1031ADD_SIGNAL(MethodInfo(SNAME("animation_changed"), PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));1032}10331034AnimationPlayer::AnimationPlayer() {1035}10361037AnimationPlayer::~AnimationPlayer() {1038}103910401041