diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2022-12-27 14:29:39 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:46:38 +0200 |
| commit | d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc (patch) | |
| tree | f87422bb91c2197f88aa8d29092592858764049d /go/ui/button.go | |
| parent | 3c2ee4ee48480a28dc606878503f314ec8862d45 (diff) | |
| download | muhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.tar.gz muhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.zip | |
intermediate commit
Diffstat (limited to 'go/ui/button.go')
| -rw-r--r-- | go/ui/button.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/go/ui/button.go b/go/ui/button.go new file mode 100644 index 00000000..6454ccac --- /dev/null +++ b/go/ui/button.go @@ -0,0 +1,60 @@ +package ui + +import ( + "image/color" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/hajimehoshi/ebiten/v2/text" + + "muhq.space/muhqs-game/go/font" +) + +type Button interface { + Widget + Click(x, y int) +} + +type SimpleButton struct { + WidgetBase + label string + OnClick func(b *SimpleButton) + BG color.Color + FG color.Color +} + +func NewSimpleButton(x, y, w, h int, label string, onClick func(b *SimpleButton)) *SimpleButton { + sb := &SimpleButton{NewWidgetBase(x, y, w, h), label, onClick, gray, color.White} + sb.renderImpl = func() *ebiten.Image { return sb.render() } + return sb +} + +func (b *SimpleButton) render() *ebiten.Image { + img := ebiten.NewImage(b.Width, b.Height) + + ebitenutil.DrawRect(img, float64(0), float64(0), float64(b.Width), float64(b.Height), b.BG) + + bounds := text.BoundString(font.Font24, b.label) + x := (b.Width - bounds.Dx()) / 2 + if x < 0 { + x = 0 + } + + y := b.Height - bounds.Dy()/2 + if y < 0 { + y = 0 + } + text.Draw(img, b.label, font.Font24, x, y, b.FG) + return img +} + +func (b *SimpleButton) FindObjectAt(x, y int) interface{} { + if b.Contains(x, y) { + return b + } + return nil +} + +func (b *SimpleButton) Click(x, y int) { + b.OnClick(b) +} |
