59 lines
1.0 KiB
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.InitFunc {
|
|
return func(d elements.Bounds) elements.Element {
|
|
b := Block{
|
|
ContainerBounds: d,
|
|
}
|
|
for i := range ops {
|
|
ops[i](&b)
|
|
}
|
|
return &b
|
|
}
|
|
}
|
|
|
|
type Block struct {
|
|
ContainerBounds elements.Bounds
|
|
backgroundColor *color.Color
|
|
mouse.NopHandler
|
|
width float64
|
|
height float64
|
|
name string
|
|
}
|
|
|
|
func (b *Block) Size() (w, h float64) {
|
|
w, h = b.ContainerBounds.Width, b.ContainerBounds.Height
|
|
if b.width != 0 {
|
|
w = b.width
|
|
}
|
|
if b.height != 0 {
|
|
h = b.height
|
|
}
|
|
return
|
|
}
|
|
|
|
func (b *Block) Draw(image *ebiten.Image) {
|
|
w, h := b.Size()
|
|
if b.backgroundColor != nil {
|
|
vector.DrawFilledRect(
|
|
image,
|
|
float32(b.ContainerBounds.Min.X),
|
|
float32(b.ContainerBounds.Min.Y),
|
|
float32(w),
|
|
float32(h),
|
|
*b.backgroundColor,
|
|
true,
|
|
)
|
|
}
|
|
return
|
|
}
|