forked from KorokEngine/Korok
/
input.go
210 lines (178 loc) · 3.88 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
package input
import (
"sync"
"korok.io/korok/math/f32"
)
// 记录一帧之内的按键,一帧时间做多支持同时按6个按键
type SparseMap struct {
keys [6]Key
stat [6]bool
used int
}
func (m *SparseMap) Put(k Key, st bool) {
m.keys[m.used] = k
m.stat[m.used] = st
m.used ++
}
func (m *SparseMap) Clear() {
m.used = 0
}
func (m *SparseMap) Get(k Key) (st, ok bool) {
for i := 0; i < m.used; i++ {
if m.keys[i] == k {
st, ok = m.stat[i], true
break
}
}
return
}
type InputSystem struct {
buttons map[string]*button
axes map[string]*VAxis
// 记录每帧的按键状态
// 无论是用数组还是哈希,这里的实现总之要达到快速
// 查询一个按键的状态的效果
dirty SparseMap
mutex sync.RWMutex
// 按照button排序,这样同一个Button的绑定按键是
// 是连续的。
binds []KeyBind
// 触摸/鼠标, 最多支持10个手指头同时触摸
// 通常情况下,active < 1
active int
pointerButton [10]button
pointers [10]PointerInput
}
func NewInputSystem() *InputSystem {
in := &InputSystem{
buttons:make(map[string]*button),
axes:make(map[string]*VAxis),
}
Input = in
return in
}
/// 查询虚拟按键的状态
func (in *InputSystem) Button(name string) *button {
return in.buttons[name]
}
func (in *InputSystem) AnyKeyChanged() bool {
if in.dirty.used > 0 {
return true
}
return false
}
/// 将物理按键映射到虚拟按键
func (in *InputSystem) RegisterButton(name string, keys ...Key) {
btn := NewButton()
in.buttons[name] = btn
for _, k := range keys {
in.binds = append(in.binds, KeyBind{k, btn})
}
// sort binds!!
}
// 更新 Button 状态....
// TODO 此处的输入状态,更新有bug!!
func (in *InputSystem) AdvanceFrame() {
if n, dirty := len(in.binds), in.dirty.used; n > 0 && dirty > 0 {
var st, ok bool
var pr *button
for _, bd := range in.binds {
if s, o := in.dirty.Get(bd.key); o {
st = st || s
ok = ok || o
}
if pr != bd.btn {
if ok {
bd.btn.Update(st)
}
st, ok = false, false
}
pr = bd.btn
}
}
}
func (in *InputSystem) Reset() {
// clear dirty map!!
in.mutex.Lock()
in.dirty.Clear()
in.mutex.Unlock()
// reset button state
for _, v := range in.buttons {
v.Reset()
}
for i := 0; i <= in.active; i++ {
in.pointerButton[i].Reset()
}
}
// 更新 key 的状态
func (in *InputSystem) SetKeyEvent(key int, pressed bool) {
in.mutex.Lock()
in.dirty.Put(Key(key), pressed)
in.mutex.Unlock()
}
// 更新 Mouse/Touch 状态
func (in *InputSystem) SetPointerEvent(key int, pressed bool, x, y float32) {
if key != -1000 {
in.mutex.Lock()
in.pointerButton[key].Update(pressed)
in.pointers[key].MousePos = f32.Vec2{x, y}
if key > in.active {
in.active = key
}
in.mutex.Unlock()
} else {
// 如果是鼠标总是记录在 0 的位置
// 如果是手指... 这就尴尬了..需要特殊处理
in.pointers[0].MousePos = f32.Vec2{x, y}
}
}
type Key int
type KeyBind struct {
key Key
btn *button
}
// short API
func Button(name string) button {
return *Input.Button(name)
}
func AnyKeyChanged() bool {
return Input.AnyKeyChanged()
}
func RegisterButton(name string, keys...Key) {
Input.RegisterButton(name, keys...)
}
func PointerButton(pb KeyPoint) button {
return Input.pointerButton[pb]
}
func PointerPosition(pb KeyPoint) PointerInput {
return Input.pointers[pb]
}
// Touch event
func Touch(fi FingerId) (btn button, pos, delta f32.Vec2) {
btn = Input.pointerButton[fi]
p := Input.pointers[fi]
pos, delta = p.MousePos, p.MouseDelta
return
}
// Mouse event
func Mouse(key int) (btn *button, pos, delta f32.Vec2) {
btn = &Input.pointerButton[key]
p := Input.pointers[key]
pos, delta = p.MousePos, p.MouseDelta
return
}
var Input *InputSystem
// mouse or finger button
type KeyPoint int
const (
KeyPoint1 KeyPoint = iota
KeyPoint2
KeyPoint3
KeyPoint4
KeyPoint5
KeyPoint6
KeyPoint7
KeyPoint8
KeyPoint9
KeyPointX
)