-
Notifications
You must be signed in to change notification settings - Fork 11
/
touch.go
274 lines (258 loc) · 6.65 KB
/
touch.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package input
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/image"
m "github.com/divVerent/aaaaxy/internal/math"
)
var (
touch = flag.Bool("touch", true, "enable touch input")
touchForce = flag.Bool("touch_force", flag.SystemDefault(map[string]bool{
"android/*": true,
"ios/*": true,
"js/*": true,
"*/*": false,
}), "always show touch controls")
touchRectLeft = flag.Text("touch_rect_left", m.Rect{Origin: m.Pos{X: 0, Y: 232}, Size: m.Delta{DX: 64, DY: 64}}, "touch rectangle for moving left")
touchRectRight = flag.Text("touch_rect_right", m.Rect{Origin: m.Pos{X: 64, Y: 232}, Size: m.Delta{DX: 64, DY: 64}}, "touch rectangle for moving right")
touchRectDown = flag.Text("touch_rect_down", m.Rect{Origin: m.Pos{X: 0, Y: 296}, Size: m.Delta{DX: 128, DY: 64}}, "touch rectangle for moving down")
touchRectUp = flag.Text("touch_rect_up", m.Rect{Origin: m.Pos{X: 0, Y: 168}, Size: m.Delta{DX: 128, DY: 64}}, "touch rectangle for moving up")
touchRectJump = flag.Text("touch_rect_jump", m.Rect{Origin: m.Pos{X: 576, Y: 296}, Size: m.Delta{DX: 64, DY: 64}}, "touch rectangle for jumping")
touchRectAction = flag.Text("touch_rect_action", m.Rect{}, "touch rectangle for performing an action")
touchRectExit = flag.Text("touch_rect_exit", m.Rect{Origin: m.Pos{X: 0, Y: 0}, Size: m.Delta{DX: 128, DY: 64}}, "touch rectangle for exiting")
)
const (
touchClickMaxFrames = 30
touchPadFrames = 300
)
type touchInfo struct {
clickFrames int
clickCancelled bool
pos m.Pos
prevPos m.Pos
hit bool
edit touchEditInfo
}
var (
touchUsePad bool
touchShowPad bool
touches = map[ebiten.TouchID]*touchInfo{}
touchIDs []ebiten.TouchID
touchHoverPos m.Pos
touchPadFrame int
touchPadUsed bool = false
)
func touchCancelClicks() {
for _, t := range touches {
t.clickCancelled = true
}
}
func touchEmulateMouse() {
hoverAcc := m.Pos{}
hoverCnt := 0
for _, t := range touches {
if !t.hit {
if t.clickFrames < touchClickMaxFrames && !t.clickCancelled {
clickPos = &t.prevPos
}
continue
}
hoverAcc = hoverAcc.Add(t.pos.Delta(m.Pos{}))
hoverCnt++
}
if hoverCnt > 0 {
touchHoverPos = hoverAcc.Add(m.Delta{DX: hoverCnt / 2, DY: hoverCnt / 2}).Div(hoverCnt)
hoverPos = &touchHoverPos
}
}
func touchUpdate(screenWidth, screenHeight, gameWidth, gameHeight int, crtK1, crtK2 float64) {
if !*touch {
return
}
for _, t := range touches {
t.hit = false
}
touchIDs = ebiten.AppendTouchIDs(touchIDs[:0])
if len(touchIDs) > 0 {
// Either support touch OR mouse. This prevents duplicate click events.
mouseCancel()
touchPadFrame = touchPadFrames
touchPadUsed = true
} else if touchPadFrame > 0 {
touchPadFrame--
}
for _, id := range touchIDs {
t, found := touches[id]
if !found {
t = &touchInfo{}
touches[id] = t
}
t.hit = true
t.clickFrames++
t.prevPos = t.pos
x, y := ebiten.TouchPosition(id)
t.pos = pointerCoords(screenWidth, screenHeight, gameWidth, gameHeight, crtK1, crtK2, x, y)
}
if touchEditUpdate(gameWidth, gameHeight) {
// log.Infof("touchEditUpdate returned true - not emulating mouse")
} else {
touchEmulateMouse()
}
for id, t := range touches {
if !t.hit {
delete(touches, id)
continue
}
}
}
func touchSetUsePad(want bool) {
if touchUsePad == want {
return
}
touchCancelClicks()
touchUsePad = want
}
func touchSetShowPad(want bool) {
if touchShowPad == want {
return
}
touchCancelClicks()
touchShowPad = want
}
func (i *impulse) touchPressed() InputMap {
if touchEditPad || !touchUsePad {
return 0
}
if i.touchRect == nil {
return 0
}
for _, t := range touches {
if i.touchRect.Size.IsZero() {
touched := false
for _, other := range impulses {
if other == i {
continue
}
if other.touchRect == nil {
continue
}
if other.touchRect.Size.IsZero() {
continue
}
if other.touchRect.DeltaPos(t.pos).IsZero() {
touched = true
break
}
}
if !touched {
return Touchscreen
}
} else if i.touchRect.DeltaPos(t.pos).IsZero() {
return Touchscreen
}
}
return 0
}
func touchInit() error {
var err error
Left.touchImage, err = image.Load("sprites", "touch_left.png")
if err != nil {
return err
}
Right.touchImage, err = image.Load("sprites", "touch_right.png")
if err != nil {
return err
}
Up.touchImage, err = image.Load("sprites", "touch_up.png")
if err != nil {
return err
}
Down.touchImage, err = image.Load("sprites", "touch_down.png")
if err != nil {
return err
}
Jump.touchImage, err = image.Load("sprites", "touch_jump.png")
if err != nil {
return err
}
Action.touchImage, err = image.Load("sprites", "touch_action.png")
if err != nil {
return err
}
Exit.touchImage, err = image.Load("sprites", "touch_exit.png")
if err != nil {
return err
}
return nil
}
func touchDraw(screen *ebiten.Image) {
if !touchEditPad {
if !touchShowPad {
return
}
if !*touchForce && touchPadFrame <= 0 {
return
}
}
touchEditDraw(screen)
for _, i := range impulses {
r := i.touchRect
if r == nil {
continue
}
img := i.touchImage
if img == nil {
continue
}
options := &ebiten.DrawImageOptions{
CompositeMode: ebiten.CompositeModeSourceOver,
Filter: ebiten.FilterNearest,
}
if r.Size.IsZero() {
if !i.Held {
continue
}
w, h := screen.Size()
r = &m.Rect{
Origin: m.Pos{X: (w - 32) / 2, Y: h - 32},
Size: m.Delta{DX: 32, DY: 32},
}
options.ColorM.Scale(1, 1, 1, 1.0/3)
}
w, h := img.Size()
ox := float64(r.Origin.X)
oy := float64(r.Origin.Y)
sw := float64(r.Size.DX) / float64(w)
sh := float64(r.Size.DY) / float64(h)
if sw < sh {
oy += float64(h) * 0.5 * (sh - sw)
sh = sw
} else if sw > sh {
ox += float64(w) * 0.5 * (sw - sh)
sw = sh
}
options.GeoM.Scale(sw, sh)
options.GeoM.Translate(ox, oy)
if i.Held {
options.ColorM.Scale(-1, -1, -1, 1)
options.ColorM.Translate(1, 1, 1, 0)
}
screen.DrawImage(img, options)
}
}
func HaveTouch() bool {
return *touchForce || touchPadUsed
}