| 1 | /**************************************************************************/ |
| 2 | /* animated_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 "animated_sprite_2d.h" |
| 32 | |
| 33 | #include "scene/main/viewport.h" |
| 34 | |
| 35 | #ifdef TOOLS_ENABLED |
| 36 | Dictionary AnimatedSprite2D::_edit_get_state() const { |
| 37 | Dictionary state = Node2D::_edit_get_state(); |
| 38 | state["offset"] = offset; |
| 39 | return state; |
| 40 | } |
| 41 | |
| 42 | void AnimatedSprite2D::_edit_set_state(const Dictionary &p_state) { |
| 43 | Node2D::_edit_set_state(p_state); |
| 44 | set_offset(p_state["offset"]); |
| 45 | } |
| 46 | |
| 47 | void AnimatedSprite2D::_edit_set_pivot(const Point2 &p_pivot) { |
| 48 | set_offset(get_offset() - p_pivot); |
| 49 | set_position(get_transform().xform(p_pivot)); |
| 50 | } |
| 51 | |
| 52 | Point2 AnimatedSprite2D::_edit_get_pivot() const { |
| 53 | return Vector2(); |
| 54 | } |
| 55 | |
| 56 | bool AnimatedSprite2D::_edit_use_pivot() const { |
| 57 | return true; |
| 58 | } |
| 59 | #endif // TOOLS_ENABLED |
| 60 | |
| 61 | #ifdef DEBUG_ENABLED |
| 62 | Rect2 AnimatedSprite2D::_edit_get_rect() const { |
| 63 | return _get_rect(); |
| 64 | } |
| 65 | |
| 66 | bool AnimatedSprite2D::_edit_use_rect() const { |
| 67 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 68 | return false; |
| 69 | } |
| 70 | if (frame < 0 || frame >= frames->get_frame_count(animation)) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | Ref<Texture2D> t; |
| 75 | if (animation) { |
| 76 | t = frames->get_frame_texture(animation, frame); |
| 77 | } |
| 78 | return t.is_valid(); |
| 79 | } |
| 80 | #endif // DEBUG_ENABLED |
| 81 | |
| 82 | Rect2 AnimatedSprite2D::get_anchorable_rect() const { |
| 83 | return _get_rect(); |
| 84 | } |
| 85 | |
| 86 | Rect2 AnimatedSprite2D::_get_rect() const { |
| 87 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 88 | return Rect2(); |
| 89 | } |
| 90 | if (frame < 0 || frame >= frames->get_frame_count(animation)) { |
| 91 | return Rect2(); |
| 92 | } |
| 93 | |
| 94 | Ref<Texture2D> t; |
| 95 | if (animation) { |
| 96 | t = frames->get_frame_texture(animation, frame); |
| 97 | } |
| 98 | if (t.is_null()) { |
| 99 | return Rect2(); |
| 100 | } |
| 101 | Size2 s = t->get_size(); |
| 102 | |
| 103 | Point2 ofs = offset; |
| 104 | if (centered) { |
| 105 | ofs -= s / 2; |
| 106 | } |
| 107 | |
| 108 | if (s == Size2(0, 0)) { |
| 109 | s = Size2(1, 1); |
| 110 | } |
| 111 | |
| 112 | return Rect2(ofs, s); |
| 113 | } |
| 114 | |
| 115 | void AnimatedSprite2D::_validate_property(PropertyInfo &p_property) const { |
| 116 | if (frames.is_null()) { |
| 117 | return; |
| 118 | } |
| 119 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 120 | if (p_property.name == "frame" && playing) { |
| 121 | p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY; |
| 122 | } |
| 123 | return; |
| 124 | } |
| 125 | if (p_property.name == "animation") { |
| 126 | List<StringName> names; |
| 127 | frames->get_animation_list(&names); |
| 128 | names.sort_custom<StringName::AlphCompare>(); |
| 129 | |
| 130 | bool current_found = false; |
| 131 | bool is_first_element = true; |
| 132 | |
| 133 | for (const StringName &E : names) { |
| 134 | if (!is_first_element) { |
| 135 | p_property.hint_string += ","; |
| 136 | } else { |
| 137 | is_first_element = false; |
| 138 | } |
| 139 | |
| 140 | p_property.hint_string += String(E); |
| 141 | if (animation == E) { |
| 142 | current_found = true; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (!current_found) { |
| 147 | if (p_property.hint_string.is_empty()) { |
| 148 | p_property.hint_string = String(animation); |
| 149 | } else { |
| 150 | p_property.hint_string = String(animation) + "," + p_property.hint_string; |
| 151 | } |
| 152 | } |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (p_property.name == "frame") { |
| 157 | if (playing) { |
| 158 | p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY; |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | p_property.hint = PROPERTY_HINT_RANGE; |
| 163 | if (frames->has_animation(animation) && frames->get_frame_count(animation) > 0) { |
| 164 | p_property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1"; |
| 165 | } else { |
| 166 | // Avoid an error, `hint_string` is required for `PROPERTY_HINT_RANGE`. |
| 167 | p_property.hint_string = "0,0,1"; |
| 168 | } |
| 169 | p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void AnimatedSprite2D::_notification(int p_what) { |
| 174 | switch (p_what) { |
| 175 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 176 | RID ae = get_accessibility_element(); |
| 177 | ERR_FAIL_COND(ae.is_null()); |
| 178 | |
| 179 | Rect2 dst_rect = _get_rect(); |
| 180 | |
| 181 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_IMAGE); |
| 182 | DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_transform()); |
| 183 | DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, dst_rect); |
| 184 | } break; |
| 185 | |
| 186 | case NOTIFICATION_READY: { |
| 187 | if (!Engine::get_singleton()->is_editor_hint() && frames.is_valid() && frames->has_animation(autoplay)) { |
| 188 | play(autoplay); |
| 189 | } |
| 190 | } break; |
| 191 | |
| 192 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 193 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | double remaining = get_process_delta_time(); |
| 198 | int i = 0; |
| 199 | while (remaining) { |
| 200 | // Animation speed may be changed by animation_finished or frame_changed signals. |
| 201 | double speed = frames->get_animation_speed(animation) * speed_scale * custom_speed_scale * frame_speed_scale; |
| 202 | double abs_speed = Math::abs(speed); |
| 203 | |
| 204 | if (speed == 0) { |
| 205 | return; // Do nothing. |
| 206 | } |
| 207 | |
| 208 | // Frame count may be changed by animation_finished or frame_changed signals. |
| 209 | int fc = frames->get_frame_count(animation); |
| 210 | |
| 211 | int last_frame = fc - 1; |
| 212 | if (!std::signbit(speed)) { |
| 213 | // Forwards. |
| 214 | if (frame_progress >= 1.0) { |
| 215 | if (frame >= last_frame) { |
| 216 | if (frames->get_animation_loop(animation)) { |
| 217 | frame = 0; |
| 218 | emit_signal("animation_looped"); |
| 219 | } else { |
| 220 | frame = last_frame; |
| 221 | pause(); |
| 222 | emit_signal(SceneStringName(animation_finished)); |
| 223 | return; |
| 224 | } |
| 225 | } else { |
| 226 | frame++; |
| 227 | } |
| 228 | _calc_frame_speed_scale(); |
| 229 | frame_progress = 0.0; |
| 230 | queue_redraw(); |
| 231 | emit_signal(SceneStringName(frame_changed)); |
| 232 | } |
| 233 | double to_process = MIN((1.0 - frame_progress) / abs_speed, remaining); |
| 234 | frame_progress += to_process * abs_speed; |
| 235 | remaining -= to_process; |
| 236 | } else { |
| 237 | // Backwards. |
| 238 | if (frame_progress <= 0) { |
| 239 | if (frame <= 0) { |
| 240 | if (frames->get_animation_loop(animation)) { |
| 241 | frame = last_frame; |
| 242 | emit_signal("animation_looped"); |
| 243 | } else { |
| 244 | frame = 0; |
| 245 | pause(); |
| 246 | emit_signal(SceneStringName(animation_finished)); |
| 247 | return; |
| 248 | } |
| 249 | } else { |
| 250 | frame--; |
| 251 | } |
| 252 | _calc_frame_speed_scale(); |
| 253 | frame_progress = 1.0; |
| 254 | queue_redraw(); |
| 255 | emit_signal(SceneStringName(frame_changed)); |
| 256 | } |
| 257 | double to_process = MIN(frame_progress / abs_speed, remaining); |
| 258 | frame_progress -= to_process * abs_speed; |
| 259 | remaining -= to_process; |
| 260 | } |
| 261 | |
| 262 | i++; |
| 263 | if (i > fc) { |
| 264 | return; // Prevents freezing if to_process is each time much less than remaining. |
| 265 | } |
| 266 | } |
| 267 | } break; |
| 268 | |
| 269 | case NOTIFICATION_DRAW: { |
| 270 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | Ref<Texture2D> texture = frames->get_frame_texture(animation, frame); |
| 275 | if (texture.is_null()) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | RID ci = get_canvas_item(); |
| 280 | |
| 281 | Size2 s = texture->get_size(); |
| 282 | Point2 ofs = offset; |
| 283 | if (centered) { |
| 284 | ofs -= s / 2; |
| 285 | } |
| 286 | |
| 287 | if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) { |
| 288 | ofs = (ofs + Point2(0.5, 0.5)).floor(); |
| 289 | } |
| 290 | |
| 291 | Rect2 dst_rect(ofs, s); |
| 292 | |
| 293 | if (hflip) { |
| 294 | dst_rect.size.x = -dst_rect.size.x; |
| 295 | } |
| 296 | if (vflip) { |
| 297 | dst_rect.size.y = -dst_rect.size.y; |
| 298 | } |
| 299 | |
| 300 | texture->draw_rect_region(ci, dst_rect, Rect2(Vector2(), texture->get_size()), Color(1, 1, 1), false); |
| 301 | } break; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void AnimatedSprite2D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) { |
| 306 | if (frames == p_frames) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | if (frames.is_valid()) { |
| 311 | frames->disconnect(CoreStringName(changed), callable_mp(this, &AnimatedSprite2D::_res_changed)); |
| 312 | } |
| 313 | stop(); |
| 314 | frames = p_frames; |
| 315 | if (frames.is_valid()) { |
| 316 | frames->connect(CoreStringName(changed), callable_mp(this, &AnimatedSprite2D::_res_changed)); |
| 317 | |
| 318 | List<StringName> al; |
| 319 | frames->get_animation_list(&al); |
| 320 | if (al.is_empty()) { |
| 321 | set_animation(StringName()); |
| 322 | autoplay = String(); |
| 323 | } else { |
| 324 | if (!frames->has_animation(animation)) { |
| 325 | set_animation(al.front()->get()); |
| 326 | } |
| 327 | if (!frames->has_animation(autoplay)) { |
| 328 | autoplay = String(); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | notify_property_list_changed(); |
| 334 | queue_redraw(); |
| 335 | update_configuration_warnings(); |
| 336 | emit_signal("sprite_frames_changed"); |
| 337 | } |
| 338 | |
| 339 | Ref<SpriteFrames> AnimatedSprite2D::get_sprite_frames() const { |
| 340 | return frames; |
| 341 | } |
| 342 | |
| 343 | void AnimatedSprite2D::set_frame(int p_frame) { |
| 344 | set_frame_and_progress(p_frame, std::signbit(get_playing_speed()) ? 1.0 : 0.0); |
| 345 | } |
| 346 | |
| 347 | int AnimatedSprite2D::get_frame() const { |
| 348 | return frame; |
| 349 | } |
| 350 | |
| 351 | void AnimatedSprite2D::set_frame_progress(real_t p_progress) { |
| 352 | frame_progress = p_progress; |
| 353 | } |
| 354 | |
| 355 | real_t AnimatedSprite2D::get_frame_progress() const { |
| 356 | return frame_progress; |
| 357 | } |
| 358 | |
| 359 | void AnimatedSprite2D::set_frame_and_progress(int p_frame, real_t p_progress) { |
| 360 | if (frames.is_null()) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | bool has_animation = frames->has_animation(animation); |
| 365 | int end_frame = has_animation ? MAX(0, frames->get_frame_count(animation) - 1) : 0; |
| 366 | bool is_changed = frame != p_frame; |
| 367 | |
| 368 | if (p_frame < 0) { |
| 369 | frame = 0; |
| 370 | } else if (has_animation && p_frame > end_frame) { |
| 371 | frame = end_frame; |
| 372 | } else { |
| 373 | frame = p_frame; |
| 374 | } |
| 375 | |
| 376 | _calc_frame_speed_scale(); |
| 377 | frame_progress = p_progress; |
| 378 | |
| 379 | if (!is_changed) { |
| 380 | return; // No change, don't redraw. |
| 381 | } |
| 382 | queue_redraw(); |
| 383 | emit_signal(SceneStringName(frame_changed)); |
| 384 | } |
| 385 | |
| 386 | void AnimatedSprite2D::set_speed_scale(float p_speed_scale) { |
| 387 | speed_scale = p_speed_scale; |
| 388 | } |
| 389 | |
| 390 | float AnimatedSprite2D::get_speed_scale() const { |
| 391 | return speed_scale; |
| 392 | } |
| 393 | |
| 394 | float AnimatedSprite2D::get_playing_speed() const { |
| 395 | if (!playing) { |
| 396 | return 0; |
| 397 | } |
| 398 | return speed_scale * custom_speed_scale; |
| 399 | } |
| 400 | |
| 401 | void AnimatedSprite2D::set_centered(bool p_center) { |
| 402 | if (centered == p_center) { |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | centered = p_center; |
| 407 | queue_redraw(); |
| 408 | item_rect_changed(); |
| 409 | } |
| 410 | |
| 411 | bool AnimatedSprite2D::is_centered() const { |
| 412 | return centered; |
| 413 | } |
| 414 | |
| 415 | void AnimatedSprite2D::set_offset(const Point2 &p_offset) { |
| 416 | if (offset == p_offset) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | offset = p_offset; |
| 421 | queue_redraw(); |
| 422 | item_rect_changed(); |
| 423 | } |
| 424 | |
| 425 | Point2 AnimatedSprite2D::get_offset() const { |
| 426 | return offset; |
| 427 | } |
| 428 | |
| 429 | void AnimatedSprite2D::set_flip_h(bool p_flip) { |
| 430 | if (hflip == p_flip) { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | hflip = p_flip; |
| 435 | queue_redraw(); |
| 436 | } |
| 437 | |
| 438 | bool AnimatedSprite2D::is_flipped_h() const { |
| 439 | return hflip; |
| 440 | } |
| 441 | |
| 442 | void AnimatedSprite2D::set_flip_v(bool p_flip) { |
| 443 | if (vflip == p_flip) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | vflip = p_flip; |
| 448 | queue_redraw(); |
| 449 | } |
| 450 | |
| 451 | bool AnimatedSprite2D::is_flipped_v() const { |
| 452 | return vflip; |
| 453 | } |
| 454 | |
| 455 | void AnimatedSprite2D::_res_changed() { |
| 456 | set_frame_and_progress(frame, frame_progress); |
| 457 | queue_redraw(); |
| 458 | notify_property_list_changed(); |
| 459 | } |
| 460 | |
| 461 | bool AnimatedSprite2D::is_playing() const { |
| 462 | return playing; |
| 463 | } |
| 464 | |
| 465 | void AnimatedSprite2D::set_autoplay(const String &p_name) { |
| 466 | if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) { |
| 467 | WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect."); |
| 468 | } |
| 469 | |
| 470 | autoplay = p_name; |
| 471 | } |
| 472 | |
| 473 | String AnimatedSprite2D::get_autoplay() const { |
| 474 | return autoplay; |
| 475 | } |
| 476 | |
| 477 | void AnimatedSprite2D::play(const StringName &p_name, float p_custom_scale, bool p_from_end) { |
| 478 | StringName name = p_name; |
| 479 | |
| 480 | if (name == StringName()) { |
| 481 | name = animation; |
| 482 | } |
| 483 | |
| 484 | ERR_FAIL_COND_MSG(frames.is_null(), vformat("There is no animation with name '%s'.", name)); |
| 485 | ERR_FAIL_COND_MSG(!frames->get_animation_names().has(name), vformat("There is no animation with name '%s'.", name)); |
| 486 | |
| 487 | if (frames->get_frame_count(name) == 0) { |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | playing = true; |
| 492 | custom_speed_scale = p_custom_scale; |
| 493 | |
| 494 | if (name != animation) { |
| 495 | animation = name; |
| 496 | int end_frame = MAX(0, frames->get_frame_count(animation) - 1); |
| 497 | |
| 498 | if (p_from_end) { |
| 499 | set_frame_and_progress(end_frame, 1.0); |
| 500 | } else { |
| 501 | set_frame_and_progress(0, 0.0); |
| 502 | } |
| 503 | emit_signal(SceneStringName(animation_changed)); |
| 504 | } else { |
| 505 | int end_frame = MAX(0, frames->get_frame_count(animation) - 1); |
| 506 | bool is_backward = std::signbit(speed_scale * custom_speed_scale); |
| 507 | |
| 508 | if (p_from_end && is_backward && frame == 0 && frame_progress <= 0.0) { |
| 509 | set_frame_and_progress(end_frame, 1.0); |
| 510 | } else if (!p_from_end && !is_backward && frame == end_frame && frame_progress >= 1.0) { |
| 511 | set_frame_and_progress(0, 0.0); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | set_process_internal(true); |
| 516 | notify_property_list_changed(); |
| 517 | queue_redraw(); |
| 518 | } |
| 519 | |
| 520 | void AnimatedSprite2D::play_backwards(const StringName &p_name) { |
| 521 | play(p_name, -1, true); |
| 522 | } |
| 523 | |
| 524 | void AnimatedSprite2D::_stop_internal(bool p_reset) { |
| 525 | playing = false; |
| 526 | if (p_reset) { |
| 527 | custom_speed_scale = 1.0; |
| 528 | set_frame_and_progress(0, 0.0); |
| 529 | } |
| 530 | notify_property_list_changed(); |
| 531 | set_process_internal(false); |
| 532 | } |
| 533 | |
| 534 | void AnimatedSprite2D::pause() { |
| 535 | _stop_internal(false); |
| 536 | } |
| 537 | |
| 538 | void AnimatedSprite2D::stop() { |
| 539 | _stop_internal(true); |
| 540 | } |
| 541 | |
| 542 | double AnimatedSprite2D::_get_frame_duration() { |
| 543 | if (frames.is_valid() && frames->has_animation(animation)) { |
| 544 | return frames->get_frame_duration(animation, frame); |
| 545 | } |
| 546 | return 1.0; |
| 547 | } |
| 548 | |
| 549 | void AnimatedSprite2D::_calc_frame_speed_scale() { |
| 550 | frame_speed_scale = 1.0 / _get_frame_duration(); |
| 551 | } |
| 552 | |
| 553 | void AnimatedSprite2D::set_animation(const StringName &p_name) { |
| 554 | if (animation == p_name) { |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | animation = p_name; |
| 559 | |
| 560 | emit_signal(SceneStringName(animation_changed)); |
| 561 | |
| 562 | if (frames.is_null()) { |
| 563 | animation = StringName(); |
| 564 | stop(); |
| 565 | ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name)); |
| 566 | } |
| 567 | |
| 568 | if (animation == StringName() || frames->get_frame_count(animation) == 0) { |
| 569 | stop(); |
| 570 | return; |
| 571 | } else if (!frames->get_animation_names().has(animation)) { |
| 572 | animation = StringName(); |
| 573 | stop(); |
| 574 | ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name)); |
| 575 | } |
| 576 | |
| 577 | if (std::signbit(get_playing_speed())) { |
| 578 | set_frame_and_progress(frames->get_frame_count(animation) - 1, 1.0); |
| 579 | } else { |
| 580 | set_frame_and_progress(0, 0.0); |
| 581 | } |
| 582 | |
| 583 | notify_property_list_changed(); |
| 584 | queue_redraw(); |
| 585 | } |
| 586 | |
| 587 | StringName AnimatedSprite2D::get_animation() const { |
| 588 | return animation; |
| 589 | } |
| 590 | |
| 591 | PackedStringArray AnimatedSprite2D::get_configuration_warnings() const { |
| 592 | PackedStringArray warnings = Node2D::get_configuration_warnings(); |
| 593 | if (frames.is_null()) { |
| 594 | warnings.push_back(RTR("A SpriteFrames resource must be created or set in the \"Sprite Frames\" property in order for AnimatedSprite2D to display frames.")); |
| 595 | } |
| 596 | return warnings; |
| 597 | } |
| 598 | |
| 599 | #ifdef TOOLS_ENABLED |
| 600 | void AnimatedSprite2D::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { |
| 601 | const String pf = p_function; |
| 602 | if (p_idx == 0 && frames.is_valid()) { |
| 603 | if (pf == "play" || pf == "play_backwards" || pf == "set_animation" || pf == "set_autoplay") { |
| 604 | List<StringName> al; |
| 605 | frames->get_animation_list(&al); |
| 606 | for (const StringName &name : al) { |
| 607 | r_options->push_back(String(name).quote()); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | Node2D::get_argument_options(p_function, p_idx, r_options); |
| 612 | } |
| 613 | #endif // TOOLS_ENABLED |
| 614 | |
| 615 | #ifndef DISABLE_DEPRECATED |
| 616 | bool AnimatedSprite2D::_set(const StringName &p_name, const Variant &p_value) { |
| 617 | if ((p_name == SNAME("frames"))) { |
| 618 | set_sprite_frames(p_value); |
| 619 | return true; |
| 620 | } |
| 621 | return false; |
| 622 | } |
| 623 | #endif |
| 624 | void AnimatedSprite2D::_bind_methods() { |
| 625 | ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite2D::set_sprite_frames); |
| 626 | ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite2D::get_sprite_frames); |
| 627 | |
| 628 | ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimatedSprite2D::set_animation); |
| 629 | ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite2D::get_animation); |
| 630 | |
| 631 | ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimatedSprite2D::set_autoplay); |
| 632 | ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimatedSprite2D::get_autoplay); |
| 633 | |
| 634 | ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite2D::is_playing); |
| 635 | |
| 636 | ClassDB::bind_method(D_METHOD("play", "name", "custom_speed", "from_end"), &AnimatedSprite2D::play, DEFVAL(StringName()), DEFVAL(1.0), DEFVAL(false)); |
| 637 | ClassDB::bind_method(D_METHOD("play_backwards", "name"), &AnimatedSprite2D::play_backwards, DEFVAL(StringName())); |
| 638 | ClassDB::bind_method(D_METHOD("pause"), &AnimatedSprite2D::pause); |
| 639 | ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite2D::stop); |
| 640 | |
| 641 | ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite2D::set_centered); |
| 642 | ClassDB::bind_method(D_METHOD("is_centered"), &AnimatedSprite2D::is_centered); |
| 643 | |
| 644 | ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AnimatedSprite2D::set_offset); |
| 645 | ClassDB::bind_method(D_METHOD("get_offset"), &AnimatedSprite2D::get_offset); |
| 646 | |
| 647 | ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &AnimatedSprite2D::set_flip_h); |
| 648 | ClassDB::bind_method(D_METHOD("is_flipped_h"), &AnimatedSprite2D::is_flipped_h); |
| 649 | |
| 650 | ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &AnimatedSprite2D::set_flip_v); |
| 651 | ClassDB::bind_method(D_METHOD("is_flipped_v"), &AnimatedSprite2D::is_flipped_v); |
| 652 | |
| 653 | ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite2D::set_frame); |
| 654 | ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite2D::get_frame); |
| 655 | |
| 656 | ClassDB::bind_method(D_METHOD("set_frame_progress", "progress"), &AnimatedSprite2D::set_frame_progress); |
| 657 | ClassDB::bind_method(D_METHOD("get_frame_progress"), &AnimatedSprite2D::get_frame_progress); |
| 658 | |
| 659 | ClassDB::bind_method(D_METHOD("set_frame_and_progress", "frame", "progress"), &AnimatedSprite2D::set_frame_and_progress); |
| 660 | |
| 661 | ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite2D::set_speed_scale); |
| 662 | ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite2D::get_speed_scale); |
| 663 | ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimatedSprite2D::get_playing_speed); |
| 664 | |
| 665 | ADD_SIGNAL(MethodInfo("sprite_frames_changed")); |
| 666 | ADD_SIGNAL(MethodInfo("animation_changed")); |
| 667 | ADD_SIGNAL(MethodInfo("frame_changed")); |
| 668 | ADD_SIGNAL(MethodInfo("animation_looped")); |
| 669 | ADD_SIGNAL(MethodInfo("animation_finished")); |
| 670 | |
| 671 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "sprite_frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames"); |
| 672 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation", PROPERTY_HINT_ENUM, ""), "set_animation", "get_animation"); |
| 673 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay"); |
| 674 | ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame"); |
| 675 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frame_progress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_frame_progress", "get_frame_progress"); |
| 676 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale"), "set_speed_scale", "get_speed_scale"); |
| 677 | ADD_GROUP("Offset", ""); |
| 678 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered"); |
| 679 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset"); |
| 680 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h"); |
| 681 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v"); |
| 682 | } |
| 683 | |
| 684 | AnimatedSprite2D::AnimatedSprite2D() { |
| 685 | } |