wip I guess
This commit is contained in:
38
common/elements/v1/base/base.go
Normal file
38
common/elements/v1/base/base.go
Normal 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
|
||||||
|
}
|
||||||
29
common/elements/v1/base/config.go
Normal file
29
common/elements/v1/base/config.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +1,27 @@
|
|||||||
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.Element {
|
type Builder elements.Builder
|
||||||
b := Block{}
|
|
||||||
for i := range ops {
|
func New(ops ...OptFunc) Builder {
|
||||||
ops[i](&b)
|
return func() elements.Element {
|
||||||
|
b := Block{}
|
||||||
|
for i := range ops {
|
||||||
|
ops[i](&b)
|
||||||
|
}
|
||||||
|
return &b
|
||||||
}
|
}
|
||||||
return &b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
elements.Core
|
base.Element
|
||||||
|
|
||||||
backgroundColor *color.Color
|
|
||||||
mouse.NopHandler
|
|
||||||
width, height int
|
width, height int
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
@@ -41,7 +40,7 @@ func (b *Block) Size() (w, h int) {
|
|||||||
|
|
||||||
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),
|
||||||
|
|||||||
@@ -1,28 +1,23 @@
|
|||||||
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/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OptFunc func(*Block)
|
type OptFunc func(*Block)
|
||||||
|
|
||||||
func Core(f elements.OptFunc) OptFunc {
|
func Core(f base.OptFunc) OptFunc {
|
||||||
return func(b *Block) {
|
return func(b *Block) {
|
||||||
f(&b.Core)
|
f(&b.Element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Size(w, h int) OptFunc {
|
func (b Builder) Size(w, h int) Builder {
|
||||||
return func(b *Block) {
|
return func() elements.Element {
|
||||||
b.width, b.height = w, h
|
bl := b().(*Block)
|
||||||
}
|
bl.width, bl.height = w, h
|
||||||
}
|
return bl
|
||||||
|
|
||||||
func BackgroundColor(c color.Color) OptFunc {
|
|
||||||
return func(b *Block) {
|
|
||||||
b.backgroundColor = &c
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,16 @@ package buttons
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"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/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)
|
||||||
|
|
||||||
func Core(f elements.OptFunc) OptFunc {
|
func Core(f base.OptFunc) OptFunc {
|
||||||
return func(b *Button) {
|
return func(b *Button) {
|
||||||
f(&b.Core)
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,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
|
||||||
}
|
}
|
||||||
@@ -38,26 +40,3 @@ 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,12 +1,14 @@
|
|||||||
package stacks
|
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)
|
type OptFunc func(*Stack)
|
||||||
|
|
||||||
func Core(f elements.OptFunc) OptFunc {
|
func Core(f base.OptFunc) OptFunc {
|
||||||
return func(s *Stack) {
|
return func(s *Stack) {
|
||||||
f(&s.Core)
|
f(&s.Base)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func New(ops ...OptFunc) elements.Element {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
elements.Core
|
elements.Base
|
||||||
mouse.NopHandler
|
mouse.NopHandler
|
||||||
horizontal bool
|
horizontal bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,52 +3,54 @@ 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.Core(blocks.BackgroundColor(color.White)),
|
// //stacks.Element(blocks.BackgroundColor(color.White)),
|
||||||
stacks.Core(elements.Name("parent")),
|
// stacks.Element(elements.Name("parent")),
|
||||||
stacks.Core(elements.Children(
|
// stacks.Element(elements.Children(
|
||||||
stacks.New(
|
// stacks.New(
|
||||||
stacks.Core(elements.Name("left")),
|
// stacks.Element(elements.Name("left")),
|
||||||
stacks.Core(elements.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.Core(elements.Name("right")),
|
// stacks.Element(elements.Name("right")),
|
||||||
stacks.Core(elements.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{}
|
||||||
@@ -57,6 +59,7 @@ func newEditor() *editor {
|
|||||||
type editor struct {
|
type editor struct {
|
||||||
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 {
|
||||||
@@ -66,7 +69,7 @@ func (e *editor) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *editor) Draw(screen *ebiten.Image) {
|
func (e *editor) Draw(screen *ebiten.Image) {
|
||||||
root.Draw(screen)
|
e.root.Draw(screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||||
@@ -76,7 +79,8 @@ func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHei
|
|||||||
Width: outsideWidth,
|
Width: outsideWidth,
|
||||||
Height: outsideHeight,
|
Height: outsideHeight,
|
||||||
}
|
}
|
||||||
e.handleMouse = mouse.Handler(root)
|
e.root = root()
|
||||||
|
e.handleMouse = mouse.Handler(e.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
return outsideWidth, outsideHeight
|
return outsideWidth, outsideHeight
|
||||||
|
|||||||
Reference in New Issue
Block a user