aboutsummaryrefslogtreecommitdiff
path: root/go/ui/widget.go
blob: 4ee0caac0a98d3abfbe0e72af77b346725f12874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ui

import (
	"github.com/hajimehoshi/ebiten/v2"
)

type Widget interface {
	Draw(*ebiten.Image)
	FindObjectAt(int, int) interface{}
	Contains(int, int) bool
}

type WidgetBase struct {
	X, Y          int
	Width, Height int
	img           *ebiten.Image
	renderImpl    func() *ebiten.Image
}

func NewWidgetBase(x, y, width, height int) WidgetBase {
	return WidgetBase{X: x, Y: y, Width: width, Height: height}
}

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
}