-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinput.py
More file actions
366 lines (314 loc) · 8.58 KB
/
input.py
File metadata and controls
366 lines (314 loc) · 8.58 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
## Copyright (c) 2020-2023 The PyUnity Team
## This file is licensed under the MIT License.
## See https://docs.pyunity.x10.bz/en/latest/license.html
__all__ = ["KeyState", "KeyCode", "MouseCode", "Input", "KeyboardAxis"]
from .errors import PyUnityException
from .scenes import SceneManager
from .values import ImmutableStruct, Mathf, Vector3
from enum import IntEnum, auto
import os
class KeyState(IntEnum):
UP = auto()
DOWN = auto()
PRESS = auto()
NONE = auto()
class KeyCode(IntEnum):
A = auto()
B = auto()
C = auto()
D = auto()
E = auto()
F = auto()
G = auto()
H = auto()
I = auto()
J = auto()
K = auto()
L = auto()
M = auto()
N = auto()
O = auto()
P = auto()
Q = auto()
R = auto()
S = auto()
T = auto()
U = auto()
V = auto()
W = auto()
X = auto()
Y = auto()
Z = auto()
Space = auto()
Alpha0 = auto()
Alpha1 = auto()
Alpha2 = auto()
Alpha3 = auto()
Alpha4 = auto()
Alpha5 = auto()
Alpha6 = auto()
Alpha7 = auto()
Alpha8 = auto()
Alpha9 = auto()
F1 = auto()
F2 = auto()
F3 = auto()
F4 = auto()
F5 = auto()
F6 = auto()
F7 = auto()
F8 = auto()
F9 = auto()
F10 = auto()
F11 = auto()
F12 = auto()
Keypad0 = auto()
Keypad1 = auto()
Keypad2 = auto()
Keypad3 = auto()
Keypad4 = auto()
Keypad5 = auto()
Keypad6 = auto()
Keypad7 = auto()
Keypad8 = auto()
Keypad9 = auto()
Up = auto()
Down = auto()
Left = auto()
Right = auto()
class MouseCode(IntEnum):
Left = auto()
Middle = auto()
Right = auto()
class KeyboardAxis:
def __init__(self, name, speed, positive, negative):
self.positive = positive
self.negative = negative
self.value = 0
self.raw = 0
self.name = name
self.speed = speed
def getValue(self, dt):
change = sum([Input.GetKey(key) for key in self.positive]) - \
sum([Input.GetKey(key) for key in self.negative])
self.raw = change
if change == 0:
if self.value != 0:
change = -abs(self.value) / self.value
if abs(dt) > abs(self.value):
self.value = 0
return 0
else:
return 0
self.value += Mathf.Clamp(change, -1, 1) * dt * self.speed
self.value = Mathf.Clamp(self.value, -1, 1)
return self.value
class Input(metaclass=ImmutableStruct):
_names = ["mousePosition"]
@classmethod
def GetKey(cls, keycode):
"""
Check if key is pressed at moment of function call
Parameters
----------
keycode : KeyCode
Key to query
Returns
-------
boolean
If the key is pressed
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getKey(keycode, KeyState.PRESS)
@classmethod
def GetKeyUp(cls, keycode):
"""
Check if key was released this frame.
Parameters
----------
keycode : KeyCode
Key to query
Returns
-------
boolean
If the key was released
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getKey(keycode, KeyState.UP)
@classmethod
def GetKeyDown(cls, keycode):
"""
Check if key was pressed down this frame.
Parameters
----------
keycode : KeyCode
Key to query
Returns
-------
boolean
If the key was pressed down
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getKey(keycode, KeyState.DOWN)
@classmethod
def GetKeyState(cls, keycode, keystate):
"""
Check key state at moment of function call
Parameters
----------
keycode : KeyCode
Key to query
keystate : KeyState
Keystate, either KeyState.PRESS, KeyState.UP or KeyState.DOWN
Returns
-------
boolean
If the key state matches
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getKey(keycode, keystate)
@classmethod
def GetMouse(cls, mousecode):
"""
Check if mouse button is pressed at moment of function call
Parameters
----------
mousecode : MouseCode
Mouse button to query
Returns
-------
boolean
If the mouse button is pressed
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getMouse(mousecode, KeyState.PRESS)
@classmethod
def GetMouseUp(cls, mousecode):
"""
Check if mouse button was released this frame.
Parameters
----------
mousecode : MouseCode
Mouse button to query
Returns
-------
boolean
If the mouse button was released
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getMouse(mousecode, KeyState.UP)
@classmethod
def GetMouseDown(cls, mousecode):
"""
Check if mouse button was pressed down this frame.
Parameters
----------
mousecode : MouseCode
Mouse button to query
Returns
-------
boolean
If the mouse button was pressed down
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getMouse(mousecode, KeyState.DOWN)
@classmethod
def GetMouseState(cls, mousecode, mousestate):
"""
Check for mouse button state at moment of function call
Parameters
----------
mousecode : MouseCode
Key to query
mousestate : KeyState
Keystate, either KeyState.PRESS, KeyState.UP or KeyState.DOWN
Returns
-------
boolean
If the mouse button state matches
"""
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return False
return SceneManager.runner.window.getMouse(mousecode, mousestate)
_axes = {"MouseX": 0, "MouseY": 0, "Horizontal": 0, "Vertical": 0}
_axisObjects = {
"Horizontal": KeyboardAxis(
"Horizontal", 3,
[KeyCode.D, KeyCode.Right],
[KeyCode.A, KeyCode.Left]
),
"Vertical": KeyboardAxis(
"Vertical", 3,
[KeyCode.W, KeyCode.Up],
[KeyCode.S, KeyCode.Down]
),
}
@classmethod
def GetAxis(cls, axis):
"""
Get the value for the specified axis. This is
always between -1 and 1.
Parameters
----------
axis : str
Specified axis
Returns
-------
float
Axis value
Raises
------
PyUnityException
If the axis is not a valid axis
"""
if axis not in cls._axes:
raise PyUnityException(f"Invalid axis: {axis!r}")
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return 0
return cls._axes[axis]
@classmethod
def GetRawAxis(cls, axis):
"""
Get the raw value for the specified axis. This is
always either -1, 0 or 1.
Parameters
----------
axis : str
Specified axis
Returns
-------
float
Raw axis value
Raises
------
PyUnityException
If the axis is not a valid axis
"""
if axis not in cls._axisObjects:
raise PyUnityException(f"Invalid axis: {axis!r}")
if os.environ["PYUNITY_INTERACTIVE"] != "1":
return 0
return cls._axisObjects[axis].raw
mousePosition = None
_mouseLast = None
@classmethod
def UpdateAxes(cls, dt):
cls._set("mousePosition", Vector3(
*SceneManager.runner.window.getMousePos(), 0))
new = cls.mousePosition
if cls._mouseLast is None:
diff = new
else:
diff = new - cls._mouseLast
cls._mouseLast = new
cls._axes["MouseX"] = diff.x
cls._axes["MouseY"] = diff.y
for axis in cls._axisObjects.values():
cls._axes[axis.name] = axis.getValue(dt)