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

62 lines
1.0 KiB
Go

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
}
}
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
}
}
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
}