From dff22c919ae7da323288eff67c46592b069fc8d7 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sat, 28 Dec 2024 13:08:34 -0800 Subject: [PATCH 1/6] add IndefiniteAnimation --- animation.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/animation.go b/animation.go index 2883774390..2d6cb141b1 100644 --- a/animation.go +++ b/animation.go @@ -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. @@ -53,16 +64,48 @@ 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) } +// 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() + }, + RepeatCount: AnimationRepeatForever, + } + i.isSetup = true + } +} + func animationEaseIn(val float32) float32 { return val * val } From 9e800343d1a9104712bea346bff09c7d34388739 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sat, 28 Dec 2024 13:13:52 -0800 Subject: [PATCH 2/6] fully initialize animation --- animation.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/animation.go b/animation.go index 2d6cb141b1..54713bd185 100644 --- a/animation.go +++ b/animation.go @@ -100,6 +100,8 @@ func (i *IndefiniteAnimation) setupAnimation() { 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 From 274dc9d0292bf3f6703c133eb293109c4392eed7 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sun, 29 Dec 2024 08:25:55 -0800 Subject: [PATCH 3/6] use early return --- animation.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/animation.go b/animation.go index 54713bd185..1655edb66c 100644 --- a/animation.go +++ b/animation.go @@ -95,17 +95,19 @@ func (i *IndefiniteAnimation) 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 + 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 { From 4042e027bb5c148a046bcad8bf2496d9b75839a6 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sun, 29 Dec 2024 08:26:44 -0800 Subject: [PATCH 4/6] Reorder functions --- animation.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/animation.go b/animation.go index 1655edb66c..f27c9bfaa4 100644 --- a/animation.go +++ b/animation.go @@ -77,17 +77,17 @@ func (a *Animation) Start() { CurrentApp().Driver().StartAnimation(a) } +// Stop will end this animation and remove it from the run-loop. +func (a *Animation) Stop() { + CurrentApp().Driver().StopAnimation(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) -} - // Stop will end this animation and remove it from the run-loop. func (i *IndefiniteAnimation) Stop() { i.setupAnimation() From 65ef3640d9eade1444ab37e27f3808077574e958 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Tue, 31 Dec 2024 14:01:52 -0800 Subject: [PATCH 5/6] update file ordering --- animation.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/animation.go b/animation.go index f27c9bfaa4..981b19f12a 100644 --- a/animation.go +++ b/animation.go @@ -44,17 +44,6 @@ 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. @@ -64,14 +53,6 @@ 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) @@ -82,6 +63,25 @@ 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() From 7f67e292d689ce4e9711400b7dd4b536c4e857fb Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Wed, 1 Jan 2025 09:09:58 -0800 Subject: [PATCH 6/6] rename Indefinite -> Infinite --- animation.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/animation.go b/animation.go index 981b19f12a..9cbd691b4e 100644 --- a/animation.go +++ b/animation.go @@ -63,38 +63,38 @@ func (a *Animation) Stop() { CurrentApp().Driver().StopAnimation(a) } -// IndefiniteAnimation represents an animation that continues indefinitely. It has no duration +// 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 IndefiniteAnimation struct { +type InfiniteAnimation struct { Tick func() isSetup bool animation Animation } -// NewIndefiniteAnimation creates an indefinite animation where the callback function will be called +// NewInfiniteAnimation creates an infinite 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} +func NewInfiniteAnimation(fn func()) *InfiniteAnimation { + return &InfiniteAnimation{Tick: fn} } // Start registers the animation with the application run-loop and starts its execution. -func (i *IndefiniteAnimation) Start() { +func (i *InfiniteAnimation) Start() { i.setupAnimation() i.animation.Start() } // Stop will end this animation and remove it from the run-loop. -func (i *IndefiniteAnimation) Stop() { +func (i *InfiniteAnimation) Stop() { i.setupAnimation() i.animation.Stop() } -func (i *IndefiniteAnimation) setupAnimation() { +func (i *InfiniteAnimation) setupAnimation() { if i.isSetup { return }