Files
games/common/elements/v1/stacks/stack.go

62 lines
1.2 KiB
Go

package stacks
import (
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/blocks"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
"github.com/hajimehoshi/ebiten/v2"
)
func New(ops ...Option) elements.InitFunc {
return func(d elements.Bounds) elements.Element {
s := Stack{
Block: blocks.Block{ContainerBounds: d},
}
for op := range ops {
ops[op](&s)
}
d.Width, d.Height = s.Block.Size()
if s.horizontal {
d.Width /= float64(len(s.childrenInit))
} else {
d.Height /= float64(len(s.childrenInit))
}
s.Children = make([]elements.Element, len(s.childrenInit))
var offsetX, offsetY float64
for i := range s.childrenInit {
s.Children[i] = s.childrenInit[i](d)
offsetX, offsetY = s.Children[i].Size()
if s.horizontal {
d.Min.X += offsetX
} else {
d.Min.Y += offsetY
}
}
return &s
}
}
type Stack struct {
blocks.Block
mouse.NopHandler
horizontal bool
childrenInit []elements.InitFunc
Children []elements.Element
}
func (s *Stack) Draw(image *ebiten.Image) {
s.Block.Draw(image)
for i := range s.Children {
s.Children[i].Draw(image)
}
}
func (s *Stack) GetChildren() []elements.Element {
return s.Children
}