Add Transform field to Scene
This commit is contained in:
parent
14407fe47d
commit
ed5092cc3e
1 changed files with 17 additions and 14 deletions
|
@ -24,12 +24,15 @@ type ZPositioner interface {
|
||||||
// Scene manages drawing and updating a bunch of components.
|
// Scene manages drawing and updating a bunch of components.
|
||||||
type Scene struct {
|
type Scene struct {
|
||||||
Components []interface{}
|
Components []interface{}
|
||||||
needsSort bool
|
Transform ebiten.GeoM
|
||||||
|
|
||||||
|
needsSort bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw draws all components in order.
|
// Draw draws all components in order.
|
||||||
func (l *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||||
for _, i := range l.Components {
|
geom.Concat(s.Transform)
|
||||||
|
for _, i := range s.Components {
|
||||||
if d, ok := i.(Drawer); ok {
|
if d, ok := i.(Drawer); ok {
|
||||||
d.Draw(screen, geom)
|
d.Draw(screen, geom)
|
||||||
}
|
}
|
||||||
|
@ -37,18 +40,18 @@ func (l *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetNeedsSort informs l that its layers may be out of order.
|
// SetNeedsSort informs l that its layers may be out of order.
|
||||||
func (l *Scene) SetNeedsSort() {
|
func (s *Scene) SetNeedsSort() {
|
||||||
l.needsSort = true
|
s.needsSort = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// sortByZ sorts the components by Z position.
|
// sortByZ sorts the components by Z position.
|
||||||
// Stable sort is used to avoid Z-fighting among layers without a Z, or
|
// Stable sort is used to avoid Z-fighting among layers without a Z, or
|
||||||
// among those with equal Z. All non-ZPositioners are sorted first.
|
// among those with equal Z. All non-ZPositioners are sorted first.
|
||||||
func (l *Scene) sortByZ() {
|
func (s *Scene) sortByZ() {
|
||||||
l.needsSort = false
|
s.needsSort = false
|
||||||
sort.SliceStable(l.Components, func(i, j int) bool {
|
sort.SliceStable(s.Components, func(i, j int) bool {
|
||||||
a, aok := l.Components[i].(ZPositioner)
|
a, aok := s.Components[i].(ZPositioner)
|
||||||
b, bok := l.Components[j].(ZPositioner)
|
b, bok := s.Components[j].(ZPositioner)
|
||||||
if aok && bok {
|
if aok && bok {
|
||||||
return a.Z() < b.Z()
|
return a.Z() < b.Z()
|
||||||
}
|
}
|
||||||
|
@ -57,16 +60,16 @@ func (l *Scene) sortByZ() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update calls Update on all Updater components.
|
// Update calls Update on all Updater components.
|
||||||
func (l *Scene) Update() error {
|
func (s *Scene) Update() error {
|
||||||
for _, c := range l.Components {
|
for _, c := range s.Components {
|
||||||
if u, ok := c.(Updater); ok {
|
if u, ok := c.(Updater); ok {
|
||||||
if err := u.Update(); err != nil {
|
if err := u.Update(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if l.needsSort {
|
if s.needsSort {
|
||||||
l.sortByZ()
|
s.sortByZ()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue