okay new ux idea

This commit is contained in:
2025-08-27 23:22:39 -04:00
parent 8b1bfc8a01
commit 573396054f
8 changed files with 134 additions and 210 deletions

View File

@@ -1,61 +1,28 @@
package elements
import (
"context"
"image/color"
"git.vezzani.net/ben/games/common/ux/v1"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"golang.org/x/image/font"
)
type Block struct {
Label string
Style struct {
Width float32
Height float32
XAlign XAlign
YAlign YAlign
Offset int
Font *font.Face
BackgroundColor *color.Color
}
}
type BlockOption func(*block)
func (b *Block) backgroundColor() color.Color {
var c *color.Color
if b.Style.BackgroundColor != nil {
c = b.Style.BackgroundColor
} else {
c = &ux.BackgroundColor
}
return *c
}
func (b *Block) size(ctx context.Context) (x, y float32) {
x = b.Style.Width
y = b.Style.Height
if x == 0 || y == 0 {
px, py := GetContainerSize(ctx)
if x == 0 {
x = px
}
if y == 0 {
y = py
func Block(ops ...BlockOption) ElementFunc {
return func(d Dimensions) Element {
b := block{}
for i := range ops {
ops[i](&b)
}
return &b
}
return
}
func (b *Block) Draw(ctx context.Context, image *ebiten.Image) error {
xz, yz := GetZero(ctx)
w, h := b.size(ctx)
vector.StrokeRect(image, xz, yz, w, h, 1, b.backgroundColor(), true)
return nil
type block struct {
width float64
height float64
xAlign xAlign
yAlign yAlign
}
func (b block) Draw(image *ebiten.Image, anchorX, anchorY float64) (w, h float64) {
return b.width, b.height
}

View File

@@ -10,24 +10,24 @@ import (
)
import "git.vezzani.net/ben/games/common/ux/v1"
type XAlign int
type YAlign int
type xAlign int
type yAlign int
const (
AlignCente XAlign = iota
AlignCente xAlign = iota
AlignLeft
AlignRight
)
const (
AlignCenter YAlign = iota
AlignCenter yAlign = iota
AlignTop
AlignBottom
)
type Button struct {
mouseHandler
Block
block
Label string
OnClick func() error
OnRightClick func() error
@@ -44,17 +44,17 @@ func (b *Button) HandleClick(_ MouseState) error {
}
func (b *Button) getFont() font.Face {
if b.Block.Style.Font == nil {
if b.block.block.Font == nil {
return ux.FontFace
}
return *b.Block.Style.Font
return *b.block.block.Font
}
func (b *Button) backgroundColor() color.Color {
var c *color.Color
if b.Block.Style.BackgroundColor != nil {
c = b.Block.Style.BackgroundColor
if b.block.block.BackgroundColor != nil {
c = b.block.block.BackgroundColor
} else {
c = &ux.BackgroundColor
}
@@ -67,8 +67,6 @@ func (b *Button) backgroundColor() color.Color {
}
func (b *Button) Draw(ctx context.Context, image *ebiten.Image) error {
xz, yz := GetZero(ctx)
w, h := b.size(ctx)
vector.StrokeRect(image, xz, yz, w, h, 1, b.backgroundColor(), true)

View File

@@ -1,60 +1,36 @@
package elements
import (
"context"
"github.com/hajimehoshi/ebiten/v2"
)
type ElementFunc func(Dimensions) Element
type MouseState struct {
X, Y int32
LeftDown, RightDown bool
LeftChanged, RightChanged bool
}
const (
ContainerSizeKey = "container_size"
ZeroKey = "zero"
)
type Clickable interface {
Element
HandleClick(ctx context.Context, s MouseState) error
HandleClick(s MouseState) error
}
type Mouseable interface {
Element
HandleMouseEnter(ctx context.Context, s MouseState) error
HandleMouseLeave(ctx context.Context, s MouseState) error
HandleMouseMove(ctx context.Context, s MouseState) error
HandleMouseDown(ctx context.Context, s MouseState) error
HandleMouseUp(ctx context.Context, s MouseState) error
HandleMouseEnter(s MouseState) error
HandleMouseLeave(s MouseState) error
HandleMouseMove(s MouseState) error
HandleMouseDown(s MouseState) error
HandleMouseUp(s MouseState) error
}
type Element interface {
Draw(ctx context.Context, image *ebiten.Image) error
Draw(image *ebiten.Image, anchorX, anchorY float64) (w, h float64)
}
func SetZero(ctx context.Context, x, y float32) context.Context {
return context.WithValue(ctx, ZeroKey, [2]float32{x, y})
}
func SetContainerSize(ctx context.Context, w, h float32) context.Context {
return context.WithValue(ctx, ContainerSizeKey, [2]float32{w, h})
}
func GetZero(ctx context.Context) (x, y float32) {
if v, ok := ctx.Value(ZeroKey).([2]float32); ok {
return v[0], v[1]
}
return 0, 0
}
func GetContainerSize(ctx context.Context) (w, h float32) {
if s, ok := ctx.Value(ContainerSizeKey).([2]float32); ok {
return s[0], s[1]
}
return 0, 0
type Dimensions struct {
zx, zy float64
wx, wy float64
}
type mouseHandler struct {

View File

@@ -0,0 +1,31 @@
package elements
import "github.com/hajimehoshi/ebiten/v2"
type StackOption func(*stack)
func Stack(ops ...StackOption) ElementFunc {
return func(parentDimensions Dimensions) Element {
}
}
type stack struct {
block
horizontal bool
children []Element
}
func (s *stack) Draw(image *ebiten.Image, anchorX, anchorY float64) (w, h float64) {
var offsetX, offsetY float64
originalX, originalY := anchorX, anchorY
for i := range s.children {
offsetX, offsetY = s.children[i].Draw(image, anchorX, anchorY)
if s.horizontal {
anchorX += offsetX
} else {
anchorY += offsetY
}
}
return anchorX - originalX, anchorY - originalY
}

View File

@@ -9,12 +9,12 @@ import (
type Table struct {
mouseHandler
Block
block
ColumnCount int
RowCount int
Cells []Element
Cells []Dimensions
Style struct {
}
@@ -70,8 +70,8 @@ func (t *Table) HandleMouseUp(ctx context.Context, s MouseState) error {
return nil
}
func (t *Table) CellSize(ctx context.Context) (w, h float32) {
w, h = GetContainerSize(ctx)
func (t *Table) CellSize() (w, h float32) {
w, h = t.
return w / float32(t.ColumnCount), h / float32(t.RowCount)
}
@@ -83,16 +83,8 @@ func (t *Table) CellZero(ctx context.Context, addr int) (x, y float32) {
return x + float32(col)*w, y + float32(row) + h
}
func (t *Table) Draw(ctx context.Context, screen *ebiten.Image) error {
}
func (t *Table) contextForCell(ctx context.Context, addr int) context.Context {
zx, zy := t.CellZero(ctx, addr)
w, h := t.CellSize(ctx)
ctx = SetZero(ctx, zx, zy)
ctx = SetContainerSize(ctx, w, h)
return ctx
func (t *Table) Draw(screen *ebiten.Image, zx, zy float64) error {
return nil
}
func (t *Table) getAddressUnderMouse() int {