godot/scene/animation/animation_player.h

325 lines
8.9 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* animation_player.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 01:10:30 +00:00
/* */
/* 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. */
/*************************************************************************/
#ifndef ANIMATION_PLAYER_H
#define ANIMATION_PLAYER_H
#include "scene/2d/node_2d.h"
#include "scene/3d/skeleton.h"
#include "scene/3d/spatial.h"
#include "scene/resources/animation.h"
2014-02-10 01:10:30 +00:00
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
#ifdef TOOLS_ENABLED
// To save/restore animated values
class AnimatedValuesBackup {
struct Entry {
Object *object;
Vector<StringName> subpath; // Unused if bone
int bone_idx; // -1 if not a bone
Variant value;
};
Vector<Entry> entries;
friend class AnimationPlayer;
public:
void update_skeletons();
};
#endif
2014-02-10 01:10:30 +00:00
class AnimationPlayer : public Node {
GDCLASS(AnimationPlayer, Node);
2014-02-10 01:10:30 +00:00
OBJ_CATEGORY("Animation Nodes");
public:
enum AnimationProcessMode {
ANIMATION_PROCESS_PHYSICS,
2014-02-10 01:10:30 +00:00
ANIMATION_PROCESS_IDLE,
};
private:
enum {
2016-03-08 23:00:52 +00:00
NODE_CACHE_UPDATE_MAX = 1024,
BLEND_FROM_MAX = 3
2014-02-10 01:10:30 +00:00
};
enum SpecialProperty {
SP_NONE,
SP_NODE2D_POS,
SP_NODE2D_ROT,
SP_NODE2D_SCALE,
};
struct TrackNodeCache {
NodePath path;
2014-02-10 01:10:30 +00:00
uint32_t id;
RES resource;
Node *node;
Spatial *spatial;
Node2D *node_2d;
2014-02-10 01:10:30 +00:00
Skeleton *skeleton;
int bone_idx;
// accumulated transforms
Vector3 loc_accum;
Quat rot_accum;
Vector3 scale_accum;
uint64_t accum_pass;
struct PropertyAnim {
TrackNodeCache *owner;
2014-02-10 01:10:30 +00:00
SpecialProperty special; //small optimization
Vector<StringName> subpath;
2014-02-10 01:10:30 +00:00
Object *object;
Variant value_accum;
uint64_t accum_pass;
PropertyAnim() {
accum_pass = 0;
object = NULL;
}
2014-02-10 01:10:30 +00:00
};
Map<StringName, PropertyAnim> property_anim;
2016-03-08 23:00:52 +00:00
TrackNodeCache() {
skeleton = NULL;
spatial = NULL;
node = NULL;
accum_pass = 0;
bone_idx = -1;
node_2d = NULL;
}
2014-02-10 01:10:30 +00:00
};
struct TrackNodeCacheKey {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
uint32_t id;
int bone_idx;
inline bool operator<(const TrackNodeCacheKey &p_right) const {
2016-03-08 23:00:52 +00:00
if (id < p_right.id)
2014-02-10 01:10:30 +00:00
return true;
else if (id > p_right.id)
2014-02-10 01:10:30 +00:00
return false;
2016-03-08 23:00:52 +00:00
else
return bone_idx < p_right.bone_idx;
2014-02-10 01:10:30 +00:00
}
};
Map<TrackNodeCacheKey, TrackNodeCache> node_cache_map;
2014-02-10 01:10:30 +00:00
TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
2014-02-10 01:10:30 +00:00
int cache_update_size;
TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
2014-02-10 01:10:30 +00:00
int cache_update_prop_size;
Map<Ref<Animation>, int> used_anims;
2014-02-10 01:10:30 +00:00
uint64_t accum_pass;
float speed_scale;
float default_blend_time;
2014-02-10 01:10:30 +00:00
struct AnimationData {
String name;
StringName next;
Vector<TrackNodeCache *> node_cache;
2014-02-10 01:10:30 +00:00
Ref<Animation> animation;
};
Map<StringName, AnimationData> 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); }
2014-02-10 01:10:30 +00:00
};
Map<BlendKey, float> blend_times;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
struct PlaybackData {
2016-03-08 23:00:52 +00:00
AnimationData *from;
2014-02-10 01:10:30 +00:00
float pos;
float speed_scale;
PlaybackData() {
2016-03-08 23:00:52 +00:00
pos = 0;
speed_scale = 1.0;
from = NULL;
2014-02-10 01:10:30 +00:00
}
};
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
struct Blend {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
PlaybackData data;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
float blend_time;
float blend_left;
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
Blend() {
2016-03-08 23:00:52 +00:00
blend_left = 0;
blend_time = 0;
2014-02-10 01:10:30 +00:00
}
};
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
struct Playback {
2016-03-08 23:00:52 +00:00
List<Blend> blend;
2014-02-10 01:10:30 +00:00
PlaybackData current;
StringName assigned;
} playback;
List<StringName> queued;
bool end_notify;
String autoplay;
AnimationProcessMode animation_process_mode;
bool processing;
bool active;
NodePath root;
2016-03-08 23:00:52 +00:00
void _animation_process_animation(AnimationData *p_anim, float p_time, float p_delta, float p_interp, bool p_allow_discrete = true);
2016-03-08 23:00:52 +00:00
void _ensure_node_caches(AnimationData *p_anim);
void _animation_process_data(PlaybackData &cd, float p_delta, float p_blend);
2014-02-10 01:10:30 +00:00
void _animation_process2(float p_delta);
void _animation_update_transforms();
void _animation_process(float p_delta);
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
void _node_removed(Node *p_node);
2016-03-08 23:00:52 +00:00
// bind helpers
PoolVector<String> _get_animation_list() const {
2016-03-08 23:00:52 +00:00
2014-02-10 01:10:30 +00:00
List<StringName> animations;
get_animation_list(&animations);
PoolVector<String> ret;
while (animations.size()) {
2016-03-08 23:00:52 +00:00
ret.push_back(animations.front()->get());
2014-02-10 01:10:30 +00:00
animations.pop_front();
}
return ret;
}
void _animation_changed();
void _ref_anim(const Ref<Animation> &p_anim);
void _unref_anim(const Ref<Animation> &p_anim);
2014-02-10 01:10:30 +00:00
void _set_process(bool p_process, bool p_force = false);
2014-02-10 01:10:30 +00:00
bool playing;
2014-02-10 01:10:30 +00: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;
2014-02-10 01:10:30 +00:00
void _notification(int p_what);
2016-03-08 23:00:52 +00:00
static void _bind_methods();
2014-02-10 01:10:30 +00:00
public:
StringName find_animation(const Ref<Animation> &p_animation) const;
2014-02-10 01:10:30 +00:00
Error add_animation(const StringName &p_name, const Ref<Animation> &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<Animation> get_animation(const StringName &p_name) const;
void get_animation_list(List<StringName> *p_animations) const;
2016-03-08 23:00:52 +00:00
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;
2014-02-10 01:10:30 +00:00
void animation_set_next(const StringName &p_animation, const StringName &p_next);
StringName animation_get_next(const StringName &p_animation) const;
2014-02-10 01:10:30 +00:00
void set_default_blend_time(float p_default);
float get_default_blend_time() const;
2016-03-08 23:00:52 +00:00
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);
2014-02-10 01:10:30 +00:00
void clear_queue();
void stop(bool p_reset = true);
2014-02-10 01:10:30 +00:00
bool is_playing() const;
String get_current_animation() const;
void set_current_animation(const String &p_anim);
2014-02-10 01:10:30 +00:00
void stop_all();
void set_active(bool p_active);
bool is_active() const;
bool is_valid() const;
2016-03-08 23:00:52 +00:00
void set_speed_scale(float p_speed);
float get_speed_scale() const;
2014-02-10 01:10:30 +00:00
void set_autoplay(const String &p_name);
2014-02-10 01:10:30 +00:00
String get_autoplay() const;
void set_animation_process_mode(AnimationProcessMode p_mode);
AnimationProcessMode get_animation_process_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;
2014-02-10 01:10:30 +00:00
float get_current_animation_length() const;
Huge Amount of BugFix -=-=-=-=-=-=-=-=-=-=- -Fixes to Collada Exporter (avoid crash situtions) -Fixed to Collada Importer (Fixed Animation Optimizer Bugs) -Fixes to RigidBody/RigidBody2D body_enter/body_exit, was buggy -Fixed ability for RigidBody/RigidBody2D to get contacts reported and bodyin/out in Kinematic mode. -Added proper trigger support for 3D Physics shapes -Changed proper value for Z-Offset in OmniLight -Fixed spot attenuation bug in SpotLight -Fixed some 3D and 2D spatial soudn bugs related to distance attenuation. -Fixed bugs in EventPlayer (channels were muted by default) -Fix in ButtonGroup (get nodes in group are now returned in order) -Fixed Linear->SRGB Conversion, previous algo sucked, new algo works OK -Changed SRGB->Linear conversion to use hardware if supported, improves texture quality a lot -Fixed options for Y-Fov and X-Fov in camera, should be more intuitive. -Fixed bugs related to viewports and transparency Huge Amount of New Stuff: -=-=-=-=-=-=-=-==-=-=-=- -Ability to manually advance an AnimationPlayer that is inactive (with advance() function) -More work in WinRT platform -Added XY normalmap support, imports on this format by default. Reduces normlmap size and enables much nice compression using LATC -Added Anisotropic filter support to textures, can be specified on import -Added support for Non-Square, Isometric and Hexagonal tilemaps in TileMap. -Added Isometric Dungeon demo. -Added simple hexagonal map demo. -Added Truck-Town demo. Shows how most types of joints and vehicles are used. Please somebody make a nicer town, this one is too hardcore. -Added an Object-Picking API to both RigidBody and Area! (and relevant demo)
2014-10-03 03:10:51 +00:00
void advance(float p_time);
void set_root(const NodePath &p_root);
2014-02-10 01:10:30 +00:00
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<String> *r_options) const;
2016-03-08 23:00:52 +00:00
#ifdef TOOLS_ENABLED
// These may be interesting for games, but are too dangerous for general use
AnimatedValuesBackup backup_animated_values();
void restore_animated_values(const AnimatedValuesBackup &p_backup);
#endif
2016-03-08 23:00:52 +00:00
AnimationPlayer();
2014-02-10 01:10:30 +00:00
~AnimationPlayer();
};
VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessMode);
2014-02-10 01:10:30 +00:00
#endif