Debian Codegraph @

scene/2d/sprite_2d.cpp

raw symbols SCIP
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 "core/input/input.h"
34 #include "scene/main/viewport.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 state["region_rect"] = region_rect;
41 return state;
42 }
43
44 void Sprite2D::_edit_set_state(const Dictionary &p_state) {
45 Node2D::_edit_set_state(p_state);
46 set_offset(p_state["offset"]);
47 set_region_rect(p_state["region_rect"]);
48 }
49
50 void Sprite2D::_edit_set_pivot(const Point2 &p_pivot) {
51 set_offset(get_offset() - p_pivot);
52 set_position(get_transform().xform(p_pivot));
53 }
54
55 Point2 Sprite2D::_edit_get_pivot() const {
56 return Vector2();
57 }
58
59 bool Sprite2D::_edit_use_pivot() const {
60 return true;
61 }
62
63 void Sprite2D::_edit_set_rect(const Rect2 &p_rect) {
64 if (texture.is_null()) {
65 return;
66 }
67 if (!region_enabled || hframes > 1 || vframes > 1 || !dragging_to_resize_rect) {
68 Node2D::_edit_set_rect(p_rect);
69 return;
70 }
71 Point2 delta = p_rect.position - (centered ? _get_rect_offset(p_rect.size) : Vector2());
72 set_region_rect(Rect2(region_rect.position, p_rect.size));
73 set_position(get_position() + get_transform().basis_xform(delta));
74 }
75 #endif // TOOLS_ENABLED
76
77 #ifdef DEBUG_ENABLED
78 bool Sprite2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
79 return is_pixel_opaque(p_point);
80 }
81
82 Rect2 Sprite2D::_edit_get_rect() const {
83 return get_rect();
84 }
85
86 bool Sprite2D::_edit_use_rect() const {
87 return texture.is_valid();
88 }
89 #endif // DEBUG_ENABLED
90
91 Rect2 Sprite2D::get_anchorable_rect() const {
92 return get_rect();
93 }
94
95 void Sprite2D::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_clip_enabled) const {
96 Rect2 base_rect;
97
98 if (region_enabled) {
99 r_filter_clip_enabled = region_filter_clip_enabled;
100 base_rect = region_rect;
101 } else {
102 r_filter_clip_enabled = false;
103 base_rect = Rect2(0, 0, texture->get_width(), texture->get_height());
104 }
105
106 Size2 frame_size = base_rect.size / Size2(hframes, vframes);
107 Point2 frame_offset = Point2(frame % hframes, frame / hframes);
108 frame_offset *= frame_size;
109
110 r_src_rect.size = frame_size;
111 r_src_rect.position = base_rect.position + frame_offset;
112
113 Point2 dest_offset = offset;
114 if (centered) {
115 dest_offset -= frame_size / 2;
116 }
117
118 if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
119 dest_offset = (dest_offset + Point2(0.5, 0.5)).floor();
120 }
121
122 r_dst_rect = Rect2(dest_offset, frame_size);
123
124 if (hflip) {
125 r_dst_rect.size.x = -r_dst_rect.size.x;
126 }
127 if (vflip) {
128 r_dst_rect.size.y = -r_dst_rect.size.y;
129 }
130 }
131
132 Point2 Sprite2D::_get_rect_offset(const Size2i &p_size) const {
133 Point2 ofs = offset;
134 if (centered) {
135 ofs -= Size2(p_size) / 2;
136 }
137
138 if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
139 ofs = (ofs + Point2(0.5, 0.5)).floor();
140 }
141
142 return ofs;
143 }
144
145 void Sprite2D::_notification(int p_what) {
146 switch (p_what) {
147 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
148 RID ae = get_accessibility_element();
149 ERR_FAIL_COND(ae.is_null());
150
151 Rect2 dst_rect = get_rect();
152
153 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_IMAGE);
154 DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform());
155 DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, dst_rect);
156 } break;
157
158 case NOTIFICATION_DRAW: {
159 if (texture.is_null()) {
160 return;
161 }
162
163 RID ci = get_canvas_item();
164
165 Rect2 src_rect, dst_rect;
166 bool filter_clip_enabled;
167 _get_rects(src_rect, dst_rect, filter_clip_enabled);
168
169 texture->draw_rect_region(ci, dst_rect, src_rect, Color(1, 1, 1), false, filter_clip_enabled);
170 } break;
171 }
172 }
173
174 void Sprite2D::set_texture(const Ref<Texture2D> &p_texture) {
175 if (p_texture == texture) {
176 return;
177 }
178
179 if (texture.is_valid()) {
180 texture->disconnect_changed(callable_mp(this, &Sprite2D::_texture_changed));
181 }
182
183 texture = p_texture;
184
185 if (texture.is_valid()) {
186 texture->connect_changed(callable_mp(this, &Sprite2D::_texture_changed));
187 }
188
189 queue_redraw();
190 emit_signal(SceneStringName(texture_changed));
191 item_rect_changed();
192 }
193
194 Ref<Texture2D> Sprite2D::get_texture() const {
195 return texture;
196 }
197
198 void Sprite2D::set_centered(bool p_center) {
199 if (centered == p_center) {
200 return;
201 }
202
203 centered = p_center;
204 queue_redraw();
205 item_rect_changed();
206 }
207
208 bool Sprite2D::is_centered() const {
209 return centered;
210 }
211
212 void Sprite2D::set_offset(const Point2 &p_offset) {
213 if (offset == p_offset) {
214 return;
215 }
216
217 offset = p_offset;
218 queue_redraw();
219 item_rect_changed();
220 }
221
222 Point2 Sprite2D::get_offset() const {
223 return offset;
224 }
225
226 void Sprite2D::set_flip_h(bool p_flip) {
227 if (hflip == p_flip) {
228 return;
229 }
230
231 hflip = p_flip;
232 queue_redraw();
233 }
234
235 bool Sprite2D::is_flipped_h() const {
236 return hflip;
237 }
238
239 void Sprite2D::set_flip_v(bool p_flip) {
240 if (vflip == p_flip) {
241 return;
242 }
243
244 vflip = p_flip;
245 queue_redraw();
246 }
247
248 bool Sprite2D::is_flipped_v() const {
249 return vflip;
250 }
251
252 void Sprite2D::set_region_enabled(bool p_region_enabled) {
253 if (region_enabled == p_region_enabled) {
254 return;
255 }
256
257 region_enabled = p_region_enabled;
258 _emit_region_rect_enabled();
259 queue_redraw();
260 }
261
262 bool Sprite2D::is_region_enabled() const {
263 return region_enabled;
264 }
265
266 void Sprite2D::set_region_rect(const Rect2 &p_region_rect) {
267 if (region_rect == p_region_rect) {
268 return;
269 }
270
271 region_rect = p_region_rect;
272
273 if (region_enabled) {
274 item_rect_changed();
275 }
276 }
277
278 Rect2 Sprite2D::get_region_rect() const {
279 return region_rect;
280 }
281
282 void Sprite2D::set_region_filter_clip_enabled(bool p_region_filter_clip_enabled) {
283 if (region_filter_clip_enabled == p_region_filter_clip_enabled) {
284 return;
285 }
286
287 region_filter_clip_enabled = p_region_filter_clip_enabled;
288 queue_redraw();
289 }
290
291 bool Sprite2D::is_region_filter_clip_enabled() const {
292 return region_filter_clip_enabled;
293 }
294
295 void Sprite2D::set_frame(int p_frame) {
296 ERR_FAIL_INDEX(p_frame, vframes * hframes);
297
298 if (frame == p_frame) {
299 return;
300 }
301
302 frame = p_frame;
303 item_rect_changed();
304 emit_signal(SceneStringName(frame_changed));
305 }
306
307 int Sprite2D::get_frame() const {
308 return frame;
309 }
310
311 void Sprite2D::set_frame_coords(const Vector2i &p_coord) {
312 ERR_FAIL_INDEX(p_coord.x, hframes);
313 ERR_FAIL_INDEX(p_coord.y, vframes);
314
315 set_frame(p_coord.y * hframes + p_coord.x);
316 }
317
318 Vector2i Sprite2D::get_frame_coords() const {
319 return Vector2i(frame % hframes, frame / hframes);
320 }
321
322 void Sprite2D::set_vframes(int p_amount) {
323 ERR_FAIL_COND_MSG(p_amount < 1, "Amount of vframes cannot be smaller than 1.");
324
325 if (vframes == p_amount) {
326 return;
327 }
328
329 vframes = p_amount;
330 if (frame >= vframes * hframes) {
331 frame = 0;
332 }
333 _emit_region_rect_enabled();
334 queue_redraw();
335 item_rect_changed();
336 notify_property_list_changed();
337 }
338
339 int Sprite2D::get_vframes() const {
340 return vframes;
341 }
342
343 void Sprite2D::set_hframes(int p_amount) {
344 ERR_FAIL_COND_MSG(p_amount < 1, "Amount of hframes cannot be smaller than 1.");
345
346 if (hframes == p_amount) {
347 return;
348 }
349
350 if (vframes > 1) {
351 // Adjust the frame to fit new sheet dimensions.
352 int original_column = frame % hframes;
353 if (original_column >= p_amount) {
354 // Frame's column was dropped, reset.
355 frame = 0;
356 } else {
357 int original_row = frame / hframes;
358 frame = original_row * p_amount + original_column;
359 }
360 }
361 hframes = p_amount;
362 if (frame >= vframes * hframes) {
363 frame = 0;
364 }
365 _emit_region_rect_enabled();
366 queue_redraw();
367 item_rect_changed();
368 notify_property_list_changed();
369 }
370
371 int Sprite2D::get_hframes() const {
372 return hframes;
373 }
374
375 bool Sprite2D::is_pixel_opaque(const Point2 &p_point) const {
376 if (texture.is_null()) {
377 return false;
378 }
379
380 if (texture->get_size().width == 0 || texture->get_size().height == 0) {
381 return false;
382 }
383
384 Rect2 src_rect, dst_rect;
385 bool filter_clip_enabled;
386 _get_rects(src_rect, dst_rect, filter_clip_enabled);
387 dst_rect.size = dst_rect.size.abs();
388
389 if (!dst_rect.has_point(p_point)) {
390 return false;
391 }
392
393 Vector2 q = (p_point - dst_rect.position) / dst_rect.size;
394 if (hflip) {
395 q.x = 1.0f - q.x;
396 }
397 if (vflip) {
398 q.y = 1.0f - q.y;
399 }
400 q = q * src_rect.size + src_rect.position;
401 TextureRepeat repeat_mode = get_texture_repeat_in_tree();
402 bool is_repeat = repeat_mode == TEXTURE_REPEAT_ENABLED || repeat_mode == TEXTURE_REPEAT_MIRROR;
403 bool is_mirrored_repeat = repeat_mode == TEXTURE_REPEAT_MIRROR;
404 if (is_repeat) {
405 int mirror_x = 0;
406 int mirror_y = 0;
407 if (is_mirrored_repeat) {
408 mirror_x = (int)(q.x / texture->get_size().width);
409 mirror_y = (int)(q.y / texture->get_size().height);
410 }
411 q.x = Math::fmod(q.x, texture->get_size().width);
412 q.y = Math::fmod(q.y, texture->get_size().height);
413 if (mirror_x % 2 == 1) {
414 q.x = texture->get_size().width - q.x - 1;
415 }
416 if (mirror_y % 2 == 1) {
417 q.y = texture->get_size().height - q.y - 1;
418 }
419 } else {
420 q = q.min(texture->get_size() - Vector2(1, 1));
421 }
422
423 return texture->is_pixel_opaque((int)q.x, (int)q.y);
424 }
425
426 bool Sprite2D::is_editor_region_rect_draggable() const {
427 return hframes <= 1 && vframes <= 1 && region_enabled;
428 }
429
430 #ifdef TOOLS_ENABLED
431 void Sprite2D::_editor_set_dragging_to_resize_rect(bool p_dragging_to_resize_rect) {
432 dragging_to_resize_rect = p_dragging_to_resize_rect;
433 }
434 bool Sprite2D::_editor_is_dragging_to_resiz_rect() const {
435 return dragging_to_resize_rect;
436 }
437 #endif
438
439 Rect2 Sprite2D::get_rect() const {
440 if (texture.is_null()) {
441 return Rect2(0, 0, 1, 1);
442 }
443
444 Size2i s;
445
446 if (region_enabled) {
447 s = region_rect.size;
448 } else {
449 s = texture->get_size();
450 }
451
452 s = s / Point2(hframes, vframes);
453
454 Point2 ofs = _get_rect_offset(s);
455
456 if (s == Size2(0, 0)) {
457 s = Size2(1, 1);
458 }
459
460 return Rect2(ofs, s);
461 }
462
463 void Sprite2D::_validate_property(PropertyInfo &p_property) const {
464 if (!Engine::get_singleton()->is_editor_hint()) {
465 return;
466 }
467 if (p_property.name == "frame") {
468 p_property.hint = PROPERTY_HINT_RANGE;
469 p_property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
470 p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
471 }
472
473 if (p_property.name == "frame_coords") {
474 p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
475 }
476 }
477
478 void Sprite2D::_texture_changed() {
479 // Changes to the texture need to trigger an update to make
480 // the editor redraw the sprite with the updated texture.
481 if (texture.is_valid()) {
482 queue_redraw();
483 }
484 }
485
486 void Sprite2D::_emit_region_rect_enabled() {
487 if (Engine::get_singleton()->is_editor_hint()) {
488 emit_signal("_editor_region_rect_enabled");
489 }
490 }
491
492 void Sprite2D::_bind_methods() {
493 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite2D::set_texture);
494 ClassDB::bind_method(D_METHOD("get_texture"), &Sprite2D::get_texture);
495
496 ClassDB::bind_method(D_METHOD("set_centered", "centered"), &Sprite2D::set_centered);
497 ClassDB::bind_method(D_METHOD("is_centered"), &Sprite2D::is_centered);
498
499 ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Sprite2D::set_offset);
500 ClassDB::bind_method(D_METHOD("get_offset"), &Sprite2D::get_offset);
501
502 ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &Sprite2D::set_flip_h);
503 ClassDB::bind_method(D_METHOD("is_flipped_h"), &Sprite2D::is_flipped_h);
504
505 ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &Sprite2D::set_flip_v);
506 ClassDB::bind_method(D_METHOD("is_flipped_v"), &Sprite2D::is_flipped_v);
507
508 ClassDB::bind_method(D_METHOD("set_region_enabled", "enabled"), &Sprite2D::set_region_enabled);
509 ClassDB::bind_method(D_METHOD("is_region_enabled"), &Sprite2D::is_region_enabled);
510
511 ClassDB::bind_method(D_METHOD("is_pixel_opaque", "pos"), &Sprite2D::is_pixel_opaque);
512
513 ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite2D::set_region_rect);
514 ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite2D::get_region_rect);
515
516 ClassDB::bind_method(D_METHOD("set_region_filter_clip_enabled", "enabled"), &Sprite2D::set_region_filter_clip_enabled);
517 ClassDB::bind_method(D_METHOD("is_region_filter_clip_enabled"), &Sprite2D::is_region_filter_clip_enabled);
518
519 ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite2D::set_frame);
520 ClassDB::bind_method(D_METHOD("get_frame"), &Sprite2D::get_frame);
521
522 ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite2D::set_frame_coords);
523 ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite2D::get_frame_coords);
524
525 ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite2D::set_vframes);
526 ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite2D::get_vframes);
527
528 ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite2D::set_hframes);
529 ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite2D::get_hframes);
530
531 ClassDB::bind_method(D_METHOD("get_rect"), &Sprite2D::get_rect);
532
533 ADD_SIGNAL(MethodInfo("frame_changed"));
534 ADD_SIGNAL(MethodInfo("texture_changed"));
535
536 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
537 ADD_GROUP("Offset", "");
538 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
539 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
540 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
541 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
542 ADD_GROUP("Animation", "");
543 ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
544 ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
545 ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
546 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords");
547
548 ADD_GROUP("Region", "region_");
549 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_region_enabled", "is_region_enabled");
550 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
551 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_filter_clip_enabled"), "set_region_filter_clip_enabled", "is_region_filter_clip_enabled");
552 }
553
554 Sprite2D::Sprite2D() {
555 if (Engine::get_singleton()->is_editor_hint()) {
556 add_user_signal(MethodInfo("_editor_region_rect_enabled"));
557 }
558 }