Compare commits
1 Commits
element-re
...
0406dfd337
| Author | SHA1 | Date | |
|---|---|---|---|
| 0406dfd337 |
@@ -3,49 +3,53 @@ package blocks
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
"git.vezzani.net/ben/games/common/geo"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
"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) *Block {
|
func New(ops ...Option) elements.ElementFunc {
|
||||||
b := &Block{}
|
return func(d elements.Bounds) elements.Element {
|
||||||
for i := range ops {
|
b := Block{
|
||||||
ops[i](b)
|
ContainerBounds: d,
|
||||||
|
}
|
||||||
|
for i := range ops {
|
||||||
|
ops[i](&b)
|
||||||
|
}
|
||||||
|
return &b
|
||||||
}
|
}
|
||||||
return b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
core.Element
|
ContainerBounds elements.Bounds
|
||||||
|
|
||||||
backgroundColor *color.Color
|
backgroundColor *color.Color
|
||||||
width, height int
|
mouse.NopHandler
|
||||||
name string
|
width float64
|
||||||
|
height float64
|
||||||
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) Bounds() geo.Bounds {
|
func (b *Block) Size() (w, h float64) {
|
||||||
return geo.Bounds{
|
w, h = b.ContainerBounds.Width, b.ContainerBounds.Height
|
||||||
Min: b.Anchor(),
|
if b.width != 0 {
|
||||||
Width: b.width,
|
w = b.width
|
||||||
Height: b.height,
|
|
||||||
}
|
}
|
||||||
|
if b.height != 0 {
|
||||||
|
h = b.height
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) Size() (w, h int) {
|
func (b *Block) Draw(image *ebiten.Image) (w, h float64) {
|
||||||
return b.width, b.height
|
w, h = b.Size()
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) Draw(image *ebiten.Image) {
|
|
||||||
bnd := b.Bounds()
|
|
||||||
if b.backgroundColor != nil {
|
if b.backgroundColor != nil {
|
||||||
vector.DrawFilledRect(
|
vector.DrawFilledRect(
|
||||||
image,
|
image,
|
||||||
float32(b.Anchor().X),
|
float32(b.ContainerBounds.Min.X),
|
||||||
float32(b.Anchor().Y),
|
float32(b.ContainerBounds.Min.Y),
|
||||||
float32(bnd.Width),
|
float32(w),
|
||||||
float32(bnd.Height),
|
float32(h),
|
||||||
*b.backgroundColor,
|
*b.backgroundColor,
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,34 +1,22 @@
|
|||||||
package blocks
|
package blocks
|
||||||
|
|
||||||
import (
|
import "image/color"
|
||||||
"image/color"
|
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
type Option func(*Block)
|
||||||
)
|
|
||||||
|
|
||||||
type OptFunc func(*Block)
|
func Size(w, h float64) Option {
|
||||||
|
|
||||||
func Core(fs ...core.OptFunc) OptFunc {
|
|
||||||
return func(b *Block) {
|
|
||||||
for _, f := range fs {
|
|
||||||
f(&b.Element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BackgroundColor(c color.Color) OptFunc {
|
func BackgroundColor(c color.Color) Option {
|
||||||
return func(b *Block) {
|
return func(b *Block) {
|
||||||
b.backgroundColor = &c
|
b.backgroundColor = &c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Name(n string) OptFunc {
|
func Name(n string) Option {
|
||||||
return func(b *Block) {
|
return func(b *Block) {
|
||||||
b.name = n
|
b.name = n
|
||||||
}
|
}
|
||||||
|
|||||||
70
common/elements/v1/button.go
Normal file
70
common/elements/v1/button.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
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
|
||||||
|
//}
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
package buttons
|
|
||||||
|
|
||||||
import (
|
|
||||||
"image/color"
|
|
||||||
|
|
||||||
"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) *Button {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package buttons
|
|
||||||
|
|
||||||
import (
|
|
||||||
"image/color"
|
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
|
||||||
)
|
|
||||||
|
|
||||||
type OptFunc func(button *Button)
|
|
||||||
|
|
||||||
func Core(fs ...core.OptFunc) OptFunc {
|
|
||||||
return func(b *Button) {
|
|
||||||
for _, f := range fs {
|
|
||||||
f(&b.Element)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import "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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
|
||||||
"git.vezzani.net/ben/games/common/geo"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Element struct {
|
|
||||||
mouse.NopHandler
|
|
||||||
|
|
||||||
anchor geo.Point
|
|
||||||
children []elements.Element
|
|
||||||
|
|
||||||
name string // For debugging
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Element) Anchor() geo.Point {
|
|
||||||
return c.anchor
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Element) SetAnchor(a geo.Point) {
|
|
||||||
d := c.Anchor().Delta(a)
|
|
||||||
for _, ch := range c.Children() {
|
|
||||||
ch.SetAnchor(ch.Anchor().Add(d))
|
|
||||||
}
|
|
||||||
c.anchor = a
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Element) Children() []elements.Element {
|
|
||||||
return c.children
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,29 @@
|
|||||||
package elements
|
package elements
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/geo"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Element interface {
|
type ElementFunc func(Bounds) Element
|
||||||
Draw(*ebiten.Image)
|
|
||||||
SetAnchor(geo.Point)
|
type Clickable interface {
|
||||||
Anchor() geo.Point
|
}
|
||||||
Size() (w, h int)
|
|
||||||
Children() []Element
|
type Mouseable interface {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Element interface {
|
||||||
|
HandleMouseEvent(s mouse.State) bool
|
||||||
|
|
||||||
|
Draw(image *ebiten.Image) (w, h float64)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Point struct {
|
||||||
|
X, Y float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Bounds struct {
|
||||||
|
Min Point
|
||||||
|
Width, Height float64
|
||||||
}
|
}
|
||||||
|
|||||||
1
common/elements/v1/menu.go
Normal file
1
common/elements/v1/menu.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package elements
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package mouse
|
package mouse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
|
||||||
"git.vezzani.net/ben/games/common/geo"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
)
|
)
|
||||||
@@ -13,10 +11,6 @@ func (n NopHandler) HandleMouseEvent(s State) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventHandler interface {
|
|
||||||
HandleMouseEvent(s State) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
X, Y int
|
X, Y int
|
||||||
LeftDown, RightDown bool
|
LeftDown, RightDown bool
|
||||||
@@ -25,35 +19,13 @@ type State struct {
|
|||||||
Clicked bool
|
Clicked bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) Point() geo.Point {
|
//func ClickHandler(e elements.Element) func() bool {
|
||||||
return geo.Point{
|
// newState := StateBuilder()
|
||||||
X: s.X,
|
// return func() bool {
|
||||||
Y: s.Y,
|
// _ = newState()
|
||||||
}
|
// if
|
||||||
}
|
// }
|
||||||
|
//}
|
||||||
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 {
|
func StateBuilder() func() State {
|
||||||
prevState := State{}
|
prevState := State{}
|
||||||
@@ -85,8 +57,6 @@ func StateBuilder() func() State {
|
|||||||
|
|
||||||
state.LeftChanged = state.LeftDown != prevState.LeftDown
|
state.LeftChanged = state.LeftDown != prevState.LeftDown
|
||||||
state.RightChanged = state.RightDown != prevState.RightDown
|
state.RightChanged = state.RightDown != prevState.RightDown
|
||||||
state.LeftClicked = state.LeftChanged && state.LeftDown
|
|
||||||
state.RightClicked = state.RightChanged && state.RightDown
|
|
||||||
|
|
||||||
prevState = state
|
prevState = state
|
||||||
return state
|
return state
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OptFunc func(*Stack)
|
type Option func(*Stack)
|
||||||
|
|
||||||
func Core(fs ...core.OptFunc) OptFunc {
|
func BlockOpt(o blocks.Option) Option {
|
||||||
return func(s *Stack) {
|
return func(s *Stack) {
|
||||||
for _, f := range fs {
|
o(&s.Block)
|
||||||
f(&s.Element)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Horizontal() OptFunc {
|
func Children(children ...elements.ElementFunc) Option {
|
||||||
|
return func(s *Stack) {
|
||||||
|
s.children = children
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Horizontal() Option {
|
||||||
return func(s *Stack) {
|
return func(s *Stack) {
|
||||||
s.horizontal = true
|
s.horizontal = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,61 +1,51 @@
|
|||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
"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/elements/v1/mouse"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) *Stack {
|
func New(ops ...Option) elements.ElementFunc {
|
||||||
s := Stack{}
|
return func(d elements.Bounds) elements.Element {
|
||||||
for op := range ops {
|
s := Stack{
|
||||||
ops[op](&s)
|
Block: blocks.Block{ContainerBounds: d},
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
for op := range ops {
|
||||||
|
ops[op](&s)
|
||||||
|
}
|
||||||
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
core.Element
|
blocks.Block
|
||||||
mouse.NopHandler
|
mouse.NopHandler
|
||||||
horizontal bool
|
horizontal bool
|
||||||
|
children []elements.ElementFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Size() (w, h int) {
|
func (s *Stack) Draw(image *ebiten.Image) (w, h float64) {
|
||||||
var cw, ch int
|
s.Block.Draw(image)
|
||||||
for _, c := range s.Children() {
|
|
||||||
cw, ch = c.Size()
|
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)
|
||||||
if s.horizontal {
|
if s.horizontal {
|
||||||
w += cw
|
d.Min.X += offsetX
|
||||||
if h < ch {
|
|
||||||
h = ch
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
h += ch
|
d.Min.Y += offsetY
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
93
common/elements/v1/table.go
Normal file
93
common/elements/v1/table.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
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)
|
||||||
|
//}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package geo
|
|
||||||
|
|
||||||
type Bounds struct {
|
|
||||||
Min Point
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package geo
|
|
||||||
|
|
||||||
type Point struct {
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
FontFace font.Face = basicfont.Face7x13
|
FontFace font.Face = basicfont.Face7x13
|
||||||
FontColor color.Color = color.Black
|
|
||||||
BackgroundColor color.Color = color.White
|
BackgroundColor color.Color = color.White
|
||||||
Scale float64 = 1.0
|
Scale float64 = 1.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,229 +0,0 @@
|
|||||||
package game
|
|
||||||
|
|
||||||
import (
|
|
||||||
"image/color"
|
|
||||||
"math/rand"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/colors"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/text"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
|
||||||
"golang.org/x/image/font/basicfont"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mouseButton int
|
|
||||||
|
|
||||||
const (
|
|
||||||
cellSize = 32
|
|
||||||
borderThickness = 1
|
|
||||||
|
|
||||||
sideMargin = cellSize
|
|
||||||
topMargin = cellSize * 2
|
|
||||||
bottomMargin = cellSize
|
|
||||||
|
|
||||||
left mouseButton = iota
|
|
||||||
right
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
backgroundColor = colors.FromInt(0xffffff)
|
|
||||||
borderColor = colors.FromInt(0x999999)
|
|
||||||
revealedColor = colors.FromInt(0xdddddd)
|
|
||||||
downColor = colors.FromInt(0xaaaaaa)
|
|
||||||
|
|
||||||
dangerColors = [8]color.Color{
|
|
||||||
colors.FromInt(0x000000),
|
|
||||||
colors.FromInt(0x440000),
|
|
||||||
colors.FromInt(0x880000),
|
|
||||||
colors.FromInt(0xaa0000),
|
|
||||||
colors.FromInt(0xcc0000),
|
|
||||||
colors.FromInt(0xdd0000),
|
|
||||||
colors.FromInt(0xee0000),
|
|
||||||
colors.FromInt(0xff0000),
|
|
||||||
}
|
|
||||||
|
|
||||||
cols, rows int32
|
|
||||||
|
|
||||||
board []square
|
|
||||||
|
|
||||||
leftDown, rightDown bool
|
|
||||||
leftClicked, rightClicked bool
|
|
||||||
|
|
||||||
mouseX, mouseY int
|
|
||||||
|
|
||||||
over bool
|
|
||||||
)
|
|
||||||
|
|
||||||
type square struct {
|
|
||||||
mined bool
|
|
||||||
revealed bool
|
|
||||||
danger uint8
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(colCount, rowCount, mineCount int32) *Core {
|
|
||||||
cols = colCount
|
|
||||||
rows = rowCount
|
|
||||||
board = make([]square, cols*rows)
|
|
||||||
for range mineCount {
|
|
||||||
addr := rand.Int31n(cols * rows)
|
|
||||||
for board[addr].mined {
|
|
||||||
addr = rand.Int31n(cols * rows)
|
|
||||||
}
|
|
||||||
board[addr].mined = true
|
|
||||||
}
|
|
||||||
|
|
||||||
initDanger()
|
|
||||||
|
|
||||||
return &Core{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Core struct{}
|
|
||||||
|
|
||||||
func (c Core) Update() error {
|
|
||||||
mouseX, mouseY = ebiten.CursorPosition()
|
|
||||||
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
|
||||||
leftDown = true
|
|
||||||
}
|
|
||||||
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
|
|
||||||
rightDown = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
|
|
||||||
leftDown = false
|
|
||||||
if !rightDown {
|
|
||||||
handleClick(left)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
|
|
||||||
rightDown = false
|
|
||||||
if !leftDown {
|
|
||||||
handleClick(right)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Core) Draw(screen *ebiten.Image) {
|
|
||||||
maxX, maxY := c.Layout(0, 0)
|
|
||||||
vector.DrawFilledRect(screen, 0, 0, float32(maxX), float32(maxY), backgroundColor, false)
|
|
||||||
|
|
||||||
drawGrid(screen)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Core) Layout(_, _ int) (screenWidth, screenHeight int) {
|
|
||||||
return int(cellSize*cols + sideMargin*2), int(cellSize*rows + topMargin + bottomMargin)
|
|
||||||
}
|
|
||||||
|
|
||||||
func drawGrid(screen *ebiten.Image) {
|
|
||||||
vector.StrokeRect(screen, sideMargin, topMargin, float32(cellSize*cols), float32(cellSize*rows), borderThickness, borderColor, false)
|
|
||||||
|
|
||||||
mouseSquare := int32(-1)
|
|
||||||
|
|
||||||
if leftDown || rightDown {
|
|
||||||
mouseSquare = mouseOverSquare()
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range rows * cols {
|
|
||||||
clr := backgroundColor
|
|
||||||
if board[i].revealed {
|
|
||||||
clr = revealedColor
|
|
||||||
} else {
|
|
||||||
if mouseSquare == i {
|
|
||||||
clr = downColor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
x, y, w, h := getCellSquare(i)
|
|
||||||
vector.DrawFilledRect(screen, x, y, w, h, clr, false)
|
|
||||||
vector.StrokeRect(screen, x, y, w, h, borderThickness, borderColor, false)
|
|
||||||
if board[i].revealed && board[i].danger > 0 {
|
|
||||||
txt := strconv.Itoa(int(board[i].danger))
|
|
||||||
|
|
||||||
text.Draw(screen, txt, basicfont.Face7x13, int(x)+12, int(y)+20, dangerColors[board[i].danger])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func mouseOverSquare() int32 {
|
|
||||||
if int32(mouseX) < sideMargin ||
|
|
||||||
int32(mouseY) < topMargin ||
|
|
||||||
int32(mouseX) > sideMargin+cellSize*cols ||
|
|
||||||
int32(mouseX) > topMargin+cellSize*rows {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
baseMouseX := (int32(mouseX) - sideMargin) / cellSize
|
|
||||||
baseMouseY := (int32(mouseY) - topMargin) / cellSize
|
|
||||||
|
|
||||||
return baseMouseY*rows + baseMouseX
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCellSquare(addr int32) (float32, float32, float32, float32) {
|
|
||||||
c, r := float32(addr%cols), float32(addr/rows)
|
|
||||||
return sideMargin + c*cellSize, topMargin + r*cellSize, cellSize, cellSize
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleClick(side mouseButton) {
|
|
||||||
sq := mouseOverSquare()
|
|
||||||
if sq == -1 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if side == left {
|
|
||||||
if board[sq].mined {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
reveal(sq)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func reveal(addr int32) {
|
|
||||||
if board[addr].revealed {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
board[addr].revealed = true
|
|
||||||
for _, nAddr := range getNeighbors(addr) {
|
|
||||||
if board[nAddr].revealed || board[nAddr].mined {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
reveal(nAddr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getNeighbors(addr int32) []int32 {
|
|
||||||
ns := make([]int32, 0)
|
|
||||||
sides := make([]int32, 0)
|
|
||||||
c, r := addr%cols, addr/rows
|
|
||||||
if c > 0 {
|
|
||||||
sides = append(sides, addr-1)
|
|
||||||
}
|
|
||||||
if c < cols-1 {
|
|
||||||
sides = append(sides, addr+1)
|
|
||||||
}
|
|
||||||
if r > 0 {
|
|
||||||
ns = append(ns, addr-cols)
|
|
||||||
for _, s := range sides {
|
|
||||||
ns = append(ns, s-cols)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if r < rows-1 {
|
|
||||||
ns = append(ns, addr+cols)
|
|
||||||
for _, s := range sides {
|
|
||||||
ns = append(ns, s+cols)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return append(ns, sides...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func initDanger() {
|
|
||||||
for i := range board {
|
|
||||||
for _, n := range getNeighbors(int32(i)) {
|
|
||||||
if board[n].mined {
|
|
||||||
board[i].danger++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
package v2
|
|
||||||
@@ -1,55 +1,36 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
"image/color"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/buttons"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/core"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/stacks"
|
|
||||||
"git.vezzani.net/ben/games/common/geo"
|
|
||||||
"git.vezzani.net/ben/games/common/sprites/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/stacks"
|
||||||
|
"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(core.Name("parent")),
|
stacks.BlockOpt(blocks.BackgroundColor(color.White)),
|
||||||
stacks.Core(core.Children(
|
stacks.BlockOpt(blocks.Size(200, 200)),
|
||||||
|
stacks.BlockOpt(blocks.Name("parent")),
|
||||||
|
stacks.Children(
|
||||||
stacks.New(
|
stacks.New(
|
||||||
stacks.Core(
|
stacks.BlockOpt(blocks.Name("left")),
|
||||||
core.Name("left"),
|
stacks.Children(
|
||||||
core.Children(
|
blocks.New(blocks.BackgroundColor(colornames.Green)),
|
||||||
buttons.New(
|
blocks.New(blocks.BackgroundColor(colornames.Yellow)),
|
||||||
buttons.BlockOpt(blocks.Size(100, 100)),
|
)),
|
||||||
buttons.Label("hello"),
|
stacks.New(
|
||||||
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
stacks.BlockOpt(blocks.Name("right")),
|
||||||
buttons.OnClick(func(ms mouse.State) {
|
stacks.Children(
|
||||||
println("green")
|
blocks.New(blocks.BackgroundColor(colornames.Blue)),
|
||||||
}),
|
blocks.New(blocks.BackgroundColor(colornames.Red)),
|
||||||
),
|
|
||||||
blocks.New(
|
|
||||||
blocks.Size(100, 100),
|
|
||||||
blocks.BackgroundColor(colornames.Yellow),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
stacks.New(
|
),
|
||||||
stacks.Core(core.Name("right")),
|
|
||||||
stacks.Core(core.Children(
|
|
||||||
blocks.New(
|
|
||||||
blocks.Size(100, 100),
|
|
||||||
blocks.BackgroundColor(colornames.Blue),
|
|
||||||
),
|
|
||||||
blocks.New(
|
|
||||||
blocks.Size(100, 100),
|
|
||||||
blocks.BackgroundColor(colornames.Red),
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
),
|
|
||||||
)),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func newEditor() *editor {
|
func newEditor() *editor {
|
||||||
@@ -57,28 +38,30 @@ func newEditor() *editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type editor struct {
|
type editor struct {
|
||||||
bounds geo.Bounds
|
root elements.Element
|
||||||
handleMouse func() bool
|
bounds elements.Bounds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//var handleClick = mouse.ClickHandler(root)
|
||||||
|
|
||||||
func (e *editor) Update() error {
|
func (e *editor) Update() error {
|
||||||
sprites.Update()
|
sprites.Update()
|
||||||
e.handleMouse()
|
//handleClick()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
if e.bounds.Width != outsideWidth || e.bounds.Height != outsideHeight {
|
if e.bounds.Width != float64(outsideWidth) || e.bounds.Height != float64(outsideHeight) {
|
||||||
e.bounds = geo.Bounds{
|
e.bounds = elements.Bounds{
|
||||||
Min: geo.Point{},
|
Min: elements.Point{},
|
||||||
Width: outsideWidth,
|
Width: float64(outsideWidth),
|
||||||
Height: outsideHeight,
|
Height: float64(outsideHeight),
|
||||||
}
|
}
|
||||||
e.handleMouse = mouse.Handler(root)
|
e.root = root(e.bounds)
|
||||||
}
|
}
|
||||||
|
|
||||||
return outsideWidth, outsideHeight
|
return outsideWidth, outsideHeight
|
||||||
|
|||||||
Reference in New Issue
Block a user