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

Revert "Merge pull request #313 from qlli/ebitenAudio" #324

Closed
wants to merge 1 commit into from
Closed
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: 20 additions & 25 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type Spriter interface {
}
type Gamer interface {
initGame(sprites []Spriter) *Game
getGame() *Game
}

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

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

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

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()
// 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
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)
}

// 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: 0 additions & 4 deletions sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,6 @@ 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