Compare commits
4 Commits
master
...
ux-cleanup
| Author | SHA1 | Date | |
|---|---|---|---|
|
5f47a811aa
|
|||
| e454d649a5 | |||
|
dda0a83202
|
|||
|
430259475b
|
@@ -5,51 +5,49 @@ import (
|
||||
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||
)
|
||||
|
||||
func New(ops ...Option) elements.ElementFunc {
|
||||
return func(d elements.Bounds) elements.Element {
|
||||
b := Block{
|
||||
ContainerBounds: d,
|
||||
}
|
||||
for i := range ops {
|
||||
ops[i](&b)
|
||||
}
|
||||
return &b
|
||||
func New(ops ...OptFunc) elements.Element {
|
||||
b := Block{}
|
||||
for i := range ops {
|
||||
ops[i](&b)
|
||||
}
|
||||
return &b
|
||||
}
|
||||
|
||||
type Block struct {
|
||||
ContainerBounds elements.Bounds
|
||||
elements.Core
|
||||
|
||||
backgroundColor *color.Color
|
||||
mouse.NopHandler
|
||||
width float64
|
||||
height float64
|
||||
name string
|
||||
width, height int
|
||||
name string
|
||||
}
|
||||
|
||||
func (b *Block) Size() (w, h float64) {
|
||||
w, h = b.ContainerBounds.Width, b.ContainerBounds.Height
|
||||
if b.width != 0 {
|
||||
w = b.width
|
||||
func (b *Block) Bounds() elements.Bounds {
|
||||
return elements.Bounds{
|
||||
Min: b.Anchor(),
|
||||
Width: b.width,
|
||||
Height: b.height,
|
||||
}
|
||||
if b.height != 0 {
|
||||
h = b.height
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (b *Block) Draw(image *ebiten.Image) (w, h float64) {
|
||||
w, h = b.Size()
|
||||
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 {
|
||||
vector.DrawFilledRect(
|
||||
image,
|
||||
float32(b.ContainerBounds.Min.X),
|
||||
float32(b.ContainerBounds.Min.Y),
|
||||
float32(w),
|
||||
float32(h),
|
||||
float32(b.Anchor().X),
|
||||
float32(b.Anchor().Y),
|
||||
float32(bnd.Width),
|
||||
float32(bnd.Height),
|
||||
*b.backgroundColor,
|
||||
true,
|
||||
)
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
package blocks
|
||||
|
||||
import "image/color"
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
type Option func(*Block)
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
)
|
||||
|
||||
func Size(w, h float64) Option {
|
||||
type OptFunc func(*Block)
|
||||
|
||||
func Core(f elements.OptFunc) OptFunc {
|
||||
return func(b *Block) {
|
||||
f(&b.Core)
|
||||
}
|
||||
}
|
||||
|
||||
func Size(w, h int) OptFunc {
|
||||
return func(b *Block) {
|
||||
b.width, b.height = w, h
|
||||
}
|
||||
}
|
||||
|
||||
func BackgroundColor(c color.Color) Option {
|
||||
func BackgroundColor(c color.Color) OptFunc {
|
||||
return func(b *Block) {
|
||||
b.backgroundColor = &c
|
||||
}
|
||||
}
|
||||
|
||||
func Name(n string) Option {
|
||||
func Name(n string) OptFunc {
|
||||
return func(b *Block) {
|
||||
b.name = n
|
||||
}
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
package elements
|
||||
|
||||
//
|
||||
//import (
|
||||
// "image/color"
|
||||
//)
|
||||
//
|
||||
//type xAlign int
|
||||
//type yAlign int
|
||||
//
|
||||
//const (
|
||||
// AlignCente xAlign = iota
|
||||
// AlignLeft
|
||||
// AlignRight
|
||||
//)
|
||||
//
|
||||
//const (
|
||||
// AlignCenter yAlign = iota
|
||||
// AlignTop
|
||||
// AlignBottom
|
||||
//)
|
||||
//
|
||||
//type Button struct {
|
||||
// mouseHandler
|
||||
// block
|
||||
// Label string
|
||||
// OnClick func() error
|
||||
// OnRightClick func() error
|
||||
// Style struct {
|
||||
// MouseDownColor *color.Color
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//func (b *Button) HandleClick(_ MouseState) error {
|
||||
// if b.OnClick == nil {
|
||||
// return nil
|
||||
// }
|
||||
// return b.OnClick()
|
||||
//}
|
||||
//
|
||||
//
|
||||
//func (b *Button) getFont() font.Face {
|
||||
// if b.block.Font == nil {
|
||||
// return ux.FontFace
|
||||
// }
|
||||
//
|
||||
// return *b.block.block.Font
|
||||
//}
|
||||
//
|
||||
//func (b *Button) backgroundColor() color.Color {
|
||||
// var c *color.Color
|
||||
// if b.block.block.BackgroundColor != nil {
|
||||
// c = b.block.block.BackgroundColor
|
||||
// } else {
|
||||
// c = &ux.BackgroundColor
|
||||
// }
|
||||
//
|
||||
// if (b.mouseState.RightDown || b.mouseState.LeftDown) && b.Style.MouseDownColor != nil {
|
||||
// c = b.Style.MouseDownColor
|
||||
// }
|
||||
//
|
||||
// return *c
|
||||
//}
|
||||
//
|
||||
//func (b *Button) Draw(ctx context.Context, image *ebiten.Image) error {
|
||||
//
|
||||
// vector.StrokeRect(image, xz, yz, w, h, 1, b.backgroundColor(), true)
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
82
common/elements/v1/buttons/button.go
Normal file
82
common/elements/v1/buttons/button.go
Normal file
@@ -0,0 +1,82 @@
|
||||
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"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/text"
|
||||
"golang.org/x/image/font"
|
||||
)
|
||||
|
||||
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 {
|
||||
blocks.Block
|
||||
|
||||
label string
|
||||
font font.Face
|
||||
color color.Color
|
||||
|
||||
onClick func(ms mouse.State)
|
||||
onMouseDown func(ms mouse.State)
|
||||
onMouseUp func(ms mouse.State)
|
||||
}
|
||||
|
||||
func (b *Button) Draw(screen *ebiten.Image) {
|
||||
b.Block.Draw(screen)
|
||||
b.drawLabel(screen)
|
||||
}
|
||||
|
||||
func (b *Button) drawLabel(screen *ebiten.Image) {
|
||||
if b.label != "" {
|
||||
strBounds, _ := font.BoundString(b.font, b.label)
|
||||
|
||||
textWidth := strBounds.Max.X - strBounds.Min.X
|
||||
textHeight := strBounds.Max.Y - strBounds.Min.Y
|
||||
|
||||
bnd := b.Bounds()
|
||||
tx := bnd.Min.X + (bnd.Width / 2) - (textWidth / 2).Round()
|
||||
ty := bnd.Min.Y + (bnd.Height / 2) - (textHeight / 2).Round()
|
||||
|
||||
text.Draw(screen, b.label, b.font, tx, ty, b.color)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
||||
bnd := b.Bounds()
|
||||
|
||||
if !bnd.Contains(ms.Point()) {
|
||||
return false
|
||||
}
|
||||
|
||||
switch {
|
||||
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
||||
b.onClick(ms)
|
||||
return true
|
||||
case b.onMouseUp != nil && (ms.RightChanged && !ms.RightDown || ms.LeftChanged && !ms.LeftDown):
|
||||
b.onMouseUp(ms)
|
||||
return true
|
||||
case b.onMouseDown != nil && (ms.RightChanged && ms.RightDown || ms.LeftChanged && ms.LeftDown):
|
||||
b.onMouseDown(ms)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
53
common/elements/v1/buttons/options.go
Normal file
53
common/elements/v1/buttons/options.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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"
|
||||
)
|
||||
|
||||
type OptFunc func(button *Button)
|
||||
|
||||
func Core(f elements.OptFunc) OptFunc {
|
||||
return func(b *Button) {
|
||||
f(&b.Core)
|
||||
}
|
||||
}
|
||||
|
||||
func OnClick(f func(ms mouse.State)) OptFunc {
|
||||
return func(button *Button) {
|
||||
button.onClick = f
|
||||
}
|
||||
}
|
||||
|
||||
func OnMouseDown(f func(ms mouse.State)) OptFunc {
|
||||
return func(button *Button) {
|
||||
button.onMouseDown = f
|
||||
}
|
||||
}
|
||||
|
||||
func OnMouseUp(f func(ms mouse.State)) OptFunc {
|
||||
return func(button *Button) {
|
||||
button.onMouseUp = f
|
||||
}
|
||||
}
|
||||
|
||||
func BlockOpt(o blocks.OptFunc) OptFunc {
|
||||
return func(s *Button) {
|
||||
o(&s.Block)
|
||||
}
|
||||
}
|
||||
|
||||
func Label(s string) OptFunc {
|
||||
return func(button *Button) {
|
||||
button.label = s
|
||||
}
|
||||
}
|
||||
|
||||
func LabelColor(color color.Color) OptFunc {
|
||||
return func(button *Button) {
|
||||
button.color = color
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,63 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type ElementFunc func(Bounds) Element
|
||||
|
||||
type Clickable interface {
|
||||
}
|
||||
|
||||
type Mouseable interface {
|
||||
}
|
||||
|
||||
type Element interface {
|
||||
HandleMouseEvent(s mouse.State) bool
|
||||
|
||||
Draw(image *ebiten.Image) (w, h float64)
|
||||
Draw(*ebiten.Image)
|
||||
SetAnchor(Point)
|
||||
Anchor() Point
|
||||
Size() (w, h int)
|
||||
Children() []Element
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
X, Y float64
|
||||
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 float64
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
package elements
|
||||
@@ -1,6 +1,7 @@
|
||||
package mouse
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
)
|
||||
@@ -11,6 +12,10 @@ func (n NopHandler) HandleMouseEvent(s State) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type EventHandler interface {
|
||||
HandleMouseEvent(s State) bool
|
||||
}
|
||||
|
||||
type State struct {
|
||||
X, Y int
|
||||
LeftDown, RightDown bool
|
||||
@@ -19,13 +24,35 @@ type State struct {
|
||||
Clicked bool
|
||||
}
|
||||
|
||||
//func ClickHandler(e elements.Element) func() bool {
|
||||
// newState := StateBuilder()
|
||||
// return func() bool {
|
||||
// _ = newState()
|
||||
// if
|
||||
// }
|
||||
//}
|
||||
func (s *State) Point() elements.Point {
|
||||
return elements.Point{
|
||||
X: s.X,
|
||||
Y: s.Y,
|
||||
}
|
||||
}
|
||||
|
||||
func Handler(e elements.Element) func() bool {
|
||||
newState := StateBuilder()
|
||||
ms := newState()
|
||||
return func() bool {
|
||||
ms = newState()
|
||||
return propagateMouse(e, ms)
|
||||
}
|
||||
}
|
||||
|
||||
func propagateMouse(e elements.Element, ms State) bool {
|
||||
for _, c := range e.Children() {
|
||||
if propagateMouse(c, ms) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if e, ok := e.(EventHandler); ok {
|
||||
return e.HandleMouseEvent(ms)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func StateBuilder() func() State {
|
||||
prevState := State{}
|
||||
@@ -57,6 +84,8 @@ func StateBuilder() func() State {
|
||||
|
||||
state.LeftChanged = state.LeftDown != prevState.LeftDown
|
||||
state.RightChanged = state.RightDown != prevState.RightDown
|
||||
state.LeftClicked = state.LeftChanged && state.LeftDown
|
||||
state.RightClicked = state.RightChanged && state.RightDown
|
||||
|
||||
prevState = state
|
||||
return state
|
||||
|
||||
@@ -1,25 +1,16 @@
|
||||
package stacks
|
||||
|
||||
import (
|
||||
"git.vezzani.net/ben/games/common/elements/v1"
|
||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||
)
|
||||
import "git.vezzani.net/ben/games/common/elements/v1"
|
||||
|
||||
type Option func(*Stack)
|
||||
type OptFunc func(*Stack)
|
||||
|
||||
func BlockOpt(o blocks.Option) Option {
|
||||
func Core(f elements.OptFunc) OptFunc {
|
||||
return func(s *Stack) {
|
||||
o(&s.Block)
|
||||
f(&s.Core)
|
||||
}
|
||||
}
|
||||
|
||||
func Children(children ...elements.ElementFunc) Option {
|
||||
return func(s *Stack) {
|
||||
s.children = children
|
||||
}
|
||||
}
|
||||
|
||||
func Horizontal() Option {
|
||||
func Horizontal() OptFunc {
|
||||
return func(s *Stack) {
|
||||
s.horizontal = true
|
||||
}
|
||||
|
||||
@@ -2,50 +2,60 @@ package stacks
|
||||
|
||||
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/mouse"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
func New(ops ...Option) elements.ElementFunc {
|
||||
return func(d elements.Bounds) elements.Element {
|
||||
s := Stack{
|
||||
Block: blocks.Block{ContainerBounds: d},
|
||||
}
|
||||
for op := range ops {
|
||||
ops[op](&s)
|
||||
}
|
||||
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 {
|
||||
blocks.Block
|
||||
elements.Core
|
||||
mouse.NopHandler
|
||||
horizontal bool
|
||||
children []elements.ElementFunc
|
||||
}
|
||||
|
||||
func (s *Stack) Draw(image *ebiten.Image) (w, h float64) {
|
||||
s.Block.Draw(image)
|
||||
|
||||
d := s.ContainerBounds
|
||||
d.Width, d.Height = s.Block.Size()
|
||||
if s.horizontal {
|
||||
d.Width /= float64(len(s.children))
|
||||
} else {
|
||||
d.Height /= float64(len(s.children))
|
||||
}
|
||||
|
||||
var offsetX, offsetY float64
|
||||
for i := range s.children {
|
||||
offsetX, offsetY = s.children[i](d).Draw(image)
|
||||
func (s *Stack) Size() (w, h int) {
|
||||
var cw, ch int
|
||||
for _, c := range s.Children() {
|
||||
cw, ch = c.Size()
|
||||
if s.horizontal {
|
||||
d.Min.X += offsetX
|
||||
w += cw
|
||||
if h < ch {
|
||||
h = ch
|
||||
}
|
||||
} else {
|
||||
d.Min.Y += offsetY
|
||||
h += ch
|
||||
if w < cw {
|
||||
w = cw
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.Block.Size()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Stack) Draw(image *ebiten.Image) {
|
||||
for _, c := range s.Children() {
|
||||
c.Draw(image)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
package elements
|
||||
|
||||
//
|
||||
//import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
//
|
||||
// "github.com/hajimehoshi/ebiten/v2"
|
||||
//)
|
||||
//
|
||||
//type Table struct {
|
||||
// mouseHandler
|
||||
// block
|
||||
//
|
||||
// ColumnCount int
|
||||
// RowCount int
|
||||
//
|
||||
// Cells []Bounds
|
||||
//
|
||||
// Style struct {
|
||||
// }
|
||||
//
|
||||
// mouseOverCell int
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleClick(ctx context.Context, s MouseState) error {
|
||||
// address := t.getAddressUnderMouse()
|
||||
// if address < 0 || address >= len(t.Cells) {
|
||||
// return fmt.Errorf("cell address under mouse is out of bounds")
|
||||
// }
|
||||
//
|
||||
// if clickable, ok := t.Cells[address].(Clickable); ok {
|
||||
// return clickable.HandleClick(s)
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseEnter(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseEnter(s)
|
||||
// address := t.getAddressUnderMouse()
|
||||
// if address < 0 || address >= len(t.Cells) {
|
||||
// return fmt.Errorf("cell address under mouse is out of bounds")
|
||||
// }
|
||||
//
|
||||
// if mouseable, ok := t.Cells[address].(Mouseable); ok {
|
||||
// s.X -=
|
||||
// return mouseable.HandleMouseEnter(s)
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseLeave(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseLeave(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseMove(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseMove(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseDown(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseDown(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) HandleMouseUp(ctx context.Context, s MouseState) error {
|
||||
// _ = t.mouseHandler.HandleMouseUp(s)
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) CellSize() (w, h float32) {
|
||||
// w, h = t.
|
||||
// return w / float32(t.ColumnCount), h / float32(t.RowCount)
|
||||
//}
|
||||
//
|
||||
//func (t *Table) CellZero(ctx context.Context, addr int) (x, y float32) {
|
||||
// x, y = GetZero(ctx)
|
||||
// w, h := t.CellSize(ctx)
|
||||
//
|
||||
// col, row := addr%t.ColumnCount, addr/t.ColumnCount
|
||||
// return x + float32(col)*w, y + float32(row) + h
|
||||
//}
|
||||
//
|
||||
//func (t *Table) Draw(screen *ebiten.Image, zx, zy float64) error {
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//func (t *Table) getAddressUnderMouse() int {
|
||||
// return int(t.mouseState.Y)*t.RowCount + int(t.mouseState.X)
|
||||
//}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
var (
|
||||
FontFace font.Face = basicfont.Face7x13
|
||||
FontColor color.Color = color.Black
|
||||
BackgroundColor color.Color = color.White
|
||||
Scale float64 = 1.0
|
||||
)
|
||||
|
||||
@@ -1,36 +1,53 @@
|
||||
package main
|
||||
|
||||
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/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.Size(200, 200)),
|
||||
stacks.BlockOpt(blocks.Name("parent")),
|
||||
stacks.Children(
|
||||
//stacks.Core(blocks.BackgroundColor(color.White)),
|
||||
stacks.Core(elements.Name("parent")),
|
||||
stacks.Core(elements.Children(
|
||||
stacks.New(
|
||||
stacks.BlockOpt(blocks.Name("left")),
|
||||
stacks.Children(
|
||||
blocks.New(blocks.BackgroundColor(colornames.Green)),
|
||||
blocks.New(blocks.BackgroundColor(colornames.Yellow)),
|
||||
stacks.Core(elements.Name("left")),
|
||||
stacks.Core(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.BlockOpt(blocks.Name("right")),
|
||||
stacks.Children(
|
||||
blocks.New(blocks.BackgroundColor(colornames.Blue)),
|
||||
blocks.New(blocks.BackgroundColor(colornames.Red)),
|
||||
),
|
||||
),
|
||||
),
|
||||
stacks.New(
|
||||
stacks.Core(elements.Name("right")),
|
||||
stacks.Core(elements.Children(
|
||||
blocks.New(
|
||||
blocks.Size(100, 100),
|
||||
blocks.BackgroundColor(colornames.Blue),
|
||||
),
|
||||
blocks.New(
|
||||
blocks.Size(100, 100),
|
||||
blocks.BackgroundColor(colornames.Red),
|
||||
),
|
||||
)),
|
||||
),
|
||||
)),
|
||||
)
|
||||
|
||||
func newEditor() *editor {
|
||||
@@ -38,30 +55,28 @@ func newEditor() *editor {
|
||||
}
|
||||
|
||||
type editor struct {
|
||||
root elements.Element
|
||||
bounds elements.Bounds
|
||||
bounds elements.Bounds
|
||||
handleMouse func() bool
|
||||
}
|
||||
|
||||
//var handleClick = mouse.ClickHandler(root)
|
||||
|
||||
func (e *editor) Update() error {
|
||||
sprites.Update()
|
||||
//handleClick()
|
||||
e.handleMouse()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *editor) Draw(screen *ebiten.Image) {
|
||||
e.root.Draw(screen)
|
||||
root.Draw(screen)
|
||||
}
|
||||
|
||||
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||
if e.bounds.Width != float64(outsideWidth) || e.bounds.Height != float64(outsideHeight) {
|
||||
if e.bounds.Width != outsideWidth || e.bounds.Height != outsideHeight {
|
||||
e.bounds = elements.Bounds{
|
||||
Min: elements.Point{},
|
||||
Width: float64(outsideWidth),
|
||||
Height: float64(outsideHeight),
|
||||
Width: outsideWidth,
|
||||
Height: outsideHeight,
|
||||
}
|
||||
e.root = root(e.bounds)
|
||||
e.handleMouse = mouse.Handler(root)
|
||||
}
|
||||
|
||||
return outsideWidth, outsideHeight
|
||||
|
||||
Reference in New Issue
Block a user