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() } func (a *Animation) SetInterval(interval time.Duration) { a.interval = interval }