cleaning up the component system just a bit more...
This commit is contained in:
@@ -7,55 +7,37 @@ import (
|
||||
"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
|
||||
func New(ops ...OptFunc) elements.Element {
|
||||
s := Stack{}
|
||||
for op := range ops {
|
||||
ops[op](&s)
|
||||
}
|
||||
|
||||
var cw, ch int
|
||||
anchor := s.Anchor()
|
||||
for _, c := range s.Children() {
|
||||
c.SetAnchor(anchor)
|
||||
cw, ch = c.Size()
|
||||
if s.horizontal {
|
||||
anchor.X += cw
|
||||
} else {
|
||||
anchor.Y += ch
|
||||
}
|
||||
}
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
type Stack struct {
|
||||
elements.Core
|
||||
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
|
||||
horizontal bool
|
||||
}
|
||||
|
||||
func (s *Stack) Size() (w, h int) {
|
||||
var cw, ch int
|
||||
for i := range s.children {
|
||||
cw, ch = s.children[i].Size()
|
||||
for _, c := range s.Children() {
|
||||
cw, ch = c.Size()
|
||||
if s.horizontal {
|
||||
w += cw
|
||||
if h < ch {
|
||||
@@ -73,11 +55,7 @@ func (s *Stack) Size() (w, h int) {
|
||||
}
|
||||
|
||||
func (s *Stack) Draw(image *ebiten.Image) {
|
||||
for i := range s.children {
|
||||
s.children[i].Draw(image)
|
||||
for _, c := range s.Children() {
|
||||
c.Draw(image)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack) Children() []elements.Element {
|
||||
return s.children
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user