Skip to content

Commit

Permalink
Merge pull request #5344 from dweymouth/infinite-animation
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth authored Jan 2, 2025
2 parents c0a8fc3 + 7f67e29 commit 0c59609
Showing 1 changed file with 47 additions and 0 deletions.
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)
}

// InfiniteAnimation represents an animation that continues infinitely unless stopped. 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 InfiniteAnimation struct {
Tick func()

isSetup bool
animation Animation
}

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

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

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

func (i *InfiniteAnimation) 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

0 comments on commit 0c59609

Please sign in to comment.