diff options
Diffstat (limited to 'go/ui/button.go')
| -rw-r--r-- | go/ui/button.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/go/ui/button.go b/go/ui/button.go new file mode 100644 index 00000000..93180204 --- /dev/null +++ b/go/ui/button.go @@ -0,0 +1,53 @@ +package ui + +import ( + "image/color" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/hajimehoshi/ebiten/v2/text" +) + +type Button interface { + Widget + Contains(x, y int) bool + Click(x, y int) +} + +type SimpleButton struct { + X, Y int + Width, Height int + img *ebiten.Image + OnClick func(b *SimpleButton) +} + +func NewSimpleButton(x, y, w, h int, onClick func(b *SimpleButton), label string) *SimpleButton { + img := ebiten.NewImage(w, h) + + // b := text.BoundString(f, label) + ebitenutil.DrawRect(img, float64(0), float64(0), float64(w), float64(h), gray) + text.Draw(img, label, Font, 20, 25, color.White) + + return &SimpleButton{x, y, w, h, img, onClick} +} + +func (b *SimpleButton) Draw(screen *ebiten.Image) { + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(b.X), float64(b.Y)) + screen.DrawImage(b.img, op) +} + +func (b *SimpleButton) Click(x, y int) { + b.OnClick(b) +} + +func (b *SimpleButton) Contains(x, y int) bool { + return x >= b.X && x <= b.X+b.Width && y >= b.Y && y <= b.Y+b.Height +} + +func (b *SimpleButton) FindObjectAt(x, y int) interface{} { + if b.Contains(x, y) { + return b + } + return nil +} |
