/
button.go
85 lines (68 loc) · 1.83 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package ui
import (
"image"
"image/color"
"github.com/apotox/go-boom-game/joystick"
mycolors "github.com/apotox/go-boom-game/mycolors"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
)
var (
emptyImage = ebiten.NewImage(3, 3)
// emptySubImage is an internal sub image of emptyImage.
// Use emptySubImage at DrawTriangles instead of emptyImage in order to avoid bleeding edges.
emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
)
type Component interface {
Draw(*ebiten.Image)
Update(*joystick.Input)
}
type Button struct {
X, Y, Width int
Action func()
Text string
Image *ebiten.Image
imagePressed *ebiten.Image
clicked bool
font font.Face
flooding int
_flooding float32
}
var mplusNormalFont font.Face
func NewButton(x, y, width int, text string, font font.Face, image *ebiten.Image, action func()) *Button {
emptyImage.Fill(color.White)
b := &Button{
X: x - width/2,
Y: y - width/2,
Width: width,
Text: text,
Image: image,
font: font,
flooding: 0,
}
b.Action = func() {
action()
}
return b
}
func (b *Button) Update(input *joystick.Input) {
// up and down animation
input.Update()
if b._flooding += 0.1; b._flooding > 1 {
b.flooding = (b.flooding + 1) % 2
b._flooding = 0
}
if ok := input.Tap(); ok {
b.Action()
}
}
func (b *Button) Draw(screen *ebiten.Image) {
if b.Image == nil {
return
}
ebitenutil.DrawRect(screen, float64(b.X), float64(b.Y+b.flooding), float64(b.Width), float64(22), color.White)
ebitenutil.DrawRect(screen, float64(b.X)+2, float64(b.Y+b.flooding+1), float64(b.Width), float64(22), mycolors.PrimaryColor)
text.Draw(screen, b.Text, b.font, b.X+11, b.Y+b.flooding+16, color.White)
}