Skip to content

Commit

Permalink
Harmonise AnimatedSprite3D and its 2D counterpart
Browse files Browse the repository at this point in the history
Add the following properties to AnimatedSprite3D:
- `backwards` parameter in `play()`;
- `speed_scale`.

Both classes' internals are more similar, down to the line spacings. They've also been updated to be clearer and less inconsistent (e.g. `!frames.is_valid()` -> `frames.is_null()`, use SceneStringNames instead of CoreStringNames, rename the internal _queue_update to _queue_redraw)
  • Loading branch information
Mickeon committed Sep 9, 2022
1 parent 8b79a19 commit b648ee4
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 91 deletions.
6 changes: 5 additions & 1 deletion doc/classes/AnimatedSprite3D.xml
Expand Up @@ -13,8 +13,9 @@
<method name="play">
<return type="void" />
<param index="0" name="anim" type="StringName" default="&amp;&quot;&quot;" />
<param index="1" name="backwards" type="bool" default="false" />
<description>
Plays the animation named [param anim]. If no [param anim] is provided, the current animation is played.
Plays the animation named [param anim]. If no [param anim] is provided, the current animation is played. If [param backwards] is [code]true[/code], the animation will be played in reverse.
</description>
</method>
<method name="stop">
Expand All @@ -37,6 +38,9 @@
<member name="playing" type="bool" setter="set_playing" getter="is_playing" default="false">
If [code]true[/code], the [member animation] is currently playing.
</member>
<member name="speed_scale" type="float" setter="set_speed_scale" getter="get_speed_scale" default="1.0">
The animation speed is multiplied by this value.
</member>
</members>
<signals>
<signal name="animation_finished">
Expand Down
58 changes: 32 additions & 26 deletions scene/2d/animated_sprite_2d.cpp
Expand Up @@ -175,33 +175,38 @@ void AnimatedSprite2D::_notification(int p_what) {
if (timeout <= 0) {
timeout = _get_frame_duration();

int fc = frames->get_frame_count(animation);
if ((!backwards && frame >= fc - 1) || (backwards && frame <= 0)) {
if (frames->get_animation_loop(animation)) {
if (backwards) {
frame = fc - 1;
} else {
frame = 0;
}

emit_signal(SceneStringNames::get_singleton()->animation_finished);
} else {
if (backwards) {
int last_frame = frames->get_frame_count(animation) - 1;
if (!backwards) {
// Forward.
if (frame >= last_frame) {
if (frames->get_animation_loop(animation)) {
frame = 0;
} else {
frame = fc - 1;
}

if (!is_over) {
is_over = true;
emit_signal(SceneStringNames::get_singleton()->animation_finished);
} else {
frame = last_frame;
if (!is_over) {
is_over = true;
emit_signal(SceneStringNames::get_singleton()->animation_finished);
}
}
} else {
frame++;
}
} else {
if (backwards) {
frame--;
// Reversed.
if (frame <= 0) {
if (frames->get_animation_loop(animation)) {
frame = last_frame;
emit_signal(SceneStringNames::get_singleton()->animation_finished);
} else {
frame = 0;
if (!is_over) {
is_over = true;
emit_signal(SceneStringNames::get_singleton()->animation_finished);
}
}
} else {
frame++;
frame--;
}
}

Expand Down Expand Up @@ -259,14 +264,15 @@ void AnimatedSprite2D::_notification(int p_what) {

void AnimatedSprite2D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
if (frames.is_valid()) {
frames->disconnect("changed", callable_mp(this, &AnimatedSprite2D::_res_changed));
frames->disconnect(SceneStringNames::get_singleton()->changed, callable_mp(this, &AnimatedSprite2D::_res_changed));
}

frames = p_frames;
if (frames.is_valid()) {
frames->connect("changed", callable_mp(this, &AnimatedSprite2D::_res_changed));
frames->connect(SceneStringNames::get_singleton()->changed, callable_mp(this, &AnimatedSprite2D::_res_changed));
}

if (!frames.is_valid()) {
if (frames.is_null()) {
frame = 0;
} else {
set_frame(frame);
Expand All @@ -283,7 +289,7 @@ Ref<SpriteFrames> AnimatedSprite2D::get_sprite_frames() const {
}

void AnimatedSprite2D::set_frame(int p_frame) {
if (!frames.is_valid()) {
if (frames.is_null()) {
return;
}

Expand Down Expand Up @@ -318,7 +324,7 @@ void AnimatedSprite2D::set_speed_scale(double p_speed_scale) {

speed_scale = MAX(p_speed_scale, 0.0f);

// We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed
// We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed.
_reset_timeout();
timeout -= elapsed;
}
Expand Down

0 comments on commit b648ee4

Please sign in to comment.