diff --git a/procedural_animation.cpp b/procedural_animation.cpp index 593e24f..057210a 100644 --- a/procedural_animation.cpp +++ b/procedural_animation.cpp @@ -56,7 +56,7 @@ PoolVector ProceduralAnimation::get_animation_keyframe_names() const { names.set(i, String::num(E->key()) + " " + _keyframe_names[E->key()]); ++i; } - + return names; } @@ -70,7 +70,7 @@ PoolVector ProceduralAnimation::get_category_indices() const { idxr.set(i, E->key()); ++i; } - + return idxr; } int ProceduralAnimation::add_category(const String &name) { @@ -91,7 +91,7 @@ int ProceduralAnimation::add_category(const String &name) { } void ProceduralAnimation::remove_category(const int index) { ERR_FAIL_COND(!_categories.has(index)); - + Category *category = _categories[index]; _categories.erase(index); @@ -124,7 +124,7 @@ PoolVector ProceduralAnimation::get_animation_indices(const int category_in idxr.set(i, E->key()); ++i; } - + return idxr; } int ProceduralAnimation::add_animation(const int category_index) { @@ -235,7 +235,7 @@ PoolVector ProceduralAnimation::get_keyframe_indices(const int category_ind idxr.set(i, E->key()); ++i; } - + return idxr; } int ProceduralAnimation::add_keyframe(const int category_index, const int animation_index) { @@ -415,7 +415,46 @@ void ProceduralAnimation::set_keyframe_node_position(const int category_index, c } void ProceduralAnimation::initialize() { + ERR_FAIL_COND(!_animation.is_valid()); + for (Map *>::Element *E = _animation_data.front(); E; E = E->next()) { + Vector *data = E->get(); + data->clear(); + memdelete(data); + } + + _animation_data.clear(); + + for (Map::Element *E = _categories.front(); E; E = E->next()) { + Category *category = E->get(); + + for (Map::Element *A = category->animations.front(); A; A = A->next()) { + AnimationEntry *anim_entry = A->get(); + + for (Map::Element *K = anim_entry->keyframes.front(); K; K = K->next()) { + int keyframe_index = K->get()->animation_keyframe_index; + + if (!_animation_data.has(keyframe_index)) + load_keyframe_data(keyframe_index); + } + } + } +} + +void ProceduralAnimation::load_keyframe_data(int keyframe_index) { + ERR_FAIL_COND(!_animation.is_valid()); + + Vector *vec = NULL; + + if (_animation_data.has(keyframe_index)) { + //reload data + vec = _animation_data[keyframe_index]; + vec->clear(); + } else { + vec = memnew(Vector); + } + + //_animation->get_ } ProceduralAnimation::ProceduralAnimation() { @@ -427,12 +466,19 @@ ProceduralAnimation::~ProceduralAnimation() { memdelete(E->get()); } + for (Map *>::Element *E = _animation_data.front(); E; E = E->next()) { + Vector *data = E->get(); + data->clear(); + memdelete(data); + } + + _animation_data.clear(); + _categories.clear(); _animation.unref(); } - ProceduralAnimation::TransformAnimationKey ProceduralAnimation::_interpolate(const ProceduralAnimation::TransformAnimationKey &p_a, const ProceduralAnimation::TransformAnimationKey &p_b, float p_c) const { TransformAnimationKey ret; @@ -580,8 +626,8 @@ float ProceduralAnimation::_cubic_interpolate(const float &p_pre_a, const float } template -T ProceduralAnimation::_interpolate(const Vector &p_keys, float p_time, ProceduralAnimation::KeyInterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const { -/* +T ProceduralAnimation::_interpolate(const Vector &p_keys, float p_time, ProceduralAnimation::KeyInterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const { + /* int len = _find(p_keys, length) + 1; // try to find last key (there may be more past the end) if (len <= 0) { @@ -953,7 +999,6 @@ void ProceduralAnimation::_get_property_list(List *p_list) const { p_list->push_back(PropertyInfo(Variant::VECTOR2, "categories/" + itos(E->key()) + "/animation/" + itos(A->key()) + "/position", PROPERTY_HINT_NONE, "", property_usange)); p_list->push_back(PropertyInfo(Variant::INT, "categories/" + itos(E->key()) + "/animation/" + itos(A->key()) + "/start_frame_index", PROPERTY_HINT_NONE, "", property_usange)); - for (Map::Element *K = animation->keyframes.front(); K; K = K->next()) { p_list->push_back(PropertyInfo(Variant::STRING, "categories/" + itos(E->key()) + "/animation/" + itos(A->key()) + "/keyframe/" + itos(K->key()) + "/name", PROPERTY_HINT_NONE, "", property_usange)); p_list->push_back(PropertyInfo(Variant::INT, "categories/" + itos(E->key()) + "/animation/" + itos(A->key()) + "/keyframe/" + itos(K->key()) + "/animation_keyframe_index", PROPERTY_HINT_NONE, "", property_usange)); diff --git a/procedural_animation.h b/procedural_animation.h index 16d4c43..307c017 100644 --- a/procedural_animation.h +++ b/procedural_animation.h @@ -26,9 +26,9 @@ SOFTWARE. #include "core/resource.h" #include "core/map.h" -#include "core/vector.h" -#include "core/pool_vector.h" #include "core/math/vector2.h" +#include "core/pool_vector.h" +#include "core/vector.h" #include "scene/resources/animation.h" #include "scene/resources/curve.h" @@ -37,11 +37,11 @@ SOFTWARE. class ProceduralAnimation : public Resource { GDCLASS(ProceduralAnimation, Resource); -friend class Animation; + friend class Animation; protected: enum AnimationKeyTrackType { - TYPE_VALUE = Animation::TYPE_VALUE, + TYPE_VALUE = Animation::TYPE_VALUE, TYPE_TRANSFORM = Animation::TYPE_TRANSFORM, TYPE_METHOD = Animation::TYPE_METHOD, TYPE_BEZIER = Animation::TYPE_BEZIER, @@ -63,7 +63,7 @@ protected: NodePath path; bool enabled; - AnimationKey() { + AnimationKey() { type = TYPE_NONE; enabled = true; } @@ -72,7 +72,7 @@ protected: struct VariantAnimationKey : public AnimationKey { Variant value; - VariantAnimationKey() { + VariantAnimationKey() { type = TYPE_VALUE; } }; @@ -82,7 +82,8 @@ protected: Quat rot; Vector3 scale; - TransformAnimationKey() : AnimationKey() { + TransformAnimationKey() : + AnimationKey() { type = TYPE_TRANSFORM; } }; @@ -91,7 +92,8 @@ protected: StringName method; Vector params; - MethodAnimationKey() : AnimationKey() { + MethodAnimationKey() : + AnimationKey() { type = TYPE_METHOD; } }; @@ -101,7 +103,8 @@ protected: float start_offset; float end_offset; - AudioAnimationKey() : AnimationKey() { + AudioAnimationKey() : + AnimationKey() { type = TYPE_AUDIO; start_offset = 0; end_offset = 0; @@ -116,7 +119,6 @@ protected: int next_keyframe; Ref in_curve; Vector2 position; - Vector keys; AnimationKeyFrame() { animation_keyframe_index = 0; @@ -126,7 +128,6 @@ protected: ~AnimationKeyFrame() { in_curve.unref(); - keys.clear(); } }; @@ -212,6 +213,7 @@ public: void set_keyframe_node_position(const int category_index, const int animation_index, const int keyframe_index, const Vector2 &value); void initialize(); + void load_keyframe_data(int keyframe_index); ProceduralAnimation(); ~ProceduralAnimation(); @@ -231,7 +233,7 @@ protected: _FORCE_INLINE_ float _cubic_interpolate(const float &p_pre_a, const float &p_a, const float &p_b, const float &p_post_b, float p_c) const; template - _FORCE_INLINE_ T _interpolate(const Vector &p_keys, float p_time, KeyInterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const; + _FORCE_INLINE_ T _interpolate(const Vector &p_keys, float p_time, KeyInterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const; protected: bool _set(const StringName &p_name, const Variant &p_value); @@ -246,6 +248,7 @@ private: String _add_editor_category_animation_name; Map _categories; + Map *> _animation_data; Ref _animation; Map _keyframe_names;