drawdfs query? nope lol

This commit is contained in:
Josh Deprez 2021-10-01 11:52:02 +10:00
parent 106ecb6d0c
commit e00782ea6c

View file

@ -30,16 +30,52 @@ type DrawDFS struct {
} }
func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) { func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
if d.Hidden() { if true {
return d.drawRecursive(d, screen, *opts)
} else {
d.drawWithQuery(screen, opts)
} }
d.drawRecursive(d.Child, screen, *opts)
} }
// ManagesDrawingSubcomponents is present so DrawDFS is recognised as a // ManagesDrawingSubcomponents is present so DrawDFS is recognised as a
// DrawManager. // DrawManager.
func (DrawDFS) ManagesDrawingSubcomponents() {} func (DrawDFS) ManagesDrawingSubcomponents() {}
// This doesn't work! This misses Hiders, Transformers that are not Drawers.
func (d *DrawDFS) drawWithQuery(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
stack := []ebiten.DrawImageOptions{*opts}
d.game.Query(d, DrawerType,
// visitPre
func(x interface{}) error {
if h, ok := x.(Hider); ok && h.Hidden() {
return Skip
}
opts := stack[len(stack)-1]
if tf, ok := x.(Transformer); ok {
opts = concatOpts(tf.Transform(), opts)
stack = append(stack, opts)
}
if x == d {
return nil
}
if dr, ok := x.(Drawer); ok {
dr.Draw(screen, &opts)
}
if _, isDM := x.(DrawManager); isDM {
return Skip
}
return nil
},
// visitPost
func(x interface{}) error {
if _, ok := x.(Transformer); ok {
stack = stack[:len(stack)-1]
}
return nil
},
)
}
func (d *DrawDFS) drawRecursive(component interface{}, screen *ebiten.Image, opts ebiten.DrawImageOptions) { func (d *DrawDFS) drawRecursive(component interface{}, screen *ebiten.Image, opts ebiten.DrawImageOptions) {
// Hidden? stop drawing // Hidden? stop drawing
if h, ok := component.(Hider); ok && h.Hidden() { if h, ok := component.(Hider); ok && h.Hidden() {
@ -49,6 +85,7 @@ func (d *DrawDFS) drawRecursive(component interface{}, screen *ebiten.Image, opt
if tf, ok := component.(Transformer); ok { if tf, ok := component.(Transformer); ok {
opts = concatOpts(tf.Transform(), opts) opts = concatOpts(tf.Transform(), opts)
} }
if component != d {
// Does it draw itself? Draw // Does it draw itself? Draw
if dr, ok := component.(Drawer); ok { if dr, ok := component.(Drawer); ok {
dr.Draw(screen, &opts) dr.Draw(screen, &opts)
@ -57,8 +94,8 @@ func (d *DrawDFS) drawRecursive(component interface{}, screen *ebiten.Image, opt
if _, ok := component.(DrawManager); ok { if _, ok := component.(DrawManager); ok {
return return
} }
}
// Has subcomponents? recurse // Has subcomponents? recurse
// TODO: use g.Query?
d.game.Children(component).Scan(func(x interface{}) error { d.game.Children(component).Scan(func(x interface{}) error {
d.drawRecursive(x, screen, opts) d.drawRecursive(x, screen, opts)
return nil return nil