/
button.go
49 lines (43 loc) · 1.17 KB
/
button.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ui
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/pwiecz/command_series/lib"
)
type Button struct {
label *Label
rect image.Rectangle
pressedTouchIDs []ebiten.TouchID // store it here to avoid reallocating it for each Update
}
func NewButton(text string, x, y float64, width, height int, font *lib.Font) *Button {
b := &Button{
label: NewLabel(text, x, y, width, height, font),
rect: image.Rect(int(x), int(y), int(x)+width, int(y)+height)}
b.label.SetTextColor(0)
b.label.SetBackgroundColor(15)
return b
}
func (b *Button) SetText(text string) {
b.label.Clear()
b.label.SetText(text, 0)
}
func (b *Button) Draw(dst *ebiten.Image) {
b.label.Draw(dst)
}
func (b *Button) Update() bool {
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
if image.Pt(x, y).In(b.rect) {
return true
}
}
b.pressedTouchIDs = b.pressedTouchIDs[:0]
for _, touchID := range inpututil.AppendJustPressedTouchIDs(b.pressedTouchIDs) {
x, y := ebiten.TouchPosition(touchID)
if image.Pt(x, y).In(b.rect) {
return true
}
}
return false
}