package stacks import ( "git.vezzani.net/ben/games/common/elements/v1" "git.vezzani.net/ben/games/common/elements/v1/mouse" "github.com/hajimehoshi/ebiten/v2" ) func New(ops ...OptFunc) elements.InitFunc { return func() elements.Element { s := Stack{} for op := range ops { ops[op](&s) } s.children = make([]elements.Element, len(s.childrenInit)) var cw, ch int anchor := s.anchor for i := range s.childrenInit { s.children[i] = s.childrenInit[i]() s.children[i].SetAnchor(anchor) cw, ch = s.children[i].Size() if s.horizontal { anchor.X += cw } else { anchor.Y += ch } } return &s } } type Stack struct { mouse.NopHandler anchor elements.Point horizontal bool childrenInit []elements.InitFunc children []elements.Element } func (s *Stack) SetAnchor(a elements.Point) { d := s.anchor.Delta(a) for i := range s.Children() { s.Children()[i].SetAnchor(s.Children()[i].Anchor().Add(d)) } s.anchor = a } func (s *Stack) Anchor() elements.Point { return s.anchor } func (s *Stack) Size() (w, h int) { var cw, ch int for i := range s.children { cw, ch = s.children[i].Size() if s.horizontal { w += cw if h < ch { h = ch } } else { h += ch if w < cw { w = cw } } } return } func (s *Stack) Draw(image *ebiten.Image) { for i := range s.children { s.children[i].Draw(image) } } func (s *Stack) Children() []elements.Element { return s.children }