wip I guess

This commit is contained in:
2025-09-05 21:21:18 -04:00
parent 4e6d720a91
commit 9a4993570e
10 changed files with 143 additions and 112 deletions

View File

@@ -0,0 +1,38 @@
package base
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
)
type Element struct {
mouse.NopHandler
anchor elements.Point
children []elements.Element
backgroundColor color.Color
name string // For debugging
}
func (e *Element) Anchor() elements.Point {
return e.anchor
}
func (e *Element) SetAnchor(a elements.Point) {
d := e.Anchor().Delta(a)
for _, ch := range e.Children() {
ch.SetAnchor(ch.Anchor().Add(d))
}
e.anchor = a
}
func (e *Element) Children() []elements.Element {
return e.children
}
func (e *Element) BackgroundColor() color.Color {
return e.backgroundColor
}

View File

@@ -0,0 +1,29 @@
package base
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
)
type OptFunc func(*Element)
func Children(children ...elements.Element) OptFunc {
return func(c *Element) {
c.children = children
}
}
func Name(name string) OptFunc {
return func(s *Element) {
s.name = name
}
}
func (b elements.Builder) BackgroundColor(c color.Color) elements.Builder {
return func() elements.Element {
ce := b().(*Element)
ce.backgroundColor = c
return ce
}
}

View File

@@ -1,28 +1,27 @@
package blocks
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
"git.vezzani.net/ben/games/common/elements/v1/base"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func New(ops ...OptFunc) elements.Element {
b := Block{}
for i := range ops {
ops[i](&b)
type Builder elements.Builder
func New(ops ...OptFunc) Builder {
return func() elements.Element {
b := Block{}
for i := range ops {
ops[i](&b)
}
return &b
}
return &b
}
type Block struct {
elements.Core
backgroundColor *color.Color
mouse.NopHandler
base.Element
width, height int
name string
}
@@ -41,7 +40,7 @@ func (b *Block) Size() (w, h int) {
func (b *Block) Draw(image *ebiten.Image) {
bnd := b.Bounds()
if b.backgroundColor != nil {
if b.BackgroundColor() != nil {
vector.DrawFilledRect(
image,
float32(b.Anchor().X),

View File

@@ -1,28 +1,23 @@
package blocks
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/base"
)
type OptFunc func(*Block)
func Core(f elements.OptFunc) OptFunc {
func Core(f base.OptFunc) OptFunc {
return func(b *Block) {
f(&b.Core)
f(&b.Element)
}
}
func Size(w, h int) OptFunc {
return func(b *Block) {
b.width, b.height = w, h
}
}
func BackgroundColor(c color.Color) OptFunc {
return func(b *Block) {
b.backgroundColor = &c
func (b Builder) Size(w, h int) Builder {
return func() elements.Element {
bl := b().(*Block)
bl.width, bl.height = w, h
return bl
}
}

View File

@@ -3,16 +3,16 @@ package buttons
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/base"
"git.vezzani.net/ben/games/common/elements/v1/blocks"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
)
type OptFunc func(button *Button)
func Core(f elements.OptFunc) OptFunc {
func Core(f base.OptFunc) OptFunc {
return func(b *Button) {
f(&b.Core)
f(&b.Element)
}
}

View File

@@ -1,15 +0,0 @@
package elements
type OptFunc func(*Core)
func Children(children ...Element) OptFunc {
return func(c *Core) {
c.children = children
}
}
func Name(name string) OptFunc {
return func(s *Core) {
s.name = name
}
}

View File

@@ -12,6 +12,8 @@ type Element interface {
Children() []Element
}
type Builder func() Element
type Point struct {
X, Y int
}
@@ -38,26 +40,3 @@ type Bounds struct {
func (b *Bounds) Contains(p Point) bool {
return p.X >= b.Min.X && p.X <= b.Min.X+b.Width && p.Y >= b.Min.Y && p.Y <= b.Min.Y+b.Height
}
type Core struct {
anchor Point
children []Element
name string // For debugging
}
func (c *Core) Anchor() Point {
return c.anchor
}
func (c *Core) SetAnchor(a Point) {
d := c.Anchor().Delta(a)
for _, ch := range c.Children() {
ch.SetAnchor(ch.Anchor().Add(d))
}
c.anchor = a
}
func (c *Core) Children() []Element {
return c.children
}

View File

@@ -1,12 +1,14 @@
package stacks
import "git.vezzani.net/ben/games/common/elements/v1"
import (
"git.vezzani.net/ben/games/common/elements/v1/base"
)
type OptFunc func(*Stack)
func Core(f elements.OptFunc) OptFunc {
func Core(f base.OptFunc) OptFunc {
return func(s *Stack) {
f(&s.Core)
f(&s.Base)
}
}

View File

@@ -29,7 +29,7 @@ func New(ops ...OptFunc) elements.Element {
}
type Stack struct {
elements.Core
elements.Base
mouse.NopHandler
horizontal bool
}