Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnimatedSprite: Remove play()'s backwards, add methods to compensate #65973

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions doc/classes/AnimatedSprite2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@
<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. If [param backwards] is [code]true[/code], the animation is played in reverse.
Plays the animation named [param anim]. If no [param anim] is provided, the current animation is played.
</description>
</method>
<method name="play_forward">
<return type="void" />
<param index="0" name="anim" type="StringName" default="&amp;&quot;&quot;" />
<description>
Sets [member speed_scale] to a positive value, playing the current animation forward. If [param anim] is provided and is not currently playing, starts [param anim] from the first frame.
</description>
</method>
<method name="play_backwards">
<return type="void" />
<param index="0" name="anim" type="StringName" default="&amp;&quot;&quot;" />
<description>
Sets [member speed_scale] to a negative value, playing the current animation in reverse. If [param anim] is provided and is not currently playing, starts [param anim] from the last frame.
</description>
</method>
<method name="stop">
Expand Down
17 changes: 15 additions & 2 deletions doc/classes/AnimatedSprite3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@
<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. If [param backwards] is [code]true[/code], the animation is played in reverse.
Plays the animation named [param anim]. If no [param anim] is provided, the current animation is played.
</description>
</method>
<method name="play_forward">
<return type="void" />
<param index="0" name="anim" type="StringName" default="&amp;&quot;&quot;" />
<description>
Sets [member speed_scale] to a positive value, playing the current animation forward. If [param anim] is provided and is not currently playing, starts [param anim] from the first frame.
</description>
</method>
<method name="play_backwards">
<return type="void" />
<param index="0" name="anim" type="StringName" default="&amp;&quot;&quot;" />
<description>
Sets [member speed_scale] to a negative value, playing the current animation in reverse. If [param anim] is provided and is not currently playing, starts [param anim] from the last frame.
</description>
</method>
<method name="stop">
Expand Down
37 changes: 28 additions & 9 deletions scene/2d/animated_sprite_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ void AnimatedSprite2D::set_speed_scale(double p_speed_scale) {
double elapsed = _get_frame_duration() - timeout;

speed_scale = p_speed_scale;
playing_backwards = signbit(speed_scale) != backwards;
playing_backwards = signbit(speed_scale);

// We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed.
_reset_timeout();
Expand Down Expand Up @@ -397,15 +397,32 @@ bool AnimatedSprite2D::is_playing() const {
return playing;
}

void AnimatedSprite2D::play(const StringName &p_animation, bool p_backwards) {
backwards = p_backwards;
playing_backwards = signbit(speed_scale) != backwards;

void AnimatedSprite2D::play(const StringName &p_animation) {
if (p_animation) {
set_animation(p_animation);
if (frames.is_valid() && playing_backwards && get_frame() == 0) {
set_frame(frames->get_frame_count(p_animation) - 1);
}
}

is_over = false;
set_playing(true);
}

void AnimatedSprite2D::play_forward(const StringName &p_animation) {
if (speed_scale < 0.0f) {
set_speed_scale(speed_scale * -1);
}

play(p_animation);
}

void AnimatedSprite2D::play_backwards(const StringName &p_animation) {
if (speed_scale > 0.0f) {
set_speed_scale(speed_scale * -1);
}

if (p_animation && animation == p_animation) {
set_animation(p_animation);
// set_animation() also sets frame to 0, so this needs to be done afterwards.
set_frame(frames->get_frame_count(p_animation) - 1);
}

is_over = false;
Expand Down Expand Up @@ -485,7 +502,9 @@ void AnimatedSprite2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_playing", "playing"), &AnimatedSprite2D::set_playing);
ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite2D::is_playing);

ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite2D::play, DEFVAL(StringName()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite2D::play, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("play_forward", "anim"), &AnimatedSprite2D::play_forward, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("play_backwards", "anim"), &AnimatedSprite2D::play_backwards, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite2D::stop);

ClassDB::bind_method(D_METHOD("set_centered", "centered"), &AnimatedSprite2D::set_centered);
Expand Down
5 changes: 3 additions & 2 deletions scene/2d/animated_sprite_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class AnimatedSprite2D : public Node2D {
Ref<SpriteFrames> frames;
bool playing = false;
bool playing_backwards = false;
bool backwards = false;
StringName animation = "default";
int frame = 0;
float speed_scale = 1.0f;
Expand Down Expand Up @@ -82,7 +81,9 @@ class AnimatedSprite2D : public Node2D {
void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
Ref<SpriteFrames> get_sprite_frames() const;

void play(const StringName &p_animation = StringName(), bool p_backwards = false);
void play(const StringName &p_animation = StringName());
void play_forward(const StringName &p_animation = StringName());
void play_backwards(const StringName &p_animation = StringName());
void stop();

void set_playing(bool p_playing);
Expand Down
37 changes: 28 additions & 9 deletions scene/3d/sprite_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ void AnimatedSprite3D::set_speed_scale(double p_speed_scale) {
double elapsed = _get_frame_duration() - timeout;

speed_scale = p_speed_scale;
playing_backwards = signbit(speed_scale) != backwards;
playing_backwards = signbit(speed_scale);

// We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed.
_reset_timeout();
Expand Down Expand Up @@ -1229,15 +1229,32 @@ bool AnimatedSprite3D::is_playing() const {
return playing;
}

void AnimatedSprite3D::play(const StringName &p_animation, bool p_backwards) {
backwards = p_backwards;
playing_backwards = signbit(speed_scale) != backwards;

void AnimatedSprite3D::play(const StringName &p_animation) {
if (p_animation) {
set_animation(p_animation);
if (frames.is_valid() && playing_backwards && get_frame() == 0) {
set_frame(frames->get_frame_count(p_animation) - 1);
}
}

is_over = false;
set_playing(true);
}

void AnimatedSprite3D::play_forward(const StringName &p_animation) {
if (speed_scale < 0.0f) {
set_speed_scale(speed_scale * -1);
}

play(p_animation);
}

void AnimatedSprite3D::play_backwards(const StringName &p_animation) {
if (speed_scale > 0.0f) {
set_speed_scale(speed_scale * -1);
}

if (p_animation && animation == p_animation) {
set_animation(p_animation);
// set_animation() also sets frame to 0, so this needs to be done afterwards.
set_frame(frames->get_frame_count(p_animation) - 1);
}

is_over = false;
Expand Down Expand Up @@ -1315,7 +1332,9 @@ void AnimatedSprite3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_playing", "playing"), &AnimatedSprite3D::set_playing);
ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);

ClassDB::bind_method(D_METHOD("play", "anim", "backwards"), &AnimatedSprite3D::play, DEFVAL(StringName()), DEFVAL(false));
ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite3D::play, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("play_forward", "anim"), &AnimatedSprite3D::play_forward, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("play_backwards", "anim"), &AnimatedSprite3D::play_backwards, DEFVAL(StringName()));
ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);

ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
Expand Down
5 changes: 3 additions & 2 deletions scene/3d/sprite_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class AnimatedSprite3D : public SpriteBase3D {
Ref<SpriteFrames> frames;
bool playing = false;
bool playing_backwards = false;
bool backwards = false;
StringName animation = "default";
int frame = 0;
float speed_scale = 1.0f;
Expand Down Expand Up @@ -238,7 +237,9 @@ class AnimatedSprite3D : public SpriteBase3D {
void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
Ref<SpriteFrames> get_sprite_frames() const;

void play(const StringName &p_animation = StringName(), bool p_backwards = false);
void play(const StringName &p_animation);
void play_forward(const StringName &p_animation);
void play_backwards(const StringName &p_animation);
void stop();

void set_playing(bool p_playing);
Expand Down