some refinement

This commit is contained in:
2025-09-05 22:18:36 -04:00
parent 4e6d720a91
commit 8e7db331a7
16 changed files with 371 additions and 119 deletions

View File

@@ -3,32 +3,30 @@ 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/core"
"git.vezzani.net/ben/games/common/geo"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func New(ops ...OptFunc) elements.Element {
b := Block{}
func New(ops ...OptFunc) *Block {
b := &Block{}
for i := range ops {
ops[i](&b)
ops[i](b)
}
return &b
return b
}
type Block struct {
elements.Core
core.Element
backgroundColor *color.Color
mouse.NopHandler
width, height int
name string
width, height int
name string
}
func (b *Block) Bounds() elements.Bounds {
return elements.Bounds{
func (b *Block) Bounds() geo.Bounds {
return geo.Bounds{
Min: b.Anchor(),
Width: b.width,
Height: b.height,

View File

@@ -3,14 +3,16 @@ package blocks
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/core"
)
type OptFunc func(*Block)
func Core(f elements.OptFunc) OptFunc {
func Core(fs ...core.OptFunc) OptFunc {
return func(b *Block) {
f(&b.Core)
for _, f := range fs {
f(&b.Element)
}
}
}

View File

@@ -3,7 +3,6 @@ package buttons
import (
"image/color"
"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"
"git.vezzani.net/ben/games/common/ux/v1"
@@ -13,7 +12,7 @@ import (
"golang.org/x/image/font"
)
func New(ops ...OptFunc) elements.Element {
func New(ops ...OptFunc) *Button {
b := Button{
Block: blocks.Block{},
font: ux.FontFace,

View File

@@ -3,16 +3,18 @@ package buttons
import (
"image/color"
"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/core"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
)
type OptFunc func(button *Button)
func Core(f elements.OptFunc) OptFunc {
func Core(fs ...core.OptFunc) OptFunc {
return func(b *Button) {
f(&b.Core)
for _, f := range fs {
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

@@ -0,0 +1,17 @@
package core
import "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
}
}

View File

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

View File

@@ -1,63 +1,14 @@
package elements
import (
"git.vezzani.net/ben/games/common/geo"
"github.com/hajimehoshi/ebiten/v2"
)
type Element interface {
Draw(*ebiten.Image)
SetAnchor(Point)
Anchor() Point
SetAnchor(geo.Point)
Anchor() geo.Point
Size() (w, h int)
Children() []Element
}
type Point struct {
X, Y int
}
func (p Point) Delta(p2 Point) Point {
return Point{
X: p2.X - p.X,
Y: p2.Y - p.Y,
}
}
func (p Point) Add(p2 Point) Point {
return Point{
X: p.X + p2.X,
Y: p.Y + p2.Y,
}
}
type Bounds struct {
Min Point
Width, Height int
}
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

@@ -2,6 +2,7 @@ package mouse
import (
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/geo"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
@@ -24,8 +25,8 @@ type State struct {
Clicked bool
}
func (s *State) Point() elements.Point {
return elements.Point{
func (s *State) Point() geo.Point {
return geo.Point{
X: s.X,
Y: s.Y,
}

View File

@@ -1,12 +1,16 @@
package stacks
import "git.vezzani.net/ben/games/common/elements/v1"
import (
"git.vezzani.net/ben/games/common/elements/v1/core"
)
type OptFunc func(*Stack)
func Core(f elements.OptFunc) OptFunc {
func Core(fs ...core.OptFunc) OptFunc {
return func(s *Stack) {
f(&s.Core)
for _, f := range fs {
f(&s.Element)
}
}
}

View File

@@ -1,13 +1,13 @@
package stacks
import (
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/core"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
"github.com/hajimehoshi/ebiten/v2"
)
func New(ops ...OptFunc) elements.Element {
func New(ops ...OptFunc) *Stack {
s := Stack{}
for op := range ops {
ops[op](&s)
@@ -29,7 +29,7 @@ func New(ops ...OptFunc) elements.Element {
}
type Stack struct {
elements.Core
core.Element
mouse.NopHandler
horizontal bool
}

10
common/geo/bounds.go Normal file
View File

@@ -0,0 +1,10 @@
package geo
type Bounds struct {
Min Point
Width, Height int
}
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
}

19
common/geo/point.go Normal file
View File

@@ -0,0 +1,19 @@
package geo
type Point struct {
X, Y int
}
func (p Point) Delta(p2 Point) Point {
return Point{
X: p2.X - p.X,
Y: p2.Y - p.Y,
}
}
func (p Point) Add(p2 Point) Point {
return Point{
X: p.X + p2.X,
Y: p.Y + p2.Y,
}
}