1 /*************************************************************************/
2 /* animated_sprite.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 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
31 #include "animated_sprite.h"
32
33 #include "core/os/os.h"
34 #include "scene/scene_string_names.h"
35
36 #define NORMAL_SUFFIX "_normal"
37
38 #ifdef TOOLS_ENABLED
_edit_get_state() const39 Dictionary AnimatedSprite::_edit_get_state() const {
40 Dictionary state = Node2D::_edit_get_state();
41 state["offset"] = offset;
42 return state;
43 }
44
_edit_set_state(const Dictionary & p_state)45 void AnimatedSprite::_edit_set_state(const Dictionary &p_state) {
46 Node2D::_edit_set_state(p_state);
47 set_offset(p_state["offset"]);
48 }
49
_edit_set_pivot(const Point2 & p_pivot)50 void AnimatedSprite::_edit_set_pivot(const Point2 &p_pivot) {
51 set_offset(get_offset() - p_pivot);
52 set_position(get_transform().xform(p_pivot));
53 }
54
_edit_get_pivot() const55 Point2 AnimatedSprite::_edit_get_pivot() const {
56 return Vector2();
57 }
58
_edit_use_pivot() const59 bool AnimatedSprite::_edit_use_pivot() const {
60 return true;
61 }
62
_edit_get_rect() const63 Rect2 AnimatedSprite::_edit_get_rect() const {
64 return _get_rect();
65 }
66
_edit_use_rect() const67 bool AnimatedSprite::_edit_use_rect() const {
68 if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
69 return false;
70 }
71 Ref<Texture> t;
72 if (animation)
73 t = frames->get_frame(animation, frame);
74 return t.is_valid();
75 }
76 #endif
77
get_anchorable_rect() const78 Rect2 AnimatedSprite::get_anchorable_rect() const {
79 return _get_rect();
80 }
81
_get_rect() const82 Rect2 AnimatedSprite::_get_rect() const {
83 if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
84 return Rect2();
85 }
86
87 Ref<Texture> t;
88 if (animation)
89 t = frames->get_frame(animation, frame);
90 if (t.is_null())
91 return Rect2();
92 Size2 s = t->get_size();
93
94 Point2 ofs = offset;
95 if (centered)
96 ofs -= Size2(s) / 2;
97
98 if (s == Size2(0, 0))
99 s = Size2(1, 1);
100
101 return Rect2(ofs, s);
102 }
103
add_frame(const StringName & p_anim,const Ref<Texture> & p_frame,int p_at_pos)104 void SpriteFrames::add_frame(const StringName &p_anim, const Ref<Texture> &p_frame, int p_at_pos) {
105
106 Map<StringName, Anim>::Element *E = animations.find(p_anim);
107 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
108
109 if (p_at_pos >= 0 && p_at_pos < E->get().frames.size())
110 E->get().frames.insert(p_at_pos, p_frame);
111 else
112 E->get().frames.push_back(p_frame);
113
114 emit_changed();
115 }
116
get_frame_count(const StringName & p_anim) const117 int SpriteFrames::get_frame_count(const StringName &p_anim) const {
118 const Map<StringName, Anim>::Element *E = animations.find(p_anim);
119 ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
120
121 return E->get().frames.size();
122 }
123
remove_frame(const StringName & p_anim,int p_idx)124 void SpriteFrames::remove_frame(const StringName &p_anim, int p_idx) {
125
126 Map<StringName, Anim>::Element *E = animations.find(p_anim);
127 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
128
129 E->get().frames.remove(p_idx);
130 emit_changed();
131 }
clear(const StringName & p_anim)132 void SpriteFrames::clear(const StringName &p_anim) {
133
134 Map<StringName, Anim>::Element *E = animations.find(p_anim);
135 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
136
137 E->get().frames.clear();
138 emit_changed();
139 }
140
clear_all()141 void SpriteFrames::clear_all() {
142
143 animations.clear();
144 add_animation("default");
145 }
146
add_animation(const StringName & p_anim)147 void SpriteFrames::add_animation(const StringName &p_anim) {
148
149 ERR_FAIL_COND_MSG(animations.has(p_anim), "SpriteFrames already has animation '" + p_anim + "'.");
150
151 animations[p_anim] = Anim();
152 animations[p_anim].normal_name = String(p_anim) + NORMAL_SUFFIX;
153 }
154
has_animation(const StringName & p_anim) const155 bool SpriteFrames::has_animation(const StringName &p_anim) const {
156
157 return animations.has(p_anim);
158 }
remove_animation(const StringName & p_anim)159 void SpriteFrames::remove_animation(const StringName &p_anim) {
160
161 animations.erase(p_anim);
162 }
163
rename_animation(const StringName & p_prev,const StringName & p_next)164 void SpriteFrames::rename_animation(const StringName &p_prev, const StringName &p_next) {
165
166 ERR_FAIL_COND_MSG(!animations.has(p_prev), "SpriteFrames doesn't have animation '" + String(p_prev) + "'.");
167 ERR_FAIL_COND_MSG(animations.has(p_next), "Animation '" + String(p_next) + "' already exists.");
168
169 Anim anim = animations[p_prev];
170 animations.erase(p_prev);
171 animations[p_next] = anim;
172 animations[p_next].normal_name = String(p_next) + NORMAL_SUFFIX;
173 }
174
_get_animation_list() const175 Vector<String> SpriteFrames::_get_animation_list() const {
176
177 Vector<String> ret;
178 List<StringName> al;
179 get_animation_list(&al);
180 for (List<StringName>::Element *E = al.front(); E; E = E->next()) {
181
182 ret.push_back(E->get());
183 }
184
185 return ret;
186 }
187
get_animation_list(List<StringName> * r_animations) const188 void SpriteFrames::get_animation_list(List<StringName> *r_animations) const {
189
190 for (const Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
191 r_animations->push_back(E->key());
192 }
193 }
194
get_animation_names() const195 Vector<String> SpriteFrames::get_animation_names() const {
196
197 Vector<String> names;
198 for (const Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
199 names.push_back(E->key());
200 }
201 names.sort();
202 return names;
203 }
204
set_animation_speed(const StringName & p_anim,float p_fps)205 void SpriteFrames::set_animation_speed(const StringName &p_anim, float p_fps) {
206
207 ERR_FAIL_COND_MSG(p_fps < 0, "Animation speed cannot be negative (" + itos(p_fps) + ").");
208 Map<StringName, Anim>::Element *E = animations.find(p_anim);
209 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
210 E->get().speed = p_fps;
211 }
get_animation_speed(const StringName & p_anim) const212 float SpriteFrames::get_animation_speed(const StringName &p_anim) const {
213
214 const Map<StringName, Anim>::Element *E = animations.find(p_anim);
215 ERR_FAIL_COND_V_MSG(!E, 0, "Animation '" + String(p_anim) + "' doesn't exist.");
216 return E->get().speed;
217 }
218
set_animation_loop(const StringName & p_anim,bool p_loop)219 void SpriteFrames::set_animation_loop(const StringName &p_anim, bool p_loop) {
220 Map<StringName, Anim>::Element *E = animations.find(p_anim);
221 ERR_FAIL_COND_MSG(!E, "Animation '" + String(p_anim) + "' doesn't exist.");
222 E->get().loop = p_loop;
223 }
get_animation_loop(const StringName & p_anim) const224 bool SpriteFrames::get_animation_loop(const StringName &p_anim) const {
225 const Map<StringName, Anim>::Element *E = animations.find(p_anim);
226 ERR_FAIL_COND_V_MSG(!E, false, "Animation '" + String(p_anim) + "' doesn't exist.");
227 return E->get().loop;
228 }
229
_set_frames(const Array & p_frames)230 void SpriteFrames::_set_frames(const Array &p_frames) {
231
232 clear_all();
233 Map<StringName, Anim>::Element *E = animations.find(SceneStringNames::get_singleton()->_default);
234 ERR_FAIL_COND(!E);
235
236 E->get().frames.resize(p_frames.size());
237 for (int i = 0; i < E->get().frames.size(); i++)
238 E->get().frames.write[i] = p_frames[i];
239 }
_get_frames() const240 Array SpriteFrames::_get_frames() const {
241
242 return Array();
243 }
244
_get_animations() const245 Array SpriteFrames::_get_animations() const {
246
247 Array anims;
248 for (Map<StringName, Anim>::Element *E = animations.front(); E; E = E->next()) {
249 Dictionary d;
250 d["name"] = E->key();
251 d["speed"] = E->get().speed;
252 d["loop"] = E->get().loop;
253 Array frames;
254 for (int i = 0; i < E->get().frames.size(); i++) {
255 frames.push_back(E->get().frames[i]);
256 }
257 d["frames"] = frames;
258 anims.push_back(d);
259 }
260
261 return anims;
262 }
_set_animations(const Array & p_animations)263 void SpriteFrames::_set_animations(const Array &p_animations) {
264
265 animations.clear();
266 for (int i = 0; i < p_animations.size(); i++) {
267
268 Dictionary d = p_animations[i];
269
270 ERR_CONTINUE(!d.has("name"));
271 ERR_CONTINUE(!d.has("speed"));
272 ERR_CONTINUE(!d.has("loop"));
273 ERR_CONTINUE(!d.has("frames"));
274
275 Anim anim;
276 anim.speed = d["speed"];
277 anim.loop = d["loop"];
278 Array frames = d["frames"];
279 for (int j = 0; j < frames.size(); j++) {
280
281 RES res = frames[j];
282 anim.frames.push_back(res);
283 }
284
285 animations[d["name"]] = anim;
286 }
287 }
288
_bind_methods()289 void SpriteFrames::_bind_methods() {
290
291 ClassDB::bind_method(D_METHOD("add_animation", "anim"), &SpriteFrames::add_animation);
292 ClassDB::bind_method(D_METHOD("has_animation", "anim"), &SpriteFrames::has_animation);
293 ClassDB::bind_method(D_METHOD("remove_animation", "anim"), &SpriteFrames::remove_animation);
294 ClassDB::bind_method(D_METHOD("rename_animation", "anim", "newname"), &SpriteFrames::rename_animation);
295
296 ClassDB::bind_method(D_METHOD("get_animation_names"), &SpriteFrames::get_animation_names);
297
298 ClassDB::bind_method(D_METHOD("set_animation_speed", "anim", "speed"), &SpriteFrames::set_animation_speed);
299 ClassDB::bind_method(D_METHOD("get_animation_speed", "anim"), &SpriteFrames::get_animation_speed);
300
301 ClassDB::bind_method(D_METHOD("set_animation_loop", "anim", "loop"), &SpriteFrames::set_animation_loop);
302 ClassDB::bind_method(D_METHOD("get_animation_loop", "anim"), &SpriteFrames::get_animation_loop);
303
304 ClassDB::bind_method(D_METHOD("add_frame", "anim", "frame", "at_position"), &SpriteFrames::add_frame, DEFVAL(-1));
305 ClassDB::bind_method(D_METHOD("get_frame_count", "anim"), &SpriteFrames::get_frame_count);
306 ClassDB::bind_method(D_METHOD("get_frame", "anim", "idx"), &SpriteFrames::get_frame);
307 ClassDB::bind_method(D_METHOD("set_frame", "anim", "idx", "txt"), &SpriteFrames::set_frame);
308 ClassDB::bind_method(D_METHOD("remove_frame", "anim", "idx"), &SpriteFrames::remove_frame);
309 ClassDB::bind_method(D_METHOD("clear", "anim"), &SpriteFrames::clear);
310 ClassDB::bind_method(D_METHOD("clear_all"), &SpriteFrames::clear_all);
311
312 ClassDB::bind_method(D_METHOD("_set_frames"), &SpriteFrames::_set_frames);
313 ClassDB::bind_method(D_METHOD("_get_frames"), &SpriteFrames::_get_frames);
314
315 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "frames", PROPERTY_HINT_NONE, "", 0), "_set_frames", "_get_frames"); //compatibility
316
317 ClassDB::bind_method(D_METHOD("_set_animations"), &SpriteFrames::_set_animations);
318 ClassDB::bind_method(D_METHOD("_get_animations"), &SpriteFrames::_get_animations);
319
320 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_animations", "_get_animations"); //compatibility
321 }
322
SpriteFrames()323 SpriteFrames::SpriteFrames() {
324
325 add_animation(SceneStringNames::get_singleton()->_default);
326 }
327
_validate_property(PropertyInfo & property) const328 void AnimatedSprite::_validate_property(PropertyInfo &property) const {
329
330 if (!frames.is_valid())
331 return;
332 if (property.name == "animation") {
333
334 property.hint = PROPERTY_HINT_ENUM;
335 List<StringName> names;
336 frames->get_animation_list(&names);
337 names.sort_custom<StringName::AlphCompare>();
338
339 bool current_found = false;
340
341 for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
342 if (E->prev()) {
343 property.hint_string += ",";
344 }
345
346 property.hint_string += String(E->get());
347 if (animation == E->get()) {
348 current_found = true;
349 }
350 }
351
352 if (!current_found) {
353 if (property.hint_string == String()) {
354 property.hint_string = String(animation);
355 } else {
356 property.hint_string = String(animation) + "," + property.hint_string;
357 }
358 }
359 }
360
361 if (property.name == "frame") {
362 property.hint = PROPERTY_HINT_RANGE;
363 if (frames->has_animation(animation) && frames->get_frame_count(animation) > 1) {
364 property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
365 }
366 property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
367 }
368 }
369
_notification(int p_what)370 void AnimatedSprite::_notification(int p_what) {
371
372 switch (p_what) {
373 case NOTIFICATION_INTERNAL_PROCESS: {
374
375 if (frames.is_null())
376 return;
377 if (!frames->has_animation(animation))
378 return;
379 if (frame < 0)
380 return;
381
382 float speed = frames->get_animation_speed(animation) * speed_scale;
383 if (speed == 0)
384 return; //do nothing
385
386 float remaining = get_process_delta_time();
387
388 while (remaining) {
389
390 if (timeout <= 0) {
391
392 timeout = _get_frame_duration();
393
394 int fc = frames->get_frame_count(animation);
395 if ((!backwards && frame >= fc - 1) || (backwards && frame <= 0)) {
396 if (frames->get_animation_loop(animation)) {
397 if (backwards)
398 frame = fc - 1;
399 else
400 frame = 0;
401
402 emit_signal(SceneStringNames::get_singleton()->animation_finished);
403 } else {
404 if (backwards)
405 frame = 0;
406 else
407 frame = fc - 1;
408
409 if (!is_over) {
410 is_over = true;
411 emit_signal(SceneStringNames::get_singleton()->animation_finished);
412 }
413 }
414 } else {
415 if (backwards)
416 frame--;
417 else
418 frame++;
419 }
420
421 update();
422 _change_notify("frame");
423 emit_signal(SceneStringNames::get_singleton()->frame_changed);
424 }
425
426 float to_process = MIN(timeout, remaining);
427 remaining -= to_process;
428 timeout -= to_process;
429 }
430 } break;
431
432 case NOTIFICATION_DRAW: {
433
434 if (frames.is_null())
435 return;
436 if (frame < 0)
437 return;
438 if (!frames->has_animation(animation))
439 return;
440
441 Ref<Texture> texture = frames->get_frame(animation, frame);
442 if (texture.is_null())
443 return;
444
445 Ref<Texture> normal = frames->get_normal_frame(animation, frame);
446
447 RID ci = get_canvas_item();
448
449 Size2i s;
450 s = texture->get_size();
451 Point2 ofs = offset;
452 if (centered)
453 ofs -= s / 2;
454
455 if (Engine::get_singleton()->get_use_pixel_snap()) {
456 ofs = ofs.floor();
457 }
458 Rect2 dst_rect(ofs, s);
459
460 if (hflip)
461 dst_rect.size.x = -dst_rect.size.x;
462 if (vflip)
463 dst_rect.size.y = -dst_rect.size.y;
464
465 texture->draw_rect_region(ci, dst_rect, Rect2(Vector2(), texture->get_size()), Color(1, 1, 1), false, normal);
466
467 } break;
468 }
469 }
470
set_sprite_frames(const Ref<SpriteFrames> & p_frames)471 void AnimatedSprite::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
472
473 if (frames.is_valid())
474 frames->disconnect("changed", this, "_res_changed");
475 frames = p_frames;
476 if (frames.is_valid())
477 frames->connect("changed", this, "_res_changed");
478
479 if (!frames.is_valid()) {
480 frame = 0;
481 } else {
482 set_frame(frame);
483 }
484
485 _change_notify();
486 _reset_timeout();
487 update();
488 update_configuration_warning();
489 }
490
get_sprite_frames() const491 Ref<SpriteFrames> AnimatedSprite::get_sprite_frames() const {
492
493 return frames;
494 }
495
set_frame(int p_frame)496 void AnimatedSprite::set_frame(int p_frame) {
497
498 if (!frames.is_valid()) {
499 return;
500 }
501
502 if (frames->has_animation(animation)) {
503 int limit = frames->get_frame_count(animation);
504 if (p_frame >= limit)
505 p_frame = limit - 1;
506 }
507
508 if (p_frame < 0)
509 p_frame = 0;
510
511 if (frame == p_frame)
512 return;
513
514 frame = p_frame;
515 _reset_timeout();
516 update();
517 _change_notify("frame");
518 emit_signal(SceneStringNames::get_singleton()->frame_changed);
519 }
get_frame() const520 int AnimatedSprite::get_frame() const {
521
522 return frame;
523 }
524
set_speed_scale(float p_speed_scale)525 void AnimatedSprite::set_speed_scale(float p_speed_scale) {
526
527 float elapsed = _get_frame_duration() - timeout;
528
529 speed_scale = MAX(p_speed_scale, 0.0f);
530
531 // We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed
532 _reset_timeout();
533 timeout -= elapsed;
534 }
535
get_speed_scale() const536 float AnimatedSprite::get_speed_scale() const {
537
538 return speed_scale;
539 }
540
set_centered(bool p_center)541 void AnimatedSprite::set_centered(bool p_center) {
542
543 centered = p_center;
544 update();
545 item_rect_changed();
546 }
547
is_centered() const548 bool AnimatedSprite::is_centered() const {
549
550 return centered;
551 }
552
set_offset(const Point2 & p_offset)553 void AnimatedSprite::set_offset(const Point2 &p_offset) {
554
555 offset = p_offset;
556 update();
557 item_rect_changed();
558 _change_notify("offset");
559 }
get_offset() const560 Point2 AnimatedSprite::get_offset() const {
561
562 return offset;
563 }
564
set_flip_h(bool p_flip)565 void AnimatedSprite::set_flip_h(bool p_flip) {
566
567 hflip = p_flip;
568 update();
569 }
is_flipped_h() const570 bool AnimatedSprite::is_flipped_h() const {
571
572 return hflip;
573 }
574
set_flip_v(bool p_flip)575 void AnimatedSprite::set_flip_v(bool p_flip) {
576
577 vflip = p_flip;
578 update();
579 }
is_flipped_v() const580 bool AnimatedSprite::is_flipped_v() const {
581
582 return vflip;
583 }
584
_res_changed()585 void AnimatedSprite::_res_changed() {
586
587 set_frame(frame);
588 _change_notify("frame");
589 _change_notify("animation");
590 update();
591 }
592
_set_playing(bool p_playing)593 void AnimatedSprite::_set_playing(bool p_playing) {
594
595 if (playing == p_playing)
596 return;
597 playing = p_playing;
598 _reset_timeout();
599 set_process_internal(playing);
600 }
601
_is_playing() const602 bool AnimatedSprite::_is_playing() const {
603
604 return playing;
605 }
606
play(const StringName & p_animation,const bool p_backwards)607 void AnimatedSprite::play(const StringName &p_animation, const bool p_backwards) {
608
609 backwards = p_backwards;
610
611 if (p_animation) {
612 set_animation(p_animation);
613 if (backwards && get_frame() == 0)
614 set_frame(frames->get_frame_count(p_animation) - 1);
615 }
616
617 _set_playing(true);
618 }
619
stop()620 void AnimatedSprite::stop() {
621
622 _set_playing(false);
623 }
624
is_playing() const625 bool AnimatedSprite::is_playing() const {
626
627 return playing;
628 }
629
_get_frame_duration()630 float AnimatedSprite::_get_frame_duration() {
631 if (frames.is_valid() && frames->has_animation(animation)) {
632 float speed = frames->get_animation_speed(animation) * speed_scale;
633 if (speed > 0) {
634 return 1.0 / speed;
635 }
636 }
637 return 0.0;
638 }
639
_reset_timeout()640 void AnimatedSprite::_reset_timeout() {
641
642 if (!playing)
643 return;
644
645 timeout = _get_frame_duration();
646 is_over = false;
647 }
648
set_animation(const StringName & p_animation)649 void AnimatedSprite::set_animation(const StringName &p_animation) {
650
651 ERR_FAIL_COND_MSG(frames == NULL, vformat("There is no animation with name '%s'.", p_animation));
652 ERR_FAIL_COND_MSG(frames->get_animation_names().find(p_animation) == -1, vformat("There is no animation with name '%s'.", p_animation));
653
654 if (animation == p_animation)
655 return;
656
657 animation = p_animation;
658 _reset_timeout();
659 set_frame(0);
660 _change_notify();
661 update();
662 }
get_animation() const663 StringName AnimatedSprite::get_animation() const {
664
665 return animation;
666 }
667
get_configuration_warning() const668 String AnimatedSprite::get_configuration_warning() const {
669
670 if (frames.is_null()) {
671 return TTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite to display frames.");
672 }
673
674 return String();
675 }
676
_bind_methods()677 void AnimatedSprite::_bind_methods() {
678
679 ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite::set_sprite_frames);
680 ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite::get_sprite_frames);
681
682 ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite::set_animation);
683 ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite::get_animation);
684
685 ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite::_set_playing);
686 ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite::_is_playing);
687
688 ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite::play, DEFVAL(StringName()), DEFVAL(false));
689 ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite::stop);
690 ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite::is_playing);
691
692 ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite::set_centered);
693 ClassDB::bind_method(D_METHOD("is_centered"), &AnimatedSprite::is_centered);
694
695 ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AnimatedSprite::set_offset);
696 ClassDB::bind_method(D_METHOD("get_offset"), &AnimatedSprite::get_offset);
697
698 ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &AnimatedSprite::set_flip_h);
699 ClassDB::bind_method(D_METHOD("is_flipped_h"), &AnimatedSprite::is_flipped_h);
700
701 ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &AnimatedSprite::set_flip_v);
702 ClassDB::bind_method(D_METHOD("is_flipped_v"), &AnimatedSprite::is_flipped_v);
703
704 ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite::set_frame);
705 ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite::get_frame);
706
707 ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite::set_speed_scale);
708 ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite::get_speed_scale);
709
710 ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite::_res_changed);
711
712 ADD_SIGNAL(MethodInfo("frame_changed"));
713 ADD_SIGNAL(MethodInfo("animation_finished"));
714
715 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
716 ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
717 ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
718 ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale"), "set_speed_scale", "get_speed_scale");
719 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
720 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
721 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
722 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
723 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
724 }
725
AnimatedSprite()726 AnimatedSprite::AnimatedSprite() {
727
728 centered = true;
729 hflip = false;
730 vflip = false;
731
732 frame = 0;
733 speed_scale = 1.0f;
734 playing = false;
735 backwards = false;
736 animation = "default";
737 timeout = 0;
738 is_over = false;
739 }
740