-
Notifications
You must be signed in to change notification settings - Fork 28
/
input.go
293 lines (267 loc) · 7.79 KB
/
input.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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 spx
import (
"sync"
"github.com/hajimehoshi/ebiten/v2"
)
type Key = ebiten.Key
const (
Key0 Key = ebiten.Key0
Key1 Key = ebiten.Key1
Key2 Key = ebiten.Key2
Key3 Key = ebiten.Key3
Key4 Key = ebiten.Key4
Key5 Key = ebiten.Key5
Key6 Key = ebiten.Key6
Key7 Key = ebiten.Key7
Key8 Key = ebiten.Key8
Key9 Key = ebiten.Key9
KeyA Key = ebiten.KeyA
KeyB Key = ebiten.KeyB
KeyC Key = ebiten.KeyC
KeyD Key = ebiten.KeyD
KeyE Key = ebiten.KeyE
KeyF Key = ebiten.KeyF
KeyG Key = ebiten.KeyG
KeyH Key = ebiten.KeyH
KeyI Key = ebiten.KeyI
KeyJ Key = ebiten.KeyJ
KeyK Key = ebiten.KeyK
KeyL Key = ebiten.KeyL
KeyM Key = ebiten.KeyM
KeyN Key = ebiten.KeyN
KeyO Key = ebiten.KeyO
KeyP Key = ebiten.KeyP
KeyQ Key = ebiten.KeyQ
KeyR Key = ebiten.KeyR
KeyS Key = ebiten.KeyS
KeyT Key = ebiten.KeyT
KeyU Key = ebiten.KeyU
KeyV Key = ebiten.KeyV
KeyW Key = ebiten.KeyW
KeyX Key = ebiten.KeyX
KeyY Key = ebiten.KeyY
KeyZ Key = ebiten.KeyZ
KeyApostrophe Key = ebiten.KeyApostrophe
KeyBackslash Key = ebiten.KeyBackslash
KeyBackspace Key = ebiten.KeyBackspace
KeyCapsLock Key = ebiten.KeyCapsLock
KeyComma Key = ebiten.KeyComma
KeyDelete Key = ebiten.KeyDelete
KeyDown Key = ebiten.KeyDown
KeyEnd Key = ebiten.KeyEnd
KeyEnter Key = ebiten.KeyEnter
KeyEqual Key = ebiten.KeyEqual
KeyEscape Key = ebiten.KeyEscape
KeyF1 Key = ebiten.KeyF1
KeyF2 Key = ebiten.KeyF2
KeyF3 Key = ebiten.KeyF3
KeyF4 Key = ebiten.KeyF4
KeyF5 Key = ebiten.KeyF5
KeyF6 Key = ebiten.KeyF6
KeyF7 Key = ebiten.KeyF7
KeyF8 Key = ebiten.KeyF8
KeyF9 Key = ebiten.KeyF9
KeyF10 Key = ebiten.KeyF10
KeyF11 Key = ebiten.KeyF11
KeyF12 Key = ebiten.KeyF12
KeyGraveAccent Key = ebiten.KeyGraveAccent
KeyHome Key = ebiten.KeyHome
KeyInsert Key = ebiten.KeyInsert
KeyKP0 Key = ebiten.KeyKP0
KeyKP1 Key = ebiten.KeyKP1
KeyKP2 Key = ebiten.KeyKP2
KeyKP3 Key = ebiten.KeyKP3
KeyKP4 Key = ebiten.KeyKP4
KeyKP5 Key = ebiten.KeyKP5
KeyKP6 Key = ebiten.KeyKP6
KeyKP7 Key = ebiten.KeyKP7
KeyKP8 Key = ebiten.KeyKP8
KeyKP9 Key = ebiten.KeyKP9
KeyKPDecimal Key = ebiten.KeyKPDecimal
KeyKPDivide Key = ebiten.KeyKPDivide
KeyKPEnter Key = ebiten.KeyKPEnter
KeyKPEqual Key = ebiten.KeyKPEqual
KeyKPMultiply Key = ebiten.KeyKPMultiply
KeyKPSubtract Key = ebiten.KeyKPSubtract
KeyLeft Key = ebiten.KeyLeft
KeyLeftBracket Key = ebiten.KeyLeftBracket
KeyMenu Key = ebiten.KeyMenu
KeyMinus Key = ebiten.KeyMinus
KeyNumLock Key = ebiten.KeyNumLock
KeyPageDown Key = ebiten.KeyPageDown
KeyPageUp Key = ebiten.KeyPageUp
KeyPause Key = ebiten.KeyPause
KeyPeriod Key = ebiten.KeyPeriod
KeyPrintScreen Key = ebiten.KeyPrintScreen
KeyRight Key = ebiten.KeyRight
KeyRightBracket Key = ebiten.KeyRightBracket
KeyScrollLock Key = ebiten.KeyScrollLock
KeySemicolon Key = ebiten.KeySemicolon
KeySlash Key = ebiten.KeySlash
KeySpace Key = ebiten.KeySpace
KeyTab Key = ebiten.KeyTab
KeyUp Key = ebiten.KeyUp
KeyAlt Key = ebiten.KeyAlt
KeyControl Key = ebiten.KeyControl
KeyShift Key = ebiten.KeyShift
KeyMax Key = ebiten.KeyMax
KeyAny Key = -1
)
// -------------------------------------------------------------------------------------
type event interface{}
type eventStart struct{}
type eventKeyDown struct {
Key ebiten.Key
}
type eventKeyUp struct {
Key ebiten.Key
}
type eventLeftButtonDown struct {
X, Y int
}
type eventLeftButtonUp struct {
X, Y int
}
type eventFirer interface {
fireEvent(ev event)
}
type inputMgr struct {
touchIDs []ebiten.TouchID
keyStates map[ebiten.Key]int
lbtnState int
keyDuration int
mouseX int
mouseY int
firer eventFirer
startFlag sync.Once
}
func (i *inputMgr) init(firer eventFirer, keyDuration int) {
const (
defaultKeyDuration = 15
)
if keyDuration == 0 {
keyDuration = defaultKeyDuration
}
i.keyStates = make(map[ebiten.Key]int)
i.lbtnState = mouseStateNone
i.keyDuration = keyDuration
i.firer = firer
}
func (i *inputMgr) reset() {
i.startFlag = sync.Once{}
}
// -------------------------------------------------------------------------------------
const (
mouseStateNone = 0x00
mouseStatePressing = 0x01
mouseFlagStates = 0x7f
mouseFlagTouching = 0x80
)
func (i *inputMgr) update() {
i.touchIDs = ebiten.AppendTouchIDs(i.touchIDs[:0])
i.startFlag.Do(func() {
i.firer.fireEvent(&eventStart{})
})
i.updateKeyboard()
i.updateMouse()
}
func (i *inputMgr) updateMouse() {
switch i.lbtnState & mouseFlagStates {
case mouseStateNone:
switch {
case len(i.touchIDs) > 0:
i.mouseX, i.mouseY = ebiten.TouchPosition(i.touchIDs[0])
i.lbtnState = mouseStatePressing | mouseFlagTouching
case ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft):
i.mouseX, i.mouseY = ebiten.CursorPosition()
i.lbtnState = mouseStatePressing
default:
return
}
i.firer.fireEvent(&eventLeftButtonDown{X: i.mouseX, Y: i.mouseY})
case mouseStatePressing:
if (i.lbtnState & mouseFlagTouching) != 0 {
if len(i.touchIDs) > 0 {
return
}
} else {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
return
}
i.mouseX, i.mouseY = ebiten.CursorPosition()
}
i.lbtnState = mouseStateNone
i.firer.fireEvent(&eventLeftButtonUp{X: i.mouseX, Y: i.mouseY})
default:
panic("unknown mouse state")
}
}
func (i *inputMgr) isMousePressed() bool {
return (i.lbtnState & mouseStatePressing) != 0
}
func (i *inputMgr) mouseXY() (int, int) {
if (i.lbtnState & mouseFlagTouching) != 0 {
if ids := i.touchIDs; len(ids) > 0 {
return ebiten.TouchPosition(ids[0])
}
return i.mouseX, i.mouseY
}
return ebiten.CursorPosition()
}
func (i *inputMgr) updateKeyboard() {
keyDuration := i.keyDuration
for key := ebiten.Key(0); key <= ebiten.KeyMax; key++ {
if ebiten.IsKeyPressed(key) {
n := i.keyStates[key]
if n > 0 {
if !isStateKey(key) {
n--
}
}
if n <= 0 {
n = keyDuration
i.firer.fireEvent(&eventKeyDown{Key: key})
}
i.keyStates[key] = n
} else {
if i.keyStates[key] > 0 {
i.firer.fireEvent(&eventKeyUp{Key: key})
i.keyStates[key] = 0
}
}
}
}
func isStateKey(key Key) bool {
switch key {
case KeyAlt, KeyControl, KeyShift:
return true
}
return false
}
func isKeyPressed(key Key) bool {
if key == KeyAny {
for key := ebiten.Key(0); key <= ebiten.KeyMax; key++ {
if ebiten.IsKeyPressed(key) {
return true
}
}
return false
}
return ebiten.IsKeyPressed(key)
}
// -------------------------------------------------------------------------------------