-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
173 lines (146 loc) · 3.23 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
package wengine
import "math"
const (
KEY_STATE_UP = iota
KEY_STATE_DOWN
)
const (
AXIS_SOURCE_KEY = iota
AXIS_SOURCE_MOUSE
)
const (
AXIS_FROM_X = iota
AXIS_FROM_Y
)
type AxisMeta struct {
Source int
// key
PositiveKey, NegativeKey string
// mouse & joystick
From int
Gravity, Dead, Sensitivity float64
Invert bool
}
const (
CURSOR_MODE_NORMAL = iota
CURSOR_MODE_DISABLED
)
type Input struct {
preKeyState map[string]int
curKeyState map[string]int
preMouseX, curMouseX float64
preMouseY, curMouseY float64
axes map[string][]*AxisMeta
axisValue map[*AxisMeta]float64
cursorMode int
currentTime, lastTime float64
}
func newInput() *Input {
return &Input{
preKeyState: map[string]int{},
curKeyState: map[string]int{},
axes: map[string][]*AxisMeta{},
axisValue: map[*AxisMeta]float64{},
}
}
func (i *Input) frameStart(currentTime float64) {
i.currentTime = currentTime
if i.lastTime == 0 {
i.lastTime = i.currentTime
}
for meta, value := range i.axisValue {
switch meta.Source {
case AXIS_SOURCE_KEY:
var deltaHold float64
if meta.PositiveKey != "" {
if i.GetKey(meta.PositiveKey) && !i.GetKeyDown(meta.PositiveKey) {
deltaHold += i.currentTime - i.lastTime
}
}
if meta.NegativeKey != "" {
if i.GetKey(meta.NegativeKey) && !i.GetKeyDown(meta.NegativeKey) {
deltaHold -= i.currentTime - i.lastTime
}
}
if deltaHold == 0 {
if value > 0 {
value -= math.Min(meta.Gravity*(i.currentTime-i.lastTime), value)
} else {
value += math.Min(meta.Gravity*(i.currentTime-i.lastTime), -value)
}
} else {
if value*deltaHold < 0 {
value = 0
}
value += deltaHold * meta.Sensitivity
}
if value > 1 {
value = 1
}
if value < -1 {
value = -1
}
case AXIS_SOURCE_MOUSE:
switch meta.From {
case AXIS_FROM_X:
value = meta.Sensitivity * (i.curMouseX - i.preMouseX)
case AXIS_FROM_Y:
value = meta.Sensitivity * (i.curMouseY - i.preMouseY)
}
}
if math.Abs(value) <= meta.Dead {
value = 0
}
i.axisValue[meta] = value
}
}
func (i *Input) frameEnd() {
for k, v := range i.curKeyState {
i.preKeyState[k] = v
}
i.preMouseX, i.preMouseY = i.curMouseX, i.curMouseY
i.lastTime = i.currentTime
}
func (i *Input) SetCursorMode(mode int) {
i.cursorMode = mode
}
func (i *Input) GetKey(key string) bool {
if i.curKeyState[key] == KEY_STATE_DOWN {
return true
}
return false
}
func (i *Input) GetKeyDown(key string) bool {
if i.curKeyState[key] == KEY_STATE_DOWN && i.preKeyState[key] == KEY_STATE_UP {
return true
}
return false
}
func (i *Input) GetKeyUp(key string) bool {
if i.curKeyState[key] == KEY_STATE_UP && i.preKeyState[key] == KEY_STATE_DOWN {
return true
}
return false
}
func (i *Input) BindAxis(axis string, meta AxisMeta) {
i.axes[axis] = append(i.axes[axis], &meta)
i.axisValue[&meta] = 0
}
func (i *Input) ResetAxis(axis string) {
for _, meta := range i.axes[axis] {
delete(i.axisValue, meta)
}
delete(i.axes, axis)
}
func (i *Input) GetAxis(axis string) (final float64) {
metas, exists := i.axes[axis]
if !exists {
return
}
for _, meta := range metas {
if math.Abs(i.axisValue[meta]) >= math.Abs(final) {
final = i.axisValue[meta]
}
}
return
}