-
Notifications
You must be signed in to change notification settings - Fork 7
/
button.go
75 lines (62 loc) · 1.63 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
package widgets
import (
"image"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/tinne26/etxt"
"github.com/yohamta/furex/examples/game/sprites"
"github.com/yohamta/furex/examples/game/text"
"github.com/yohamta/furex/v2"
"github.com/yohamta/ganim8/v2"
)
type Button struct {
Color color.Color
OnClick func()
mouseover bool
pressed bool
}
var (
_ furex.ButtonHandler = (*Button)(nil)
_ furex.Drawer = (*Button)(nil)
_ furex.MouseEnterLeaveHandler = (*Button)(nil)
)
func (b *Button) HandlePress(x, y int, t ebiten.TouchID) {
b.pressed = true
}
func (b *Button) HandleRelease(x, y int, isCancel bool) {
b.pressed = false
if !isCancel {
if b.OnClick != nil {
b.OnClick()
}
}
}
func (b *Button) Draw(screen *ebiten.Image, frame image.Rectangle, view *furex.View) {
x, y := float64(frame.Min.X+frame.Dx()/2), float64(frame.Min.Y+frame.Dy()/2)
sprite := view.Attrs["sprite"]
spritePressed := view.Attrs["sprite_pressed"]
opts := ganim8.DrawOpts(x, y, 0, 1, 1, .5, .5)
if b.mouseover {
opts.ColorM.Scale(1.1, 1.1, 1.1, 1)
}
if b.pressed && spritePressed != "" {
ganim8.DrawSpriteWithOpts(screen, sprites.Get(spritePressed), 0, opts, nil)
} else if sprite != "" {
ganim8.DrawSpriteWithOpts(screen, sprites.Get(sprite), 0, opts, nil)
}
text.R.SetAlign(etxt.YCenter, etxt.XCenter)
text.R.SetTarget(screen)
if b.Color != nil {
text.R.SetColor(b.Color)
} else {
text.R.SetColor(color.White)
}
text.R.Draw(view.Text, int(x), int(y))
}
func (b *Button) HandleMouseEnter(x, y int) bool {
b.mouseover = true
return true
}
func (b *Button) HandleMouseLeave() {
b.mouseover = false
}