| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/resources/texture.h" |
| 34 |
|
| 35 |
static const float SPRITE_FRAME_MINIMUM_DURATION = 0.01; |
| 36 |
|
| 37 |
class SpriteFrames : public Resource { |
| 38 |
GDCLASS(SpriteFrames, Resource); |
| 39 |
|
| 40 |
struct Frame { |
| 41 |
Ref<Texture2D> texture; |
| 42 |
float duration = 1.0; |
| 43 |
}; |
| 44 |
|
| 45 |
struct Anim { |
| 46 |
double speed = 5.0; |
| 47 |
bool loop = true; |
| 48 |
Vector<Frame> frames; |
| 49 |
}; |
| 50 |
|
| 51 |
HashMap<StringName, Anim> animations; |
| 52 |
|
| 53 |
Array _get_animations() const; |
| 54 |
void _set_animations(const Array &p_animations); |
| 55 |
|
| 56 |
protected: |
| 57 |
static void _bind_methods(); |
| 58 |
|
| 59 |
public: |
| 60 |
void add_animation(const StringName &p_anim); |
| 61 |
bool has_animation(const StringName &p_anim) const; |
| 62 |
void duplicate_animation(const StringName &p_from, const StringName &p_to); |
| 63 |
void remove_animation(const StringName &p_anim); |
| 64 |
void rename_animation(const StringName &p_prev, const StringName &p_next); |
| 65 |
|
| 66 |
void get_animation_list(List<StringName> *r_animations) const; |
| 67 |
Vector<String> get_animation_names() const; |
| 68 |
|
| 69 |
void set_animation_speed(const StringName &p_anim, double p_fps); |
| 70 |
double get_animation_speed(const StringName &p_anim) const; |
| 71 |
|
| 72 |
void set_animation_loop(const StringName &p_anim, bool p_loop); |
| 73 |
bool get_animation_loop(const StringName &p_anim) const; |
| 74 |
|
| 75 |
void add_frame(const StringName &p_anim, const Ref<Texture2D> &p_texture, float p_duration = 1.0, int p_at_pos = -1); |
| 76 |
void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture2D> &p_texture, float p_duration = 1.0); |
| 77 |
void remove_frame(const StringName &p_anim, int p_idx); |
| 78 |
|
| 79 |
int get_frame_count(const StringName &p_anim) const; |
| 80 |
|
| 81 |
_FORCE_INLINE_ Ref<Texture2D> get_frame_texture(const StringName &p_anim, int p_idx) const { |
| 82 |
HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim); |
| 83 |
ERR_FAIL_COND_V_MSG(!E, Ref<Texture2D>(), "Animation '" + String(p_anim) + "' doesn't exist."); |
| 84 |
ERR_FAIL_COND_V(p_idx < 0, Ref<Texture2D>()); |
| 85 |
if (p_idx >= E->value.frames.size()) { |
| 86 |
return Ref<Texture2D>(); |
| 87 |
} |
| 88 |
|
| 89 |
return E->value.frames[p_idx].texture; |
| 90 |
} |
| 91 |
|
| 92 |
_FORCE_INLINE_ float get_frame_duration(const StringName &p_anim, int p_idx) const { |
| 93 |
HashMap<StringName, Anim>::ConstIterator E = animations.find(p_anim); |
| 94 |
ERR_FAIL_COND_V_MSG(!E, 1.0, "Animation '" + String(p_anim) + "' doesn't exist."); |
| 95 |
ERR_FAIL_COND_V(p_idx < 0, 1.0); |
| 96 |
if (p_idx >= E->value.frames.size()) { |
| 97 |
return 1.0; |
| 98 |
} |
| 99 |
|
| 100 |
return E->value.frames[p_idx].duration; |
| 101 |
} |
| 102 |
|
| 103 |
void clear(const StringName &p_anim); |
| 104 |
void clear_all(); |
| 105 |
|
| 106 |
#ifdef TOOLS_ENABLED |
| 107 |
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override; |
| 108 |
#endif |
| 109 |
|
| 110 |
SpriteFrames(); |
| 111 |
}; |