okay giant ux refactor to hopefully make it cleaner

This commit is contained in:
2025-08-31 13:29:52 -04:00
parent 430259475b
commit dda0a83202
12 changed files with 214 additions and 198 deletions

View File

@@ -2,37 +2,29 @@ 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},
}
func New(ops ...OptFunc) elements.InitFunc {
return func() elements.Element {
s := Stack{}
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
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](d)
offsetX, offsetY = s.Children[i].Size()
s.children[i] = s.childrenInit[i]()
s.children[i].SetAnchor(anchor)
cw, ch = s.children[i].Size()
if s.horizontal {
d.Min.X += offsetX
anchor.X += cw
} else {
d.Min.Y += offsetY
anchor.Y += ch
}
}
@@ -41,21 +33,51 @@ func New(ops ...Option) elements.InitFunc {
}
type Stack struct {
blocks.Block
mouse.NopHandler
anchor elements.Point
horizontal bool
childrenInit []elements.InitFunc
Children []elements.Element
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) {
s.Block.Draw(image)
for i := range s.Children {
s.Children[i].Draw(image)
for i := range s.children {
s.children[i].Draw(image)
}
}
func (s *Stack) GetChildren() []elements.Element {
return s.Children
func (s *Stack) Children() []elements.Element {
return s.children
}