holy shit components are rendering whaaaaaaaaaaaaaaaat
This commit was merged in pull request #4.
This commit is contained in:
26
common/elements/v1/stacks/options.go
Normal file
26
common/elements/v1/stacks/options.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package stacks
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
)
|
||||
|
||||
type Option func(*Stack)
|
||||
|
||||
func BlockOpt(o blocks.Option) Option {
|
||||
return func(s *Stack) {
|
||||
o(&s.Block)
|
||||
}
|
||||
}
|
||||
|
||||
func Children(children ...elements.ElementFunc) Option {
|
||||
return func(s *Stack) {
|
||||
s.children = children
|
||||
}
|
||||
}
|
||||
|
||||
func Horizontal() Option {
|
||||
return func(s *Stack) {
|
||||
s.horizontal = true
|
||||
}
|
||||
}
|
||||
51
common/elements/v1/stacks/stack.go
Normal file
51
common/elements/v1/stacks/stack.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user