Files
games/common/elements/v1/button.go
2025-08-28 08:33:31 -04:00

75 lines
1.2 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.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
}