This commit is contained in:
Josh Deprez 2021-09-27 16:57:29 +10:00
parent e03731c7e7
commit b63c21f6b0
2 changed files with 8 additions and 6 deletions

View file

@ -102,7 +102,8 @@ func (d *DrawDAG) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
}) })
} }
// exists so DrawDAG is recognised as a DrawManager // ManagesDrawingSubcomponents is present so DrawDAG is recognised as a
// DrawManager.
func (DrawDAG) ManagesDrawingSubcomponents() {} func (DrawDAG) ManagesDrawingSubcomponents() {}
// Prepare adds all subcomponents to the DAG. // Prepare adds all subcomponents to the DAG.

View file

@ -29,13 +29,14 @@ func (d *DrawDFS) Draw(screen *ebiten.Image, opts *ebiten.DrawImageOptions) {
if d.Hidden() { if d.Hidden() {
return return
} }
d.draw(d.Child, screen, *opts) d.drawRecursive(d.Child, screen, *opts)
} }
// exists so DrawDFS is recognised as a DrawManager // ManagesDrawingSubcomponents is present so DrawDFS is recognised as a
// DrawManager.
func (DrawDFS) ManagesDrawingSubcomponents() {} func (DrawDFS) ManagesDrawingSubcomponents() {}
func (d *DrawDFS) draw(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() {
return return
@ -48,14 +49,14 @@ func (d *DrawDFS) draw(component interface{}, screen *ebiten.Image, opts ebiten.
if dr, ok := component.(Drawer); ok { if dr, ok := component.(Drawer); ok {
dr.Draw(screen, &opts) dr.Draw(screen, &opts)
} }
// Is it a DrawManager? return early (don't recurse) // Is it a DrawManager? It manages drawing all its subcomponents.
if _, ok := component.(DrawManager); ok { if _, ok := component.(DrawManager); ok {
return return
} }
// Has subcomponents? recurse // Has subcomponents? recurse
if sc, ok := component.(Scanner); ok { if sc, ok := component.(Scanner); ok {
sc.Scan(func(x interface{}) error { sc.Scan(func(x interface{}) error {
d.draw(x, screen, opts) d.drawRecursive(x, screen, opts)
return nil return nil
}) })
} }