Compare commits
3 Commits
e454d649a5
...
config-sim
| Author | SHA1 | Date | |
|---|---|---|---|
|
9a4993570e
|
|||
| 4e6d720a91 | |||
|
5f47a811aa
|
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,16 +1,16 @@
|
||||
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/base"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"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 {
|
||||
b := Block{}
|
||||
for i := range ops {
|
||||
@@ -21,44 +21,30 @@ func New(ops ...OptFunc) elements.InitFunc {
|
||||
}
|
||||
|
||||
type Block struct {
|
||||
anchor elements.Point
|
||||
backgroundColor *color.Color
|
||||
mouse.NopHandler
|
||||
base.Element
|
||||
width, height int
|
||||
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 {
|
||||
return elements.Bounds{
|
||||
Min: b.anchor,
|
||||
Min: b.Anchor(),
|
||||
Width: b.width,
|
||||
Height: b.height,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Block) Anchor() elements.Point {
|
||||
return b.anchor
|
||||
}
|
||||
|
||||
func (b *Block) Size() (w, h int) {
|
||||
return b.width, b.height
|
||||
}
|
||||
|
||||
func (b *Block) Draw(image *ebiten.Image) {
|
||||
bnd := b.Bounds()
|
||||
if b.backgroundColor != nil {
|
||||
if b.BackgroundColor() != nil {
|
||||
vector.DrawFilledRect(
|
||||
image,
|
||||
float32(b.anchor.X),
|
||||
float32(b.anchor.Y),
|
||||
float32(b.Anchor().X),
|
||||
float32(b.Anchor().Y),
|
||||
float32(bnd.Width),
|
||||
float32(bnd.Height),
|
||||
*b.backgroundColor,
|
||||
@@ -67,7 +53,3 @@ func (b *Block) Draw(image *ebiten.Image) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (b *Block) Children() []elements.Element {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
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)
|
||||
|
||||
func Size(w, h int) OptFunc {
|
||||
func Core(f base.OptFunc) OptFunc {
|
||||
return func(b *Block) {
|
||||
b.width, b.height = w, h
|
||||
f(&b.Element)
|
||||
}
|
||||
}
|
||||
|
||||
func BackgroundColor(c color.Color) OptFunc {
|
||||
return func(b *Block) {
|
||||
b.backgroundColor = &c
|
||||
func (b Builder) Size(w, h int) Builder {
|
||||
return func() elements.Element {
|
||||
bl := b().(*Block)
|
||||
bl.width, bl.height = w, h
|
||||
return bl
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,19 +13,17 @@ import (
|
||||
"golang.org/x/image/font"
|
||||
)
|
||||
|
||||
func New(ops ...OptFunc) elements.InitFunc {
|
||||
return func() elements.Element {
|
||||
b := Button{
|
||||
Block: blocks.Block{},
|
||||
font: ux.FontFace,
|
||||
color: ux.FontColor,
|
||||
}
|
||||
for op := range ops {
|
||||
ops[op](&b)
|
||||
}
|
||||
|
||||
return &b
|
||||
func New(ops ...OptFunc) elements.Element {
|
||||
b := Button{
|
||||
Block: blocks.Block{},
|
||||
font: ux.FontFace,
|
||||
color: ux.FontColor,
|
||||
}
|
||||
for op := range ops {
|
||||
ops[op](&b)
|
||||
}
|
||||
|
||||
return &b
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
@@ -67,7 +65,7 @@ func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
||||
if !bnd.Contains(ms.Point()) {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
switch {
|
||||
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
||||
b.onClick(ms)
|
||||
|
||||
@@ -3,14 +3,17 @@ package buttons
|
||||
import (
|
||||
"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/mouse"
|
||||
)
|
||||
|
||||
type OptFunc func(button *Button)
|
||||
|
||||
type Option interface {
|
||||
OptFunc | blocks.OptFunc
|
||||
func Core(f base.OptFunc) OptFunc {
|
||||
return func(b *Button) {
|
||||
f(&b.Element)
|
||||
}
|
||||
}
|
||||
|
||||
func OnClick(f func(ms mouse.State)) OptFunc {
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type InitFunc func() Element
|
||||
|
||||
type Element interface {
|
||||
Draw(*ebiten.Image)
|
||||
SetAnchor(Point)
|
||||
@@ -14,6 +12,8 @@ type Element interface {
|
||||
Children() []Element
|
||||
}
|
||||
|
||||
type Builder func() Element
|
||||
|
||||
type Point struct {
|
||||
X, Y int
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package stacks
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/base"
|
||||
)
|
||||
|
||||
type OptFunc func(*Stack)
|
||||
|
||||
func Children(children ...elements.InitFunc) OptFunc {
|
||||
func Core(f base.OptFunc) OptFunc {
|
||||
return func(s *Stack) {
|
||||
s.childrenInit = children
|
||||
f(&s.Base)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,55 +7,37 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
func New(ops ...OptFunc) elements.InitFunc {
|
||||
return func() elements.Element {
|
||||
s := Stack{}
|
||||
for op := range ops {
|
||||
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
|
||||
func New(ops ...OptFunc) elements.Element {
|
||||
s := Stack{}
|
||||
for op := range ops {
|
||||
ops[op](&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 {
|
||||
elements.Base
|
||||
mouse.NopHandler
|
||||
anchor elements.Point
|
||||
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
|
||||
horizontal bool
|
||||
}
|
||||
|
||||
func (s *Stack) Size() (w, h int) {
|
||||
var cw, ch int
|
||||
for i := range s.children {
|
||||
cw, ch = s.children[i].Size()
|
||||
for _, c := range s.Children() {
|
||||
cw, ch = c.Size()
|
||||
if s.horizontal {
|
||||
w += cw
|
||||
if h < ch {
|
||||
@@ -73,11 +55,7 @@ func (s *Stack) Size() (w, h int) {
|
||||
}
|
||||
|
||||
func (s *Stack) Draw(image *ebiten.Image) {
|
||||
for i := range s.children {
|
||||
s.children[i].Draw(image)
|
||||
for _, c := range s.Children() {
|
||||
c.Draw(image)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack) Children() []elements.Element {
|
||||
return s.children
|
||||
}
|
||||
|
||||
@@ -3,61 +3,63 @@ package main
|
||||
import (
|
||||
"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/buttons"
|
||||
"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"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"golang.org/x/image/colornames"
|
||||
)
|
||||
|
||||
var root = stacks.New(
|
||||
stacks.Horizontal(),
|
||||
//stacks.BlockOpt(blocks.BackgroundColor(color.White)),
|
||||
//stacks.BlockOpt(blocks.Name("parent")),
|
||||
stacks.Children(
|
||||
stacks.New(
|
||||
//stacks.BlockOpt(blocks.Name("left")),
|
||||
stacks.Children(
|
||||
buttons.New(
|
||||
buttons.BlockOpt(blocks.Size(100, 100)),
|
||||
buttons.Label("hello"),
|
||||
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
||||
buttons.OnClick(func(ms mouse.State) {
|
||||
println("green")
|
||||
}),
|
||||
),
|
||||
blocks.New(
|
||||
blocks.Size(100, 100),
|
||||
blocks.BackgroundColor(colornames.Yellow),
|
||||
),
|
||||
),
|
||||
),
|
||||
stacks.New(
|
||||
//stacks.BlockOpt(blocks.Name("right")),
|
||||
stacks.Children(
|
||||
blocks.New(
|
||||
blocks.Size(100, 100),
|
||||
blocks.BackgroundColor(colornames.Blue),
|
||||
),
|
||||
blocks.New(
|
||||
blocks.Size(100, 100),
|
||||
blocks.BackgroundColor(colornames.Red),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
//var root = stacks.New(
|
||||
// stacks.Horizontal(),
|
||||
// //stacks.Element(blocks.BackgroundColor(color.White)),
|
||||
// stacks.Element(elements.Name("parent")),
|
||||
// stacks.Element(elements.Children(
|
||||
// stacks.New(
|
||||
// stacks.Element(elements.Name("left")),
|
||||
// stacks.Element(elements.Children(
|
||||
// buttons.New(
|
||||
// buttons.BlockOpt(blocks.Size(100, 100)),
|
||||
// buttons.Label("hello"),
|
||||
// buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
||||
// buttons.OnClick(func(ms mouse.State) {
|
||||
// println("green")
|
||||
// }),
|
||||
// ),
|
||||
// blocks.New(
|
||||
// blocks.Size(100, 100),
|
||||
// blocks.BackgroundColor(colornames.Yellow),
|
||||
// ),
|
||||
// )),
|
||||
// ),
|
||||
// stacks.New(
|
||||
// stacks.Element(elements.Name("right")),
|
||||
// stacks.Element(elements.Children(
|
||||
// blocks.New(
|
||||
// blocks.Size(100, 100),
|
||||
// blocks.BackgroundColor(colornames.Blue),
|
||||
// ),
|
||||
// blocks.New(
|
||||
// blocks.Size(100, 100),
|
||||
// blocks.BackgroundColor(colornames.Red),
|
||||
// ),
|
||||
// )),
|
||||
// ),
|
||||
// )),
|
||||
//)
|
||||
|
||||
var root = blocks.New().
|
||||
Size(100, 100).
|
||||
BackgroundColor(colornames.Red)
|
||||
|
||||
func newEditor() *editor {
|
||||
return &editor{}
|
||||
}
|
||||
|
||||
type editor struct {
|
||||
root elements.Element
|
||||
bounds elements.Bounds
|
||||
handleMouse func() bool
|
||||
root elements.Element
|
||||
}
|
||||
|
||||
func (e *editor) Update() error {
|
||||
|
||||
Reference in New Issue
Block a user