From 513e89b5112840be7ed83fc9413e8186fae81a6f Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 25 Mar 2020 18:41:26 +0100 Subject: [PATCH] Brought over all the code from godot's AnimationPlayer into ProceduralAnimationPlayer, and did the first set of rework to both it, and the ProceduralAnimation itself, so the apis match. Doesn't work for now, but it compiles. --- procedural_animation.cpp | 657 +++++++++++++ procedural_animation.h | 44 + procedural_animation_player.cpp | 1621 +++++++++++++++++++++++++++++++ procedural_animation_player.h | 311 +++++- 4 files changed, 2632 insertions(+), 1 deletion(-) diff --git a/procedural_animation.cpp b/procedural_animation.cpp index 81dc314..d3622ec 100644 --- a/procedural_animation.cpp +++ b/procedural_animation.cpp @@ -81,6 +81,22 @@ void ProceduralAnimation::set_start_frame_index(const int value) { _start_frame_index = value; } +float ProceduralAnimation::get_length() const { + return _length; +} +void ProceduralAnimation::set_length(float p_length) { + _length = p_length; + emit_changed(); +} + +bool ProceduralAnimation::has_loop() const { + return _loop; +} +void ProceduralAnimation::set_loop(bool p_enabled) { + _loop = p_enabled; + emit_changed(); +} + //Keyframes PoolVector ProceduralAnimation::get_keyframe_indices() const { PoolVector idxr; @@ -272,10 +288,643 @@ void ProceduralAnimation::load_keyframe_data(int keyframe_index) { } } +float ProceduralAnimation::track_get_key_time(int p_track, int p_key_idx) const { + //add time to all keyframes + //sum them up, store, and return that + + return 0; +} + +float ProceduralAnimation::track_get_key_transition(int p_track, int p_key_idx) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), -1); + Track *t = tracks[p_track]; + + switch (t->type) { + + case TYPE_TRANSFORM: { + + TransformTrack *tt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1); + return tt->transforms[p_key_idx].transition; + } break; + case TYPE_VALUE: { + + ValueTrack *vt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1); + return vt->values[p_key_idx].transition; + + } break; + case TYPE_METHOD: { + + MethodTrack *mt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1); + return mt->methods[p_key_idx].transition; + + } break; + case TYPE_BEZIER: { + + return 1; //bezier does not really use transitions + } break; + case TYPE_AUDIO: { + + return 1; //audio does not really use transitions + } break; + case TYPE_ANIMATION: { + + return 1; //animation does not really use transitions + } break; + } + + ERR_FAIL_V(0); + */ + + //sotre, return + + return 1; +} + +Variant ProceduralAnimation::track_get_key_value(int p_track, int p_key_idx) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), Variant()); + Track *t = tracks[p_track]; + + switch (t->type) { + + case TYPE_TRANSFORM: { + + TransformTrack *tt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), Variant()); + + Dictionary d; + d["location"] = tt->transforms[p_key_idx].value.loc; + d["rotation"] = tt->transforms[p_key_idx].value.rot; + d["scale"] = tt->transforms[p_key_idx].value.scale; + + return d; + } break; + case TYPE_VALUE: { + + ValueTrack *vt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), Variant()); + return vt->values[p_key_idx].value; + + } break; + case TYPE_METHOD: { + + MethodTrack *mt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), Variant()); + Dictionary d; + d["method"] = mt->methods[p_key_idx].method; + d["args"] = mt->methods[p_key_idx].params; + return d; + + } break; + case TYPE_BEZIER: { + + BezierTrack *bt = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), Variant()); + + Array arr; + arr.resize(5); + arr[0] = bt->values[p_key_idx].value.value; + arr[1] = bt->values[p_key_idx].value.in_handle.x; + arr[2] = bt->values[p_key_idx].value.in_handle.y; + arr[3] = bt->values[p_key_idx].value.out_handle.x; + arr[4] = bt->values[p_key_idx].value.out_handle.y; + return arr; + + } break; + case TYPE_AUDIO: { + + AudioTrack *at = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant()); + + Dictionary k; + k["start_offset"] = at->values[p_key_idx].value.start_offset; + k["end_offset"] = at->values[p_key_idx].value.end_offset; + k["stream"] = at->values[p_key_idx].value.stream; + return k; + + } break; + case TYPE_ANIMATION: { + + AnimationTrack *at = static_cast(t); + ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant()); + + return at->values[p_key_idx].value; + + } break; + } + + ERR_FAIL_V(Variant()); + */ + + return Variant(); +} + +void ProceduralAnimation::get_key_indices(int p_track, float p_time, float p_delta, List *p_indices) const { + /* + ERR_FAIL_INDEX(p_track, tracks.size()); + Track *t = tracks[p_track]; + ERR_FAIL_COND(t->type != TYPE_VALUE); + + ValueTrack *vt = static_cast(t); + + float from_time = p_time - p_delta; + float to_time = p_time; + + if (from_time > to_time) + SWAP(from_time, to_time); + + if (loop) { + + from_time = Math::fposmod(from_time, length); + to_time = Math::fposmod(to_time, length); + + if (from_time > to_time) { + // handle loop by splitting + _value_track_get_key_indices_in_range(vt, from_time, length, p_indices); + _value_track_get_key_indices_in_range(vt, 0, to_time, p_indices); + return; + } + } else { + + if (from_time < 0) + from_time = 0; + if (from_time > length) + from_time = length; + + if (to_time < 0) + to_time = 0; + if (to_time > length) + to_time = length; + } + + _value_track_get_key_indices_in_range(vt, from_time, to_time, p_indices); + */ +} + +void ProceduralAnimation::track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List *p_indices) const { + /* + ERR_FAIL_INDEX(p_track, tracks.size()); + const Track *t = tracks[p_track]; + + float from_time = p_time - p_delta; + float to_time = p_time; + + if (from_time > to_time) + SWAP(from_time, to_time); + + if (loop) { + + if (from_time > length || from_time < 0) + from_time = Math::fposmod(from_time, length); + + if (to_time > length || to_time < 0) + to_time = Math::fposmod(to_time, length); + + if (from_time > to_time) { + // handle loop by splitting + + switch (t->type) { + + case TYPE_TRANSFORM: { + + const TransformTrack *tt = static_cast(t); + _track_get_key_indices_in_range(tt->transforms, from_time, length, p_indices); + _track_get_key_indices_in_range(tt->transforms, 0, to_time, p_indices); + + } break; + case TYPE_VALUE: { + + const ValueTrack *vt = static_cast(t); + _track_get_key_indices_in_range(vt->values, from_time, length, p_indices); + _track_get_key_indices_in_range(vt->values, 0, to_time, p_indices); + + } break; + case TYPE_METHOD: { + + const MethodTrack *mt = static_cast(t); + _track_get_key_indices_in_range(mt->methods, from_time, length, p_indices); + _track_get_key_indices_in_range(mt->methods, 0, to_time, p_indices); + + } break; + case TYPE_BEZIER: { + + const BezierTrack *bz = static_cast(t); + _track_get_key_indices_in_range(bz->values, from_time, length, p_indices); + _track_get_key_indices_in_range(bz->values, 0, to_time, p_indices); + + } break; + case TYPE_AUDIO: { + + const AudioTrack *ad = static_cast(t); + _track_get_key_indices_in_range(ad->values, from_time, length, p_indices); + _track_get_key_indices_in_range(ad->values, 0, to_time, p_indices); + + } break; + case TYPE_ANIMATION: { + + const AnimationTrack *an = static_cast(t); + _track_get_key_indices_in_range(an->values, from_time, length, p_indices); + _track_get_key_indices_in_range(an->values, 0, to_time, p_indices); + + } break; + } + return; + } + } else { + + if (from_time < 0) + from_time = 0; + if (from_time > length) + from_time = length; + + if (to_time < 0) + to_time = 0; + if (to_time > length) + to_time = length; + } + + switch (t->type) { + + case TYPE_TRANSFORM: { + + const TransformTrack *tt = static_cast(t); + _track_get_key_indices_in_range(tt->transforms, from_time, to_time, p_indices); + + } break; + case TYPE_VALUE: { + + const ValueTrack *vt = static_cast(t); + _track_get_key_indices_in_range(vt->values, from_time, to_time, p_indices); + + } break; + case TYPE_METHOD: { + + const MethodTrack *mt = static_cast(t); + _track_get_key_indices_in_range(mt->methods, from_time, to_time, p_indices); + + } break; + case TYPE_BEZIER: { + + const BezierTrack *bz = static_cast(t); + _track_get_key_indices_in_range(bz->values, from_time, to_time, p_indices); + + } break; + case TYPE_AUDIO: { + + const AudioTrack *ad = static_cast(t); + _track_get_key_indices_in_range(ad->values, from_time, to_time, p_indices); + + } break; + case TYPE_ANIMATION: { + + const AnimationTrack *an = static_cast(t); + _track_get_key_indices_in_range(an->values, from_time, to_time, p_indices); + + } break; + } + */ +} + +int ProceduralAnimation::track_find_key(int p_track, float p_time, bool p_exact) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), -1); + Track *t = tracks[p_track]; + + switch (t->type) { + case TYPE_TRANSFORM: { + + TransformTrack *tt = static_cast(t); + int k = _find(tt->transforms, p_time); + if (k < 0 || k >= tt->transforms.size()) + return -1; + if (tt->transforms[k].time != p_time && p_exact) + return -1; + return k; + + } break; + case TYPE_VALUE: { + + ValueTrack *vt = static_cast(t); + int k = _find(vt->values, p_time); + if (k < 0 || k >= vt->values.size()) + return -1; + if (vt->values[k].time != p_time && p_exact) + return -1; + return k; + + } break; + case TYPE_METHOD: { + + MethodTrack *mt = static_cast(t); + int k = _find(mt->methods, p_time); + if (k < 0 || k >= mt->methods.size()) + return -1; + if (mt->methods[k].time != p_time && p_exact) + return -1; + return k; + + } break; + case TYPE_BEZIER: { + + BezierTrack *bt = static_cast(t); + int k = _find(bt->values, p_time); + if (k < 0 || k >= bt->values.size()) + return -1; + if (bt->values[k].time != p_time && p_exact) + return -1; + return k; + + } break; + case TYPE_AUDIO: { + + AudioTrack *at = static_cast(t); + int k = _find(at->values, p_time); + if (k < 0 || k >= at->values.size()) + return -1; + if (at->values[k].time != p_time && p_exact) + return -1; + return k; + + } break; + case TYPE_ANIMATION: { + + AnimationTrack *at = static_cast(t); + int k = _find(at->values, p_time); + if (k < 0 || k >= at->values.size()) + return -1; + if (at->values[k].time != p_time && p_exact) + return -1; + return k; + + } break; + } + + return -1;*/ + + return -1; +} + +Vector ProceduralAnimation::method_track_get_params(int p_track, int p_key_idx) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector()); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector()); + + MethodTrack *pm = static_cast(t); + + ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), Vector()); + + const MethodKey &mk = pm->methods[p_key_idx]; + + return mk.params;*/ + + return Vector(); +} +StringName ProceduralAnimation::method_track_get_name(int p_track, int p_key_idx) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName()); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName()); + + MethodTrack *pm = static_cast(t); + + ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), StringName()); + + return pm->methods[p_key_idx].method; + */ + + return ""; +} + +RES ProceduralAnimation::audio_track_get_key_stream(int p_track, int p_key) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), RES()); + const Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_AUDIO, RES()); + + const AudioTrack *at = static_cast(t); + + ERR_FAIL_INDEX_V(p_key, at->values.size(), RES()); + + return at->values[p_key].value.stream; + */ + + return RES(); +} +float ProceduralAnimation::audio_track_get_key_start_offset(int p_track, int p_key) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), 0); + const Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0); + + const AudioTrack *at = static_cast(t); + + ERR_FAIL_INDEX_V(p_key, at->values.size(), 0); + + return at->values[p_key].value.start_offset; + */ + return 0; +} +float ProceduralAnimation::audio_track_get_key_end_offset(int p_track, int p_key) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), 0); + const Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0); + + const AudioTrack *at = static_cast(t); + + ERR_FAIL_INDEX_V(p_key, at->values.size(), 0); + + return at->values[p_key].value.end_offset; + */ + return 0; +} + +StringName ProceduralAnimation::animation_track_get_key_animation(int p_track, int p_key) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName()); + const Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, StringName()); + + const AnimationTrack *at = static_cast(t); + + ERR_FAIL_INDEX_V(p_key, at->values.size(), StringName()); + + return at->values[p_key].value; + */ + + return ""; +} + +Error ProceduralAnimation::transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER); + + TransformTrack *tt = static_cast(t); + + bool ok = false; + + TransformKey tk = _interpolate(tt->transforms, p_time, tt->interpolation, tt->loop_wrap, &ok); + + if (!ok) + return ERR_UNAVAILABLE; + + if (r_loc) + *r_loc = tk.loc; + + if (r_rot) + *r_rot = tk.rot; + + if (r_scale) + *r_scale = tk.scale; +*/ + return OK; +} + +Variant ProceduralAnimation::value_track_interpolate(int p_track, float p_time) const { + /* + ERR_FAIL_INDEX_V(p_track, tracks.size(), 0); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_VALUE, Variant()); + ValueTrack *vt = static_cast(t); + + bool ok = false; + + Variant res = _interpolate(vt->values, p_time, (vt->update_mode == UPDATE_CONTINUOUS || vt->update_mode == UPDATE_CAPTURE) ? vt->interpolation : INTERPOLATION_NEAREST, vt->loop_wrap, &ok); + + if (ok) { + + return res; + } +*/ + return Variant(); +} + +float ProceduralAnimation::bezier_track_interpolate(int p_track, float p_time) const { + /* + //this uses a different interpolation scheme + ERR_FAIL_INDEX_V(p_track, tracks.size(), 0); + Track *track = tracks[p_track]; + ERR_FAIL_COND_V(track->type != TYPE_BEZIER, 0); + + BezierTrack *bt = static_cast(track); + + int len = _find(bt->values, length) + 1; // try to find last key (there may be more past the end) + + if (len <= 0) { + // (-1 or -2 returned originally) (plus one above) + return 0; + } else if (len == 1) { // one key found (0+1), return it + return bt->values[0].value.value; + } + + int idx = _find(bt->values, p_time); + + ERR_FAIL_COND_V(idx == -2, 0); + + //there really is no looping interpolation on bezier + + if (idx < 0) { + return bt->values[0].value.value; + } + + if (idx >= bt->values.size() - 1) { + return bt->values[bt->values.size() - 1].value.value; + } + + float t = p_time - bt->values[idx].time; + + int iterations = 10; + + float duration = bt->values[idx + 1].time - bt->values[idx].time; // time duration between our two keyframes + float low = 0; // 0% of the current animation segment + float high = 1; // 100% of the current animation segment + float middle; + + Vector2 start(0, bt->values[idx].value.value); + Vector2 start_out = start + bt->values[idx].value.out_handle; + Vector2 end(duration, bt->values[idx + 1].value.value); + Vector2 end_in = end + bt->values[idx + 1].value.in_handle; + + //narrow high and low as much as possible + for (int i = 0; i < iterations; i++) { + + middle = (low + high) / 2; + + Vector2 interp = _bezier_interp(middle, start, start_out, end_in, end); + + if (interp.x < t) { + low = middle; + } else { + high = middle; + } + } + + //interpolate the result: + Vector2 low_pos = _bezier_interp(low, start, start_out, end_in, end); + Vector2 high_pos = _bezier_interp(high, start, start_out, end_in, end); + float c = (t - low_pos.x) / (high_pos.x - low_pos.x); + + return low_pos.linear_interpolate(high_pos, c).y; + */ + + return 0; +} + +int ProceduralAnimation::get_track_count() const { + + //return tracks.size(); + return 0; +} + +Animation::TrackType ProceduralAnimation::track_get_type(int p_track) const { + ERR_FAIL_COND_V(!_animation.is_valid(), Animation::TYPE_TRANSFORM); + + return _animation->track_get_type(p_track); +} + +NodePath ProceduralAnimation::track_get_path(int p_track) const { + ERR_FAIL_COND_V(!_animation.is_valid(), NodePath()); + + return _animation->track_get_path(p_track); +} + +int ProceduralAnimation::find_track(const NodePath &p_path) const { + ERR_FAIL_COND_V(!_animation.is_valid(), -1); + + return _animation->find_track(p_path); +}; + +bool ProceduralAnimation::track_is_enabled(int p_track) const { + ERR_FAIL_COND_V(!_animation.is_valid(), false); + + return _animation->track_is_enabled(p_track); +} + +int ProceduralAnimation::track_get_key_count(int p_track) const { + ERR_FAIL_COND_V(!_animation.is_valid(), -1); + + return _animation->track_get_key_count(p_track); +} + +Animation::UpdateMode ProceduralAnimation::value_track_get_update_mode(int p_track) const { + ERR_FAIL_COND_V(!_animation.is_valid(), Animation::UPDATE_CONTINUOUS); + + return _animation->value_track_get_update_mode(p_track); +} + ProceduralAnimation::ProceduralAnimation() { _initialized = false; _animation_fps = 15; _start_frame_index = -1; + _length = 0; + _loop = 0; } ProceduralAnimation::~ProceduralAnimation() { @@ -770,6 +1419,14 @@ void ProceduralAnimation::_bind_methods() { ClassDB::bind_method(D_METHOD("get_start_frame_index"), &ProceduralAnimation::get_start_frame_index); ClassDB::bind_method(D_METHOD("set_start_frame_index", "value"), &ProceduralAnimation::set_start_frame_index); + ClassDB::bind_method(D_METHOD("get_length"), &ProceduralAnimation::get_length); + ClassDB::bind_method(D_METHOD("set_length", "time_sec"), &ProceduralAnimation::set_length); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001"), "set_length", "get_length"); + + ClassDB::bind_method(D_METHOD("has_loop"), &ProceduralAnimation::has_loop); + ClassDB::bind_method(D_METHOD("set_loop", "enabled"), &ProceduralAnimation::set_loop); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop"); + //Keyframes ClassDB::bind_method(D_METHOD("get_keyframe_indices"), &ProceduralAnimation::get_keyframe_indices); ClassDB::bind_method(D_METHOD("add_keyframe"), &ProceduralAnimation::add_keyframe); diff --git a/procedural_animation.h b/procedural_animation.h index b9b4d1b..6e6de30 100644 --- a/procedural_animation.h +++ b/procedural_animation.h @@ -149,6 +149,12 @@ public: int get_start_frame_index() const; void set_start_frame_index(const int value); + void set_length(float p_length); + float get_length() const; + + void set_loop(bool p_enabled); + bool has_loop() const; + //Keyframes PoolVector get_keyframe_indices() const; int add_keyframe(); @@ -173,6 +179,42 @@ public: void initialize(); void load_keyframe_data(int keyframe_index); + float track_get_key_time(int p_track, int p_key_idx) const; + float track_get_key_transition(int p_track, int p_key_idx) const; + Variant track_get_key_value(int p_track, int p_key_idx) const; + + void get_key_indices(int p_track, float p_time, float p_delta, List *p_indices) const; + void track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List *p_indices) const; + + int track_find_key(int p_track, float p_time, bool p_exact = false) const; + + Vector method_track_get_params(int p_track, int p_key_idx) const; + StringName method_track_get_name(int p_track, int p_key_idx) const; + + RES audio_track_get_key_stream(int p_track, int p_key) const; + float audio_track_get_key_start_offset(int p_track, int p_key) const; + float audio_track_get_key_end_offset(int p_track, int p_key) const; + + StringName animation_track_get_key_animation(int p_track, int p_key) const; + + //Interpolations + Error transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const; + Variant value_track_interpolate(int p_track, float p_time) const; + float bezier_track_interpolate(int p_track, float p_time) const; + + //Animation forwards + int get_track_count() const; + Animation::TrackType track_get_type(int p_track) const; + + NodePath track_get_path(int p_track) const; + int find_track(const NodePath &p_path) const; + + bool track_is_enabled(int p_track) const; + + int track_get_key_count(int p_track) const; + + Animation::UpdateMode value_track_get_update_mode(int p_track) const; + ProceduralAnimation(); ~ProceduralAnimation(); @@ -202,6 +244,8 @@ protected: private: bool _initialized; int _animation_fps; + float _length; + bool _loop; String _editor_add_category_name; String _add_editor_category_animation_name; diff --git a/procedural_animation_player.cpp b/procedural_animation_player.cpp index 3d0d710..f767ccb 100644 --- a/procedural_animation_player.cpp +++ b/procedural_animation_player.cpp @@ -21,3 +21,1624 @@ SOFTWARE. */ #include "procedural_animation_player.h" + +#include "core/engine.h" +#include "core/message_queue.h" +#include "scene/resources/animation.h" +#include "scene/scene_string_names.h" +#include "servers/audio/audio_stream.h" + +#ifdef TOOLS_ENABLED +#include "editor/editor_settings.h" +#endif + +bool ProceduralAnimationPlayer::_set(const StringName &p_name, const Variant &p_value) { + + String name = p_name; + + if (name.begins_with("playback/play")) { // bw compatibility + + set_current_animation(p_value); + + } else if (name.begins_with("anims/")) { + + String which = name.get_slicec('/', 1); + add_animation(which, p_value); + + } else if (name.begins_with("next/")) { + + String which = name.get_slicec('/', 1); + animation_set_next(which, p_value); + + } else if (p_name == SceneStringNames::get_singleton()->blend_times) { + + Array array = p_value; + int len = array.size(); + ERR_FAIL_COND_V(len % 3, false); + + for (int i = 0; i < len / 3; i++) { + + StringName from = array[i * 3 + 0]; + StringName to = array[i * 3 + 1]; + float time = array[i * 3 + 2]; + + set_blend_time(from, to, time); + } + + } else + return false; + + return true; +} + +bool ProceduralAnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const { + + String name = p_name; + + if (name == "playback/play") { // bw compatibility + + r_ret = get_current_animation(); + + } else if (name.begins_with("anims/")) { + + String which = name.get_slicec('/', 1); + r_ret = get_animation(which).get_ref_ptr(); + + } else if (name.begins_with("next/")) { + + String which = name.get_slicec('/', 1); + + r_ret = animation_get_next(which); + + } else if (name == "blend_times") { + + Vector keys; + for (Map::Element *E = blend_times.front(); E; E = E->next()) { + + keys.ordered_insert(E->key()); + } + + Array array; + for (int i = 0; i < keys.size(); i++) { + + array.push_back(keys[i].from); + array.push_back(keys[i].to); + array.push_back(blend_times[keys[i]]); + } + + r_ret = array; + } else + return false; + + return true; +} + +void ProceduralAnimationPlayer::_validate_property(PropertyInfo &property) const { + + if (property.name == "current_animation") { + List names; + + for (Map::Element *E = animation_set.front(); E; E = E->next()) { + names.push_back(E->key()); + } + names.sort(); + names.push_front("[stop]"); + String hint; + for (List::Element *E = names.front(); E; E = E->next()) { + + if (E != names.front()) + hint += ","; + hint += E->get(); + } + + property.hint_string = hint; + } +} + +void ProceduralAnimationPlayer::_get_property_list(List *p_list) const { + + List anim_names; + + for (Map::Element *E = animation_set.front(); E; E = E->next()) { + + 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)); + if (E->get().next != StringName()) + anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E->key()), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + } + + anim_names.sort(); + + for (List::Element *E = anim_names.front(); E; E = E->next()) { + p_list->push_back(E->get()); + } + + p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); +} + +void ProceduralAnimationPlayer::advance(float p_time) { + + _animation_process(p_time); +} + +void ProceduralAnimationPlayer::_notification(int p_what) { + + switch (p_what) { + + case NOTIFICATION_ENTER_TREE: { + + if (!processing) { + //make sure that a previous process state was not saved + //only process if "processing" is set + set_physics_process_internal(false); + set_process_internal(false); + } + //_set_process(false); + clear_caches(); + } break; + case NOTIFICATION_READY: { + + if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) { + play(autoplay); + _animation_process(0); + } + } break; + case NOTIFICATION_INTERNAL_PROCESS: { + if (animation_process_mode == ANIMATION_PROCESS_PHYSICS) + break; + + if (processing) + _animation_process(get_process_delta_time()); + } break; + case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { + + if (animation_process_mode == ANIMATION_PROCESS_IDLE) + break; + + if (processing) + _animation_process(get_physics_process_delta_time()); + } break; + case NOTIFICATION_EXIT_TREE: { + + clear_caches(); + } break; + } +} + +void ProceduralAnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { + + // Already cached? + if (p_anim->node_cache.size() == p_anim->animation->get_track_count()) + return; + + Node *parent = get_node(root); + + ERR_FAIL_COND(!parent); + + ProceduralAnimation *a = p_anim->animation.operator->(); + + p_anim->node_cache.resize(a->get_track_count()); + + for (int i = 0; i < a->get_track_count(); i++) { + + p_anim->node_cache.write[i] = NULL; + RES resource; + Vector leftover_path; + Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path); + ERR_CONTINUE_MSG(!child, "On Animation: '" + p_anim->name + "', couldn't resolve track: '" + String(a->track_get_path(i)) + "'."); // couldn't find the child node + uint32_t id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id(); + int bone_idx = -1; + + if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to(child)) { + + Skeleton *sk = Object::cast_to(child); + bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0)); + if (bone_idx == -1) { + + continue; + } + } + + { + if (!child->is_connected("tree_exiting", this, "_node_removed")) + child->connect("tree_exiting", this, "_node_removed", make_binds(child), CONNECT_ONESHOT); + } + + TrackNodeCacheKey key; + key.id = id; + key.bone_idx = bone_idx; + + if (!node_cache_map.has(key)) + node_cache_map[key] = TrackNodeCache(); + + p_anim->node_cache.write[i] = &node_cache_map[key]; + p_anim->node_cache[i]->path = a->track_get_path(i); + p_anim->node_cache[i]->node = child; + p_anim->node_cache[i]->resource = resource; + p_anim->node_cache[i]->node_2d = Object::cast_to(child); + if (a->track_get_type(i) == Animation::TYPE_TRANSFORM) { + // special cases and caches for transform tracks + + // cache spatial + p_anim->node_cache[i]->spatial = Object::cast_to(child); + // cache skeleton + p_anim->node_cache[i]->skeleton = Object::cast_to(child); + if (p_anim->node_cache[i]->skeleton) { + if (a->track_get_path(i).get_subname_count() == 1) { + StringName bone_name = a->track_get_path(i).get_subname(0); + + p_anim->node_cache[i]->bone_idx = p_anim->node_cache[i]->skeleton->find_bone(bone_name); + if (p_anim->node_cache[i]->bone_idx < 0) { + // broken track (nonexistent bone) + p_anim->node_cache[i]->skeleton = NULL; + p_anim->node_cache[i]->spatial = NULL; + ERR_CONTINUE(p_anim->node_cache[i]->bone_idx < 0); + } + } else { + // no property, just use spatialnode + p_anim->node_cache[i]->skeleton = NULL; + } + } + } + + if (a->track_get_type(i) == Animation::TYPE_VALUE) { + + if (!p_anim->node_cache[i]->property_anim.has(a->track_get_path(i).get_concatenated_subnames())) { + + TrackNodeCache::PropertyAnim pa; + pa.subpath = leftover_path; + pa.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child; + pa.special = SP_NONE; + pa.owner = p_anim->node_cache[i]; + if (false && p_anim->node_cache[i]->node_2d) { + + if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_pos) + pa.special = SP_NODE2D_POS; + else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_rot) + pa.special = SP_NODE2D_ROT; + else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_scale) + pa.special = SP_NODE2D_SCALE; + } + p_anim->node_cache[i]->property_anim[a->track_get_path(i).get_concatenated_subnames()] = pa; + } + } + + if (a->track_get_type(i) == Animation::TYPE_BEZIER && leftover_path.size()) { + + if (!p_anim->node_cache[i]->bezier_anim.has(a->track_get_path(i).get_concatenated_subnames())) { + + TrackNodeCache::BezierAnim ba; + ba.bezier_property = leftover_path; + ba.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child; + ba.owner = p_anim->node_cache[i]; + + p_anim->node_cache[i]->bezier_anim[a->track_get_path(i).get_concatenated_subnames()] = ba; + } + } + } +} + +void ProceduralAnimationPlayer::_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) { + + _ensure_node_caches(p_anim); + ERR_FAIL_COND(p_anim->node_cache.size() != p_anim->animation->get_track_count()); + + ProceduralAnimation *a = p_anim->animation.operator->(); + bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint(); + + for (int i = 0; i < a->get_track_count(); i++) { + + // If an animation changes this animation (or it animates itself) + // we need to recreate our animation cache + if (p_anim->node_cache.size() != a->get_track_count()) { + _ensure_node_caches(p_anim); + } + + TrackNodeCache *nc = p_anim->node_cache[i]; + + if (!nc) + continue; // no node cache for this track, skip it + + if (!a->track_is_enabled(i)) + continue; // do nothing if the track is disabled + + if (a->track_get_key_count(i) == 0) + continue; // do nothing if track is empty + + switch (a->track_get_type(i)) { + + case Animation::TYPE_TRANSFORM: { + + if (!nc->spatial) + continue; + + Vector3 loc; + Quat rot; + Vector3 scale; + + Error err = a->transform_track_interpolate(i, p_time, &loc, &rot, &scale); + //ERR_CONTINUE(err!=OK); //used for testing, should be removed + + if (err != OK) + continue; + + if (nc->accum_pass != accum_pass) { + ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX); + cache_update[cache_update_size++] = nc; + nc->accum_pass = accum_pass; + nc->loc_accum = loc; + nc->rot_accum = rot; + nc->scale_accum = scale; + + } else { + + nc->loc_accum = nc->loc_accum.linear_interpolate(loc, p_interp); + nc->rot_accum = nc->rot_accum.slerp(rot, p_interp); + nc->scale_accum = nc->scale_accum.linear_interpolate(scale, p_interp); + } + + } break; + case Animation::TYPE_VALUE: { + + if (!nc->node) + continue; + + //StringName property=a->track_get_path(i).get_property(); + + Map::Element *E = nc->property_anim.find(a->track_get_path(i).get_concatenated_subnames()); + ERR_CONTINUE(!E); //should it continue, or create a new one? + + TrackNodeCache::PropertyAnim *pa = &E->get(); + + Animation::UpdateMode update_mode = a->value_track_get_update_mode(i); + + if (update_mode == Animation::UPDATE_CAPTURE) { + + if (p_started) { + pa->capture = pa->object->get_indexed(pa->subpath); + } + + int key_count = a->track_get_key_count(i); + if (key_count == 0) + continue; //eeh not worth it + + float first_key_time = a->track_get_key_time(i, 0); + float transition = 1.0; + int first_key = 0; + + if (first_key_time == 0.0) { + //ignore, use for transition + if (key_count == 1) + continue; //with one key we can't do anything + transition = a->track_get_key_transition(i, 0); + first_key_time = a->track_get_key_time(i, 1); + first_key = 1; + } + + if (p_time < first_key_time) { + float c = Math::ease(p_time / first_key_time, transition); + Variant first_value = a->track_get_key_value(i, first_key); + Variant interp_value; + Variant::interpolate(pa->capture, first_value, c, interp_value); + + if (pa->accum_pass != accum_pass) { + ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX); + cache_update_prop[cache_update_prop_size++] = pa; + pa->value_accum = interp_value; + pa->accum_pass = accum_pass; + } else { + Variant::interpolate(pa->value_accum, interp_value, p_interp, pa->value_accum); + } + + continue; //handled + } + } + + if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE || (p_delta == 0 && update_mode == Animation::UPDATE_DISCRETE)) { //delta == 0 means seek + + Variant value = a->value_track_interpolate(i, p_time); + + if (value == Variant()) + continue; + + //thanks to trigger mode, this should be solved now.. + /* + if (p_delta==0 && value.get_type()==Variant::STRING) + continue; // doing this with strings is messy, should find another way + */ + if (pa->accum_pass != accum_pass) { + ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX); + cache_update_prop[cache_update_prop_size++] = pa; + pa->value_accum = value; + pa->accum_pass = accum_pass; + } else { + Variant::interpolate(pa->value_accum, value, p_interp, pa->value_accum); + } + + } else if (p_is_current && p_delta != 0) { + + List indices; + a->get_key_indices(i, p_time, p_delta, &indices); + + for (List::Element *F = indices.front(); F; F = F->next()) { + + Variant value = a->track_get_key_value(i, F->get()); + switch (pa->special) { + + case SP_NONE: { + bool valid; + pa->object->set_indexed(pa->subpath, value, &valid); //you are not speshul +#ifdef DEBUG_ENABLED + if (!valid) { + 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() + "'."); + } +#endif + + } break; + case SP_NODE2D_POS: { +#ifdef DEBUG_ENABLED + if (value.get_type() != Variant::VECTOR2) { + 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() + "'."); + } +#endif + static_cast(pa->object)->set_position(value); + } break; + case SP_NODE2D_ROT: { +#ifdef DEBUG_ENABLED + if (value.is_num()) { + 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() + "'."); + } +#endif + + static_cast(pa->object)->set_rotation(Math::deg2rad((double)value)); + } break; + case SP_NODE2D_SCALE: { +#ifdef DEBUG_ENABLED + if (value.get_type() != Variant::VECTOR2) { + 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() + "'."); + } +#endif + + static_cast(pa->object)->set_scale(value); + } break; + } + } + } + + } break; + case Animation::TYPE_METHOD: { + + if (!nc->node) + continue; + if (p_delta == 0) { + continue; + } + if (!p_is_current) + break; + + List indices; + + a->get_key_indices(i, p_time, p_delta, &indices); + + for (List::Element *E = indices.front(); E; E = E->next()) { + + StringName method = a->method_track_get_name(i, E->get()); + Vector params = a->method_track_get_params(i, E->get()); + + int s = params.size(); + + ERR_CONTINUE(s > VARIANT_ARG_MAX); +#ifdef DEBUG_ENABLED + if (!nc->node->has_method(method)) { + ERR_PRINTS("Invalid method call '" + method + "'. '" + a->get_name() + "' at node '" + get_path() + "'."); + } +#endif + + if (can_call) { + if (method_call_mode == ANIMATION_METHOD_CALL_DEFERRED) { + MessageQueue::get_singleton()->push_call( + nc->node, + method, + s >= 1 ? params[0] : Variant(), + s >= 2 ? params[1] : Variant(), + s >= 3 ? params[2] : Variant(), + s >= 4 ? params[3] : Variant(), + s >= 5 ? params[4] : Variant()); + } else { + nc->node->call( + method, + s >= 1 ? params[0] : Variant(), + s >= 2 ? params[1] : Variant(), + s >= 3 ? params[2] : Variant(), + s >= 4 ? params[3] : Variant(), + s >= 5 ? params[4] : Variant()); + } + } + } + + } break; + case Animation::TYPE_BEZIER: { + + if (!nc->node) + continue; + + Map::Element *E = nc->bezier_anim.find(a->track_get_path(i).get_concatenated_subnames()); + ERR_CONTINUE(!E); //should it continue, or create a new one? + + TrackNodeCache::BezierAnim *ba = &E->get(); + + float bezier = a->bezier_track_interpolate(i, p_time); + if (ba->accum_pass != accum_pass) { + ERR_CONTINUE(cache_update_bezier_size >= NODE_CACHE_UPDATE_MAX); + cache_update_bezier[cache_update_bezier_size++] = ba; + ba->bezier_accum = bezier; + ba->accum_pass = accum_pass; + } else { + ba->bezier_accum = Math::lerp(ba->bezier_accum, bezier, p_interp); + } + + } break; + case Animation::TYPE_AUDIO: { + + if (!nc->node) + continue; + if (p_delta == 0) { + continue; + } + + if (p_seeked) { + //find whathever should be playing + int idx = a->track_find_key(i, p_time); + if (idx < 0) + continue; + + Ref stream = a->audio_track_get_key_stream(i, idx); + if (!stream.is_valid()) { + nc->node->call("stop"); + nc->audio_playing = false; + playing_caches.erase(nc); + } else { + float start_ofs = a->audio_track_get_key_start_offset(i, idx); + start_ofs += p_time - a->track_get_key_time(i, idx); + float end_ofs = a->audio_track_get_key_end_offset(i, idx); + float len = stream->get_length(); + + if (start_ofs > len - end_ofs) { + nc->node->call("stop"); + nc->audio_playing = false; + playing_caches.erase(nc); + continue; + } + + nc->node->call("set_stream", stream); + nc->node->call("play", start_ofs); + + nc->audio_playing = true; + playing_caches.insert(nc); + if (len && end_ofs > 0) { //force a end at a time + nc->audio_len = len - start_ofs - end_ofs; + } else { + nc->audio_len = 0; + } + + nc->audio_start = p_time; + } + + } else { + //find stuff to play + List to_play; + a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play); + if (to_play.size()) { + int idx = to_play.back()->get(); + + Ref stream = a->audio_track_get_key_stream(i, idx); + if (!stream.is_valid()) { + nc->node->call("stop"); + nc->audio_playing = false; + playing_caches.erase(nc); + } else { + float start_ofs = a->audio_track_get_key_start_offset(i, idx); + float end_ofs = a->audio_track_get_key_end_offset(i, idx); + float len = stream->get_length(); + + nc->node->call("set_stream", stream); + nc->node->call("play", start_ofs); + + nc->audio_playing = true; + playing_caches.insert(nc); + if (len && end_ofs > 0) { //force a end at a time + nc->audio_len = len - start_ofs - end_ofs; + } else { + nc->audio_len = 0; + } + + nc->audio_start = p_time; + } + } else if (nc->audio_playing) { + + bool loop = a->has_loop(); + + bool stop = false; + + if (!loop && p_time < nc->audio_start) { + stop = true; + } else if (nc->audio_len > 0) { + float len = nc->audio_start > p_time ? (a->get_length() - nc->audio_start) + p_time : p_time - nc->audio_start; + + if (len > nc->audio_len) { + stop = true; + } + } + + if (stop) { + //time to stop + nc->node->call("stop"); + nc->audio_playing = false; + playing_caches.erase(nc); + } + } + } + + } break; + case Animation::TYPE_ANIMATION: { + + ProceduralAnimationPlayer *player = Object::cast_to(nc->node); + if (!player) + continue; + + if (p_delta == 0 || p_seeked) { + //seek + int idx = a->track_find_key(i, p_time); + if (idx < 0) + continue; + + float pos = a->track_get_key_time(i, idx); + + StringName anim_name = a->animation_track_get_key_animation(i, idx); + if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) + continue; + + Ref anim = player->get_animation(anim_name); + + float at_anim_pos; + + if (anim->has_loop()) { + at_anim_pos = Math::fposmod(p_time - pos, anim->get_length()); //seek to loop + } else { + at_anim_pos = MAX(anim->get_length(), p_time - pos); //seek to end + } + + if (player->is_playing() || p_seeked) { + player->play(anim_name); + player->seek(at_anim_pos); + nc->animation_playing = true; + playing_caches.insert(nc); + } else { + player->set_assigned_animation(anim_name); + player->seek(at_anim_pos, true); + } + } else { + //find stuff to play + List to_play; + a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play); + if (to_play.size()) { + int idx = to_play.back()->get(); + + StringName anim_name = a->animation_track_get_key_animation(i, idx); + if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) { + + if (playing_caches.has(nc)) { + playing_caches.erase(nc); + player->stop(); + nc->animation_playing = false; + } + } else { + player->play(anim_name); + nc->animation_playing = true; + playing_caches.insert(nc); + } + } + } + + } break; + } + } +} + +void ProceduralAnimationPlayer::_animation_process_data(PlaybackData &cd, float p_delta, float p_blend, bool p_seeked, bool p_started) { + + float delta = p_delta * speed_scale * cd.speed_scale; + float next_pos = cd.pos + delta; + + float len = cd.from->animation->get_length(); + bool loop = cd.from->animation->has_loop(); + + if (!loop) { + + if (next_pos < 0) + next_pos = 0; + else if (next_pos > len) + next_pos = len; + + // fix delta + delta = next_pos - cd.pos; + + if (&cd == &playback.current) { + + bool backwards = delta < 0; + + if (!backwards && cd.pos <= len && next_pos == len /*&& playback.blend.empty()*/) { + //playback finished + end_reached = true; + end_notify = cd.pos < len; // Notify only if not already at the end + } + + if (backwards && cd.pos >= 0 && next_pos == 0 /*&& playback.blend.empty()*/) { + //playback finished + end_reached = true; + end_notify = cd.pos > 0; // Notify only if not already at the beginning + } + } + + } else { + + float looped_next_pos = Math::fposmod(next_pos, len); + if (looped_next_pos == 0 && next_pos != 0) { + // Loop multiples of the length to it, rather than 0 + // so state at time=length is previewable in the editor + next_pos = len; + } else { + next_pos = looped_next_pos; + } + } + + cd.pos = next_pos; + + _animation_process_animation(cd.from, cd.pos, delta, p_blend, &cd == &playback.current, p_seeked, p_started); +} +void ProceduralAnimationPlayer::_animation_process2(float p_delta, bool p_started) { + + Playback &c = playback; + + accum_pass++; + + _animation_process_data(c.current, p_delta, 1.0f, c.seeked && p_delta != 0, p_started); + if (p_delta != 0) { + c.seeked = false; + } + + List::Element *prev = NULL; + for (List::Element *E = c.blend.back(); E; E = prev) { + + Blend &b = E->get(); + float blend = b.blend_left / b.blend_time; + _animation_process_data(b.data, p_delta, blend, false, false); + + b.blend_left -= Math::absf(speed_scale * p_delta); + + prev = E->prev(); + if (b.blend_left < 0) { + + c.blend.erase(E); + } + } +} + +void ProceduralAnimationPlayer::_animation_update_transforms() { + { + Transform t; + for (int i = 0; i < cache_update_size; i++) { + + TrackNodeCache *nc = cache_update[i]; + + ERR_CONTINUE(nc->accum_pass != accum_pass); + + t.origin = nc->loc_accum; + t.basis.set_quat_scale(nc->rot_accum, nc->scale_accum); + if (nc->skeleton && nc->bone_idx >= 0) { + + nc->skeleton->set_bone_pose(nc->bone_idx, t); + + } else if (nc->spatial) { + + nc->spatial->set_transform(t); + } + } + } + + cache_update_size = 0; + + for (int i = 0; i < cache_update_prop_size; i++) { + + TrackNodeCache::PropertyAnim *pa = cache_update_prop[i]; + + ERR_CONTINUE(pa->accum_pass != accum_pass); + + switch (pa->special) { + + case SP_NONE: { + bool valid; + pa->object->set_indexed(pa->subpath, pa->value_accum, &valid); //you are not speshul +#ifdef DEBUG_ENABLED + if (!valid) { + 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"); + } +#endif + + } break; + case SP_NODE2D_POS: { +#ifdef DEBUG_ENABLED + if (pa->value_accum.get_type() != Variant::VECTOR2) { + 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()"); + } +#endif + static_cast(pa->object)->set_position(pa->value_accum); + } break; + case SP_NODE2D_ROT: { +#ifdef DEBUG_ENABLED + if (pa->value_accum.is_num()) { + 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"); + } +#endif + + static_cast(pa->object)->set_rotation(Math::deg2rad((double)pa->value_accum)); + } break; + case SP_NODE2D_SCALE: { +#ifdef DEBUG_ENABLED + if (pa->value_accum.get_type() != Variant::VECTOR2) { + 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()"); + } +#endif + + static_cast(pa->object)->set_scale(pa->value_accum); + } break; + } + } + + cache_update_prop_size = 0; + + for (int i = 0; i < cache_update_bezier_size; i++) { + + TrackNodeCache::BezierAnim *ba = cache_update_bezier[i]; + + ERR_CONTINUE(ba->accum_pass != accum_pass); + ba->object->set_indexed(ba->bezier_property, ba->bezier_accum); + } + + cache_update_bezier_size = 0; +} + +void ProceduralAnimationPlayer::_animation_process(float p_delta) { + + if (playback.current.from) { + + end_reached = false; + end_notify = false; + _animation_process2(p_delta, playback.started); + + if (playback.started) { + playback.started = false; + } + + _animation_update_transforms(); + if (end_reached) { + if (queued.size()) { + String old = playback.assigned; + play(queued.front()->get()); + String new_name = playback.assigned; + queued.pop_front(); + if (end_notify) + emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name); + } else { + //stop(); + playing = false; + _set_process(false); + if (end_notify) + emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned); + } + end_reached = false; + } + + } else { + _set_process(false); + } +} + +Error ProceduralAnimationPlayer::add_animation(const StringName &p_name, const Ref &p_animation) { + +#ifdef DEBUG_ENABLED + ERR_FAIL_COND_V_MSG(String(p_name).find("/") != -1 || String(p_name).find(":") != -1 || String(p_name).find(",") != -1 || String(p_name).find("[") != -1, ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + "."); +#endif + + ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER); + + if (animation_set.has(p_name)) { + + _unref_anim(animation_set[p_name].animation); + animation_set[p_name].animation = p_animation; + clear_caches(); + } else { + + AnimationData ad; + ad.animation = p_animation; + ad.name = p_name; + animation_set[p_name] = ad; + } + + _ref_anim(p_animation); + _change_notify(); + return OK; +} + +void ProceduralAnimationPlayer::remove_animation(const StringName &p_name) { + + ERR_FAIL_COND(!animation_set.has(p_name)); + + stop(); + _unref_anim(animation_set[p_name].animation); + animation_set.erase(p_name); + + clear_caches(); + _change_notify(); +} + +void ProceduralAnimationPlayer::_ref_anim(const Ref &p_anim) { + + Ref(p_anim)->connect(SceneStringNames::get_singleton()->tracks_changed, this, "_animation_changed", varray(), CONNECT_REFERENCE_COUNTED); +} + +void ProceduralAnimationPlayer::_unref_anim(const Ref &p_anim) { + + Ref(p_anim)->disconnect(SceneStringNames::get_singleton()->tracks_changed, this, "_animation_changed"); +} + +void ProceduralAnimationPlayer::rename_animation(const StringName &p_name, const StringName &p_new_name) { + + ERR_FAIL_COND(!animation_set.has(p_name)); + ERR_FAIL_COND(String(p_new_name).find("/") != -1 || String(p_new_name).find(":") != -1); + ERR_FAIL_COND(animation_set.has(p_new_name)); + + stop(); + AnimationData ad = animation_set[p_name]; + ad.name = p_new_name; + animation_set.erase(p_name); + animation_set[p_new_name] = ad; + + List to_erase; + Map to_insert; + for (Map::Element *E = blend_times.front(); E; E = E->next()) { + + BlendKey bk = E->key(); + BlendKey new_bk = bk; + bool erase = false; + if (bk.from == p_name) { + new_bk.from = p_new_name; + erase = true; + } + if (bk.to == p_name) { + new_bk.to = p_new_name; + erase = true; + } + + if (erase) { + to_erase.push_back(bk); + to_insert[new_bk] = E->get(); + } + } + + while (to_erase.size()) { + + blend_times.erase(to_erase.front()->get()); + to_erase.pop_front(); + } + + while (to_insert.size()) { + blend_times[to_insert.front()->key()] = to_insert.front()->get(); + to_insert.erase(to_insert.front()); + } + + if (autoplay == p_name) + autoplay = p_new_name; + + clear_caches(); + _change_notify(); +} + +bool ProceduralAnimationPlayer::has_animation(const StringName &p_name) const { + + return animation_set.has(p_name); +} +Ref ProceduralAnimationPlayer::get_animation(const StringName &p_name) const { + + ERR_FAIL_COND_V(!animation_set.has(p_name), Ref()); + + const AnimationData &data = animation_set[p_name]; + + return data.animation; +} +void ProceduralAnimationPlayer::get_animation_list(List *p_animations) const { + + List anims; + + for (Map::Element *E = animation_set.front(); E; E = E->next()) { + + anims.push_back(E->key()); + } + + anims.sort(); + + for (List::Element *E = anims.front(); E; E = E->next()) { + + p_animations->push_back(E->get()); + } +} + +void ProceduralAnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time) { + + ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0."); + + BlendKey bk; + bk.from = p_animation1; + bk.to = p_animation2; + if (p_time == 0) + blend_times.erase(bk); + else + blend_times[bk] = p_time; +} + +float ProceduralAnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const { + + BlendKey bk; + bk.from = p_animation1; + bk.to = p_animation2; + + if (blend_times.has(bk)) + return blend_times[bk]; + else + return 0; +} + +void ProceduralAnimationPlayer::queue(const StringName &p_name) { + + if (!is_playing()) + play(p_name); + else + queued.push_back(p_name); +} + +PoolVector ProceduralAnimationPlayer::get_queue() { + PoolVector ret; + for (List::Element *E = queued.front(); E; E = E->next()) { + ret.push_back(E->get()); + } + + return ret; +} + +void ProceduralAnimationPlayer::clear_queue() { + queued.clear(); +} + +void ProceduralAnimationPlayer::play_backwards(const StringName &p_name, float p_custom_blend) { + + play(p_name, p_custom_blend, -1, true); +} + +void ProceduralAnimationPlayer::play(const StringName &p_name, float p_custom_blend, float p_custom_scale, bool p_from_end) { + + StringName name = p_name; + + if (String(name) == "") + name = playback.assigned; + + ERR_FAIL_COND_MSG(!animation_set.has(name), "Animation not found: " + name + "."); + + Playback &c = playback; + + if (c.current.from) { + + float blend_time = 0; + // find if it can blend + BlendKey bk; + bk.from = c.current.from->name; + bk.to = name; + + if (p_custom_blend >= 0) { + blend_time = p_custom_blend; + } else if (blend_times.has(bk)) { + + blend_time = blend_times[bk]; + } else { + + bk.from = "*"; + if (blend_times.has(bk)) { + + blend_time = blend_times[bk]; + } else { + + bk.from = c.current.from->name; + bk.to = "*"; + + if (blend_times.has(bk)) { + + blend_time = blend_times[bk]; + } + } + } + + if (p_custom_blend < 0 && blend_time == 0 && default_blend_time) + blend_time = default_blend_time; + if (blend_time > 0) { + + Blend b; + b.data = c.current; + b.blend_time = b.blend_left = blend_time; + c.blend.push_back(b); + } + } + + if (get_current_animation() != p_name) { + _stop_playing_caches(); + } + + c.current.from = &animation_set[name]; + + if (c.assigned != name) { // reset + c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0; + } else { + if (p_from_end && c.current.pos == 0) { + // Animation reset BUT played backwards, set position to the end + c.current.pos = c.current.from->animation->get_length(); + } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) { + // Animation resumed but already ended, set position to the beginning + c.current.pos = 0; + } + } + + c.current.speed_scale = p_custom_scale; + c.assigned = name; + c.seeked = false; + c.started = true; + + if (!end_reached) + queued.clear(); + _set_process(true); // always process when starting an animation + playing = true; + + emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned); + + if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) + return; // no next in this case + + StringName next = animation_get_next(p_name); + if (next != StringName() && animation_set.has(next)) { + queue(next); + } +} + +bool ProceduralAnimationPlayer::is_playing() const { + + return playing; + /* + if (playback.current.from==NULL) + return false; + + float len=playback.current.from->animation->get_length(); + float pos = playback.current.pos; + bool loop=playback.current.from->animation->has_loop(); + if (!loop && pos >= len) { + return false; + }; + + return true; + */ +} + +void ProceduralAnimationPlayer::set_current_animation(const String &p_anim) { + + if (p_anim == "[stop]" || p_anim == "") { + stop(); + } else if (!is_playing() || playback.assigned != p_anim) { + play(p_anim); + } else { + // Same animation, do not replay from start + } +} + +String ProceduralAnimationPlayer::get_current_animation() const { + + return (is_playing() ? playback.assigned : ""); +} + +void ProceduralAnimationPlayer::set_assigned_animation(const String &p_anim) { + + if (is_playing()) { + play(p_anim); + } else { + ERR_FAIL_COND(!animation_set.has(p_anim)); + playback.current.pos = 0; + playback.current.from = &animation_set[p_anim]; + playback.assigned = p_anim; + } +} + +String ProceduralAnimationPlayer::get_assigned_animation() const { + + return playback.assigned; +} + +void ProceduralAnimationPlayer::stop(bool p_reset) { + + _stop_playing_caches(); + Playback &c = playback; + c.blend.clear(); + if (p_reset) { + c.current.from = NULL; + c.current.speed_scale = 1; + c.current.pos = 0; + } + _set_process(false); + queued.clear(); + playing = false; +} + +void ProceduralAnimationPlayer::set_speed_scale(float p_speed) { + + speed_scale = p_speed; +} +float ProceduralAnimationPlayer::get_speed_scale() const { + + return speed_scale; +} +float ProceduralAnimationPlayer::get_playing_speed() const { + + if (!playing) { + return 0; + } + return speed_scale * playback.current.speed_scale; +} + +void ProceduralAnimationPlayer::seek(float p_time, bool p_update) { + + if (!playback.current.from) { + if (playback.assigned) { + ERR_FAIL_COND(!animation_set.has(playback.assigned)); + playback.current.from = &animation_set[playback.assigned]; + } + ERR_FAIL_COND(!playback.current.from); + } + + playback.current.pos = p_time; + playback.seeked = true; + if (p_update) { + _animation_process(0); + } +} + +void ProceduralAnimationPlayer::seek_delta(float p_time, float p_delta) { + + if (!playback.current.from) { + if (playback.assigned) { + ERR_FAIL_COND(!animation_set.has(playback.assigned)); + playback.current.from = &animation_set[playback.assigned]; + } + ERR_FAIL_COND(!playback.current.from); + } + + playback.current.pos = p_time - p_delta; + if (speed_scale != 0.0) + p_delta /= speed_scale; + _animation_process(p_delta); + //playback.current.pos=p_time; +} + +bool ProceduralAnimationPlayer::is_valid() const { + + return (playback.current.from); +} + +float ProceduralAnimationPlayer::get_current_animation_position() const { + + ERR_FAIL_COND_V(!playback.current.from, 0); + return playback.current.pos; +} + +float ProceduralAnimationPlayer::get_current_animation_length() const { + + ERR_FAIL_COND_V(!playback.current.from, 0); + return playback.current.from->animation->get_length(); +} + +void ProceduralAnimationPlayer::_animation_changed() { + + clear_caches(); + emit_signal("caches_cleared"); + if (is_playing()) { + playback.seeked = true; //need to restart stuff, like audio + } +} + +void ProceduralAnimationPlayer::_stop_playing_caches() { + + for (Set::Element *E = playing_caches.front(); E; E = E->next()) { + + if (E->get()->node && E->get()->audio_playing) { + E->get()->node->call("stop"); + } + if (E->get()->node && E->get()->animation_playing) { + ProceduralAnimationPlayer *player = Object::cast_to(E->get()->node); + if (!player) + continue; + player->stop(); + } + } + + playing_caches.clear(); +} + +void ProceduralAnimationPlayer::_node_removed(Node *p_node) { + + clear_caches(); // nodes contained here ar being removed, clear the caches +} + +void ProceduralAnimationPlayer::clear_caches() { + + _stop_playing_caches(); + + node_cache_map.clear(); + + for (Map::Element *E = animation_set.front(); E; E = E->next()) { + + E->get().node_cache.clear(); + } + + cache_update_size = 0; + cache_update_prop_size = 0; + cache_update_bezier_size = 0; +} + +void ProceduralAnimationPlayer::set_active(bool p_active) { + + if (active == p_active) + return; + + active = p_active; + _set_process(processing, true); +} + +bool ProceduralAnimationPlayer::is_active() const { + + return active; +} + +StringName ProceduralAnimationPlayer::find_animation(const Ref &p_animation) const { + + for (Map::Element *E = animation_set.front(); E; E = E->next()) { + + if (E->get().animation == p_animation) + return E->key(); + } + + return ""; +} + +void ProceduralAnimationPlayer::set_autoplay(const String &p_name) { + if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) + WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect."); + + autoplay = p_name; +} + +String ProceduralAnimationPlayer::get_autoplay() const { + + return autoplay; +} + +void ProceduralAnimationPlayer::set_animation_process_mode(AnimationProcessMode p_mode) { + + if (animation_process_mode == p_mode) + return; + + bool pr = processing; + if (pr) + _set_process(false); + animation_process_mode = p_mode; + if (pr) + _set_process(true); +} + +ProceduralAnimationPlayer::AnimationProcessMode ProceduralAnimationPlayer::get_animation_process_mode() const { + + return animation_process_mode; +} + +void ProceduralAnimationPlayer::set_method_call_mode(AnimationMethodCallMode p_mode) { + + method_call_mode = p_mode; +} + +ProceduralAnimationPlayer::AnimationMethodCallMode ProceduralAnimationPlayer::get_method_call_mode() const { + + return method_call_mode; +} + +void ProceduralAnimationPlayer::_set_process(bool p_process, bool p_force) { + + if (processing == p_process && !p_force) + return; + + switch (animation_process_mode) { + + case ANIMATION_PROCESS_PHYSICS: set_physics_process_internal(p_process && active); break; + case ANIMATION_PROCESS_IDLE: set_process_internal(p_process && active); break; + case ANIMATION_PROCESS_MANUAL: break; + } + + processing = p_process; +} + +void ProceduralAnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) { + + ERR_FAIL_COND(!animation_set.has(p_animation)); + animation_set[p_animation].next = p_next; +} + +StringName ProceduralAnimationPlayer::animation_get_next(const StringName &p_animation) const { + + if (!animation_set.has(p_animation)) + return StringName(); + return animation_set[p_animation].next; +} + +void ProceduralAnimationPlayer::set_default_blend_time(float p_default) { + + default_blend_time = p_default; +} + +float ProceduralAnimationPlayer::get_default_blend_time() const { + + return default_blend_time; +} + +void ProceduralAnimationPlayer::set_root(const NodePath &p_root) { + + root = p_root; + clear_caches(); +} + +NodePath ProceduralAnimationPlayer::get_root() const { + + return root; +} + +void ProceduralAnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List *r_options) const { + +#ifdef TOOLS_ENABLED + const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\""; +#else + const String quote_style = "\""; +#endif + + String pf = p_function; + if (p_function == "play" || p_function == "play_backwards" || p_function == "remove_animation" || p_function == "has_animation" || p_function == "queue") { + List al; + get_animation_list(&al); + for (List::Element *E = al.front(); E; E = E->next()) { + + r_options->push_back(quote_style + String(E->get()) + quote_style); + } + } + Node::get_argument_options(p_function, p_idx, r_options); +} + +void ProceduralAnimationPlayer::_bind_methods() { + + ClassDB::bind_method(D_METHOD("_node_removed"), &ProceduralAnimationPlayer::_node_removed); + ClassDB::bind_method(D_METHOD("_animation_changed"), &ProceduralAnimationPlayer::_animation_changed); + + ClassDB::bind_method(D_METHOD("add_animation", "name", "animation"), &ProceduralAnimationPlayer::add_animation); + ClassDB::bind_method(D_METHOD("remove_animation", "name"), &ProceduralAnimationPlayer::remove_animation); + ClassDB::bind_method(D_METHOD("rename_animation", "name", "newname"), &ProceduralAnimationPlayer::rename_animation); + ClassDB::bind_method(D_METHOD("has_animation", "name"), &ProceduralAnimationPlayer::has_animation); + ClassDB::bind_method(D_METHOD("get_animation", "name"), &ProceduralAnimationPlayer::get_animation); + ClassDB::bind_method(D_METHOD("get_animation_list"), &ProceduralAnimationPlayer::_get_animation_list); + + ClassDB::bind_method(D_METHOD("animation_set_next", "anim_from", "anim_to"), &ProceduralAnimationPlayer::animation_set_next); + ClassDB::bind_method(D_METHOD("animation_get_next", "anim_from"), &ProceduralAnimationPlayer::animation_get_next); + + ClassDB::bind_method(D_METHOD("set_blend_time", "anim_from", "anim_to", "sec"), &ProceduralAnimationPlayer::set_blend_time); + ClassDB::bind_method(D_METHOD("get_blend_time", "anim_from", "anim_to"), &ProceduralAnimationPlayer::get_blend_time); + + ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &ProceduralAnimationPlayer::set_default_blend_time); + ClassDB::bind_method(D_METHOD("get_default_blend_time"), &ProceduralAnimationPlayer::get_default_blend_time); + + ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &ProceduralAnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &ProceduralAnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("stop", "reset"), &ProceduralAnimationPlayer::stop, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("is_playing"), &ProceduralAnimationPlayer::is_playing); + + ClassDB::bind_method(D_METHOD("set_current_animation", "anim"), &ProceduralAnimationPlayer::set_current_animation); + ClassDB::bind_method(D_METHOD("get_current_animation"), &ProceduralAnimationPlayer::get_current_animation); + ClassDB::bind_method(D_METHOD("set_assigned_animation", "anim"), &ProceduralAnimationPlayer::set_assigned_animation); + ClassDB::bind_method(D_METHOD("get_assigned_animation"), &ProceduralAnimationPlayer::get_assigned_animation); + ClassDB::bind_method(D_METHOD("queue", "name"), &ProceduralAnimationPlayer::queue); + ClassDB::bind_method(D_METHOD("get_queue"), &ProceduralAnimationPlayer::get_queue); + ClassDB::bind_method(D_METHOD("clear_queue"), &ProceduralAnimationPlayer::clear_queue); + + ClassDB::bind_method(D_METHOD("set_active", "active"), &ProceduralAnimationPlayer::set_active); + ClassDB::bind_method(D_METHOD("is_active"), &ProceduralAnimationPlayer::is_active); + + ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &ProceduralAnimationPlayer::set_speed_scale); + ClassDB::bind_method(D_METHOD("get_speed_scale"), &ProceduralAnimationPlayer::get_speed_scale); + ClassDB::bind_method(D_METHOD("get_playing_speed"), &ProceduralAnimationPlayer::get_playing_speed); + + ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &ProceduralAnimationPlayer::set_autoplay); + ClassDB::bind_method(D_METHOD("get_autoplay"), &ProceduralAnimationPlayer::get_autoplay); + + ClassDB::bind_method(D_METHOD("set_root", "path"), &ProceduralAnimationPlayer::set_root); + ClassDB::bind_method(D_METHOD("get_root"), &ProceduralAnimationPlayer::get_root); + + ClassDB::bind_method(D_METHOD("find_animation", "animation"), &ProceduralAnimationPlayer::find_animation); + + ClassDB::bind_method(D_METHOD("clear_caches"), &ProceduralAnimationPlayer::clear_caches); + + ClassDB::bind_method(D_METHOD("set_animation_process_mode", "mode"), &ProceduralAnimationPlayer::set_animation_process_mode); + ClassDB::bind_method(D_METHOD("get_animation_process_mode"), &ProceduralAnimationPlayer::get_animation_process_mode); + + ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &ProceduralAnimationPlayer::set_method_call_mode); + ClassDB::bind_method(D_METHOD("get_method_call_mode"), &ProceduralAnimationPlayer::get_method_call_mode); + + ClassDB::bind_method(D_METHOD("get_current_animation_position"), &ProceduralAnimationPlayer::get_current_animation_position); + ClassDB::bind_method(D_METHOD("get_current_animation_length"), &ProceduralAnimationPlayer::get_current_animation_length); + + ClassDB::bind_method(D_METHOD("seek", "seconds", "update"), &ProceduralAnimationPlayer::seek, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("advance", "delta"), &ProceduralAnimationPlayer::advance); + + ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_node"), "set_root", "get_root"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ANIMATE_AS_TRIGGER), "set_current_animation", "get_current_animation"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "assigned_animation", PROPERTY_HINT_NONE, "", 0), "set_assigned_animation", "get_assigned_animation"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_autoplay", "get_autoplay"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "current_animation_length", PROPERTY_HINT_NONE, "", 0), "", "get_current_animation_length"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "current_animation_position", PROPERTY_HINT_NONE, "", 0), "", "get_current_animation_position"); + + ADD_GROUP("Playback Options", "playback_"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_animation_process_mode", "get_animation_process_mode"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_default_blend_time", "get_default_blend_time"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", 0), "set_active", "is_active"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "method_call_mode", PROPERTY_HINT_ENUM, "Deferred,Immediate"), "set_method_call_mode", "get_method_call_mode"); + + ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING, "anim_name"))); + ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name"))); + ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING, "anim_name"))); + ADD_SIGNAL(MethodInfo("caches_cleared")); + + BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS); + BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE); + BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL); + + BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_DEFERRED); + BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_IMMEDIATE); +} + +ProceduralAnimationPlayer::ProceduralAnimationPlayer() { + + accum_pass = 1; + cache_update_size = 0; + cache_update_prop_size = 0; + cache_update_bezier_size = 0; + speed_scale = 1; + end_reached = false; + end_notify = false; + animation_process_mode = ANIMATION_PROCESS_IDLE; + method_call_mode = ANIMATION_METHOD_CALL_DEFERRED; + processing = false; + default_blend_time = 0; + root = SceneStringNames::get_singleton()->path_pp; + playing = false; + active = true; + playback.seeked = false; + playback.started = false; +} + +ProceduralAnimationPlayer::~ProceduralAnimationPlayer() { +} diff --git a/procedural_animation_player.h b/procedural_animation_player.h index fe51b3b..36eb06d 100644 --- a/procedural_animation_player.h +++ b/procedural_animation_player.h @@ -27,10 +27,319 @@ SOFTWARE. #include "procedural_animation.h" +#include "scene/2d/node_2d.h" +#include "scene/3d/skeleton.h" +#include "scene/3d/spatial.h" + class ProceduralAnimationPlayer : public Node { GDCLASS(ProceduralAnimationPlayer, Node); + OBJ_CATEGORY("Animation Nodes"); - friend class Animation; +public: + enum AnimationProcessMode { + ANIMATION_PROCESS_PHYSICS, + ANIMATION_PROCESS_IDLE, + ANIMATION_PROCESS_MANUAL, + }; + + enum AnimationMethodCallMode { + ANIMATION_METHOD_CALL_DEFERRED, + ANIMATION_METHOD_CALL_IMMEDIATE, + }; + +private: + enum { + + NODE_CACHE_UPDATE_MAX = 1024, + BLEND_FROM_MAX = 3 + }; + + enum SpecialProperty { + SP_NONE, + SP_NODE2D_POS, + SP_NODE2D_ROT, + SP_NODE2D_SCALE, + }; + + struct TrackNodeCache { + + NodePath path; + uint32_t id; + RES resource; + Node *node; + Spatial *spatial; + Node2D *node_2d; + Skeleton *skeleton; + int bone_idx; + // accumulated transforms + + Vector3 loc_accum; + Quat rot_accum; + Vector3 scale_accum; + uint64_t accum_pass; + + bool audio_playing; + float audio_start; + float audio_len; + + bool animation_playing; + + struct PropertyAnim { + + TrackNodeCache *owner; + SpecialProperty special; //small optimization + Vector subpath; + Object *object; + Variant value_accum; + uint64_t accum_pass; + Variant capture; + + PropertyAnim() : + owner(NULL), + special(SP_NONE), + object(NULL), + accum_pass(0) {} + }; + + Map property_anim; + + struct BezierAnim { + + Vector bezier_property; + TrackNodeCache *owner; + float bezier_accum; + Object *object; + uint64_t accum_pass; + + BezierAnim() : + owner(NULL), + bezier_accum(0.0), + object(NULL), + accum_pass(0) {} + }; + + Map bezier_anim; + + TrackNodeCache() : + id(0), + node(NULL), + spatial(NULL), + node_2d(NULL), + skeleton(NULL), + bone_idx(-1), + accum_pass(0), + audio_playing(false), + audio_start(0.0), + audio_len(0.0), + animation_playing(false) {} + }; + + struct TrackNodeCacheKey { + + uint32_t id; + int bone_idx; + + inline bool operator<(const TrackNodeCacheKey &p_right) const { + + if (id < p_right.id) + return true; + else if (id > p_right.id) + return false; + else + return bone_idx < p_right.bone_idx; + } + }; + + Map node_cache_map; + + TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX]; + int cache_update_size; + TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX]; + int cache_update_prop_size; + TrackNodeCache::BezierAnim *cache_update_bezier[NODE_CACHE_UPDATE_MAX]; + int cache_update_bezier_size; + Set playing_caches; + + uint64_t accum_pass; + float speed_scale; + float default_blend_time; + + struct AnimationData { + String name; + StringName next; + Vector node_cache; + Ref animation; + }; + + Map animation_set; + struct BlendKey { + + StringName from; + StringName to; + bool operator<(const BlendKey &bk) const { return from == bk.from ? String(to) < String(bk.to) : String(from) < String(bk.from); } + }; + + Map blend_times; + + struct PlaybackData { + + AnimationData *from; + float pos; + float speed_scale; + + PlaybackData() { + + pos = 0; + speed_scale = 1.0; + from = NULL; + } + }; + + struct Blend { + + PlaybackData data; + + float blend_time; + float blend_left; + + Blend() { + + blend_left = 0; + blend_time = 0; + } + }; + + struct Playback { + + List blend; + PlaybackData current; + StringName assigned; + bool seeked; + bool started; + } playback; + + List queued; + + bool end_reached; + bool end_notify; + + String autoplay; + AnimationProcessMode animation_process_mode; + AnimationMethodCallMode method_call_mode; + bool processing; + bool active; + + NodePath root; + + void _animation_process_animation(AnimationData *p_anim, float p_time, float p_delta, float p_interp, bool p_is_current = true, bool p_seeked = false, bool p_started = false); + + void _ensure_node_caches(AnimationData *p_anim); + void _animation_process_data(PlaybackData &cd, float p_delta, float p_blend, bool p_seeked, bool p_started); + void _animation_process2(float p_delta, bool p_started); + void _animation_update_transforms(); + void _animation_process(float p_delta); + + void _node_removed(Node *p_node); + void _stop_playing_caches(); + + // bind helpers + PoolVector _get_animation_list() const { + + List animations; + get_animation_list(&animations); + PoolVector ret; + while (animations.size()) { + + ret.push_back(animations.front()->get()); + animations.pop_front(); + } + return ret; + } + + void _animation_changed(); + void _ref_anim(const Ref &p_anim); + void _unref_anim(const Ref &p_anim); + + void _set_process(bool p_process, bool p_force = false); + + bool playing; + +protected: + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + virtual void _validate_property(PropertyInfo &property) const; + void _get_property_list(List *p_list) const; + void _notification(int p_what); + + static void _bind_methods(); + +public: + StringName find_animation(const Ref &p_animation) const; + + Error add_animation(const StringName &p_name, const Ref &p_animation); + void remove_animation(const StringName &p_name); + void rename_animation(const StringName &p_name, const StringName &p_new_name); + bool has_animation(const StringName &p_name) const; + Ref get_animation(const StringName &p_name) const; + void get_animation_list(List *p_animations) const; + + void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time); + float get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const; + + void animation_set_next(const StringName &p_animation, const StringName &p_next); + StringName animation_get_next(const StringName &p_animation) const; + + void set_default_blend_time(float p_default); + float get_default_blend_time() const; + + void play(const StringName &p_name = StringName(), float p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false); + void play_backwards(const StringName &p_name = StringName(), float p_custom_blend = -1); + void queue(const StringName &p_name); + PoolVector get_queue(); + void clear_queue(); + void stop(bool p_reset = true); + bool is_playing() const; + String get_current_animation() const; + void set_current_animation(const String &p_anim); + String get_assigned_animation() const; + void set_assigned_animation(const String &p_anim); + void stop_all(); + void set_active(bool p_active); + bool is_active() const; + bool is_valid() const; + + void set_speed_scale(float p_speed); + float get_speed_scale() const; + float get_playing_speed() const; + + void set_autoplay(const String &p_name); + String get_autoplay() const; + + void set_animation_process_mode(AnimationProcessMode p_mode); + AnimationProcessMode get_animation_process_mode() const; + + void set_method_call_mode(AnimationMethodCallMode p_mode); + AnimationMethodCallMode get_method_call_mode() const; + + void seek(float p_time, bool p_update = false); + void seek_delta(float p_time, float p_delta); + float get_current_animation_position() const; + float get_current_animation_length() const; + + void advance(float p_time); + + void set_root(const NodePath &p_root); + NodePath get_root() const; + + void clear_caches(); ///< must be called by hand if an animation was modified after added + + void get_argument_options(const StringName &p_function, int p_idx, List *r_options) const; + + ProceduralAnimationPlayer(); + ~ProceduralAnimationPlayer(); }; +VARIANT_ENUM_CAST(ProceduralAnimationPlayer::AnimationProcessMode); +VARIANT_ENUM_CAST(ProceduralAnimationPlayer::AnimationMethodCallMode); + #endif \ No newline at end of file