diff options
Diffstat (limited to 'go/ui/widget.go')
| -rw-r--r-- | go/ui/widget.go | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/go/ui/widget.go b/go/ui/widget.go index c8fb0816..d783eb36 100644 --- a/go/ui/widget.go +++ b/go/ui/widget.go @@ -5,6 +5,42 @@ import ( ) type Widget interface { - Draw(screen *ebiten.Image) - FindObjectAt(x, y int) interface{} + Draw(*ebiten.Image) + FindObjectAt(int, int) interface{} +} + +type WidgetBase struct { + X, Y int + Width, Height int + img *ebiten.Image + renderImpl func() *ebiten.Image +} + +func renderDummy() *ebiten.Image { + return nil +} + +func NewWidgetBase(x, y, width, height int) WidgetBase { + return WidgetBase{x, y, width, height, nil, renderDummy} +} + +func (w *WidgetBase) Draw(screen *ebiten.Image) { + if w.img == nil { + w.img = w.renderImpl() + } + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(w.X), float64(w.Y)) + screen.DrawImage(w.img, op) +} + +func (w *WidgetBase) ForceRedraw() { + w.img = nil +} + +func (w *WidgetBase) Contains(x, y int) bool { + return x >= 0 && y >= 0 && x >= w.X && x <= w.X+w.Width && y >= w.Y && y <= w.Y+w.Height +} + +func (w *WidgetBase) FindObjectAt(int, int) interface{} { + return nil } |
