diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-01-20 03:18:06 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:44 +0100 |
| commit | c4cb9637f5c85d03aa162968f0dfec3c193fc9dc (patch) | |
| tree | 3ad32e5dfd7c4ae72f66b86e77edef7b700a8b7a /go/ui/button.go | |
| parent | 9df5cc7b55d8ac9ecd775a14a14f80a6c36c4d74 (diff) | |
| download | muhqs-game-c4cb9637f5c85d03aa162968f0dfec3c193fc9dc.tar.gz muhqs-game-c4cb9637f5c85d03aa162968f0dfec3c193fc9dc.zip | |
intermediate commit
Implement actions and multiple ui widgets
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 +} |
