1 | /**************************************************************************/ |
2 | /* sprite_2d.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
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 | |
31 | #include "sprite_2d.h" |
32 | |
33 | #include "scene/main/window.h" |
34 | #include "scene/scene_string_names.h" |
35 | |
36 | #ifdef TOOLS_ENABLED |
37 | Dictionary Sprite2D::_edit_get_state() const { |
38 | Dictionary state = Node2D::_edit_get_state(); |
39 | state["offset" ] = offset; |
40 | return state; |
41 | } |
42 | |
43 | void Sprite2D::_edit_set_state(const Dictionary &p_state) { |
44 | Node2D::_edit_set_state(p_state); |
45 | set_offset(p_state["offset" ]); |
46 | } |
47 | |
48 | void Sprite2D::_edit_set_pivot(const Point2 &p_pivot) { |
49 | set_offset(get_offset() - p_pivot); |
50 | set_position(get_transform().xform(p_pivot)); |
51 | } |
52 | |
53 | Point2 Sprite2D::_edit_get_pivot() const { |
54 | return Vector2(); |
55 | } |
56 | |
57 | bool Sprite2D::_edit_use_pivot() const { |
58 | return true; |
59 | } |
60 | |
61 | bool Sprite2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
62 | return is_pixel_opaque(p_point); |
63 | } |
64 | |
65 | Rect2 Sprite2D::_edit_get_rect() const { |
66 | return get_rect(); |
67 | } |
68 | |
69 | bool Sprite2D::_edit_use_rect() const { |
70 | return texture.is_valid(); |
71 | } |
72 | #endif |
73 | |
74 | Rect2 Sprite2D::get_anchorable_rect() const { |
75 | return get_rect(); |
76 | } |
77 | |
78 | void Sprite2D::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_clip_enabled) const { |
79 | Rect2 base_rect; |
80 | |
81 | if (region_enabled) { |
82 | r_filter_clip_enabled = region_filter_clip_enabled; |
83 | base_rect = region_rect; |
84 | } else { |
85 | r_filter_clip_enabled = false; |
86 | base_rect = Rect2(0, 0, texture->get_width(), texture->get_height()); |
87 | } |
88 | |
89 | Size2 frame_size = base_rect.size / Size2(hframes, vframes); |
90 | Point2 frame_offset = Point2(frame % hframes, frame / hframes); |
91 | frame_offset *= frame_size; |
92 | |
93 | r_src_rect.size = frame_size; |
94 | r_src_rect.position = base_rect.position + frame_offset; |
95 | |
96 | Point2 dest_offset = offset; |
97 | if (centered) { |
98 | dest_offset -= frame_size / 2; |
99 | } |
100 | |
101 | if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) { |
102 | dest_offset = dest_offset.floor(); |
103 | } |
104 | |
105 | r_dst_rect = Rect2(dest_offset, frame_size); |
106 | |
107 | if (hflip) { |
108 | r_dst_rect.size.x = -r_dst_rect.size.x; |
109 | } |
110 | if (vflip) { |
111 | r_dst_rect.size.y = -r_dst_rect.size.y; |
112 | } |
113 | } |
114 | |
115 | void Sprite2D::_notification(int p_what) { |
116 | switch (p_what) { |
117 | case NOTIFICATION_DRAW: { |
118 | if (texture.is_null()) { |
119 | return; |
120 | } |
121 | |
122 | RID ci = get_canvas_item(); |
123 | |
124 | Rect2 src_rect, dst_rect; |
125 | bool filter_clip_enabled; |
126 | _get_rects(src_rect, dst_rect, filter_clip_enabled); |
127 | |
128 | texture->draw_rect_region(ci, dst_rect, src_rect, Color(1, 1, 1), false, filter_clip_enabled); |
129 | } break; |
130 | } |
131 | } |
132 | |
133 | void Sprite2D::set_texture(const Ref<Texture2D> &p_texture) { |
134 | if (p_texture == texture) { |
135 | return; |
136 | } |
137 | |
138 | if (texture.is_valid()) { |
139 | texture->disconnect_changed(callable_mp(this, &Sprite2D::_texture_changed)); |
140 | } |
141 | |
142 | texture = p_texture; |
143 | |
144 | if (texture.is_valid()) { |
145 | texture->connect_changed(callable_mp(this, &Sprite2D::_texture_changed)); |
146 | } |
147 | |
148 | queue_redraw(); |
149 | emit_signal(SceneStringNames::get_singleton()->texture_changed); |
150 | item_rect_changed(); |
151 | } |
152 | |
153 | Ref<Texture2D> Sprite2D::get_texture() const { |
154 | return texture; |
155 | } |
156 | |
157 | void Sprite2D::set_centered(bool p_center) { |
158 | centered = p_center; |
159 | queue_redraw(); |
160 | item_rect_changed(); |
161 | } |
162 | |
163 | bool Sprite2D::is_centered() const { |
164 | return centered; |
165 | } |
166 | |
167 | void Sprite2D::set_offset(const Point2 &p_offset) { |
168 | offset = p_offset; |
169 | queue_redraw(); |
170 | item_rect_changed(); |
171 | } |
172 | |
173 | Point2 Sprite2D::get_offset() const { |
174 | return offset; |
175 | } |
176 | |
177 | void Sprite2D::set_flip_h(bool p_flip) { |
178 | hflip = p_flip; |
179 | queue_redraw(); |
180 | } |
181 | |
182 | bool Sprite2D::is_flipped_h() const { |
183 | return hflip; |
184 | } |
185 | |
186 | void Sprite2D::set_flip_v(bool p_flip) { |
187 | vflip = p_flip; |
188 | queue_redraw(); |
189 | } |
190 | |
191 | bool Sprite2D::is_flipped_v() const { |
192 | return vflip; |
193 | } |
194 | |
195 | void Sprite2D::set_region_enabled(bool p_region_enabled) { |
196 | if (p_region_enabled == region_enabled) { |
197 | return; |
198 | } |
199 | |
200 | region_enabled = p_region_enabled; |
201 | queue_redraw(); |
202 | notify_property_list_changed(); |
203 | } |
204 | |
205 | bool Sprite2D::is_region_enabled() const { |
206 | return region_enabled; |
207 | } |
208 | |
209 | void Sprite2D::set_region_rect(const Rect2 &p_region_rect) { |
210 | if (region_rect == p_region_rect) { |
211 | return; |
212 | } |
213 | |
214 | region_rect = p_region_rect; |
215 | |
216 | if (region_enabled) { |
217 | item_rect_changed(); |
218 | } |
219 | } |
220 | |
221 | Rect2 Sprite2D::get_region_rect() const { |
222 | return region_rect; |
223 | } |
224 | |
225 | void Sprite2D::set_region_filter_clip_enabled(bool p_region_filter_clip_enabled) { |
226 | region_filter_clip_enabled = p_region_filter_clip_enabled; |
227 | queue_redraw(); |
228 | } |
229 | |
230 | bool Sprite2D::is_region_filter_clip_enabled() const { |
231 | return region_filter_clip_enabled; |
232 | } |
233 | |
234 | void Sprite2D::set_frame(int p_frame) { |
235 | ERR_FAIL_INDEX(p_frame, vframes * hframes); |
236 | |
237 | if (frame != p_frame) { |
238 | item_rect_changed(); |
239 | } |
240 | |
241 | frame = p_frame; |
242 | |
243 | emit_signal(SceneStringNames::get_singleton()->frame_changed); |
244 | } |
245 | |
246 | int Sprite2D::get_frame() const { |
247 | return frame; |
248 | } |
249 | |
250 | void Sprite2D::set_frame_coords(const Vector2i &p_coord) { |
251 | ERR_FAIL_INDEX(p_coord.x, hframes); |
252 | ERR_FAIL_INDEX(p_coord.y, vframes); |
253 | |
254 | set_frame(p_coord.y * hframes + p_coord.x); |
255 | } |
256 | |
257 | Vector2i Sprite2D::get_frame_coords() const { |
258 | return Vector2i(frame % hframes, frame / hframes); |
259 | } |
260 | |
261 | void Sprite2D::set_vframes(int p_amount) { |
262 | ERR_FAIL_COND_MSG(p_amount < 1, "Amount of vframes cannot be smaller than 1." ); |
263 | vframes = p_amount; |
264 | queue_redraw(); |
265 | item_rect_changed(); |
266 | notify_property_list_changed(); |
267 | } |
268 | |
269 | int Sprite2D::get_vframes() const { |
270 | return vframes; |
271 | } |
272 | |
273 | void Sprite2D::set_hframes(int p_amount) { |
274 | ERR_FAIL_COND_MSG(p_amount < 1, "Amount of hframes cannot be smaller than 1." ); |
275 | hframes = p_amount; |
276 | queue_redraw(); |
277 | item_rect_changed(); |
278 | notify_property_list_changed(); |
279 | } |
280 | |
281 | int Sprite2D::get_hframes() const { |
282 | return hframes; |
283 | } |
284 | |
285 | bool Sprite2D::is_pixel_opaque(const Point2 &p_point) const { |
286 | if (texture.is_null()) { |
287 | return false; |
288 | } |
289 | |
290 | if (texture->get_size().width == 0 || texture->get_size().height == 0) { |
291 | return false; |
292 | } |
293 | |
294 | Rect2 src_rect, dst_rect; |
295 | bool filter_clip_enabled; |
296 | _get_rects(src_rect, dst_rect, filter_clip_enabled); |
297 | dst_rect.size = dst_rect.size.abs(); |
298 | |
299 | if (!dst_rect.has_point(p_point)) { |
300 | return false; |
301 | } |
302 | |
303 | Vector2 q = (p_point - dst_rect.position) / dst_rect.size; |
304 | if (hflip) { |
305 | q.x = 1.0f - q.x; |
306 | } |
307 | if (vflip) { |
308 | q.y = 1.0f - q.y; |
309 | } |
310 | q = q * src_rect.size + src_rect.position; |
311 | TextureRepeat repeat_mode = get_texture_repeat_in_tree(); |
312 | bool is_repeat = repeat_mode == TEXTURE_REPEAT_ENABLED || repeat_mode == TEXTURE_REPEAT_MIRROR; |
313 | bool is_mirrored_repeat = repeat_mode == TEXTURE_REPEAT_MIRROR; |
314 | if (is_repeat) { |
315 | int mirror_x = 0; |
316 | int mirror_y = 0; |
317 | if (is_mirrored_repeat) { |
318 | mirror_x = (int)(q.x / texture->get_size().width); |
319 | mirror_y = (int)(q.y / texture->get_size().height); |
320 | } |
321 | q.x = Math::fmod(q.x, texture->get_size().width); |
322 | q.y = Math::fmod(q.y, texture->get_size().height); |
323 | if (mirror_x % 2 == 1) { |
324 | q.x = texture->get_size().width - q.x - 1; |
325 | } |
326 | if (mirror_y % 2 == 1) { |
327 | q.y = texture->get_size().height - q.y - 1; |
328 | } |
329 | } else { |
330 | q.x = MIN(q.x, texture->get_size().width - 1); |
331 | q.y = MIN(q.y, texture->get_size().height - 1); |
332 | } |
333 | |
334 | return texture->is_pixel_opaque((int)q.x, (int)q.y); |
335 | } |
336 | |
337 | Rect2 Sprite2D::get_rect() const { |
338 | if (texture.is_null()) { |
339 | return Rect2(0, 0, 1, 1); |
340 | } |
341 | |
342 | Size2i s; |
343 | |
344 | if (region_enabled) { |
345 | s = region_rect.size; |
346 | } else { |
347 | s = texture->get_size(); |
348 | } |
349 | |
350 | s = s / Point2(hframes, vframes); |
351 | |
352 | Point2 ofs = offset; |
353 | if (centered) { |
354 | ofs -= Size2(s) / 2; |
355 | } |
356 | |
357 | if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) { |
358 | ofs = ofs.floor(); |
359 | } |
360 | |
361 | if (s == Size2(0, 0)) { |
362 | s = Size2(1, 1); |
363 | } |
364 | |
365 | return Rect2(ofs, s); |
366 | } |
367 | |
368 | void Sprite2D::_validate_property(PropertyInfo &p_property) const { |
369 | if (p_property.name == "frame" ) { |
370 | p_property.hint = PROPERTY_HINT_RANGE; |
371 | p_property.hint_string = "0," + itos(vframes * hframes - 1) + ",1" ; |
372 | p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; |
373 | } |
374 | |
375 | if (p_property.name == "frame_coords" ) { |
376 | p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; |
377 | } |
378 | |
379 | if (!region_enabled && (p_property.name == "region_rect" || p_property.name == "region_filter_clip" )) { |
380 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
381 | } |
382 | } |
383 | |
384 | void Sprite2D::_texture_changed() { |
385 | // Changes to the texture need to trigger an update to make |
386 | // the editor redraw the sprite with the updated texture. |
387 | if (texture.is_valid()) { |
388 | queue_redraw(); |
389 | } |
390 | } |
391 | |
392 | void Sprite2D::_bind_methods() { |
393 | ClassDB::bind_method(D_METHOD("set_texture" , "texture" ), &Sprite2D::set_texture); |
394 | ClassDB::bind_method(D_METHOD("get_texture" ), &Sprite2D::get_texture); |
395 | |
396 | ClassDB::bind_method(D_METHOD("set_centered" , "centered" ), &Sprite2D::set_centered); |
397 | ClassDB::bind_method(D_METHOD("is_centered" ), &Sprite2D::is_centered); |
398 | |
399 | ClassDB::bind_method(D_METHOD("set_offset" , "offset" ), &Sprite2D::set_offset); |
400 | ClassDB::bind_method(D_METHOD("get_offset" ), &Sprite2D::get_offset); |
401 | |
402 | ClassDB::bind_method(D_METHOD("set_flip_h" , "flip_h" ), &Sprite2D::set_flip_h); |
403 | ClassDB::bind_method(D_METHOD("is_flipped_h" ), &Sprite2D::is_flipped_h); |
404 | |
405 | ClassDB::bind_method(D_METHOD("set_flip_v" , "flip_v" ), &Sprite2D::set_flip_v); |
406 | ClassDB::bind_method(D_METHOD("is_flipped_v" ), &Sprite2D::is_flipped_v); |
407 | |
408 | ClassDB::bind_method(D_METHOD("set_region_enabled" , "enabled" ), &Sprite2D::set_region_enabled); |
409 | ClassDB::bind_method(D_METHOD("is_region_enabled" ), &Sprite2D::is_region_enabled); |
410 | |
411 | ClassDB::bind_method(D_METHOD("is_pixel_opaque" , "pos" ), &Sprite2D::is_pixel_opaque); |
412 | |
413 | ClassDB::bind_method(D_METHOD("set_region_rect" , "rect" ), &Sprite2D::set_region_rect); |
414 | ClassDB::bind_method(D_METHOD("get_region_rect" ), &Sprite2D::get_region_rect); |
415 | |
416 | ClassDB::bind_method(D_METHOD("set_region_filter_clip_enabled" , "enabled" ), &Sprite2D::set_region_filter_clip_enabled); |
417 | ClassDB::bind_method(D_METHOD("is_region_filter_clip_enabled" ), &Sprite2D::is_region_filter_clip_enabled); |
418 | |
419 | ClassDB::bind_method(D_METHOD("set_frame" , "frame" ), &Sprite2D::set_frame); |
420 | ClassDB::bind_method(D_METHOD("get_frame" ), &Sprite2D::get_frame); |
421 | |
422 | ClassDB::bind_method(D_METHOD("set_frame_coords" , "coords" ), &Sprite2D::set_frame_coords); |
423 | ClassDB::bind_method(D_METHOD("get_frame_coords" ), &Sprite2D::get_frame_coords); |
424 | |
425 | ClassDB::bind_method(D_METHOD("set_vframes" , "vframes" ), &Sprite2D::set_vframes); |
426 | ClassDB::bind_method(D_METHOD("get_vframes" ), &Sprite2D::get_vframes); |
427 | |
428 | ClassDB::bind_method(D_METHOD("set_hframes" , "hframes" ), &Sprite2D::set_hframes); |
429 | ClassDB::bind_method(D_METHOD("get_hframes" ), &Sprite2D::get_hframes); |
430 | |
431 | ClassDB::bind_method(D_METHOD("get_rect" ), &Sprite2D::get_rect); |
432 | |
433 | ADD_SIGNAL(MethodInfo("frame_changed" )); |
434 | ADD_SIGNAL(MethodInfo("texture_changed" )); |
435 | |
436 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture" , PROPERTY_HINT_RESOURCE_TYPE, "Texture2D" ), "set_texture" , "get_texture" ); |
437 | ADD_GROUP("Offset" , "" ); |
438 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered" ), "set_centered" , "is_centered" ); |
439 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset" , PROPERTY_HINT_NONE, "suffix:px" ), "set_offset" , "get_offset" ); |
440 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h" ), "set_flip_h" , "is_flipped_h" ); |
441 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v" ), "set_flip_v" , "is_flipped_v" ); |
442 | ADD_GROUP("Animation" , "" ); |
443 | ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes" , PROPERTY_HINT_RANGE, "1,16384,1" ), "set_hframes" , "get_hframes" ); |
444 | ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes" , PROPERTY_HINT_RANGE, "1,16384,1" ), "set_vframes" , "get_vframes" ); |
445 | ADD_PROPERTY(PropertyInfo(Variant::INT, "frame" ), "set_frame" , "get_frame" ); |
446 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "frame_coords" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_EDITOR), "set_frame_coords" , "get_frame_coords" ); |
447 | |
448 | ADD_GROUP("Region" , "region_" ); |
449 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled" ), "set_region_enabled" , "is_region_enabled" ); |
450 | ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect" ), "set_region_rect" , "get_region_rect" ); |
451 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_filter_clip_enabled" ), "set_region_filter_clip_enabled" , "is_region_filter_clip_enabled" ); |
452 | } |
453 | |
454 | Sprite2D::Sprite2D() { |
455 | } |
456 | |
457 | Sprite2D::~Sprite2D() { |
458 | } |
459 | |