diff options
Diffstat (limited to 'go/ui/button.go')
| -rw-r--r-- | go/ui/button.go | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/go/ui/button.go b/go/ui/button.go index 93180204..97dbad22 100644 --- a/go/ui/button.go +++ b/go/ui/button.go @@ -6,43 +6,36 @@ import ( "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 - 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) + WidgetBase + label string + OnClick func(b *SimpleButton) + BG color.Color + FG color.Color } -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 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) 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) render() *ebiten.Image { + img := ebiten.NewImage(b.Width, b.Height) -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 + // b := text.BoundString(f, label) + ebitenutil.DrawRect(img, float64(0), float64(0), float64(b.Width), float64(b.Height), b.BG) + text.Draw(img, b.label, font.Font, 20, 25, b.FG) + return img } func (b *SimpleButton) FindObjectAt(x, y int) interface{} { @@ -51,3 +44,7 @@ func (b *SimpleButton) FindObjectAt(x, y int) interface{} { } return nil } + +func (b *SimpleButton) Click(x, y int) { + b.OnClick(b) +} |
