/
main.go
165 lines (139 loc) · 3 KB
/
main.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
165
// An app that says which of my kids gets to pick the first bedtime story for the day.
package main
import (
"image"
"log"
"time"
_ "image/jpeg"
"golang.org/x/mobile/app"
"golang.org/x/mobile/asset"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/exp/f32"
"golang.org/x/mobile/exp/gl/glutil"
"golang.org/x/mobile/exp/sprite"
"golang.org/x/mobile/exp/sprite/clock"
"golang.org/x/mobile/exp/sprite/glsprite"
"golang.org/x/mobile/gl"
)
const (
width = 100
height = 100
)
var (
startTime = time.Now()
images *glutil.Images
eng sprite.Engine
scene *sprite.Node
sz size.Event
)
func main() {
app.Main(func(a app.App) {
var glctx gl.Context
for e := range a.Events() {
switch e := a.Filter(e).(type) {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
glctx, _ = e.DrawContext.(gl.Context)
onStart(glctx)
a.Send(paint.Event{})
case lifecycle.CrossOff:
onStop()
glctx = nil
}
case size.Event:
sz = e
case paint.Event:
if glctx == nil || e.External {
continue
}
onPaint(glctx)
a.Publish()
a.Send(paint.Event{}) // keep animating
}
}
})
}
func onStart(glctx gl.Context) {
images = glutil.NewImages(glctx)
eng = glsprite.Engine(images)
loadScene()
}
func onStop() {
eng.Release()
images.Release()
}
func onPaint(glctx gl.Context) {
glctx.ClearColor(1, 1, 1, 1)
glctx.Clear(gl.COLOR_BUFFER_BIT)
now := clock.Time(time.Since(startTime) * 60 / time.Second)
eng.Render(scene, now, sz)
}
func newNode() *sprite.Node {
n := &sprite.Node{}
eng.Register(n)
scene.AppendChild(n)
return n
}
func loadScene() {
kid := loadKid()
scene = &sprite.Node{}
eng.Register(scene)
eng.SetTransform(scene, f32.Affine{
{1, 0, 0},
{0, 1, 0},
})
var x, y float32
dx, dy := float32(1), float32(1)
n := newNode()
// TODO do I need this arranger thing?
n.Arranger = arrangerFunc(func(eng sprite.Engine, n *sprite.Node, t clock.Time) {
eng.SetSubTex(n, kid)
if x < 0 {
dx = 1
}
if y < 0 {
dy = 1
}
if x+width > float32(sz.WidthPt) {
dx = -1
}
if y+height > float32(sz.HeightPt) {
dy = -1
}
x += dx
y += dy
eng.SetTransform(n, f32.Affine{
{width, 0, x},
{0, height, y},
})
})
}
func loadKid() sprite.SubTex {
file := picForDay(time.Now())
a, err := asset.Open(file)
if err != nil {
log.Fatal(err)
}
defer a.Close()
img, _, err := image.Decode(a)
if err != nil {
log.Fatal(err)
}
t, err := eng.LoadTexture(img)
if err != nil {
log.Fatal(err)
}
return sprite.SubTex{T: t, R: image.Rect(0, 0, 466, 466)}
}
type arrangerFunc func(e sprite.Engine, n *sprite.Node, t clock.Time)
func (a arrangerFunc) Arrange(e sprite.Engine, n *sprite.Node, t clock.Time) { a(e, n, t) }
func picForDay(day time.Time) string {
d := day.Sub(time.Date(2016, time.January, 1, 0, 0, 0, 0, time.Local))
if int(d.Hours()/24)%2 == 0 {
return "carter.jpeg"
}
return "kell.jpeg"
}