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

View File

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

View File

@@ -1,60 +1,36 @@
package elements package elements
import ( import (
"context"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
type ElementFunc func(Dimensions) Element
type MouseState struct { type MouseState struct {
X, Y int32 X, Y int32
LeftDown, RightDown bool LeftDown, RightDown bool
LeftChanged, RightChanged bool LeftChanged, RightChanged bool
} }
const (
ContainerSizeKey = "container_size"
ZeroKey = "zero"
)
type Clickable interface { type Clickable interface {
Element HandleClick(s MouseState) error
HandleClick(ctx context.Context, s MouseState) error
} }
type Mouseable interface { type Mouseable interface {
Element HandleMouseEnter(s MouseState) error
HandleMouseEnter(ctx context.Context, s MouseState) error HandleMouseLeave(s MouseState) error
HandleMouseLeave(ctx context.Context, s MouseState) error HandleMouseMove(s MouseState) error
HandleMouseMove(ctx context.Context, s MouseState) error HandleMouseDown(s MouseState) error
HandleMouseDown(ctx context.Context, s MouseState) error HandleMouseUp(s MouseState) error
HandleMouseUp(ctx context.Context, s MouseState) error
} }
type Element interface { 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 { type Dimensions struct {
return context.WithValue(ctx, ZeroKey, [2]float32{x, y}) zx, zy float64
} wx, wy float64
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 mouseHandler struct { 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 { type Table struct {
mouseHandler mouseHandler
Block block
ColumnCount int ColumnCount int
RowCount int RowCount int
Cells []Element Cells []Dimensions
Style struct { Style struct {
} }
@@ -70,8 +70,8 @@ func (t *Table) HandleMouseUp(ctx context.Context, s MouseState) error {
return nil return nil
} }
func (t *Table) CellSize(ctx context.Context) (w, h float32) { func (t *Table) CellSize() (w, h float32) {
w, h = GetContainerSize(ctx) w, h = t.
return w / float32(t.ColumnCount), h / float32(t.RowCount) 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 return x + float32(col)*w, y + float32(row) + h
} }
func (t *Table) Draw(ctx context.Context, screen *ebiten.Image) error { func (t *Table) Draw(screen *ebiten.Image, zx, zy float64) error {
return nil
}
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) getAddressUnderMouse() int { func (t *Table) getAddressUnderMouse() int {

View File

@@ -3,7 +3,7 @@ package sprites
import ( import (
"image" "image"
"git.vezzani.net/ben/games/common/window/v1" "git.vezzani.net/ben/games/common/ux/v1"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
) )
@@ -17,7 +17,7 @@ func Update() {
type animation struct { type animation struct {
RowNumber uint8 RowNumber uint8
FrameCount uint8 TickCount uint8
Scale float32 Scale float32
} }
@@ -32,6 +32,11 @@ type Sprite struct {
animations map[string]animation animations map[string]animation
baseAnim *animation baseAnim *animation
currentAnim *animation
animationStartedAt int
animationStopAt int
animateOnce bool
} }
func (s *Sprite) baseAnimation() animation { func (s *Sprite) baseAnimation() animation {
@@ -50,22 +55,17 @@ func (s *Sprite) baseAnimation() animation {
return animation{} return animation{}
} }
func (s *Sprite) getAnimation(id string) animation { func (s *Sprite) getAnimation() animation {
if id == "" { if s.currentAnim != nil {
return s.baseAnimation() return *s.currentAnim
} }
if anim, ok := s.animations[id]; ok {
return anim
}
return s.baseAnimation() return s.baseAnimation()
} }
func (s *Sprite) Image(ops imageOptions) image.Image { func (s *Sprite) Image(ops imageOptions) image.Image {
anim := s.getAnimation(ops.animation) anim := s.getAnimation()
xOffset := updateCount % int(anim.FrameCount) * s.width xOffset := updateCount % int(anim.TickCount) * s.width
yOffset := int(anim.RowNumber) * s.height yOffset := int(anim.RowNumber) * s.height
r := image.Rect(xOffset, yOffset, s.width, s.height).Intersect(s.imgData.Bounds()) r := image.Rect(xOffset, yOffset, s.width, s.height).Intersect(s.imgData.Bounds())
@@ -74,9 +74,12 @@ func (s *Sprite) Image(ops imageOptions) image.Image {
} }
func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) { func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
if s.getAnimation().RowNumber == 0 {
}
ops := imageOptions{ ops := imageOptions{
scaleX: window.Scale, scaleX: ux.Scale,
scaleY: window.Scale, scaleY: ux.Scale,
} }
for _, o := range options { for _, o := range options {
@@ -98,11 +101,28 @@ func (s *Sprite) Draw(screen *ebiten.Image, options ...ImageOption) {
) )
} }
func (s *Sprite) StartAnimation(id string, once bool) {
if a, ok := s.animations[id]; ok {
s.currentAnim = &a
}
if once {
s.animateOnce = true
s.animationStopAt = updateCount + int(s.currentAnim.TickCount)
}
s.animationStartedAt = updateCount
}
func (s *Sprite) StopAnimation() {
s.currentAnim = nil
s.animationStartedAt = 0
s.animationStopAt = 0
s.animateOnce = false
}
type imageOptions struct { type imageOptions struct {
x, y float64 x, y float64
scaleX, scaleY float64 scaleX, scaleY float64
rotateTheta float64 rotateTheta float64
animation string
} }
type ImageOption func(options *imageOptions) type ImageOption func(options *imageOptions)
@@ -114,12 +134,6 @@ func ToScale(x, y float64) ImageOption {
} }
} }
func Animation(name string) ImageOption {
return func(o *imageOptions) {
o.animation = name
}
}
func AtPosition(x, y float64) ImageOption { func AtPosition(x, y float64) ImageOption {
return func(o *imageOptions) { return func(o *imageOptions) {
o.x, o.y = x, y o.x, o.y = x, y

21
tools/spritedit/editor.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import "github.com/hajimehoshi/ebiten/v2"
type editor struct {
}
func (e *editor) Update() error {
//TODO implement me
panic("implement me")
}
func (e *editor) Draw(screen *ebiten.Image) {
//TODO implement me
panic("implement me")
}
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
//TODO implement me
panic("implement me")
}

View File

@@ -3,88 +3,13 @@ package main
import ( import (
"bytes" "bytes"
"github.com/ebitenui/ebitenui"
"github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text/v2"
"golang.org/x/image/colornames"
"golang.org/x/image/font/gofont/goregular"
) )
type Editor struct {
ui *ebitenui.UI
}
func NewEditor() *Editor {
root := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(
image.NewNineSliceColor(colornames.Gainsboro),
))
root.AddChild(
widget.NewButton(
widget.ButtonOpts.TextLabel("Open Sprite Sheet..."),
widget.ButtonOpts.TextFace(DefaultFont()),
widget.ButtonOpts.TextColor(&widget.ButtonTextColor{
Idle: colornames.Gainsboro,
Hover: colornames.Gainsboro,
Pressed: colornames.Gainsboro,
}),
widget.ButtonOpts.Image(&widget.ButtonImage{
Idle: DefaultNineSlice(colornames.Darkslategray),
Hover: DefaultNineSlice(Mix(colornames.Darkslategray, colornames.Mediumseagreen, 0.4)),
Disabled: DefaultNineSlice(Mix(colornames.Darkslategray, colornames.Gainsboro, 0.8)),
Pressed: PressedNineSlice(Mix(colornames.Darkslategray, colornames.Black, 0.4)),
PressedHover: PressedNineSlice(Mix(colornames.Darkslategray, colornames.Black, 0.4)),
}),
widget.ButtonOpts.WidgetOpts(
widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
VerticalPosition: widget.AnchorLayoutPositionCenter,
HorizontalPosition: widget.AnchorLayoutPositionCenter,
}),
widget.WidgetOpts.MinSize(180, 48),
),
),
)
return &Editor{
ui: &ebitenui.UI{Container: root},
}
}
func (e *Editor) Update() error {
e.ui.Update()
return nil
}
func (e *Editor) Draw(screen *ebiten.Image) {
e.ui.Draw(screen)
}
func (e *Editor) Layout(w, h int) (int, int) {
return w, h
}
func DefaultFont() *text.Face {
s, err := text.NewGoTextFaceSource(bytes.NewReader(goregular.TTF))
if err != nil {
panic(err)
}
var f text.Face
f = &text.GoTextFace{
Source: s,
Size: 20,
}
return &f
}
func main() { func main() {
ebiten.SetWindowSize(480, 320) ebiten.SetWindowSize(480, 320)
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled) ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
if err := ebiten.RunGame(NewEditor()); err != nil { if err := ebiten.RunGame(); err != nil {
panic(err) panic(err)
} }
} }