cleaning up the component system just a bit more...
This commit is contained in:
@@ -10,44 +10,31 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) elements.InitFunc {
|
func New(ops ...OptFunc) elements.Element {
|
||||||
return func() elements.Element {
|
b := Block{}
|
||||||
b := Block{}
|
for i := range ops {
|
||||||
for i := range ops {
|
ops[i](&b)
|
||||||
ops[i](&b)
|
|
||||||
}
|
|
||||||
return &b
|
|
||||||
}
|
}
|
||||||
|
return &b
|
||||||
}
|
}
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
anchor elements.Point
|
elements.Core
|
||||||
|
|
||||||
backgroundColor *color.Color
|
backgroundColor *color.Color
|
||||||
mouse.NopHandler
|
mouse.NopHandler
|
||||||
width, height int
|
width, height int
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) SetAnchor(a elements.Point) {
|
|
||||||
d := b.anchor.Delta(a)
|
|
||||||
for i := range b.Children() {
|
|
||||||
b.Children()[i].SetAnchor(b.Children()[i].Anchor().Add(d))
|
|
||||||
}
|
|
||||||
b.anchor = a
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) Bounds() elements.Bounds {
|
func (b *Block) Bounds() elements.Bounds {
|
||||||
return elements.Bounds{
|
return elements.Bounds{
|
||||||
Min: b.anchor,
|
Min: b.Anchor(),
|
||||||
Width: b.width,
|
Width: b.width,
|
||||||
Height: b.height,
|
Height: b.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) Anchor() elements.Point {
|
|
||||||
return b.anchor
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) Size() (w, h int) {
|
func (b *Block) Size() (w, h int) {
|
||||||
return b.width, b.height
|
return b.width, b.height
|
||||||
}
|
}
|
||||||
@@ -57,8 +44,8 @@ func (b *Block) Draw(image *ebiten.Image) {
|
|||||||
if b.backgroundColor != nil {
|
if b.backgroundColor != nil {
|
||||||
vector.DrawFilledRect(
|
vector.DrawFilledRect(
|
||||||
image,
|
image,
|
||||||
float32(b.anchor.X),
|
float32(b.Anchor().X),
|
||||||
float32(b.anchor.Y),
|
float32(b.Anchor().Y),
|
||||||
float32(bnd.Width),
|
float32(bnd.Width),
|
||||||
float32(bnd.Height),
|
float32(bnd.Height),
|
||||||
*b.backgroundColor,
|
*b.backgroundColor,
|
||||||
@@ -67,7 +54,3 @@ func (b *Block) Draw(image *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) Children() []elements.Element {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,9 +1,19 @@
|
|||||||
package blocks
|
package blocks
|
||||||
|
|
||||||
import "image/color"
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
)
|
||||||
|
|
||||||
type OptFunc func(*Block)
|
type OptFunc func(*Block)
|
||||||
|
|
||||||
|
func Core(f elements.OptFunc) OptFunc {
|
||||||
|
return func(b *Block) {
|
||||||
|
f(&b.Core)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Size(w, h int) OptFunc {
|
func Size(w, h int) OptFunc {
|
||||||
return func(b *Block) {
|
return func(b *Block) {
|
||||||
b.width, b.height = w, h
|
b.width, b.height = w, h
|
||||||
|
|||||||
@@ -13,19 +13,17 @@ import (
|
|||||||
"golang.org/x/image/font"
|
"golang.org/x/image/font"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) elements.InitFunc {
|
func New(ops ...OptFunc) elements.Element {
|
||||||
return func() elements.Element {
|
b := Button{
|
||||||
b := Button{
|
Block: blocks.Block{},
|
||||||
Block: blocks.Block{},
|
font: ux.FontFace,
|
||||||
font: ux.FontFace,
|
color: ux.FontColor,
|
||||||
color: ux.FontColor,
|
|
||||||
}
|
|
||||||
for op := range ops {
|
|
||||||
ops[op](&b)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &b
|
|
||||||
}
|
}
|
||||||
|
for op := range ops {
|
||||||
|
ops[op](&b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &b
|
||||||
}
|
}
|
||||||
|
|
||||||
type Button struct {
|
type Button struct {
|
||||||
@@ -67,7 +65,7 @@ func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
|||||||
if !bnd.Contains(ms.Point()) {
|
if !bnd.Contains(ms.Point()) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
||||||
b.onClick(ms)
|
b.onClick(ms)
|
||||||
|
|||||||
@@ -3,14 +3,17 @@ package buttons
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"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/blocks"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OptFunc func(button *Button)
|
type OptFunc func(button *Button)
|
||||||
|
|
||||||
type Option interface {
|
func Core(f elements.OptFunc) OptFunc {
|
||||||
OptFunc | blocks.OptFunc
|
return func(b *Button) {
|
||||||
|
f(&b.Core)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnClick(f func(ms mouse.State)) OptFunc {
|
func OnClick(f func(ms mouse.State)) OptFunc {
|
||||||
|
|||||||
15
common/elements/v1/config.go
Normal file
15
common/elements/v1/config.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InitFunc func() Element
|
|
||||||
|
|
||||||
type Element interface {
|
type Element interface {
|
||||||
Draw(*ebiten.Image)
|
Draw(*ebiten.Image)
|
||||||
SetAnchor(Point)
|
SetAnchor(Point)
|
||||||
@@ -40,3 +38,26 @@ type Bounds struct {
|
|||||||
func (b *Bounds) Contains(p Point) bool {
|
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
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import "git.vezzani.net/ben/games/common/elements/v1"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
|
||||||
)
|
|
||||||
|
|
||||||
type OptFunc func(*Stack)
|
type OptFunc func(*Stack)
|
||||||
|
|
||||||
func Children(children ...elements.InitFunc) OptFunc {
|
func Core(f elements.OptFunc) OptFunc {
|
||||||
return func(s *Stack) {
|
return func(s *Stack) {
|
||||||
s.childrenInit = children
|
f(&s.Core)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,55 +7,37 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) elements.InitFunc {
|
func New(ops ...OptFunc) elements.Element {
|
||||||
return func() elements.Element {
|
s := Stack{}
|
||||||
s := Stack{}
|
for op := range ops {
|
||||||
for op := range ops {
|
ops[op](&s)
|
||||||
ops[op](&s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.children = make([]elements.Element, len(s.childrenInit))
|
|
||||||
var cw, ch int
|
|
||||||
anchor := s.anchor
|
|
||||||
for i := range s.childrenInit {
|
|
||||||
s.children[i] = s.childrenInit[i]()
|
|
||||||
s.children[i].SetAnchor(anchor)
|
|
||||||
cw, ch = s.children[i].Size()
|
|
||||||
if s.horizontal {
|
|
||||||
anchor.X += cw
|
|
||||||
} else {
|
|
||||||
anchor.Y += ch
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cw, ch int
|
||||||
|
anchor := s.Anchor()
|
||||||
|
for _, c := range s.Children() {
|
||||||
|
c.SetAnchor(anchor)
|
||||||
|
cw, ch = c.Size()
|
||||||
|
if s.horizontal {
|
||||||
|
anchor.X += cw
|
||||||
|
} else {
|
||||||
|
anchor.Y += ch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
|
elements.Core
|
||||||
mouse.NopHandler
|
mouse.NopHandler
|
||||||
anchor elements.Point
|
horizontal bool
|
||||||
horizontal bool
|
|
||||||
childrenInit []elements.InitFunc
|
|
||||||
children []elements.Element
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stack) SetAnchor(a elements.Point) {
|
|
||||||
d := s.anchor.Delta(a)
|
|
||||||
for i := range s.Children() {
|
|
||||||
s.Children()[i].SetAnchor(s.Children()[i].Anchor().Add(d))
|
|
||||||
}
|
|
||||||
s.anchor = a
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stack) Anchor() elements.Point {
|
|
||||||
return s.anchor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Size() (w, h int) {
|
func (s *Stack) Size() (w, h int) {
|
||||||
var cw, ch int
|
var cw, ch int
|
||||||
for i := range s.children {
|
for _, c := range s.Children() {
|
||||||
cw, ch = s.children[i].Size()
|
cw, ch = c.Size()
|
||||||
if s.horizontal {
|
if s.horizontal {
|
||||||
w += cw
|
w += cw
|
||||||
if h < ch {
|
if h < ch {
|
||||||
@@ -73,11 +55,7 @@ func (s *Stack) Size() (w, h int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Draw(image *ebiten.Image) {
|
func (s *Stack) Draw(image *ebiten.Image) {
|
||||||
for i := range s.children {
|
for _, c := range s.Children() {
|
||||||
s.children[i].Draw(image)
|
c.Draw(image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Children() []elements.Element {
|
|
||||||
return s.children
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ import (
|
|||||||
|
|
||||||
var root = stacks.New(
|
var root = stacks.New(
|
||||||
stacks.Horizontal(),
|
stacks.Horizontal(),
|
||||||
//stacks.BlockOpt(blocks.BackgroundColor(color.White)),
|
//stacks.Core(blocks.BackgroundColor(color.White)),
|
||||||
//stacks.BlockOpt(blocks.Name("parent")),
|
stacks.Core(elements.Name("parent")),
|
||||||
stacks.Children(
|
stacks.Core(elements.Children(
|
||||||
stacks.New(
|
stacks.New(
|
||||||
//stacks.BlockOpt(blocks.Name("left")),
|
stacks.Core(elements.Name("left")),
|
||||||
stacks.Children(
|
stacks.Core(elements.Children(
|
||||||
buttons.New(
|
buttons.New(
|
||||||
buttons.BlockOpt(blocks.Size(100, 100)),
|
buttons.BlockOpt(blocks.Size(100, 100)),
|
||||||
buttons.Label("hello"),
|
buttons.Label("hello"),
|
||||||
@@ -32,11 +32,11 @@ var root = stacks.New(
|
|||||||
blocks.Size(100, 100),
|
blocks.Size(100, 100),
|
||||||
blocks.BackgroundColor(colornames.Yellow),
|
blocks.BackgroundColor(colornames.Yellow),
|
||||||
),
|
),
|
||||||
),
|
)),
|
||||||
),
|
),
|
||||||
stacks.New(
|
stacks.New(
|
||||||
//stacks.BlockOpt(blocks.Name("right")),
|
stacks.Core(elements.Name("right")),
|
||||||
stacks.Children(
|
stacks.Core(elements.Children(
|
||||||
blocks.New(
|
blocks.New(
|
||||||
blocks.Size(100, 100),
|
blocks.Size(100, 100),
|
||||||
blocks.BackgroundColor(colornames.Blue),
|
blocks.BackgroundColor(colornames.Blue),
|
||||||
@@ -45,9 +45,9 @@ var root = stacks.New(
|
|||||||
blocks.Size(100, 100),
|
blocks.Size(100, 100),
|
||||||
blocks.BackgroundColor(colornames.Red),
|
blocks.BackgroundColor(colornames.Red),
|
||||||
),
|
),
|
||||||
),
|
)),
|
||||||
),
|
),
|
||||||
),
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
func newEditor() *editor {
|
func newEditor() *editor {
|
||||||
@@ -55,7 +55,6 @@ func newEditor() *editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type editor struct {
|
type editor struct {
|
||||||
root elements.Element
|
|
||||||
bounds elements.Bounds
|
bounds elements.Bounds
|
||||||
handleMouse func() bool
|
handleMouse func() bool
|
||||||
}
|
}
|
||||||
@@ -67,7 +66,7 @@ func (e *editor) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *editor) Draw(screen *ebiten.Image) {
|
func (e *editor) Draw(screen *ebiten.Image) {
|
||||||
e.root.Draw(screen)
|
root.Draw(screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||||
@@ -77,8 +76,7 @@ func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHei
|
|||||||
Width: outsideWidth,
|
Width: outsideWidth,
|
||||||
Height: outsideHeight,
|
Height: outsideHeight,
|
||||||
}
|
}
|
||||||
e.root = root()
|
e.handleMouse = mouse.Handler(root)
|
||||||
e.handleMouse = mouse.Handler(e.root)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return outsideWidth, outsideHeight
|
return outsideWidth, outsideHeight
|
||||||
|
|||||||
Reference in New Issue
Block a user