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

Add TRANS_SPRING to Tween #76899

Merged
merged 1 commit into from
May 22, 2023
Merged
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
3 changes: 3 additions & 0 deletions doc/classes/Tween.xml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@
<constant name="TRANS_BACK" value="10" enum="TransitionType">
The animation is interpolated backing out at ends.
</constant>
<constant name="TRANS_SPRING" value="11" enum="TransitionType">
The animation is interpolated like a spring towards the end.
</constant>
<constant name="EASE_IN" value="0" enum="EaseType">
The interpolation starts slowly and speeds up towards the end.
</constant>
Expand Down
29 changes: 29 additions & 0 deletions scene/animation/easing_equations.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,33 @@ static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
}
}; // namespace back

namespace spring {
static real_t out(real_t t, real_t b, real_t c, real_t d) {
t /= d;
real_t s = 1.0 - t;
t = (sin(t * Math_PI * (0.2 + 2.5 * t * t * t)) * pow(s, 2.2) + t) * (1.0 + (1.2 * s));
return c * t + b;
}

static real_t in(real_t t, real_t b, real_t c, real_t d) {
return c - out(d - t, 0, c, d) + b;
}

static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
if (t < d / 2) {
return in(t * 2, b, c / 2, d);
}
real_t h = c / 2;
return out(t * 2 - d, b + h, h, d);
}

static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
if (t < d / 2) {
return out(t * 2, b, c / 2, d);
}
real_t h = c / 2;
return in(t * 2 - d, b + h, h, d);
}
}; // namespace spring

#endif // EASING_EQUATIONS_H
2 changes: 2 additions & 0 deletions scene/animation/tween.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Tween::interpolater Tween::interpolaters[Tween::TRANS_MAX][Tween::EASE_MAX] = {
{ &circ::in, &circ::out, &circ::in_out, &circ::out_in },
{ &bounce::in, &bounce::out, &bounce::in_out, &bounce::out_in },
{ &back::in, &back::out, &back::in_out, &back::out_in },
{ &spring::in, &spring::out, &spring::in_out, &spring::out_in },
};

void Tweener::set_tween(const Ref<Tween> &p_tween) {
Expand Down Expand Up @@ -483,6 +484,7 @@ void Tween::_bind_methods() {
BIND_ENUM_CONSTANT(TRANS_CIRC);
BIND_ENUM_CONSTANT(TRANS_BOUNCE);
BIND_ENUM_CONSTANT(TRANS_BACK);
BIND_ENUM_CONSTANT(TRANS_SPRING);

BIND_ENUM_CONSTANT(EASE_IN);
BIND_ENUM_CONSTANT(EASE_OUT);
Expand Down
1 change: 1 addition & 0 deletions scene/animation/tween.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Tween : public RefCounted {
TRANS_CIRC,
TRANS_BOUNCE,
TRANS_BACK,
TRANS_SPRING,
TRANS_MAX
};

Expand Down