Debian Codegraph @

examples/input/controller.py

raw symbols SCIP
1 import pyglet
2
3 from pyglet.math import Vec2
4 from pyglet.shapes import Circle, Rectangle, Arc
5
6
7 window = pyglet.window.Window(720, 480)
8 batch = pyglet.graphics.Batch()
9
10
11 @window.event
12 def on_draw():
13 window.clear()
14 batch.draw()
15
16
17 class ControllerDisplay:
18 """A class to visualize all the Controller inputs."""
19
20 def __init__(self, batch):
21
22 self.label = pyglet.text.Label("No Controller connected.", x=10, y=window.height - 20,
23 multiline=True, width=720, batch=batch)
24
25 self.left_trigger = Rectangle(70, 310, 40, 10, batch=batch)
26 self.right_trigger = Rectangle(610, 310, 40, 10, batch=batch)
27 self.d_pad = Rectangle(280, 185, 10, 10, batch=batch)
28
29 self.left_stick = Arc(180, 240, 20, batch=batch)
30 self.left_stick_label = pyglet.text.Label("(0.00, 0.00)", x=180, y=50, anchor_x='center', batch=batch)
31 self.left_stick_bar_x = Rectangle(180, 30, 0, 10, batch=batch)
32 self.left_stick_bar_y = Rectangle(180, 10, 0, 10, batch=batch)
33
34 self.right_stick = Arc(540, 240, 20, batch=batch)
35 self.right_stick_label = pyglet.text.Label("(0.00, 0.00)", x=540, y=50, anchor_x='center', batch=batch)
36 self.right_stick_bar_x = Rectangle(540, 30, 0, 10, batch=batch)
37 self.right_stick_bar_y = Rectangle(540, 10, 0, 10, batch=batch)
38
39 self.l_outline1 = Arc(180, 240, 75, color=(44, 44, 44), batch=batch)
40 self.l_outline2 = Arc(285, 190, 35, color=(44, 44, 44), batch=batch)
41 self.r_outline1 = Arc(540, 240, 75, color=(44, 44, 44), batch=batch)
42 self.r_outline2 = Arc(435, 190, 35, color=(44, 44, 44), batch=batch)
43
44 self.buttons = {'a': Circle(435, 170, 9, color=(124, 178, 232), batch=batch),
45 'b': Circle(455, 190, 9, color=(255, 102, 102), batch=batch),
46 'x': Circle(415, 190, 9, color=(255, 105, 248), batch=batch),
47 'y': Circle(435, 210, 9, color=(64, 226, 160), batch=batch),
48 'leftshoulder': Rectangle(70, 290, 40, 10, batch=batch),
49 'rightshoulder': Rectangle(610, 290, 40, 10, batch=batch),
50 'start': Circle(390, 240, 9, batch=batch),
51 'guide': Circle(360, 240, 9, color=(255, 255, 100), batch=batch),
52 'back': Circle(330, 240, 9, batch=batch),
53 'leftstick': Circle(180, 240, 9, batch=batch),
54 'rightstick': Circle(540, 240, 9, batch=batch)}
55
56 for button in self.buttons.values():
57 button.visible = False
58
59 def on_button_press(self, controller, button_name):
60 if button := self.buttons.get(button_name, None):
61 button.visible = True
62
63 controller.rumble_play_weak(1.0, 0.1)
64
65 def on_button_release(self, controller, button_name):
66 if button := self.buttons.get(button_name, None):
67 button.visible = False
68
69 def on_dpad_motion(self, controller, vector):
70 self.d_pad.position = Vec2(280, 185) + vector.normalize() * 25
71
72 def on_stick_motion(self, controller, stick, vector):
73 if stick == "leftstick":
74 self.left_stick.position = Vec2(180, 240) + vector * 50
75 self.left_stick_label.text = f"({vector.x:.2f}, {vector.y:.2f})"
76 self.left_stick_bar_x.width = vector.x * 100
77 self.left_stick_bar_y.width = vector.y * 100
78 elif stick == "rightstick":
79 self.right_stick.position = Vec2(540, 240) + vector * 50
80 self.right_stick_label.text = f"({vector.x:.2f}, {vector.y:.2f})"
81 self.right_stick_bar_x.width = vector.x * 100
82 self.right_stick_bar_y.width = vector.y * 100
83
84 def on_trigger_motion(self, controller, trigger, value):
85 if trigger == "lefttrigger":
86 self.left_trigger.y = 310 + (value*50)
87 controller.rumble_play_weak(value, duration=5)
88 elif trigger == "righttrigger":
89 self.right_trigger.y = 310 + (value*50)
90 controller.rumble_play_strong(value, duration=5)
91
92
93 controller_display = ControllerDisplay(batch=batch)
94
95
96 def on_connect(controller):
97 controller.open()
98 controller.rumble_play_weak(1.0, 0.1)
99 controller_display.label.text = f"Detected: {controller.name}\nController GUID: {controller.guid}"
100 controller.push_handlers(controller_display)
101
102
103 def on_disconnect(controller):
104 controller_display.label.text = "No Controller connected."
105 controller.remove_handlers(controller_display)
106
107
108 # ControllerManager instance to handle hot-plugging:
109 controller_manager = pyglet.input.ControllerManager()
110 controller_manager.on_connect = on_connect
111 controller_manager.on_disconnect = on_disconnect
112
113 # Handle already connected controller:
114 if controllers := controller_manager.get_controllers():
115 on_connect(controllers[0])
116
117 pyglet.app.run()