Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ispx with generics #312

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ type Spriter interface {
}
type Gamer interface {
initGame(sprites []Spriter) *Game
getGame() *Game
Copy link
Collaborator Author

@nighca nighca Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now we need to maintain the same interface Gamer in ispx to make it work in browser, see details in https://github.com/goplus/builder/blob/35c6e0883c5c66b29eaeed2bf99d0731f06774f9/tools/ispx/main.go#L74-L76

It is better to keep Gamer as simple as possible.

We defined another interface ShapeGetter to implement getWidget. ShapeGetter is implemented by both *Game & *Sprite.

}

func (p *Game) IsRunned() bool {
Expand Down Expand Up @@ -1351,32 +1350,38 @@ func (p *Game) ShowVar(name string) {
p.setStageMonitor("", getVarPrefix+name, true)
}

func (p *Game) getAllShapes() []Shape {
return p.items
}

// -----------------------------------------------------------------------------
// Widget

// GetWidget returns the widget instance with given name. It panics if not found.
func Gopt_Game_Gopx_GetWidget[T any](game interface{}, name string) *T {
var gamePtr *Game
switch ptr := game.(type) {
case *Game:
gamePtr = ptr
case interface{ Parent() *Game }:
gamePtr = ptr.Parent()
case Gamer:
gamePtr = ptr.getGame()
default:
panic("GetWidget: unexpected game type" + reflect.TypeOf(game).String())
}
items := gamePtr.items
type ShapeGetter interface {
getAllShapes() []Shape
}

// GetWidget_ returns the widget instance with given name. It panics if not found.
// Instead of being used directly, it is meant to be called by `Gopt_Game_Gopx_GetWidget` only.
// We extract `GetWidget_` to keep `Gopt_Game_Gopx_GetWidget` simple, which simplifies work in ispx,
// see details in https://github.com/goplus/builder/issues/765#issuecomment-2313915805.
func GetWidget_(sg ShapeGetter, name string) Widget {
items := sg.getAllShapes()
for _, item := range items {
widget, ok := item.(Widget)
if ok && widget.GetName() == name {
if result, ok := widget.(interface{}).(*T); ok {
return result
} else {
panic("GetWidget: type mismatch - expected " + reflect.TypeOf((*T)(nil)).Elem().String() + ", got " + reflect.TypeOf(widget).String())
}
return widget
}
}
panic("GetWidget: widget not found - " + name)
}

// GetWidget returns the widget instance (in given type) with given name. It panics if not found.
func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name string) *T {
widget := GetWidget_(sg, name)
if result, ok := widget.(interface{}).(*T); ok {
return result
} else {
panic("GetWidget: type mismatch")
}
}
4 changes: 4 additions & 0 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (p *Sprite) Parent() *Game {
return p.g
}

func (p *Sprite) getAllShapes() []Shape {
return p.g.getAllShapes()
}

func (p *Sprite) init(
base string, g *Game, name string, sprite *spriteConfig, gamer reflect.Value, shared *sharedImages) {
if sprite.Costumes != nil {
Expand Down
Loading