1 /*************************************************************************/
2 /*  sprite_3d.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 SPRITE_3D_H
31 #define SPRITE_3D_H
32 
33 #include "scene/2d/animated_sprite.h"
34 #include "scene/3d/visual_instance.h"
35 
36 class SpriteBase3D : public GeometryInstance {
37 
38 	OBJ_TYPE(SpriteBase3D, GeometryInstance);
39 
40 public:
41 	enum DrawFlags {
42 		FLAG_TRANSPARENT,
43 		FLAG_SHADED,
44 		FLAG_DOUBLE_SIDED,
45 		FLAG_ONTOP,
46 		FLAG_MAX
47 
48 	};
49 
50 	enum AlphaCutMode {
51 		ALPHA_CUT_DISABLED,
52 		ALPHA_CUT_DISCARD,
53 		ALPHA_CUT_OPAQUE_PREPASS
54 	};
55 
56 private:
57 	bool color_dirty;
58 	Color color_accum;
59 
60 	SpriteBase3D *parent_sprite;
61 	List<SpriteBase3D *> children;
62 	List<SpriteBase3D *>::Element *pI;
63 
64 	bool centered;
65 	Point2 offset;
66 
67 	bool hflip;
68 	bool vflip;
69 
70 	Color modulate;
71 	float opacity;
72 
73 	Vector3::Axis axis;
74 	float pixel_size;
75 	AABB aabb;
76 
77 	RID immediate;
78 
79 	bool flags[FLAG_MAX];
80 	AlphaCutMode alpha_cut;
81 	bool pending_update;
82 	void _im_update();
83 
84 	void _propagate_color_changed();
85 
86 protected:
87 	Color _get_color_accum();
88 	void _notification(int p_what);
89 	static void _bind_methods();
90 	virtual void _draw() = 0;
set_aabb(const AABB & p_aabb)91 	_FORCE_INLINE_ void set_aabb(const AABB &p_aabb) { aabb = p_aabb; }
get_immediate()92 	_FORCE_INLINE_ RID &get_immediate() { return immediate; }
93 	void _queue_update();
94 
95 public:
96 	void set_centered(bool p_center);
97 	bool is_centered() const;
98 
99 	void set_offset(const Point2 &p_offset);
100 	Point2 get_offset() const;
101 
102 	void set_flip_h(bool p_flip);
103 	bool is_flipped_h() const;
104 
105 	void set_flip_v(bool p_flip);
106 	bool is_flipped_v() const;
107 
108 	void set_region(bool p_region);
109 	bool is_region() const;
110 
111 	void set_region_rect(const Rect2 &p_region_rect);
112 	Rect2 get_region_rect() const;
113 
114 	void set_modulate(const Color &p_color);
115 	Color get_modulate() const;
116 
117 	void set_opacity(float p_amount);
118 	float get_opacity() const;
119 
120 	void set_pixel_size(float p_amount);
121 	float get_pixel_size() const;
122 
123 	void set_axis(Vector3::Axis p_amount);
124 	Vector3::Axis get_axis() const;
125 
126 	void set_draw_flag(DrawFlags p_flag, bool p_enable);
127 	bool get_draw_flag(DrawFlags p_flag) const;
128 
129 	void set_alpha_cut_mode(AlphaCutMode p_mode);
130 	AlphaCutMode get_alpha_cut_mode() const;
131 
132 	virtual Rect2 get_item_rect() const = 0;
133 
134 	virtual AABB get_aabb() const;
135 	virtual DVector<Face3> get_faces(uint32_t p_usage_flags) const;
136 
137 	SpriteBase3D();
138 	~SpriteBase3D();
139 };
140 
141 class Sprite3D : public SpriteBase3D {
142 
143 	OBJ_TYPE(Sprite3D, SpriteBase3D);
144 	Ref<Texture> texture;
145 
146 	bool region;
147 	Rect2 region_rect;
148 
149 	int frame;
150 
151 	int vframes;
152 	int hframes;
153 
154 protected:
155 	virtual void _draw();
156 	static void _bind_methods();
157 
158 	virtual void _validate_property(PropertyInfo &property) const;
159 
160 public:
161 	void set_texture(const Ref<Texture> &p_texture);
162 	Ref<Texture> get_texture() const;
163 
164 	void set_region(bool p_region);
165 	bool is_region() const;
166 
167 	void set_region_rect(const Rect2 &p_region_rect);
168 	Rect2 get_region_rect() const;
169 
170 	void set_frame(int p_frame);
171 	int get_frame() const;
172 
173 	void set_vframes(int p_amount);
174 	int get_vframes() const;
175 
176 	void set_hframes(int p_amount);
177 	int get_hframes() const;
178 
179 	virtual Rect2 get_item_rect() const;
180 
181 	Sprite3D();
182 	//	~Sprite3D();
183 };
184 
185 #if 0
186 class AnimatedSprite3D : public SpriteBase3D {
187 
188 	OBJ_TYPE(AnimatedSprite3D,SpriteBase3D);
189 	Ref<SpriteFrames> frames;
190 
191 
192 	StringName animation;
193 	int frame;
194 
195 protected:
196 	virtual void _draw();
197 	static void _bind_methods();
198 public:
199 
200 
201 
202 	void set_sprite_frames(const Ref<SpriteFrames>& p_sprite_frames);
203 	Ref<SpriteFrames> get_sprite_frames() const;
204 
205 	void set_frame(int p_frame);
206 	int get_frame() const;
207 
208 
209 	virtual Rect2 get_item_rect() const;
210 
211 	AnimatedSprite3D();
212 //	~AnimatedSprite3D();
213 };
214 #endif
215 
216 class AnimatedSprite3D : public SpriteBase3D {
217 
218 	OBJ_TYPE(AnimatedSprite3D, SpriteBase3D);
219 
220 	Ref<SpriteFrames> frames;
221 	bool playing;
222 	StringName animation;
223 	int frame;
224 
225 	bool centered;
226 	Point2 offset;
227 
228 	float timeout;
229 
230 	bool hflip;
231 	bool vflip;
232 
233 	Color modulate;
234 
235 	void _res_changed();
236 
237 	void _reset_timeout();
238 	void _set_playing(bool p_playing);
239 	bool _is_playing() const;
240 
241 protected:
242 	virtual void _draw();
243 	static void _bind_methods();
244 	void _notification(int p_what);
245 	virtual void _validate_property(PropertyInfo &property) const;
246 
247 public:
248 	void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
249 	Ref<SpriteFrames> get_sprite_frames() const;
250 
251 	void play(const StringName &p_animation = StringName());
252 	void stop();
253 	bool is_playing() const;
254 
255 	void set_animation(const StringName &p_animation);
256 	StringName get_animation() const;
257 
258 	void set_frame(int p_frame);
259 	int get_frame() const;
260 
261 	virtual Rect2 get_item_rect() const;
262 
263 	virtual String get_configuration_warning() const;
264 	AnimatedSprite3D();
265 };
266 
267 VARIANT_ENUM_CAST(SpriteBase3D::DrawFlags);
268 VARIANT_ENUM_CAST(SpriteBase3D::AlphaCutMode);
269 #endif // SPRITE_3D_H
270