This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_controls.py
123 lines (103 loc) · 4.98 KB
/
u_controls.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
from panda3d.core import InputDevice
class ConfigControls:
def __init__(self):
self.gamepad = None
self.axis_threshold = 0.5
self.axis_threshold_negative = 0 - self.axis_threshold
self.controlList = [
["arrow_left", self.setDirection, ["left", True]],
["arrow_right", self.setDirection, ["right", True]],
["arrow_up", self.setDirection, ["up", True]],
["arrow_down", self.setDirection, ["down", True]],
["arrow_left-up", self.setDirection, ["left", False]],
["arrow_right-up", self.setDirection, ["right", False]],
["arrow_up-up", self.setDirection, ["up", False]],
["arrow_down-up", self.setDirection, ["down", False]],
["space", self.setCommand, ["confirm", True]],
["space-up", self.setCommand, ["confirm", False]],
["enter", self.setCommand, ["confirm", True]],
["enter-up", self.setCommand, ["confirm", False]],
["z", self.setCommand, ["cancel", True]],
["z-up", self.setCommand, ["cancel", False]],
["x", self.setCommand, ["confirm", True]],
["x-up", self.setCommand, ["confirm", False]],
["escape", self.setCommand, ["cancel", True]],
["escape-up", self.setCommand, ["cancel", False]],
["backspace", self.setCommand, ["cancel", True]],
["backspace-up", self.setCommand, ["cancel", False]],
["gamepad-dpad_right", self.setDirection, ["right", True]],
["gamepad-dpad_right-up", self.setDirection, ["right", False]],
["gamepad-dpad_left", self.setDirection, ["left", True]],
["gamepad-dpad_left-up", self.setDirection, ["left", False]],
["gamepad-dpad_up", self.setDirection, ["up", True]],
["gamepad-dpad_up-up", self.setDirection, ["up", False]],
["gamepad-dpad_down", self.setDirection, ["down", True]],
["gamepad-dpad_down-up", self.setDirection, ["down", False]],
["gamepad-face_a", self.setCommand, ["confirm", True]],
["gamepad-face_a-up", self.setCommand, ["confirm", False]],
["gamepad-face_b", self.setCommand, ["cancel", True]],
["gamepad-face_b-up", self.setCommand, ["cancel", False]],
]
# self.initController()
def initController(self):
self.disableController()
for control in self.controlList:
base.accept(control[0], control[1], control[2])
# Accept device dis-/connection events
base.accept("connect-device", self.connect)
base.accept("disconnect-device", self.disconnect)
def connect(self, device):
# gamepads = base.devices.getDevices(InputDevice.DeviceClass.gamepad)
if device.device_class == InputDevice.DeviceClass.gamepad and not self.gamepad:
self.gamepad = device
base.attachInputDevice(device, prefix="gamepad")
def disconnect(self, device):
if self.gamepad != device:
return
base.detachInputDevice(device)
self.gamepad = None
def resetButtons(self):
self.directionMap = {"left": False,
"right": False, "down": False, "up": False}
self.commandMap = {"confirm": False, "cancel": False}
def setDirection(self, key, value):
self.directionMap[key] = value
def setCommand(self, key, value):
self.commandMap[key] = value
def disableController(self):
self.resetButtons()
base.ignore("connect-device")
base.ignore("disconnect-device")
for control in self.controlList:
base.ignore(control[0])
def read_axis_left(self):
if not self.gamepad:
return {'x': 0, 'y': 0}
left_x = self.gamepad.findAxis(InputDevice.Axis.left_x)
left_y = self.gamepad.findAxis(InputDevice.Axis.left_y)
return {'x': left_x.value, 'y': left_y.value}
def read_axis_right(self):
if not self.gamepad:
return {'x': 0, 'y': 0}
right_x = self.gamepad.findAxis(InputDevice.Axis.right_x)
right_y = self.gamepad.findAxis(InputDevice.Axis.right_y)
return {'x': right_x.value, 'y': right_y.value}
def move_from_axis(self, is_left_axis):
axis_obj = self.read_axis_left() if is_left_axis else self.read_axis_right()
print(axis_obj)
if axis_obj['x'] <= self.axis_threshold_negative:
self.setDirection('left', True)
else:
self.setDirection('left', False)
if axis_obj['x'] >= self.axis_threshold:
self.setDirection('right', True)
else:
self.setDirection('right', False)
if axis_obj['y'] <= self.axis_threshold_negative:
self.setDirection('down', True)
else:
self.setDirection('down', False)
if axis_obj['y'] >= self.axis_threshold:
self.setDirection('up', True)
else:
self.setDirection('up', False)