Skip to content

Commit 971e4a0

Browse files
authored
refactor: prefer typed params for API funcs (#463)
- Introduce `SpriteName`, `SpriteCostumeName`, `SpriteAnimationName`, `SoundName`, `BackdropName`, `WidgetName`. - Prefer func overloading over using `interface{}` or variadic params as optional params. Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
1 parent 845f2c0 commit 971e4a0

7 files changed

Lines changed: 305 additions & 101 deletions

File tree

camera.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func (c *Camera) worldToScreen(point *math32.Vector2) *math32.Vector2 {
5959
}
6060
*/
6161

62-
func (c *Camera) On(obj interface{}) {
62+
func (c *Camera) on(obj interface{}) {
6363
switch v := obj.(type) {
64-
case string:
64+
case SpriteName:
6565
sp := c.g.findSprite(v)
6666
if sp == nil {
6767
log.Println("Camera.On: sprite not found -", v)
@@ -83,6 +83,22 @@ func (c *Camera) On(obj interface{}) {
8383
c.on_ = obj
8484
}
8585

86+
func (c *Camera) On__0(sprite Sprite) {
87+
c.on(sprite)
88+
}
89+
90+
func (c *Camera) On__1(sprite *SpriteImpl) {
91+
c.on(sprite)
92+
}
93+
94+
func (c *Camera) On__2(sprite SpriteName) {
95+
c.on(sprite)
96+
}
97+
98+
func (c *Camera) On__3(obj specialObj) {
99+
c.on(obj)
100+
}
101+
86102
func (c *Camera) updateOnObj() {
87103
switch v := c.on_.(type) {
88104
case *SpriteImpl:

event.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ func (p *eventSinkMgr) doWhenIReceive(msg string, data interface{}, wait bool) {
213213
})
214214
}
215215

216-
func (p *eventSinkMgr) doWhenBackdropChanged(name string, wait bool) {
216+
func (p *eventSinkMgr) doWhenBackdropChanged(name BackdropName, wait bool) {
217217
p.allWhenBackdropChanged.call(wait, name, func(ev *eventSink) {
218-
ev.sink.(func(string))(name)
218+
ev.sink.(func(BackdropName))(name)
219219
})
220220
}
221221

222222
// -------------------------------------------------------------------------------------
223223
type IEventSinks interface {
224224
OnAnyKey(onKey func(key Key))
225-
OnBackdrop__0(onBackdrop func(name string))
226-
OnBackdrop__1(name string, onBackdrop func())
225+
OnBackdrop__0(onBackdrop func(name BackdropName))
226+
OnBackdrop__1(name BackdropName, onBackdrop func())
227227
OnClick(onClick func())
228228
OnKey__0(key Key, onKey func())
229229
OnKey__1(keys []Key, onKey func(Key))
@@ -361,26 +361,26 @@ func (p *eventSinks) OnMsg__1(msg string, onMsg func()) {
361361
}
362362
}
363363

364-
func (p *eventSinks) OnBackdrop__0(onBackdrop func(name string)) {
364+
func (p *eventSinks) OnBackdrop__0(onBackdrop func(name BackdropName)) {
365365
p.allWhenBackdropChanged = &eventSink{
366366
prev: p.allWhenBackdropChanged,
367367
pthis: p.pthis,
368368
sink: onBackdrop,
369369
}
370370
}
371371

372-
func (p *eventSinks) OnBackdrop__1(name string, onBackdrop func()) {
372+
func (p *eventSinks) OnBackdrop__1(name BackdropName, onBackdrop func()) {
373373
p.allWhenBackdropChanged = &eventSink{
374374
prev: p.allWhenBackdropChanged,
375375
pthis: p.pthis,
376-
sink: func(name string) {
376+
sink: func(name BackdropName) {
377377
if debugEvent {
378378
log.Println("==> onBackdrop", name, nameOf(p.pthis))
379379
}
380380
onBackdrop()
381381
},
382382
cond: func(data interface{}) bool {
383-
return data.(string) == name
383+
return data.(BackdropName) == name
384384
},
385385
}
386386
}

game.go

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func (p *Game) loadIndex(g reflect.Value, proj *projConfig) (err error) {
473473

474474
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeOnlyFullscreenEnabled)
475475
if proj.Camera != nil && proj.Camera.On != "" {
476-
p.Camera.On(proj.Camera.On)
476+
p.Camera.On__2(proj.Camera.On)
477477
}
478478
if loader, ok := g.Addr().Interface().(interface{ OnLoaded() }); ok {
479479
loader.OnLoaded()
@@ -860,7 +860,7 @@ func (p *Game) touchingSpriteBy(dst *SpriteImpl, name string) *SpriteImpl {
860860

861861
func (p *Game) objectPos(obj interface{}) (float64, float64) {
862862
switch v := obj.(type) {
863-
case string:
863+
case SpriteName:
864864
if sp := p.findSprite(v); sp != nil {
865865
return sp.getXY()
866866
}
@@ -1033,7 +1033,7 @@ func (p *Game) doFindSprite(src Shape) int {
10331033
return -1
10341034
}
10351035

1036-
func (p *Game) findSprite(name string) *SpriteImpl {
1036+
func (p *Game) findSprite(name SpriteName) *SpriteImpl {
10371037
for _, item := range p.items {
10381038
if sp, ok := item.(*SpriteImpl); ok {
10391039
if !sp.isCloned_ && sp.name == name {
@@ -1045,7 +1045,6 @@ func (p *Game) findSprite(name string) *SpriteImpl {
10451045
}
10461046

10471047
// -----------------------------------------------------------------------------
1048-
10491048
func (p *Game) drawBackground(dc drawContext) {
10501049
c := p.costumes[p.costumeIndex_]
10511050
img, _, _ := c.needImage(p.fs)
@@ -1156,7 +1155,9 @@ func (p *Game) onHit(hc hitContext) (hr hitResult, ok bool) {
11561155

11571156
// -----------------------------------------------------------------------------
11581157

1159-
func (p *Game) BackdropName() string {
1158+
type BackdropName = string
1159+
1160+
func (p *Game) BackdropName() BackdropName {
11601161
return p.getCostumeName()
11611162
}
11621163

@@ -1166,24 +1167,64 @@ func (p *Game) BackdropIndex() int {
11661167

11671168
// StartBackdrop func:
11681169
//
1169-
// StartBackdrop(backdropName) or
1170-
// StartBackdrop(backdropIndex) or
1170+
// StartBackdrop(backdrop) or
1171+
// StartBackdrop(index) or
11711172
// StartBackdrop(spx.Next)
11721173
// StartBackdrop(spx.Prev)
1173-
func (p *Game) StartBackdrop(backdrop interface{}, wait ...bool) {
1174+
func (p *Game) startBackdrop(backdrop interface{}, wait bool) {
11741175
if p.goSetCostume(backdrop) {
11751176
p.windowWidth_ = 0
11761177
p.doWindowSize()
1177-
p.doWhenBackdropChanged(p.getCostumeName(), wait != nil && wait[0])
1178+
p.doWhenBackdropChanged(p.getCostumeName(), wait)
11781179
}
11791180
}
11801181

1181-
func (p *Game) NextBackdrop(wait ...bool) {
1182-
p.StartBackdrop(Next, wait...)
1182+
func (p *Game) StartBackdrop__0(backdrop BackdropName) {
1183+
p.startBackdrop(backdrop, false)
1184+
}
1185+
1186+
func (p *Game) StartBackdrop__1(backdrop BackdropName, wait bool) {
1187+
p.startBackdrop(backdrop, wait)
1188+
}
1189+
1190+
func (p *Game) StartBackdrop__2(index int) {
1191+
p.startBackdrop(index, false)
1192+
}
1193+
1194+
func (p *Game) StartBackdrop__3(index int, wait bool) {
1195+
p.startBackdrop(index, wait)
1196+
}
1197+
1198+
func (p *Game) StartBackdrop__4(index float64) {
1199+
p.startBackdrop(index, false)
1200+
}
1201+
1202+
func (p *Game) StartBackdrop__5(index float64, wait bool) {
1203+
p.startBackdrop(index, wait)
11831204
}
11841205

1185-
func (p *Game) PrevBackdrop(wait ...bool) {
1186-
p.StartBackdrop(Prev, wait...)
1206+
func (p *Game) StartBackdrop__6(action switchAction) {
1207+
p.startBackdrop(action, false)
1208+
}
1209+
1210+
func (p *Game) StartBackdrop__7(action switchAction, wait bool) {
1211+
p.startBackdrop(action, wait)
1212+
}
1213+
1214+
func (p *Game) NextBackdrop__0() {
1215+
p.StartBackdrop__6(Next)
1216+
}
1217+
1218+
func (p *Game) NextBackdrop__1(wait bool) {
1219+
p.StartBackdrop__7(Next, wait)
1220+
}
1221+
1222+
func (p *Game) PrevBackdrop__0() {
1223+
p.StartBackdrop__6(Prev)
1224+
}
1225+
1226+
func (p *Game) PrevBackdrop__1(wait bool) {
1227+
p.StartBackdrop__7(Prev, wait)
11871228
}
11881229

11891230
// -----------------------------------------------------------------------------
@@ -1282,6 +1323,8 @@ func (p *Game) ClearSoundEffects() {
12821323

12831324
type Sound *soundConfig
12841325

1326+
type SoundName = string
1327+
12851328
func (p *Game) canBindSound(name string) bool {
12861329
// auto bind the sound, if assets/sounds/{name}/index.json exists.
12871330
prefix := "sounds/" + name
@@ -1293,7 +1336,7 @@ func (p *Game) canBindSound(name string) bool {
12931336
return true
12941337
}
12951338

1296-
func (p *Game) loadSound(name string) (media Sound, err error) {
1339+
func (p *Game) loadSound(name SoundName) (media Sound, err error) {
12971340
if media, ok := p.sounds.audios[name]; ok {
12981341
return media, nil
12991342
}
@@ -1317,9 +1360,6 @@ func (p *Game) loadSound(name string) (media Sound, err error) {
13171360
// Play(video) -- maybe
13181361
// Play(media, wait) -- sync
13191362
// Play(media, opts)
1320-
// Play(mediaName)
1321-
// Play(mediaName, wait) -- sync
1322-
// Play(mediaName, opts)
13231363
func (p *Game) Play__0(media Sound) {
13241364
p.Play__2(media, &PlayOptions{})
13251365
}
@@ -1338,21 +1378,22 @@ func (p *Game) Play__2(media Sound, action *PlayOptions) {
13381378
panic(err)
13391379
}
13401380
}
1341-
func (p *Game) Play__3(mediaName string) {
1342-
p.Play__5(mediaName, &PlayOptions{})
1381+
1382+
func (p *Game) Play__3(media SoundName) {
1383+
p.Play__5(media, &PlayOptions{})
13431384
}
13441385

1345-
func (p *Game) Play__4(mediaName string, wait bool) {
1346-
p.Play__5(mediaName, &PlayOptions{Wait: wait})
1386+
func (p *Game) Play__4(media SoundName, wait bool) {
1387+
p.Play__5(media, &PlayOptions{Wait: wait})
13471388
}
13481389

1349-
func (p *Game) Play__5(mediaName string, action *PlayOptions) {
1350-
media, err := p.loadSound(mediaName)
1390+
func (p *Game) Play__5(media SoundName, action *PlayOptions) {
1391+
m, err := p.loadSound(media)
13511392
if err != nil {
13521393
log.Println(err)
13531394
return
13541395
}
1355-
p.Play__2(media, action)
1396+
p.Play__2(m, action)
13561397
}
13571398

13581399
func (p *Game) StopAllSounds() {
@@ -1433,7 +1474,7 @@ type ShapeGetter interface {
14331474
// Instead of being used directly, it is meant to be called by `Gopt_Game_Gopx_GetWidget` only.
14341475
// We extract `GetWidget_` to keep `Gopt_Game_Gopx_GetWidget` simple, which simplifies work in ispx,
14351476
// see details in https://github.com/goplus/builder/issues/765#issuecomment-2313915805.
1436-
func GetWidget_(sg ShapeGetter, name string) Widget {
1477+
func GetWidget_(sg ShapeGetter, name WidgetName) Widget {
14371478
items := sg.getAllShapes()
14381479
for _, item := range items {
14391480
widget, ok := item.(Widget)
@@ -1445,7 +1486,7 @@ func GetWidget_(sg ShapeGetter, name string) Widget {
14451486
}
14461487

14471488
// GetWidget returns the widget instance (in given type) with given name. It panics if not found.
1448-
func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name string) *T {
1489+
func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name WidgetName) *T {
14491490
widget := GetWidget_(sg, name)
14501491
if result, ok := widget.(interface{}).(*T); ok {
14511492
return result

monitor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
// Monitor class.
3838
type Monitor struct {
3939
game *Game
40-
name string
40+
name WidgetName
4141
size float64
4242
target string
4343
val string
@@ -304,7 +304,7 @@ func (p *Monitor) hit(hc hitContext) (hr hitResult, ok bool) {
304304

305305
// -------------------------------------------------------------------------------------
306306
// IWidget
307-
func (pself *Monitor) GetName() string {
307+
func (pself *Monitor) GetName() WidgetName {
308308
return pself.name
309309
}
310310

spbase.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (p *imageLoaderByCostumeSet) load(fs spxfs.Dir, pt *imagePoint) (gdi.Image,
174174
// -------------------------------------------------------------------------------------
175175

176176
type costume struct {
177-
name string
177+
name SpriteCostumeName
178178
img delayloadImage
179179
faceRight float64
180180
bitmapResolution int
@@ -296,7 +296,7 @@ func initCSPart(p *baseObj, img *costumeSetImage, faceRight float64, bitmapResol
296296
}
297297
}
298298

299-
func addCostumeWith(p *baseObj, name string, img *costumeSetImage, faceRight float64, i, bitmapResolution int) {
299+
func addCostumeWith(p *baseObj, name SpriteCostumeName, img *costumeSetImage, faceRight float64, i, bitmapResolution int) {
300300
c := newCostumeWith(name, img, faceRight, i, bitmapResolution)
301301
p.costumes = append(p.costumes, c)
302302
}
@@ -334,7 +334,7 @@ func (p *baseObj) initFrom(src *baseObj) {
334334
p.costumeIndex_ = src.costumeIndex_
335335
}
336336

337-
func (p *baseObj) findCostume(name string) int {
337+
func (p *baseObj) findCostume(name SpriteCostumeName) int {
338338
for i, c := range p.costumes {
339339
if c.name == name {
340340
return i
@@ -345,7 +345,7 @@ func (p *baseObj) findCostume(name string) int {
345345

346346
func (p *baseObj) goSetCostume(val interface{}) bool {
347347
switch v := val.(type) {
348-
case string:
348+
case SpriteCostumeName:
349349
return p.setCostumeByName(v)
350350
case int:
351351
return p.setCostumeByIndex(v)
@@ -373,8 +373,7 @@ func (p *baseObj) setCostumeByIndex(idx int) bool {
373373
}
374374
return false
375375
}
376-
377-
func (p *baseObj) setCostumeByName(name string) bool {
376+
func (p *baseObj) setCostumeByName(name SpriteCostumeName) bool {
378377
if idx := p.findCostume(name); idx >= 0 {
379378
return p.setCostumeByIndex(idx)
380379
}
@@ -393,7 +392,7 @@ func (p *baseObj) getCostumeIndex() int {
393392
return p.costumeIndex_
394393
}
395394

396-
func (p *baseObj) getCostumeName() string {
395+
func (p *baseObj) getCostumeName() SpriteCostumeName {
397396
return p.costumes[p.costumeIndex_].name
398397
}
399398

0 commit comments

Comments
 (0)