1 /*************************************************************************/
2 /*  animated_sprite.h                                                    */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #ifndef ANIMATED_SPRITE_H
31 #define ANIMATED_SPRITE_H
32 
33 #include "scene/2d/node_2d.h"
34 #include "scene/resources/texture.h"
35 
36 class SpriteFrames : public Resource {
37 
38 	OBJ_TYPE(SpriteFrames, Resource);
39 
40 	struct Anim {
41 
42 		float speed;
43 		bool loop;
44 		Vector<Ref<Texture> > frames;
45 
AnimAnim46 		Anim() {
47 			loop = true;
48 			speed = 5;
49 		}
50 	};
51 
52 	Map<StringName, Anim> animations;
53 
54 	Array _get_frames() const;
55 	void _set_frames(const Array &p_frames);
56 
57 	Array _get_animations() const;
58 	void _set_animations(const Array &p_animations);
59 
60 	Vector<String> _get_animation_list() const;
61 
62 protected:
63 	static void _bind_methods();
64 
65 public:
66 	void add_animation(const StringName &p_anim);
67 	bool has_animation(const StringName &p_anim) const;
68 	void remove_animation(const StringName &p_anim);
69 	void rename_animation(const StringName &p_prev, const StringName &p_next);
70 
71 	void get_animation_list(List<StringName> *r_animations) const;
72 
73 	void set_animation_speed(const StringName &p_anim, float p_fps);
74 	float get_animation_speed(const StringName &p_anim) const;
75 
76 	void set_animation_loop(const StringName &p_anim, bool p_loop);
77 	bool get_animation_loop(const StringName &p_anim) const;
78 
79 	void add_frame(const StringName &p_anim, const Ref<Texture> &p_frame, int p_at_pos = -1);
80 	int get_frame_count(const StringName &p_anim) const;
get_frame(const StringName & p_anim,int p_idx)81 	_FORCE_INLINE_ Ref<Texture> get_frame(const StringName &p_anim, int p_idx) const {
82 
83 		const Map<StringName, Anim>::Element *E = animations.find(p_anim);
84 		ERR_FAIL_COND_V(!E, Ref<Texture>());
85 		ERR_FAIL_COND_V(p_idx < 0, Ref<Texture>());
86 		if (p_idx >= E->get().frames.size())
87 			return Ref<Texture>();
88 
89 		return E->get().frames[p_idx];
90 	}
91 
set_frame(const StringName & p_anim,int p_idx,const Ref<Texture> & p_frame)92 	void set_frame(const StringName &p_anim, int p_idx, const Ref<Texture> &p_frame) {
93 		Map<StringName, Anim>::Element *E = animations.find(p_anim);
94 		ERR_FAIL_COND(!E);
95 		ERR_FAIL_COND(p_idx < 0);
96 		if (p_idx >= E->get().frames.size())
97 			return;
98 		E->get().frames[p_idx] = p_frame;
99 	}
100 	void remove_frame(const StringName &p_anim, int p_idx);
101 	void clear(const StringName &p_anim);
102 	void clear_all();
103 
104 	SpriteFrames();
105 };
106 
107 class AnimatedSprite : public Node2D {
108 
109 	OBJ_TYPE(AnimatedSprite, Node2D);
110 
111 	Ref<SpriteFrames> frames;
112 	bool playing;
113 	StringName animation;
114 	int frame;
115 
116 	bool centered;
117 	Point2 offset;
118 
119 	float timeout;
120 
121 	bool hflip;
122 	bool vflip;
123 
124 	Color modulate;
125 
126 	void _res_changed();
127 
128 	void _reset_timeout();
129 	void _set_playing(bool p_playing);
130 	bool _is_playing() const;
131 
132 protected:
133 	static void _bind_methods();
134 	void _notification(int p_what);
135 	virtual void _validate_property(PropertyInfo &property) const;
136 
137 public:
138 	virtual void edit_set_pivot(const Point2 &p_pivot);
139 	virtual Point2 edit_get_pivot() const;
140 	virtual bool edit_has_pivot() const;
141 
142 	void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
143 	Ref<SpriteFrames> get_sprite_frames() const;
144 
145 	void play(const StringName &p_animation = StringName());
146 	void stop();
147 	bool is_playing() const;
148 
149 	void set_animation(const StringName &p_animation);
150 	StringName get_animation() const;
151 
152 	void set_frame(int p_frame);
153 	int get_frame() const;
154 
155 	void set_centered(bool p_center);
156 	bool is_centered() const;
157 
158 	void set_offset(const Point2 &p_offset);
159 	Point2 get_offset() const;
160 
161 	void set_flip_h(bool p_flip);
162 	bool is_flipped_h() const;
163 
164 	void set_flip_v(bool p_flip);
165 	bool is_flipped_v() const;
166 
167 	void set_modulate(const Color &p_color);
168 	Color get_modulate() const;
169 
170 	virtual Rect2 get_item_rect() const;
171 
172 	virtual String get_configuration_warning() const;
173 	AnimatedSprite();
174 };
175 
176 #endif // ANIMATED_SPRITE_H
177