/
directional_pad.go
149 lines (127 loc) 路 3.57 KB
/
directional_pad.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package pad
import (
"sort"
"github.com/hajimehoshi/ebiten"
)
// DirectionalPad is the directional pad for a game.
type DirectionalPad struct {
baseImg *ebiten.Image
buttons map[Direction]*directionalButton
selectedDirection Direction
op *ebiten.DrawImageOptions
}
// NewDirectionalPad returns a new DirectionalPad.
func NewDirectionalPad(pad, button *ebiten.Image) (*DirectionalPad, error) {
dp := &DirectionalPad{
baseImg: pad,
op: &ebiten.DrawImageOptions{},
}
err := dp.createButtons(button)
if err != nil {
return nil, err
}
return dp, nil
}
func (dp *DirectionalPad) createButtons(img *ebiten.Image) error {
if dp.buttons == nil {
dp.buttons = make(map[Direction]*directionalButton, 4)
}
ds := []Direction{Left, Upper, Right, Lower}
for _, direc := range ds {
b, err := newDirectionalButton(img, direc)
if err != nil {
return err
}
dp.buttons[direc] = b
}
return nil
}
// SetLocation sets the location to draw this directional pad.
func (dp *DirectionalPad) SetLocation(x, y int) {
dp.op.GeoM.Translate(float64(x), float64(y))
wp, _ := dp.baseImg.Size()
halfWp := int(wp / 2)
wb, _ := dp.buttons[Left].Size()
halfWb := int(wb / 2)
outerMargin := int(0.25 * float64(halfWp))
innerMargin := int(0.08 * float64(halfWp))
dp.buttons[Left].SetLocation(x+outerMargin, y+halfWp-halfWb)
dp.buttons[Upper].SetLocation(x+halfWp-halfWb, y+outerMargin)
dp.buttons[Right].SetLocation(x+halfWp+innerMargin, y+halfWp-halfWb)
dp.buttons[Lower].SetLocation(x+halfWp-halfWb, y+halfWp+innerMargin)
}
// Update updates the internal status of this struct.
func (dp *DirectionalPad) Update() error {
dp.updateDirection()
dp.updateButtons()
return nil
}
func (dp *DirectionalPad) updateDirection() {
IDs := ebiten.TouchIDs()
// There are no touches
if len(IDs) == 0 {
dp.selectedDirection = None
return
}
directions := []Direction{}
// Find the newly touched direction key.
sort.Slice(IDs, func(i, j int) bool {
return IDs[i] < IDs[j]
})
for index := range IDs {
for key := range dp.buttons {
if isTouched(IDs[index], dp.buttons[key].GetRectangle()) {
directions = append(directions, key)
}
}
}
dp.defineDirection(directions)
}
func (dp *DirectionalPad) defineDirection(directions []Direction) {
current := None
for index := range directions {
current = getMergedDirection(current, directions[index])
}
dp.selectedDirection = current
}
func (dp *DirectionalPad) updateButtons() {
for key := range dp.buttons {
dp.buttons[key].SelectButton(false)
}
if dp.selectedDirection == None {
return
}
if dp.selectedDirection == UpperLeft {
dp.buttons[Upper].SelectButton(true)
dp.buttons[Left].SelectButton(true)
} else if dp.selectedDirection == UpperRight {
dp.buttons[Upper].SelectButton(true)
dp.buttons[Right].SelectButton(true)
} else if dp.selectedDirection == LowerLeft {
dp.buttons[Lower].SelectButton(true)
dp.buttons[Left].SelectButton(true)
} else if dp.selectedDirection == LowerRight {
dp.buttons[Lower].SelectButton(true)
dp.buttons[Right].SelectButton(true)
} else {
dp.buttons[dp.selectedDirection].SelectButton(true)
}
}
// Draw draws the directional buttons belong this struct.
func (dp *DirectionalPad) Draw(screen *ebiten.Image) error {
err := screen.DrawImage(dp.baseImg, dp.op)
if err != nil {
return err
}
for index := range dp.buttons {
e := dp.buttons[index].Draw(screen)
if err != nil {
return e
}
}
return nil
}
// GetDirection returns the currently selected direction.
func (dp *DirectionalPad) GetDirection() Direction {
return dp.selectedDirection
}