/
anim.go
164 lines (132 loc) · 3.12 KB
/
anim.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package engine
import (
"fmt"
"golang.org/x/exp/shiny/screen"
"image"
"sync"
"time"
)
// Anim is an animated image. An Anim can also be a static image by
// simply having a single frame. An Anim is normally initialized by a
// State.
type Anim struct {
image screen.Texture
cur image.Rectangle
// TODO: Find a cleaner way to do this.
m sync.Mutex
delay chan time.Duration
done chan struct{}
}
func newAnim(tex screen.Texture, frameW int) (*Anim, error) {
bnds := tex.Bounds()
if bnds.Dx()%frameW != 0 {
return nil, &InvalidFrameWidthError{bnds.Dx(), frameW}
}
return &Anim{
image: tex,
cur: image.Rect(bnds.Min.X, bnds.Min.Y, frameW, bnds.Dy()),
}, nil
}
// Copy returns a new Anim which references the same image in memory.
func (anim *Anim) Copy() *Anim {
anim.m.Lock()
defer anim.m.Unlock()
bnds := anim.image.Bounds()
return &Anim{
image: anim.image,
cur: image.Rectangle{
Min: bnds.Min,
Max: bnds.Min.Add(anim.cur.Size()),
},
}
}
func (anim *Anim) advance(loop bool) {
anim.m.Lock()
defer anim.m.Unlock()
s := anim.image.Size()
w := anim.cur.Dx()
anim.cur.Min.X += w
anim.cur.Max.X += w
if anim.cur.Min.X >= s.X {
if !loop {
anim.cur.Min.X -= w
anim.cur.Max.X -= w
anim.Stop()
return
}
anim.cur.Min.X, anim.cur.Max.X = 0, w
}
}
func (anim *Anim) animate(done <-chan struct{}, delay time.Duration, loop bool) {
t := time.NewTicker(delay)
last := delay
for {
select {
case <-t.C:
anim.advance(loop)
case delay := <-anim.delay:
if delay == last {
continue
}
t.Stop()
t = time.NewTicker(delay)
last = delay
case <-done:
t.Stop()
return
}
}
}
// Start starts the animation, delaying by the specified amount
// between frames. If the animation has already been started, it
// adjusts the delay of the running animation.
//
// If loop is true, then the animation will loop; otherwise, it will
// stop the animation on the last frame. If the animation is already
// running, loop has no effect.
func (anim *Anim) Start(delay time.Duration, loop bool) {
if anim.done != nil {
anim.delay <- delay
return
}
anim.delay = make(chan time.Duration)
anim.done = make(chan struct{})
go anim.animate(anim.done, delay, loop)
}
// Stop stops the running animation. If the animation isn't currently
// running, it does nothing.
func (anim *Anim) Stop() {
if anim.done == nil {
return
}
select {
case <-anim.done:
default:
close(anim.done)
}
anim.done = nil
}
// Frames returns the number of frames in the animation.
func (anim *Anim) Frames() int {
anim.m.Lock()
defer anim.m.Unlock()
return anim.image.Size().X / anim.cur.Dx()
}
// Size returns the size of one frame of the animation.
func (anim *Anim) Size() image.Point {
anim.m.Lock()
defer anim.m.Unlock()
return anim.cur.Size()
}
func (anim *Anim) Image() (screen.Texture, image.Rectangle) {
anim.m.Lock()
defer anim.m.Unlock()
return anim.image, anim.cur
}
type InvalidFrameWidthError struct {
ImageW int
FrameW int
}
func (err InvalidFrameWidthError) Error() string {
return fmt.Sprintf("Image width (%v) is not divisible by frame width (%v)", err.ImageW, err.FrameW)
}