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 5 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
47 changes: 47 additions & 0 deletions animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,53 @@ func (a *Animation) Stop() {
CurrentApp().Driver().StopAnimation(a)
}

// 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
}

// 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 (i *IndefiniteAnimation) Start() {
i.setupAnimation()
i.animation.Start()
}

// 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 {
return
}

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
}

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