/
main.go
292 lines (277 loc) · 8.12 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// An app that draws and moves triangles on the screen and pushes them on to
// other screens.
//
// See https://github.com/asimshankar/triangles
package main
import (
"fmt"
"github.com/asimshankar/triangles/spec"
"golang.org/x/mobile/app"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/event/touch"
"golang.org/x/mobile/gl"
"log"
"time"
)
func main() {
app.Main(func(a app.App) {
var (
scene Scene
myGL *GL
debug *GLDebug
sz size.Event
touches = make(map[touch.Sequence]*touchEvents) // Active touch events
touchedTriangles = make(map[*spec.Triangle]struct{}) // Triangles currently being touched
chMyScreen = make(chan *spec.Triangle) // New triangles to draw on my screen
leftScreen = newOtherScreen(nil, chMyScreen)
rightScreen = newOtherScreen(nil, chMyScreen)
networkChannels = SetupNetwork(chMyScreen)
spawnTriangle = func(x float32) {
c := scene.TopBanner
scene.Triangles = append(scene.Triangles, &spec.Triangle{
X: x, Y: 1, R: c.R, G: c.G, B: c.B})
}
invitationActive bool
invitation Invitation
invitationTicker *time.Ticker
invitationBannerTicker <-chan time.Time
clearInvitation = func() {
invitationActive = false
invitation = Invitation{}
invitationTicker.Stop()
invitationBannerTicker = nil
scene.LeftBanner = nil
}
)
for {
select {
case ready := <-networkChannels.Ready:
switch v := ready.(type) {
case error:
log.Panic(v)
case Color:
scene.TopBanner = v
spawnTriangle(0)
default:
log.Panicf("Unexpected type from the Ready channel: %T (%v)", ready, ready)
}
networkChannels.Ready = nil // To stop this select clause from being hit again.
case inv := <-networkChannels.Invitations:
invitationActive = true
invitation = inv
invitationTicker = time.NewTicker(time.Second)
invitationBannerTicker = invitationTicker.C
log.Printf("Notifying user of invitation from %v", inv.Name)
case <-invitationBannerTicker:
// Flash the banner
if scene.LeftBanner == nil {
scene.LeftBanner = &invitation.Color
break
}
scene.LeftBanner = nil
case <-invitation.Withdrawn:
log.Printf("Invitation from %v withdrawn", invitation.Name)
clearInvitation()
case ch := <-networkChannels.NewLeftScreen:
leftScreen.close()
leftScreen = newOtherScreen(ch, chMyScreen)
case ch := <-networkChannels.NewRightScreen:
rightScreen.close()
rightScreen = newOtherScreen(ch, chMyScreen)
case t := <-chMyScreen:
scene.Triangles = append(scene.Triangles, t)
case e := <-a.Events():
switch e := a.Filter(e).(type) {
case lifecycle.Event:
switch e.Crosses(lifecycle.StageVisible) {
case lifecycle.CrossOn:
glctx, _ := e.DrawContext.(gl.Context)
var err error
if myGL, err = NewGL(glctx); err != nil {
log.Panic(err)
}
debug = NewGLDebug(glctx)
a.Send(paint.Event{})
case lifecycle.CrossOff:
if exitOnLifecycleCrossOff() {
return
}
myGL.Release()
debug.Release()
debug = nil
myGL = nil
}
case paint.Event:
if e.External {
continue
}
var mine, left, right []*spec.Triangle
// Handle any collisions between triangles.
for i, t1 := range scene.Triangles {
for j := i + 1; j < len(scene.Triangles); j++ {
t2 := scene.Triangles[j]
if dx, dy := (t1.X - t2.X), (t1.Y - t2.Y); dx*dx+dy*dy < triangleSide*triangleSide {
t1.Dx, t2.Dx = t2.Dx, t1.Dx
t1.Dy, t2.Dy = t2.Dy, t1.Dy
}
}
}
for _, t := range scene.Triangles {
if _, touched := touchedTriangles[t]; !touched {
// Only move a triangle if it is not currently being manipulated by the user.
moveTriangle(t)
}
switch {
case t.X < -1:
left = append(left, t)
case t.X > 1:
right = append(right, t)
default:
mine = append(mine, t)
}
}
if len(left) > 0 {
go leftScreen.send(left)
}
if len(right) > 0 {
go rightScreen.send(right)
}
scene.Triangles = mine
myGL.Paint(scene)
debug.Paint(sz)
a.Publish()
a.Send(paint.Event{})
case size.Event:
sz = e
case touch.Event:
switch e.Type {
case touch.TypeBegin:
var (
touchedT *spec.Triangle
x, y = touch2coords(e, sz)
)
for _, t := range scene.Triangles {
if dx, dy := (x - t.X), (y - t.Y); dx*dx+dy*dy < triangleSide*triangleSide {
log.Printf("Triangle %+v touched by user", t)
touchedT = t
touchedTriangles[t] = struct{}{}
break
}
}
touches[e.Sequence] = &touchEvents{Start: e, Triangle: touchedT}
case touch.TypeMove:
tch := touches[e.Sequence]
if tch.Triangle != nil {
tch.Triangle.X, tch.Triangle.Y = touch2coords(e, sz)
}
case touch.TypeEnd:
tch := touches[e.Sequence]
delete(touches, e.Sequence)
x, y := touch2coords(tch.Start, sz)
if t := tch.Triangle; t != nil {
// Set triangle velocity based on movement from the original position.
t.X, t.Y = touch2coords(e, sz)
t.Dx = (e.X - tch.Start.X) / float32(sz.WidthPx)
t.Dy = (e.Y - tch.Start.Y) / float32(sz.HeightPx)
delete(touchedTriangles, t)
break
}
if invitationActive && x < bannerWidth {
// Touched in the left invitation banner:
// Swipe = reject, Tap = accept.
var swipeThreshold = float32(sz.WidthPx) / 2
if dx, dy := (e.X - tch.Start.X), (e.Y - tch.Start.Y); dx*dx+dy*dy > swipeThreshold*swipeThreshold {
log.Printf("Swiped (%v, %v) pixels, rejecting invitation from %q", dx, dy, invitation.Name)
invitation.Response <- fmt.Errorf("user rejected")
} else {
log.Printf("Accepting invitation from %q", invitation.Name)
invitation.Response <- nil
}
clearInvitation()
break
}
if y >= 1-(2*bannerWidth) {
// Touched top banner, spawn a new triangle
log.Printf("Top banner touched, spawning new triangle (Y=%v, threshold=%v)", y, -1+bannerWidth)
spawnTriangle(x)
break
}
}
}
}
}
})
}
type touchEvents struct {
Start touch.Event // Where the touch event began
Triangle *spec.Triangle // The triangle being manipulated by touch, if any
}
type otherScreen struct {
chTriangles chan<- *spec.Triangle
chLost chan struct{}
chSelf chan<- *spec.Triangle
}
func newOtherScreen(other, self chan<- *spec.Triangle) *otherScreen {
return &otherScreen{
chTriangles: other,
chLost: make(chan struct{}),
chSelf: self,
}
}
func (s *otherScreen) close() {
close(s.chLost)
}
func (s *otherScreen) send(triangles []*spec.Triangle) {
if s.chTriangles == nil {
for _, t := range triangles {
returnTriangle(t, s.chSelf)
}
return
}
for i, t := range triangles {
select {
case <-s.chLost:
// Lost the other screen, so reflect the remaining triangles back onto my screen.
for i < len(triangles) {
returnTriangle(t, s.chSelf)
i++
}
return
case s.chTriangles <- t:
}
}
}
// touch2coords transforms coordinates from the touch.Event coordinate system
// to the GL and Triangles coordinate system.
//
// Pixel coordinates <--> GL coordinates;
// (0, 0) <--> (-1, 1) // bottom left
// (W/2, H/2) <--> (0, 0)
// (W, H) <--> (1, -1) // top right
func touch2coords(t touch.Event, sz size.Event) (x, y float32) {
return 2*t.X/float32(sz.WidthPx) - 1, 1 - 2*t.Y/float32(sz.HeightPx)
}
func moveTriangle(t *spec.Triangle) {
t.Dy = t.Dy - gravity
t.X = t.X + t.Dx*timeBetweenPaints
t.Y = t.Y + t.Dy*timeBetweenPaints
if t.Y <= -1 {
t.Dy = -1 * t.Dy
t.Y = -1
} else if maxY := 1 - triangleCenterHeight; t.Y >= maxY {
t.Dy = -1 * t.Dy
t.Y = maxY
}
}
func returnTriangle(t *spec.Triangle, myScreen chan<- *spec.Triangle) {
t.Dx = -1 * t.Dx
moveTriangle(t)
myScreen <- t
}
const (
acceptInvitationDuration = time.Second
gravity = 0.001
timeBetweenPaints = 0.1
)