4 Commits

10 changed files with 174 additions and 139 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,16 +1,16 @@
package blocks package blocks
import ( import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1" "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"
"github.com/hajimehoshi/ebiten/v2/vector" "github.com/hajimehoshi/ebiten/v2/vector"
) )
func New(ops ...OptFunc) elements.InitFunc { type Builder elements.Builder
func New(ops ...OptFunc) Builder {
return func() elements.Element { return func() elements.Element {
b := Block{} b := Block{}
for i := range ops { for i := range ops {
@@ -21,44 +21,30 @@ func New(ops ...OptFunc) elements.InitFunc {
} }
type Block struct { type Block struct {
anchor elements.Point base.Element
backgroundColor *color.Color
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
} }
func (b *Block) Draw(image *ebiten.Image) { func (b *Block) Draw(image *ebiten.Image) {
bnd := b.Bounds() bnd := b.Bounds()
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 +53,3 @@ func (b *Block) Draw(image *ebiten.Image) {
} }
return return
} }
func (b *Block) Children() []elements.Element {
return nil
}

View File

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

View File

@@ -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)

View File

@@ -3,14 +3,17 @@ package buttons
import ( import (
"image/color" "image/color"
"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/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 base.OptFunc) OptFunc {
OptFunc | blocks.OptFunc return func(b *Button) {
f(&b.Element)
}
} }
func OnClick(f func(ms mouse.State)) OptFunc { func OnClick(f func(ms mouse.State)) OptFunc {

View File

@@ -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)
@@ -14,6 +12,8 @@ type Element interface {
Children() []Element Children() []Element
} }
type Builder func() Element
type Point struct { type Point struct {
X, Y int X, Y int
} }

View File

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

View File

@@ -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.Base
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
}

View File

@@ -3,61 +3,63 @@ package main
import ( import (
"git.vezzani.net/ben/games/common/elements/v1" "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/buttons"
"git.vezzani.net/ben/games/common/elements/v1/mouse" "git.vezzani.net/ben/games/common/elements/v1/mouse"
"git.vezzani.net/ben/games/common/elements/v1/stacks"
"git.vezzani.net/ben/games/common/sprites/v1" "git.vezzani.net/ben/games/common/sprites/v1"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/colornames" "golang.org/x/image/colornames"
) )
var root = stacks.New( //var root = stacks.New(
stacks.Horizontal(), // stacks.Horizontal(),
//stacks.BlockOpt(blocks.BackgroundColor(color.White)), // //stacks.Element(blocks.BackgroundColor(color.White)),
//stacks.BlockOpt(blocks.Name("parent")), // stacks.Element(elements.Name("parent")),
stacks.Children( // stacks.Element(elements.Children(
stacks.New( // stacks.New(
//stacks.BlockOpt(blocks.Name("left")), // stacks.Element(elements.Name("left")),
stacks.Children( // stacks.Element(elements.Children(
buttons.New( // buttons.New(
buttons.BlockOpt(blocks.Size(100, 100)), // buttons.BlockOpt(blocks.Size(100, 100)),
buttons.Label("hello"), // buttons.Label("hello"),
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)), // buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
buttons.OnClick(func(ms mouse.State) { // buttons.OnClick(func(ms mouse.State) {
println("green") // println("green")
}), // }),
), // ),
blocks.New( // blocks.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.Element(elements.Name("right")),
stacks.Children( // stacks.Element(elements.Children(
blocks.New( // blocks.New(
blocks.Size(100, 100), // blocks.Size(100, 100),
blocks.BackgroundColor(colornames.Blue), // blocks.BackgroundColor(colornames.Blue),
), // ),
blocks.New( // blocks.New(
blocks.Size(100, 100), // blocks.Size(100, 100),
blocks.BackgroundColor(colornames.Red), // blocks.BackgroundColor(colornames.Red),
), // ),
), // )),
), // ),
), // )),
) //)
var root = blocks.New().
Size(100, 100).
BackgroundColor(colornames.Red)
func newEditor() *editor { func newEditor() *editor {
return &editor{} return &editor{}
} }
type editor struct { type editor struct {
root elements.Element
bounds elements.Bounds bounds elements.Bounds
handleMouse func() bool handleMouse func() bool
root elements.Element
} }
func (e *editor) Update() error { func (e *editor) Update() error {