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.
|
||||
type Scene struct {
|
||||
Components []interface{}
|
||||
needsSort bool
|
||||
Transform ebiten.GeoM
|
||||
|
||||
needsSort bool
|
||||
}
|
||||
|
||||
// Draw draws all components in order.
|
||||
func (l *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||
for _, i := range l.Components {
|
||||
func (s *Scene) Draw(screen *ebiten.Image, geom ebiten.GeoM) {
|
||||
geom.Concat(s.Transform)
|
||||
for _, i := range s.Components {
|
||||
if d, ok := i.(Drawer); ok {
|
||||
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.
|
||||
func (l *Scene) SetNeedsSort() {
|
||||
l.needsSort = true
|
||||
func (s *Scene) SetNeedsSort() {
|
||||
s.needsSort = true
|
||||
}
|
||||
|
||||
// sortByZ sorts the components by Z position.
|
||||
// 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.
|
||||
func (l *Scene) sortByZ() {
|
||||
l.needsSort = false
|
||||
sort.SliceStable(l.Components, func(i, j int) bool {
|
||||
a, aok := l.Components[i].(ZPositioner)
|
||||
b, bok := l.Components[j].(ZPositioner)
|
||||
func (s *Scene) sortByZ() {
|
||||
s.needsSort = false
|
||||
sort.SliceStable(s.Components, func(i, j int) bool {
|
||||
a, aok := s.Components[i].(ZPositioner)
|
||||
b, bok := s.Components[j].(ZPositioner)
|
||||
if aok && bok {
|
||||
return a.Z() < b.Z()
|
||||
}
|
||||
|
@ -57,16 +60,16 @@ func (l *Scene) sortByZ() {
|
|||
}
|
||||
|
||||
// Update calls Update on all Updater components.
|
||||
func (l *Scene) Update() error {
|
||||
for _, c := range l.Components {
|
||||
func (s *Scene) Update() error {
|
||||
for _, c := range s.Components {
|
||||
if u, ok := c.(Updater); ok {
|
||||
if err := u.Update(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if l.needsSort {
|
||||
l.sortByZ()
|
||||
if s.needsSort {
|
||||
s.sortByZ()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue