-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinpututil.go
186 lines (162 loc) · 3.9 KB
/
inpututil.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
package koebiten
import (
"sync"
)
type Key int
const (
KeyMax = 20
)
const (
Key0 Key = iota
Key1
Key2
Key3
Key4
Key5
Key6
Key7
Key8
Key9
Key10
Key11
KeyRotaryButton
KeyJoystick
KeyRotaryLeft
KeyRotaryRight
KeyLeft
KeyRight
KeyUp
KeyDown
)
type pos struct {
x int
y int
}
type inputState struct {
keyDurations []int
prevKeyDurations []int
state []bool
m sync.RWMutex
}
var theInputState = &inputState{
keyDurations: make([]int, KeyMax+1),
prevKeyDurations: make([]int, KeyMax+1),
state: make([]bool, KeyMax+1),
}
var isKeyPressed = make([]bool, KeyMax)
func (i *inputState) update() {
i.m.Lock()
defer i.m.Unlock()
// Keyboard
copy(i.prevKeyDurations, i.keyDurations)
for k := Key(0); k <= KeyMax; k++ {
if i.state[k] {
i.keyDurations[k]++
} else {
i.keyDurations[k] = 0
}
}
}
// AppendPressedKeys append currently pressed keyboard keys to keys and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
//
// AppendPressedKeys must be called in a game's Update, not Draw.
//
// AppendPressedKeys is concurrent safe.
func AppendPressedKeys(keys []Key) []Key {
theInputState.m.Lock()
defer theInputState.m.Unlock()
for _, k := range keys {
theInputState.state[k] = true
}
for i, d := range theInputState.keyDurations {
if d == 0 {
continue
}
keys = append(keys, Key(i))
}
return keys
}
// PressedKeys returns a set of currently pressed keyboard keys.
//
// PressedKeys must be called in a game's Update, not Draw.
//
// Deprecated: as of v2.2. Use AppendPressedKeys instead.
func PressedKeys() []Key {
return AppendPressedKeys(nil)
}
// AppendJustPressedKeys append just pressed keyboard keys to keys and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
//
// AppendJustPressedKeys must be called in a game's Update, not Draw.
//
// AppendJustPressedKeys is concurrent safe.
func AppendJustPressedKeys(keys []Key) []Key {
theInputState.m.Lock()
defer theInputState.m.Unlock()
for _, k := range keys {
theInputState.state[k] = true
}
for i, d := range theInputState.keyDurations {
if d != 1 {
continue
}
keys = append(keys, Key(i))
}
return keys
}
// AppendJustReleasedKeys append just released keyboard keys to keys and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
//
// AppendJustReleasedKeys must be called in a game's Update, not Draw.
//
// AppendJustReleasedKeys is concurrent safe.
func AppendJustReleasedKeys(keys []Key) []Key {
theInputState.m.Lock()
defer theInputState.m.Unlock()
for _, k := range keys {
theInputState.state[k] = false
}
for k := Key(0); k <= KeyMax; k++ {
if theInputState.keyDurations[k] != 0 {
continue
}
if theInputState.prevKeyDurations[k] == 0 {
continue
}
keys = append(keys, k)
}
return keys
}
// IsKeyJustPressed returns a boolean value indicating
// whether the given key is pressed just in the current tick.
//
// IsKeyJustPressed must be called in a game's Update, not Draw.
//
// IsKeyJustPressed is concurrent safe.
func IsKeyJustPressed(key Key) bool {
return KeyPressDuration(key) == 1
}
// IsKeyJustReleased returns a boolean value indicating
// whether the given key is released just in the current tick.
//
// IsKeyJustReleased must be called in a game's Update, not Draw.
//
// IsKeyJustReleased is concurrent safe.
func IsKeyJustReleased(key Key) bool {
theInputState.m.RLock()
r := theInputState.keyDurations[key] == 0 && theInputState.prevKeyDurations[key] > 0
theInputState.m.RUnlock()
return r
}
// KeyPressDuration returns how long the key is pressed in ticks (Update).
//
// KeyPressDuration must be called in a game's Update, not Draw.
//
// KeyPressDuration is concurrent safe.
func KeyPressDuration(key Key) int {
theInputState.m.RLock()
s := theInputState.keyDurations[key]
theInputState.m.RUnlock()
return s
}