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 a new InfiniteAnimation #5344

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Changes from 2 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
45 changes: 45 additions & 0 deletions animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ type Animation struct {
Tick func(float32)
}

// IndefiniteAnimation represents an animation that continues indefinitely. It has no duration
// or curve, and when started, the Tick function will be called every frame until Stop is invoked.
//
// Since: 2.6
type IndefiniteAnimation struct {
Tick func()

isSetup bool
animation Animation
}

// NewAnimation creates a very basic animation where the callback function will be called for every
// rendered frame between [time.Now] and the specified duration. The callback values start at 0.0 and
// will be 1.0 when the animation completes.
Expand All @@ -53,16 +64,50 @@ func NewAnimation(d time.Duration, fn func(float32)) *Animation {
return &Animation{Duration: d, Tick: fn}
}

// NewIndefiniteAnimation creates an indefinite animation where the callback function will be called
// for every rendered frame once started, until stopped.
//
// Since: 2.6
func NewIndefiniteAnimation(fn func()) *IndefiniteAnimation {
return &IndefiniteAnimation{Tick: fn}
}

// Start registers the animation with the application run-loop and starts its execution.
func (a *Animation) Start() {
CurrentApp().Driver().StartAnimation(a)
}

// Start registers the animation with the application run-loop and starts its execution.
func (i *IndefiniteAnimation) Start() {
i.setupAnimation()
i.animation.Start()
}

// Stop will end this animation and remove it from the run-loop.
func (a *Animation) Stop() {
CurrentApp().Driver().StopAnimation(a)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of how the methods of different types are interleaved. I'd much rather have all methods for one typ after each other in alphabetical order before the next type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: I'd rather have struct, methods, struct, methods and so on. I think that's how Go files generally are structured. Without your latest change it is struct, struct, methods, methods?


// Stop will end this animation and remove it from the run-loop.
func (i *IndefiniteAnimation) Stop() {
i.setupAnimation()
i.animation.Stop()
}

func (i *IndefiniteAnimation) setupAnimation() {
if !i.isSetup {
i.animation = Animation{
Tick: func(_ float32) {
i.Tick()
},
Curve: AnimationLinear, // any curve will work
Duration: 1 * time.Second, // anything positive here will work
RepeatCount: AnimationRepeatForever,
}
i.isSetup = true
}
dweymouth marked this conversation as resolved.
Show resolved Hide resolved
}

func animationEaseIn(val float32) float32 {
return val * val
}
Expand Down
Loading