52 lines
1017 B
Go
52 lines
1017 B
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.ElementFunc {
|
|
return func(d elements.Bounds) elements.Element {
|
|
s := Stack{
|
|
Block: blocks.Block{ContainerBounds: d},
|
|
}
|
|
for op := range ops {
|
|
ops[op](&s)
|
|
}
|
|
return &s
|
|
}
|
|
}
|
|
|
|
type Stack struct {
|
|
blocks.Block
|
|
mouse.NopHandler
|
|
horizontal bool
|
|
children []elements.ElementFunc
|
|
}
|
|
|
|
func (s *Stack) Draw(image *ebiten.Image) (w, h float64) {
|
|
s.Block.Draw(image)
|
|
|
|
d := s.ContainerBounds
|
|
d.Width, d.Height = s.Block.Size()
|
|
if s.horizontal {
|
|
d.Width /= float64(len(s.children))
|
|
} else {
|
|
d.Height /= float64(len(s.children))
|
|
}
|
|
|
|
var offsetX, offsetY float64
|
|
for i := range s.children {
|
|
offsetX, offsetY = s.children[i](d).Draw(image)
|
|
if s.horizontal {
|
|
d.Min.X += offsetX
|
|
} else {
|
|
d.Min.Y += offsetY
|
|
}
|
|
}
|
|
return s.Block.Size()
|
|
}
|