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

74 lines
1.3 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 ...OptFunc) elements.InitFunc {
return func() elements.Element {
b := Block{}
for i := range ops {
ops[i](&b)
}
return &b
}
}
type Block struct {
anchor elements.Point
backgroundColor *color.Color
mouse.NopHandler
width, height int
name string
}
func (b *Block) SetAnchor(a elements.Point) {
d := b.anchor.Delta(a)
for i := range b.Children() {
b.Children()[i].SetAnchor(b.Children()[i].Anchor().Add(d))
}
b.anchor = a
}
func (b *Block) Bounds() elements.Bounds {
return elements.Bounds{
Min: b.anchor,
Width: b.width,
Height: b.height,
}
}
func (b *Block) Anchor() elements.Point {
return b.anchor
}
func (b *Block) Size() (w, h int) {
return b.width, b.height
}
func (b *Block) Draw(image *ebiten.Image) {
bnd := b.Bounds()
if b.backgroundColor != nil {
vector.DrawFilledRect(
image,
float32(b.anchor.X),
float32(b.anchor.Y),
float32(bnd.Width),
float32(bnd.Height),
*b.backgroundColor,
true,
)
}
return
}
func (b *Block) Children() []elements.Element {
return nil
}