holy shit components are rendering whaaaaaaaaaaaaaaaat
This commit is contained in:
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.Dimensions) elements.Element {
|
||||
s := Stack{
|
||||
Block: blocks.Block{InheritedDimensions: 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.InheritedDimensions
|
||||
d.WX, d.WY = s.Block.Size()
|
||||
if s.horizontal {
|
||||
d.WX = d.WX / float64(len(s.children))
|
||||
} else {
|
||||
d.WY = d.WY / 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.ZX += offsetX
|
||||
} else {
|
||||
d.ZY += offsetY
|
||||
}
|
||||
}
|
||||
return s.Block.Size()
|
||||
}
|
||||
Reference in New Issue
Block a user