-
Notifications
You must be signed in to change notification settings - Fork 0
/
j2klib.py
336 lines (249 loc) · 10.1 KB
/
j2klib.py
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
import XInput
import winput
import json
from typing import Self
__version__ = "0.1.0"
TRIGGER_THRESHOLD = 0.25
STICK_THRESHOLD = 0.5
class JoyMapper:
@staticmethod
def from_dict(data : dict) -> Self:
assert "type" in data
match data["type"]:
case "button":
return JoyButtonMapper.from_dict(data)
case "trigger":
return JoyTriggerMapper.from_dict(data)
case "stick":
return JoyStickMapper.from_dict(data)
raise ValueError("Invalid data")
def press(self) -> None:
raise NotImplementedError()
def release(self) -> None:
raise NotImplementedError()
def to_dict(self) -> dict:
raise NotImplementedError()
def explain(self) -> str:
raise NotImplementedError()
class JoyButtonMapper(JoyMapper):
def __init__(self, joy_button : int, winput_key : int):
self.__button = joy_button
self.__key = winput_key
@classmethod
def from_dict(cls, data : dict) -> None:
assert "button" in data
assert "key" in data
assert isinstance(data["button"], int)
assert isinstance(data["key"], int)
return cls(data["button"], data["key"])
def to_dict(self) -> dict:
return {
"type" : "button",
"button" : self.__button,
"key" : self.__key
}
@property
def button(self) -> int:
return self.__button
def press(self):
winput.press_key(self.__key)
def release(self):
winput.release_key(self.__key)
def explain(self):
return f"{XInput._button_dict[self.__button]} is mapped to {winput.all_vk_codes[self.__key]}"
class JoyTriggerMapper(JoyMapper):
def __init__(self, trigger : int, winput_key : int):
self.__trigger = trigger
self.__key = winput_key
self.__pressed = False
@classmethod
def from_dict(cls, data : dict) -> None:
assert "trigger" in data
assert "key" in data
assert isinstance(data["trigger"], int)
assert isinstance(data["key"], int)
return cls(data["trigger"], data["key"])
def to_dict(self) -> dict:
return {
"type" : "trigger",
"trigger" : self.__trigger,
"key" : self.__key
}
@property
def trigger(self) -> int:
return self.__trigger
def press(self):
if self.__pressed:
return
self.__pressed = True
winput.press_key(self.__key)
def release(self):
if not self.__pressed:
return
self.__pressed = False
winput.release_key(self.__key)
def explain(self):
name = "LEFT_TRIGGER" if self.__trigger == XInput.LEFT else "RIGHT_TRIGGER"
return f"{name} is mapped to {winput.all_vk_codes[self.__key]}"
class JoyStickMapper(JoyMapper):
def __init__(self, stick : int, direction : tuple[int], winput_key : int):
self.__stick = stick
self.__direction = direction
self.__key = winput_key
self.__pressed = False
@classmethod
def from_dict(cls, data : dict) -> None:
assert "stick" in data
assert "direction" in data
assert "key" in data
assert isinstance(data["stick"], int)
assert isinstance(data["direction"], list)
assert isinstance(data["key"], int)
return cls(data["stick"], data["direction"], data["key"])
def to_dict(self) -> dict:
return {
"type" : "stick",
"stick" : self.__stick,
"direction" : self.__direction,
"key" : self.__key
}
@property
def stick(self) -> int:
return self.__stick
@property
def direction(self) -> tuple[int]:
return self.__direction
def press(self):
if self.__pressed:
return
self.__pressed = True
winput.press_key(self.__key)
def release(self):
if not self.__pressed:
return
self.__pressed = False
winput.release_key(self.__key)
def explain(self):
name = "LEFT_STICK" if self.__stick == XInput.LEFT else "RIGHT_STICK"
direction = "RIGHT" if self.__direction[0] == 1 else \
"LEFT" if self.__direction[0] == -1 else \
"UP" if self.__direction[1] == 1 else \
"DOWN"
return f"{name} {direction} is mapped to {winput.all_vk_codes[self.__key]}"
class JoyToKeyConfig:
def __init__(self, joy_mappers : list[JoyMapper]):
self.__mappers = joy_mappers
@property
def button_mappers(self) -> list[JoyButtonMapper]:
return list(filter(lambda mapper: isinstance(mapper, JoyButtonMapper), self.__mappers))
@property
def trigger_mappers(self) -> list[JoyTriggerMapper]:
return list(filter(lambda mapper: isinstance(mapper, JoyTriggerMapper), self.__mappers))
@property
def stick_mappers(self) -> list[JoyStickMapper]:
return list(filter(lambda mapper: isinstance(mapper, JoyStickMapper), self.__mappers))
def explain(self) -> str:
return "\n".join([mapper.explain() for mapper in self.__mappers])
@classmethod
def from_file(cls, filepath : str):
with open(filepath, "r") as file:
data = json.load(file)
assert "version" in data
assert "mappers" in data
assert data["version"] <= __version__, "The specified file was created in a newer version."
mappers = [JoyMapper.from_dict(mapper_dict) for mapper_dict in data["mappers"]]
return cls(mappers)
def to_file(self, filepath : str):
with open(filepath, "w") as file:
data = {
"version" : __version__,
"mappers" : [mapper.to_dict() for mapper in self.__mappers]
}
json.dump(data, file, indent=2)
class JoyToKey:
def __init__(self, config : JoyToKeyConfig):
self.__button_mappers = {mapper.button : mapper for mapper in config.button_mappers}
self.__trigger_mappers = {mapper.trigger : mapper for mapper in config.trigger_mappers}
self.__stick_mappers = {(mapper.stick, *mapper.direction) : mapper for mapper in config.stick_mappers}
self.__config = config
def run(self):
while True:
for event in XInput.get_events():
if event.type == XInput.EVENT_BUTTON_PRESSED:
self.__process_button_press_event(event)
elif event.type == XInput.EVENT_BUTTON_RELEASED:
self.__process_button_release_event(event)
elif event.type == XInput.EVENT_TRIGGER_MOVED:
self.__process_trigger_event(event)
elif event.type == XInput.EVENT_STICK_MOVED:
self.__process_stick_event(event)
def __process_button_press_event(self, event : XInput.Event):
if event.button_id not in self.__button_mappers:
return
self.__button_mappers[event.button_id].press()
def __process_button_release_event(self, event : XInput.Event):
if event.button_id not in self.__button_mappers:
return
self.__button_mappers[event.button_id].release()
def __process_trigger_event(self, event : XInput.Event):
if event.trigger not in self.__trigger_mappers:
return
if event.value >= TRIGGER_THRESHOLD:
self.__trigger_mappers[event.trigger].press()
else:
self.__trigger_mappers[event.trigger].release()
def __process_stick_event(self, event : XInput.Event):
for stick, x, y in self.__stick_mappers:
if stick != event.stick:
continue
mapper = self.__stick_mappers[(stick, x, y)]
match (x, y):
case (1, 0):
if event.x > STICK_THRESHOLD:
mapper.press()
else:
mapper.release()
case (-1, 0):
if event.x < -STICK_THRESHOLD:
mapper.press()
else:
mapper.release()
case (0, 1):
if event.y > STICK_THRESHOLD:
mapper.press()
else:
mapper.release()
case (0, -1):
if event.y < -STICK_THRESHOLD:
mapper.press()
else:
mapper.release()
class JoyToKeyCreator:
def __init__(self):
pass
def listen_for_xinput(self):
list(XInput.get_events())
while True:
events = XInput.get_events()
for event in events:
if event.type == XInput.EVENT_BUTTON_PRESSED:
return {"type" : "button", "button" : event.button_id}
elif event.type == XInput.EVENT_TRIGGER_MOVED and event.value > TRIGGER_THRESHOLD:
return {"type" : "trigger", "trigger" : event.trigger}
elif event.type == XInput.EVENT_STICK_MOVED:
if event.x > STICK_THRESHOLD:
return {"type" : "stick", "stick" : event.stick, "direction" : [1, 0]}
elif event.x < -STICK_THRESHOLD:
return {"type" : "stick", "stick" : event.stick, "direction" : [-1, 0]}
elif event.y > STICK_THRESHOLD:
return {"type" : "stick", "stick" : event.stick, "direction" : [0, 1]}
elif event.y < -STICK_THRESHOLD:
return {"type" : "stick", "stick" : event.stick, "direction" : [0, -1]}
def __winput_callback(self, event: winput.KeyboardEvent):
self.__winput_key_result = event.key
return winput.WP_DONT_PASS_INPUT_ON | winput.WP_UNHOOK | winput.WP_STOP
def listen_for_winput(self) -> int:
winput.hook_keyboard(self.__winput_callback)
winput.wait_messages()
winput.unhook_keyboard()
return self.__winput_key_result