Files
games/common/elements/v1/button.go
2025-08-20 14:12:56 -04:00

77 lines
1.3 KiB
Go

package elements
import (
"context"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
"golang.org/x/image/font"
)
import "git.vezzani.net/ben/games/common/ux/v1"
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.Style.Font == nil {
return ux.FontFace
}
return *b.Block.Style.Font
}
func (b *Button) backgroundColor() color.Color {
var c *color.Color
if b.Block.Style.BackgroundColor != nil {
c = b.Block.Style.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 {
xz, yz := GetZero(ctx)
w, h := b.size(ctx)
vector.StrokeRect(image, xz, yz, w, h, 1, b.backgroundColor(), true)
return nil
}