some refinement and beginning of a table element
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
17
common/elements/v1/core/config.go
Normal file
17
common/elements/v1/core/config.go
Normal 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
|
||||
}
|
||||
}
|
||||
32
common/elements/v1/core/core.go
Normal file
32
common/elements/v1/core/core.go
Normal 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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
3
common/elements/v1/tables/config.go
Normal file
3
common/elements/v1/tables/config.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package tables
|
||||
|
||||
type OptFunc func(*Table)
|
||||
19
common/elements/v1/tables/table.go
Normal file
19
common/elements/v1/tables/table.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package tables
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
core.Element
|
||||
}
|
||||
|
||||
func New(ops ...OptFunc) *Table {
|
||||
t := &Table{}
|
||||
|
||||
for i := range ops {
|
||||
ops[i](t)
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
Reference in New Issue
Block a user