Files
games/common/elements/v1/blocks/block.go

59 lines
1.0 KiB
Go

package blocks
import (
"image/color"
"git.vezzani.net/ben/games/common/elements/v1"
"git.vezzani.net/ben/games/common/elements/v1/mouse"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func New(ops ...Option) elements.ElementFunc {
return func(d elements.Dimensions) elements.Element {
b := Block{
InheritedDimensions: d,
}
for i := range ops {
ops[i](&b)
}
return &b
}
}
type Block struct {
InheritedDimensions elements.Dimensions
backgroundColor *color.Color
mouse.NopHandler
width float64
height float64
name string
}
func (b *Block) Size() (w, h float64) {
w, h = b.InheritedDimensions.WX, b.InheritedDimensions.WY
if b.width != 0 {
w = b.width
}
if b.height != 0 {
h = b.height
}
return
}
func (b *Block) Draw(image *ebiten.Image) (w, h float64) {
w, h = b.Size()
if b.backgroundColor != nil {
vector.DrawFilledRect(
image,
float32(b.InheritedDimensions.ZX),
float32(b.InheritedDimensions.ZY),
float32(w),
float32(h),
*b.backgroundColor,
true,
)
}
return
}