2020-01-31 19:39:58 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2020 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-01-24 15:29:39 +01:00
|
|
|
#ifndef PROCEDURAL_ANIMATION_H
|
|
|
|
#define PROCEDURAL_ANIMATION_H
|
|
|
|
|
|
|
|
#include "core/resource.h"
|
|
|
|
|
2020-01-27 03:04:53 +01:00
|
|
|
#include "core/map.h"
|
|
|
|
#include "core/math/vector2.h"
|
2020-02-27 14:54:52 +01:00
|
|
|
#include "core/pool_vector.h"
|
|
|
|
#include "core/vector.h"
|
2020-01-24 15:29:39 +01:00
|
|
|
#include "scene/resources/animation.h"
|
|
|
|
#include "scene/resources/curve.h"
|
|
|
|
|
|
|
|
#include "scene/resources/animation.h"
|
|
|
|
|
|
|
|
class ProceduralAnimation : public Resource {
|
|
|
|
GDCLASS(ProceduralAnimation, Resource);
|
|
|
|
|
2020-02-27 14:54:52 +01:00
|
|
|
friend class Animation;
|
2020-01-28 22:30:58 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
protected:
|
|
|
|
enum AnimationKeyTrackType {
|
2020-02-27 14:54:52 +01:00
|
|
|
TYPE_VALUE = Animation::TYPE_VALUE,
|
2020-02-10 18:38:41 +01:00
|
|
|
TYPE_TRANSFORM = Animation::TYPE_TRANSFORM,
|
|
|
|
TYPE_METHOD = Animation::TYPE_METHOD,
|
|
|
|
TYPE_BEZIER = Animation::TYPE_BEZIER,
|
|
|
|
TYPE_AUDIO = Animation::TYPE_AUDIO,
|
|
|
|
TYPE_ANIMATION = Animation::TYPE_ANIMATION,
|
|
|
|
TYPE_NONE,
|
|
|
|
};
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
enum KeyInterpolationType {
|
|
|
|
INTERPOLATION_NEAREST = Animation::INTERPOLATION_NEAREST,
|
|
|
|
INTERPOLATION_LINEAR = Animation::INTERPOLATION_LINEAR,
|
|
|
|
INTERPOLATION_CUBIC = Animation::INTERPOLATION_CUBIC,
|
|
|
|
INTERPOLATION_CURVE,
|
|
|
|
};
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
/* Key data */
|
|
|
|
struct AnimationKey {
|
|
|
|
AnimationKeyTrackType type;
|
|
|
|
NodePath path;
|
|
|
|
bool enabled;
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-02-27 14:54:52 +01:00
|
|
|
AnimationKey() {
|
2020-02-10 18:38:41 +01:00
|
|
|
type = TYPE_NONE;
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
};
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
struct VariantAnimationKey : public AnimationKey {
|
|
|
|
Variant value;
|
2020-01-27 03:04:53 +01:00
|
|
|
|
2020-02-27 14:54:52 +01:00
|
|
|
VariantAnimationKey() {
|
2020-02-10 18:38:41 +01:00
|
|
|
type = TYPE_VALUE;
|
|
|
|
}
|
|
|
|
};
|
2020-01-27 03:04:53 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
struct TransformAnimationKey : public AnimationKey {
|
|
|
|
Vector3 loc;
|
|
|
|
Quat rot;
|
|
|
|
Vector3 scale;
|
2020-01-27 03:04:53 +01:00
|
|
|
|
2020-02-27 14:54:52 +01:00
|
|
|
TransformAnimationKey() :
|
|
|
|
AnimationKey() {
|
2020-02-10 18:38:41 +01:00
|
|
|
type = TYPE_TRANSFORM;
|
|
|
|
}
|
|
|
|
};
|
2020-01-27 03:04:53 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
struct MethodAnimationKey : public AnimationKey {
|
|
|
|
StringName method;
|
|
|
|
Vector<Variant> params;
|
2020-01-27 03:04:53 +01:00
|
|
|
|
2020-02-27 14:54:52 +01:00
|
|
|
MethodAnimationKey() :
|
|
|
|
AnimationKey() {
|
2020-02-10 18:38:41 +01:00
|
|
|
type = TYPE_METHOD;
|
|
|
|
}
|
|
|
|
};
|
2020-01-27 03:04:53 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
struct AudioAnimationKey : public AnimationKey {
|
|
|
|
RES stream;
|
|
|
|
float start_offset;
|
|
|
|
float end_offset;
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-02-27 14:54:52 +01:00
|
|
|
AudioAnimationKey() :
|
|
|
|
AnimationKey() {
|
2020-02-10 18:38:41 +01:00
|
|
|
type = TYPE_AUDIO;
|
|
|
|
start_offset = 0;
|
|
|
|
end_offset = 0;
|
|
|
|
}
|
|
|
|
};
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
/* Animation data */
|
2020-01-24 15:29:39 +01:00
|
|
|
|
2020-01-27 03:04:53 +01:00
|
|
|
struct AnimationKeyFrame {
|
|
|
|
String name;
|
|
|
|
int animation_keyframe_index;
|
|
|
|
int next_keyframe;
|
2020-01-24 15:29:39 +01:00
|
|
|
Ref<Curve> in_curve;
|
2020-01-27 03:04:53 +01:00
|
|
|
Vector2 position;
|
|
|
|
|
|
|
|
AnimationKeyFrame() {
|
|
|
|
animation_keyframe_index = 0;
|
|
|
|
next_keyframe = -1;
|
2020-01-28 22:30:58 +01:00
|
|
|
in_curve.instance();
|
2020-01-27 03:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~AnimationKeyFrame() {
|
|
|
|
in_curve.unref();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AnimationEntry {
|
|
|
|
String name;
|
|
|
|
Vector2 position;
|
|
|
|
int start_frame_index;
|
|
|
|
Map<int, AnimationKeyFrame *> keyframes;
|
2020-01-24 15:29:39 +01:00
|
|
|
|
|
|
|
AnimationEntry() {
|
2020-01-27 03:04:53 +01:00
|
|
|
start_frame_index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AnimationEntry() {
|
|
|
|
for (Map<int, AnimationKeyFrame *>::Element *E = keyframes.front(); E; E = E->next())
|
|
|
|
memdelete(E->get());
|
|
|
|
|
|
|
|
keyframes.clear();
|
2020-01-24 15:29:39 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
public:
|
|
|
|
Ref<Animation> get_animation() const;
|
|
|
|
void set_animation(const Ref<Animation> &value);
|
|
|
|
|
2020-02-28 07:44:22 +01:00
|
|
|
int get_animation_fps() const;
|
|
|
|
void set_animation_fps(const int index);
|
|
|
|
|
2020-02-10 18:38:41 +01:00
|
|
|
String get_animation_keyframe_name(int keyframe_index) const;
|
|
|
|
void set_animation_keyframe_name(int keyframe_index, const String &value);
|
|
|
|
void remove_animation_keyframe_name(int keyframe_index);
|
|
|
|
PoolVector<String> get_animation_keyframe_names() const;
|
|
|
|
|
|
|
|
//Animations
|
2020-03-25 15:05:10 +01:00
|
|
|
PoolVector<int> get_animation_indices() const;
|
|
|
|
int add_animation();
|
|
|
|
void remove_animation(const int animation_index);
|
|
|
|
bool has_animation(const int animation_index) const;
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
String get_animation_name(const int animation_index) const;
|
|
|
|
void set_animation_name(const int animation_index, const String &value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
Vector2 get_animation_node_position(int animation_index) const;
|
|
|
|
void set_animation_node_position(const int animation_index, const Vector2 &value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
int get_animation_start_frame_index(const int animation_index) const;
|
|
|
|
void set_animation_start_frame_index(const int animation_index, const int value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
|
|
|
//Keyframes
|
2020-03-25 15:05:10 +01:00
|
|
|
PoolVector<int> get_keyframe_indices(const int animation_index) const;
|
|
|
|
int add_keyframe(const int animation_index);
|
|
|
|
void remove_keyframe(const int animation_index, const int keyframe_index);
|
|
|
|
bool has_keyframe(const int animation_index, const int keyframe_index) const;
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
String get_keyframe_name(const int animation_index, const int keyframe_index) const;
|
|
|
|
void set_keyframe_name(const int animation_index, const int keyframe_index, const String &value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
int get_keyframe_animation_keyframe_index(int animation_index, const int keyframe_index) const;
|
|
|
|
void set_keyframe_animation_keyframe_index(int animation_index, const int keyframe_index, int value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
int get_keyframe_next_keyframe_index(const int animation_index, const int keyframe_index) const;
|
|
|
|
void set_keyframe_next_keyframe_index(const int animation_index, const int keyframe_index, const int value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
Ref<Curve> get_keyframe_in_curve(const int animation_index, const int keyframe_index) const;
|
|
|
|
void set_keyframe_in_curve(const int animation_index, const int keyframe_index, const Ref<Curve> &value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
Vector2 get_keyframe_node_position(int animation_index, const int keyframe_index) const;
|
|
|
|
void set_keyframe_node_position(const int animation_index, const int keyframe_index, const Vector2 &value);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
|
|
|
void initialize();
|
2020-02-27 14:54:52 +01:00
|
|
|
void load_keyframe_data(int keyframe_index);
|
2020-02-10 18:38:41 +01:00
|
|
|
|
|
|
|
ProceduralAnimation();
|
|
|
|
~ProceduralAnimation();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
_FORCE_INLINE_ TransformAnimationKey _interpolate(const TransformAnimationKey &p_a, const TransformAnimationKey &p_b, float p_c) const;
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const;
|
|
|
|
_FORCE_INLINE_ Quat _interpolate(const Quat &p_a, const Quat &p_b, float p_c) const;
|
|
|
|
_FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, float p_c) const;
|
|
|
|
_FORCE_INLINE_ float _interpolate(const float &p_a, const float &p_b, float p_c) const;
|
|
|
|
|
|
|
|
_FORCE_INLINE_ TransformAnimationKey _cubic_interpolate(const TransformAnimationKey &p_pre_a, const TransformAnimationKey &p_a, const TransformAnimationKey &p_b, const TransformAnimationKey &p_post_b, float p_c) const;
|
|
|
|
_FORCE_INLINE_ Vector3 _cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, float p_c) const;
|
|
|
|
_FORCE_INLINE_ Quat _cubic_interpolate(const Quat &p_pre_a, const Quat &p_a, const Quat &p_b, const Quat &p_post_b, float p_c) const;
|
|
|
|
_FORCE_INLINE_ Variant _cubic_interpolate(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, float p_c) const;
|
|
|
|
_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 <class T>
|
2020-02-27 14:54:52 +01:00
|
|
|
_FORCE_INLINE_ T _interpolate(const Vector<AnimationKey> &p_keys, float p_time, KeyInterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const;
|
2020-02-10 18:38:41 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool _set(const StringName &p_name, const Variant &p_value);
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const;
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const;
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2020-01-24 15:29:39 +01:00
|
|
|
private:
|
2020-02-10 18:38:41 +01:00
|
|
|
bool _initialized;
|
2020-02-28 07:44:22 +01:00
|
|
|
int _animation_fps;
|
2020-02-10 18:38:41 +01:00
|
|
|
|
2020-01-24 15:29:39 +01:00
|
|
|
String _editor_add_category_name;
|
|
|
|
String _add_editor_category_animation_name;
|
|
|
|
|
2020-03-25 15:05:10 +01:00
|
|
|
Map<int, AnimationEntry *> _animations;
|
2020-02-27 14:54:52 +01:00
|
|
|
Map<int, Vector<AnimationKey> *> _animation_data;
|
2020-01-24 15:29:39 +01:00
|
|
|
|
|
|
|
Ref<Animation> _animation;
|
2020-01-27 03:04:53 +01:00
|
|
|
Map<int, String> _keyframe_names;
|
2020-01-24 15:29:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|