krampus19/gut/animation.go

50 lines
921 B
Go
Raw Normal View History

package gut
import "time"
type Animation interface {
Animate(start, now time.Duration) bool
}
type AnimationDoneFn func()
type Animations struct {
anis []animation
}
type animation struct {
start time.Duration
ani Animation
done AnimationDoneFn
}
func (a *Animations) Idle() bool {
return len(a.anis) == 0
}
func (a *Animations) Start(now time.Duration, ani Animation) {
a.StartFn(now, ani, nil)
}
func (a *Animations) StartFn(now time.Duration, ani Animation, done AnimationDoneFn) {
a.anis = append(a.anis, animation{now, ani, done})
}
func (a *Animations) Animate(now time.Duration) {
active := make([]animation, 0, len(a.anis))
done := make([]animation, 0)
for _, ani := range a.anis {
if ani.ani.Animate(ani.start, now) {
active = append(active, ani)
} else {
done = append(done, ani)
}
}
a.anis = active
for _, ani := range done {
if ani.done != nil {
ani.done()
}
}
}