Files
games/common/elements/v1/element.go

64 lines
964 B
Go

package elements
import (
"github.com/hajimehoshi/ebiten/v2"
)
type Element interface {
Draw(*ebiten.Image)
SetAnchor(Point)
Anchor() Point
Size() (w, h int)
Children() []Element
}
type Point struct {
X, Y int
}
func (p Point) Delta(p2 Point) Point {
return Point{
X: p2.X - p.X,
Y: p2.Y - p.Y,
}
}
func (p Point) Add(p2 Point) Point {
return Point{
X: p.X + p2.X,
Y: p.Y + p2.Y,
}
}
type Bounds struct {
Min Point
Width, Height int
}
func (b *Bounds) Contains(p Point) bool {
return p.X >= b.Min.X && p.X <= b.Min.X+b.Width && p.Y >= b.Min.Y && p.Y <= b.Min.Y+b.Height
}
type Core struct {
anchor Point
children []Element
name string // For debugging
}
func (c *Core) Anchor() Point {
return c.anchor
}
func (c *Core) SetAnchor(a Point) {
d := c.Anchor().Delta(a)
for _, ch := range c.Children() {
ch.SetAnchor(ch.Anchor().Add(d))
}
c.anchor = a
}
func (c *Core) Children() []Element {
return c.children
}