aboutsummaryrefslogtreecommitdiff
path: root/go/ui/button.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/ui/button.go')
-rw-r--r--go/ui/button.go53
1 files changed, 38 insertions, 15 deletions
diff --git a/go/ui/button.go b/go/ui/button.go
index a755c5c0..b6cec672 100644
--- a/go/ui/button.go
+++ b/go/ui/button.go
@@ -10,45 +10,68 @@ import (
"muhq.space/muhqs-game/go/font"
)
-
type Button interface {
Widget
- Clickable
}
type SimpleButton struct {
WidgetBase
- label string
- OnClick func(b *SimpleButton)
- Bg color.Color
- Fg color.Color
- font *text.GoTextFace
+ label string
+ Bg color.Color
+ Fg color.Color
+ font *text.GoTextFace
lineSpacing float64
}
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, font.Font24, 1.5}
- sb.renderImpl = func() *ebiten.Image { return sb.render() }
+ sb := &SimpleButton{NewWidgetBase(x, y, w, h), label, Gray, color.White, font.Font24, 1.5}
+ sb.EventHandlersMap["click"] = func(int, int) { onClick(sb) }
+ sb.renderImpl = func() *ebiten.Image {
+ if sb.img == nil {
+ sb.img = sb.renderRect()
+ }
+ return sb.img
+ }
return sb
}
-func (b *SimpleButton) render() *ebiten.Image {
- img := ebiten.NewImage(b.Width, b.Height)
+func NewRoundSimpleButton(x, y, r int, label string, onClick func(b *SimpleButton)) *SimpleButton {
+ sb := &SimpleButton{NewWidgetBase(x, y, r, r), label, Gray, color.White, font.Font24, 1.5}
+ sb.EventHandlersMap["click"] = func(int, int) { onClick(sb) }
+ sb.renderImpl = func() *ebiten.Image {
+ if sb.img == nil {
+ sb.img = sb.renderCircle()
+ }
+ return sb.img
+ }
+ return sb
+}
- vector.DrawFilledRect(img, float32(0), float32(0), float32(b.Width), float32(b.Height), b.Bg, false)
+func (b *SimpleButton) drawLabel(img *ebiten.Image) {
- w, h := text.Measure(b.label, b.font, b.font.Size * b.lineSpacing)
+ w, h := text.Measure(b.label, b.font, b.font.Size*b.lineSpacing)
x := float64(b.Width)/2 - w/2
y := float64(b.Height)/2 - h/2
op := &text.DrawOptions{}
op.GeoM.Translate(x, y)
op.ColorScale.ScaleWithColor(b.Fg)
text.Draw(img, b.label, font.Font24, op)
+}
+
+func (b *SimpleButton) renderRect() *ebiten.Image {
+ img := ebiten.NewImage(b.Width, b.Height)
+
+ vector.DrawFilledRect(img, float32(0), float32(0), float32(b.Width), float32(b.Height), b.Bg, false)
+ b.drawLabel(img)
return img
}
-func (b *SimpleButton) Click(x, y int) {
- b.OnClick(b)
+func (b *SimpleButton) renderCircle() *ebiten.Image {
+ img := ebiten.NewImage(b.Width, b.Height)
+
+ vector.DrawFilledCircle(img, float32(b.Width)/2, float32(b.Height)/2, float32(b.Width/2), b.Bg, false)
+ b.drawLabel(img)
+ return img
}
func (b *SimpleButton) UpdateLabel(label string) {