-
Notifications
You must be signed in to change notification settings - Fork 28
/
event.go
406 lines (360 loc) · 9.61 KB
/
event.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import (
"log"
"sync"
"github.com/goplus/spx/internal/coroutine"
)
// -------------------------------------------------------------------------------------
type eventSink struct {
prev *eventSink
pthis threadObj
cond func(interface{}) bool
sink interface{}
}
func (p *eventSink) doDeleteClone(this interface{}) (ret *eventSink) {
ret = p
pp := &ret
for {
p := *pp
if p == nil {
return
}
if p.pthis == this {
*pp = p.prev
} else {
pp = &p.prev
}
}
}
func (p *eventSink) asyncCall(start bool, data interface{}, doSth func(*eventSink)) {
for p != nil {
if p.cond == nil || p.cond(data) {
copy := p
gco.CreateAndStart(start, p.pthis, func(coroutine.Thread) int {
doSth(copy)
return 0
})
}
p = p.prev
}
}
func (p *eventSink) syncCall(data interface{}, doSth func(*eventSink)) {
var wg sync.WaitGroup
for p != nil {
if p.cond == nil || p.cond(data) {
wg.Add(1)
copy := p
gco.CreateAndStart(false, p.pthis, func(coroutine.Thread) int {
defer wg.Done()
doSth(copy)
return 0
})
}
p = p.prev
}
waitToDo(wg.Wait)
}
func (p *eventSink) call(wait bool, data interface{}, doSth func(*eventSink)) {
if wait {
p.syncCall(data, doSth)
} else {
p.asyncCall(false, data, doSth)
}
}
// -------------------------------------------------------------------------------------
type eventSinkMgr struct {
allWhenStart *eventSink
allWhenKeyPressed *eventSink
allWhenIReceive *eventSink
allWhenBackdropChanged *eventSink
allWhenCloned *eventSink
allWhenTouched *eventSink
allWhenClick *eventSink
allWhenMoving *eventSink
allWhenTurning *eventSink
calledStart bool
}
func (p *eventSinkMgr) reset() {
p.allWhenStart = nil
p.allWhenKeyPressed = nil
p.allWhenIReceive = nil
p.allWhenBackdropChanged = nil
p.allWhenCloned = nil
p.allWhenTouched = nil
p.allWhenClick = nil
p.allWhenMoving = nil
p.allWhenTurning = nil
p.calledStart = false
}
func (p *eventSinkMgr) doDeleteClone(this interface{}) {
p.allWhenStart = p.allWhenStart.doDeleteClone(this)
p.allWhenKeyPressed = p.allWhenKeyPressed.doDeleteClone(this)
p.allWhenIReceive = p.allWhenIReceive.doDeleteClone(this)
p.allWhenBackdropChanged = p.allWhenBackdropChanged.doDeleteClone(this)
p.allWhenCloned = p.allWhenCloned.doDeleteClone(this)
p.allWhenTouched = p.allWhenTouched.doDeleteClone(this)
p.allWhenClick = p.allWhenClick.doDeleteClone(this)
p.allWhenMoving = p.allWhenMoving.doDeleteClone(this)
p.allWhenTurning = p.allWhenTurning.doDeleteClone(this)
}
func (p *eventSinkMgr) doWhenStart() {
if !p.calledStart {
p.calledStart = true
p.allWhenStart.asyncCall(false, nil, func(ev *eventSink) {
if debugEvent {
log.Println("==> onStart", nameOf(ev.pthis))
}
ev.sink.(func())()
})
}
}
func (p *eventSinkMgr) doWhenKeyPressed(key Key) {
p.allWhenKeyPressed.asyncCall(false, key, func(ev *eventSink) {
ev.sink.(func(Key))(key)
})
}
func (p *eventSinkMgr) doWhenClick(this threadObj) {
p.allWhenClick.asyncCall(false, this, func(ev *eventSink) {
if debugEvent {
log.Println("==> onClick", nameOf(this))
}
ev.sink.(func())()
})
}
func (p *eventSinkMgr) doWhenTouched(this threadObj, obj *Sprite) {
p.allWhenTouched.asyncCall(false, this, func(ev *eventSink) {
if debugEvent {
log.Println("==> onTouched", nameOf(this), obj.name)
}
ev.sink.(func(*Sprite))(obj)
})
}
func (p *eventSinkMgr) doWhenCloned(this threadObj, data interface{}) {
p.allWhenCloned.asyncCall(true, this, func(ev *eventSink) {
if debugEvent {
log.Println("==> onCloned", nameOf(this))
}
ev.sink.(func(interface{}))(data)
})
}
func (p *eventSinkMgr) doWhenMoving(this threadObj, mi *MovingInfo) {
p.allWhenMoving.asyncCall(true, this, func(ev *eventSink) {
ev.sink.(func(*MovingInfo))(mi)
})
}
func (p *eventSinkMgr) doWhenTurning(this threadObj, mi *TurningInfo) {
p.allWhenTurning.asyncCall(true, this, func(ev *eventSink) {
ev.sink.(func(*TurningInfo))(mi)
})
}
func (p *eventSinkMgr) doWhenIReceive(msg string, data interface{}, wait bool) {
p.allWhenIReceive.call(wait, msg, func(ev *eventSink) {
ev.sink.(func(string, interface{}))(msg, data)
})
}
func (p *eventSinkMgr) doWhenBackdropChanged(name string, wait bool) {
p.allWhenBackdropChanged.call(wait, name, func(ev *eventSink) {
ev.sink.(func(string))(name)
})
}
// -------------------------------------------------------------------------------------
type eventSinks struct {
*eventSinkMgr
pthis threadObj
}
func nameOf(this interface{}) string {
if spr, ok := this.(*Sprite); ok {
return spr.name
}
if _, ok := this.(*Game); ok {
return "Game"
}
panic("eventSinks: unexpected this object")
}
func (p *eventSinks) init(mgr *eventSinkMgr, this threadObj) {
p.eventSinkMgr = mgr
p.pthis = this
}
func (p *eventSinks) initFrom(src *eventSinks, this threadObj) {
p.eventSinkMgr = src.eventSinkMgr
p.pthis = this
}
func (p *eventSinks) doDeleteClone() {
p.eventSinkMgr.doDeleteClone(p.pthis)
}
// -------------------------------------------------------------------------------------
func (p *eventSinks) OnStart(onStart func()) {
p.allWhenStart = &eventSink{
prev: p.allWhenStart,
pthis: p.pthis,
sink: onStart,
}
}
func (p *eventSinks) OnClick(onClick func()) {
pthis := p.pthis
p.allWhenClick = &eventSink{
prev: p.allWhenClick,
pthis: pthis,
sink: onClick,
cond: func(data interface{}) bool {
return data == pthis
},
}
}
func (p *eventSinks) OnAnyKey(onKey func(key Key)) {
p.allWhenKeyPressed = &eventSink{
prev: p.allWhenKeyPressed,
pthis: p.pthis,
sink: onKey,
}
}
func (p *eventSinks) OnKey__0(key Key, onKey func()) {
p.allWhenKeyPressed = &eventSink{
prev: p.allWhenKeyPressed,
pthis: p.pthis,
sink: func(Key) {
if debugEvent {
log.Println("==> onKey", key, nameOf(p.pthis))
}
onKey()
},
cond: func(data interface{}) bool {
return data.(Key) == key
},
}
}
func (p *eventSinks) OnKey__1(keys []Key, onKey func(Key)) {
p.allWhenKeyPressed = &eventSink{
prev: p.allWhenKeyPressed,
pthis: p.pthis,
sink: func(key Key) {
if debugEvent {
log.Println("==> onKey", keys, nameOf(p.pthis))
}
onKey(key)
},
cond: func(data interface{}) bool {
keyIn := data.(Key)
for _, key := range keys {
if key == keyIn {
return true
}
}
return false
},
}
}
func (p *eventSinks) OnKey__2(keys []Key, onKey func()) {
p.OnKey__1(keys, func(Key) {
onKey()
})
}
func (p *eventSinks) OnMsg__0(onMsg func(msg string, data interface{})) {
p.allWhenIReceive = &eventSink{
prev: p.allWhenIReceive,
pthis: p.pthis,
sink: onMsg,
}
}
func (p *eventSinks) OnMsg__1(msg string, onMsg func()) {
p.allWhenIReceive = &eventSink{
prev: p.allWhenIReceive,
pthis: p.pthis,
sink: func(msg string, data interface{}) {
if debugEvent {
log.Println("==> onMsg", msg, nameOf(p.pthis))
}
onMsg()
},
cond: func(data interface{}) bool {
return data.(string) == msg
},
}
}
func (p *eventSinks) OnBackdrop__0(onBackdrop func(name string)) {
p.allWhenBackdropChanged = &eventSink{
prev: p.allWhenBackdropChanged,
pthis: p.pthis,
sink: onBackdrop,
}
}
func (p *eventSinks) OnBackdrop__1(name string, onBackdrop func()) {
p.allWhenBackdropChanged = &eventSink{
prev: p.allWhenBackdropChanged,
pthis: p.pthis,
sink: func(name string) {
if debugEvent {
log.Println("==> onBackdrop", name, nameOf(p.pthis))
}
onBackdrop()
},
cond: func(data interface{}) bool {
return data.(string) == name
},
}
}
// -------------------------------------------------------------------------------------
type StopKind int
const (
_All StopKind = All // stop all scripts of stage/sprites and abort this script
AllOtherScripts StopKind = -100 // stop all other scripts
AllSprites StopKind = -101 // stop all scripts of sprites
ThisSprite StopKind = -102 // stop all scripts of this sprite
ThisScript StopKind = -103 // abort this script
OtherScriptsInSprite StopKind = -104 // stop other scripts of this sprite
)
func (p *eventSinks) Stop(kind StopKind) {
var filter func(th coroutine.Thread) bool
switch kind {
case AllSprites:
filter = func(th coroutine.Thread) bool {
return isSprite(th.Obj)
}
case ThisSprite:
this := p.pthis
filter = func(th coroutine.Thread) bool {
return th.Obj == this
}
case OtherScriptsInSprite:
this := p.pthis
filter = func(th coroutine.Thread) bool {
return th.Obj == this && th != gco.Current()
}
case AllOtherScripts:
filter = func(th coroutine.Thread) bool {
return (isSprite(th.Obj) || isGame(th.Obj)) && th != gco.Current()
}
case All:
gco.StopIf(func(th coroutine.Thread) bool {
return isSprite(th.Obj) || isGame(th.Obj)
})
fallthrough
case ThisScript:
gco.Abort()
}
gco.StopIf(filter)
}
func isGame(obj threadObj) bool {
_, ok := obj.(*Game)
return ok
}
func isSprite(obj threadObj) bool {
_, ok := obj.(*Sprite)
return ok
}
// -------------------------------------------------------------------------------------