okay giant ux refactor to hopefully make it cleaner

This commit is contained in:
2025-08-31 13:29:52 -04:00
parent 430259475b
commit dda0a83202
12 changed files with 214 additions and 198 deletions

View File

@@ -1,15 +1,24 @@
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 ...Option) elements.InitFunc {
return func(bounds elements.Bounds) elements.Element {
func New(ops ...OptFunc) elements.InitFunc {
return func() elements.Element {
b := Button{
Block: blocks.Block{ContainerBounds: bounds},
Block: blocks.Block{},
font: ux.FontFace,
color: ux.FontColor,
}
for op := range ops {
ops[op](&b)
@@ -22,18 +31,43 @@ func New(ops ...Option) elements.InitFunc {
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 {
if !b.Block.ContainerBounds.Contains(elements.Point{
X: float64(ms.X),
Y: float64(ms.Y),
}) {
bnd := b.Bounds()
if !bnd.Contains(ms.Point()) {
return false
}
switch {
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
b.onClick(ms)