-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
182 lines (157 loc) · 7.09 KB
/
controller.py
File metadata and controls
182 lines (157 loc) · 7.09 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
from objc_util import ObjCClass, load_framework
import time
load_framework("GameController")
GCController = ObjCClass("GCController")
def btn_pressed(btn):
return bool(btn and btn.isPressed())
def axis_val(axis):
return float(axis.value()) if axis else 0.0
class Controller:
def __init__(self, obj, index=0):
self.obj = obj
self.index = index
self.name = str(obj.vendorName() or f"Controller {index+1}")
self.prev_buttons, self.prev_axes = {}, {}
self.deadzone = 0.05
self.connected = True
self.callbacks = {
"button_down": {},
"button_up": {},
"axis": {},
}
def on_button_down(self, btn, func): self.callbacks["button_down"][btn] = func
def on_button_up(self, btn, func): self.callbacks["button_up"][btn] = func
def on_axis(self, axis, func): self.callbacks["axis"][axis] = func
def is_button_pressed(self, name): return self.prev_buttons.get(name, False)
def get_axis(self, name): return self.prev_axes.get(name, 0.0)
def set_deadzone(self, dz): self.deadzone = max(0.0, min(dz, 1.0))
def set_player_index(self, idx):
self.index = idx
if hasattr(self.obj, "setPlayerIndex_"):
try: self.obj.setPlayerIndex_(idx)
except Exception: pass
def vibrate(self, low_freq=1.0, high_freq=1.0, duration=0.3):
try:
haptics = getattr(self.obj, "haptics", None)
if haptics:
engine = haptics().createEngineWithLocality_("all")
if engine:
engine.start()
event = getattr(engine, "createContinuousHapticEventWithIntensity_frequency_relativeTime_duration_", None)
if event:
event_obj = event(max(low_freq, high_freq), 1.0, 0.0, duration)
engine.playPattern_(event_obj)
else:
time.sleep(duration)
engine.stop()
return True
profile = getattr(self.obj, "physicalInputProfile", lambda: None)()
rumble = getattr(self.obj, "rumble", None) or getattr(profile, "rumble", None)
if rumble and hasattr(rumble, "startWithIntensity_duration_"):
rumble.startWithIntensity_duration_(max(low_freq, high_freq), duration)
time.sleep(duration)
rumble.stop()
return True
return False
except Exception as e:
print(f"{self.name}: Error triggering rumble → {e}")
return False
def disconnect(self):
self.connected = False
def reconnect(self, obj, index):
self.obj, self.index, self.connected = obj, index, True
def get_state(self):
return {"buttons": dict(self.prev_buttons), "axes": dict(self.prev_axes)}
def get_buttons(self):
gp = self.obj.extendedGamepad() or self.obj.microGamepad()
if not gp: return {}
button_map = {
"A": "buttonA", "B": "buttonB", "X": "buttonX", "Y": "buttonY",
"LB": "leftShoulder", "RB": "rightShoulder",
"LT": "leftTrigger", "RT": "rightTrigger",
"L3": "leftThumbstickButton", "R3": "rightThumbstickButton",
"Menu": "buttonMenu", "Options": "buttonOptions", "Home": "buttonHome",
}
btns = {name: getattr(gp, attr)() for name, attr in button_map.items() if hasattr(gp, attr)}
if hasattr(gp, "dpad"):
dpad = gp.dpad()
if dpad:
btns.update({
"DPad Up": dpad.up(),
"DPad Down": dpad.down(),
"DPad Left": dpad.left(),
"DPad Right": dpad.right(),
})
return btns
def get_axes(self):
gp = self.obj.extendedGamepad() or self.obj.microGamepad()
if not gp: return {}
axes = {}
if hasattr(gp, "leftThumbstick"):
stick = gp.leftThumbstick()
axes["LX"], axes["LY"] = axis_val(stick.xAxis()), axis_val(stick.yAxis())
if hasattr(gp, "rightThumbstick"):
stick = gp.rightThumbstick()
axes["RX"], axes["RY"] = axis_val(stick.xAxis()), axis_val(stick.yAxis())
return axes
def poll(self):
if not self.connected: return
btns, axes = self.get_buttons(), self.get_axes()
for name, btn in btns.items():
pressed, prev = btn_pressed(btn), self.prev_buttons.get(name, False)
if pressed != prev:
cb_type = "button_down" if pressed else "button_up"
if name in self.callbacks[cb_type]:
self.callbacks[cb_type][name](self)
self.prev_buttons[name] = pressed
for axis, val in axes.items():
prev_val = self.prev_axes.get(axis, 0.0)
if abs(val - prev_val) > self.deadzone and axis in self.callbacks["axis"]:
self.callbacks["axis"][axis](self, val)
self.prev_axes[axis] = val
class ControllerManager:
def __init__(self):
self.controllers = []
self.discover()
def discover(self, timeout=5):
arr = GCController.controllers()
if arr and arr.count() > 0:
self.controllers = [Controller(c, i) for i, c in enumerate(arr)]
return
try: GCController.startWirelessControllerDiscoveryWithCompletionHandler_(None)
except Exception: pass
for _ in range(timeout):
arr = GCController.controllers()
if arr and arr.count() > 0:
self.controllers = [Controller(c, i) for i, c in enumerate(arr)]
return
time.sleep(1)
def poll_all(self):
current_objs = list(GCController.controllers()) or []
current_map = {int(obj.hash()): obj for obj in current_objs}
for ctrl in self.controllers:
if ctrl.connected and int(ctrl.obj.hash()) not in current_map:
ctrl.disconnect()
for i, obj in enumerate(current_objs):
obj_id = int(obj.hash())
found = next((c for c in self.controllers if int(c.obj.hash()) == obj_id), None)
if not found:
new_ctrl = Controller(obj, i)
self.controllers.append(new_ctrl)
elif not found.connected:
found.reconnect(obj, i)
for c in self.controllers:
if c.connected:
c.poll()
def get_controller(self, index): return self.controllers[index] if 0 <= index < len(self.controllers) else None
def broadcast(self, func): [func(c) for c in self.controllers if c.connected]
def active_controllers(self): return [c for c in self.controllers if c.connected]
def wait_for_new_controller(self, timeout=10):
start, prev_count = time.time(), len(self.controllers)
while time.time() - start < timeout:
arr = GCController.controllers()
if arr and arr.count() > prev_count:
self.controllers = [Controller(c, i) for i, c in enumerate(arr)]
return self.controllers[-1]
time.sleep(0.5)
return None