Skip to content

Commit

Permalink
Convert func "GetWidget" to generic
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Aug 15, 2024
1 parent 74ad6f4 commit 8e3982f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ type Spriter interface {
type Gamer interface {
initGame(sprites []Spriter) *Game
}

var (
gameInstance *Game
)
func (p *Game) IsRunned() bool {
return p.isRunned
}
Expand Down Expand Up @@ -180,6 +182,7 @@ func (p *Game) initGame(sprites []Spriter) *Game {
// Gopt_Game_Main is required by Go+ compiler as the entry of a .gmx project.
func Gopt_Game_Main(game Gamer, sprites ...Spriter) {
g := game.initGame(sprites)
gameInstance = g
if me, ok := game.(interface{ MainEntry() }); ok {
me.MainEntry()
}
Expand Down Expand Up @@ -1338,12 +1341,17 @@ func (p *Game) ShowVar(name string) {
// Widget

// GetWidget returns the widget instance with given name. It panics if not found.
func (p *Game) GetWidget(name string) Widget {
items := p.items

func Gopx_GetWidget[T any](name string) *T{
items := gameInstance.items
for _, item := range items {
widget, ok := item.(Widget)
if ok && widget.GetName() == name {
return widget
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())
}
}
}
panic("GetWidget: widget not found - " + name)
Expand Down

0 comments on commit 8e3982f

Please sign in to comment.