2020-05-10 15:16:18 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type Animation struct {
|
|
|
|
active bool
|
|
|
|
start time.Time
|
|
|
|
interval time.Duration
|
|
|
|
lastUpdate time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnimation(interval time.Duration) Animation {
|
|
|
|
return Animation{
|
|
|
|
active: true,
|
|
|
|
start: time.Now(),
|
|
|
|
interval: interval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnimationPtr(interval time.Duration) *Animation {
|
|
|
|
ani := NewAnimation(interval)
|
|
|
|
return &ani
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) Animate() bool {
|
|
|
|
since := time.Since(a.start)
|
|
|
|
if !a.active || since < a.lastUpdate+a.interval {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
a.lastUpdate += a.interval
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) AnimateFn(fn func()) {
|
|
|
|
since := time.Since(a.start)
|
|
|
|
for a.active && since > a.lastUpdate+a.interval {
|
|
|
|
fn()
|
|
|
|
a.lastUpdate += a.interval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) Pause() {
|
|
|
|
a.active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) Run() {
|
|
|
|
if a.active {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.active = true
|
|
|
|
a.start = time.Now()
|
2020-05-11 12:41:42 +00:00
|
|
|
a.lastUpdate = 0
|
2020-05-10 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) SetInterval(interval time.Duration) {
|
|
|
|
a.interval = interval
|
|
|
|
}
|