2019-12-23 14:54:15 +00:00
|
|
|
package gut
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type Animation interface {
|
|
|
|
Animate(start, now time.Duration) bool
|
|
|
|
}
|
|
|
|
|
2019-12-23 17:10:11 +00:00
|
|
|
type AnimationDoneFn func()
|
|
|
|
|
2019-12-23 14:54:15 +00:00
|
|
|
type Animations struct {
|
|
|
|
anis []animation
|
|
|
|
}
|
|
|
|
|
|
|
|
type animation struct {
|
|
|
|
start time.Duration
|
|
|
|
ani Animation
|
2019-12-23 17:10:11 +00:00
|
|
|
done AnimationDoneFn
|
2019-12-23 14:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animations) Idle() bool {
|
|
|
|
return len(a.anis) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animations) Start(now time.Duration, ani Animation) {
|
2019-12-23 17:10:11 +00:00
|
|
|
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})
|
2019-12-23 14:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animations) Animate(now time.Duration) {
|
|
|
|
active := make([]animation, 0, len(a.anis))
|
2019-12-23 17:10:11 +00:00
|
|
|
done := make([]animation, 0)
|
2019-12-23 14:54:15 +00:00
|
|
|
for _, ani := range a.anis {
|
|
|
|
if ani.ani.Animate(ani.start, now) {
|
|
|
|
active = append(active, ani)
|
2019-12-23 17:10:11 +00:00
|
|
|
} else {
|
|
|
|
done = append(done, ani)
|
2019-12-23 14:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
a.anis = active
|
2019-12-23 17:10:11 +00:00
|
|
|
for _, ani := range done {
|
|
|
|
if ani.done != nil {
|
|
|
|
ani.done()
|
|
|
|
}
|
|
|
|
}
|
2019-12-23 14:54:15 +00:00
|
|
|
}
|