Skip to content

Commit

Permalink
examples.input.controller: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benmoran56 committed Jun 21, 2023
1 parent 3ccd663 commit bda9b85
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions examples/input/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,45 @@ def on_draw():
class ControllerDisplay:
"""A class to visualize all the Controller inputs."""

label = pyglet.text.Label("No Controller connected.", x=10, y=window.height - 20,
multiline=True, width=720, batch=batch)

left_trigger = Rectangle(70, 310, 40, 10, batch=batch)
right_trigger = Rectangle(610, 310, 40, 10, batch=batch)
d_pad = Rectangle(280, 185, 10, 10, batch=batch)
left_stick = Arc(180, 240, 20, batch=batch)
right_stick = Arc(540, 240, 20, batch=batch)

l_outline1 = Arc(180, 240, 75, color=(44, 44, 44), batch=batch)
l_outline2 = Arc(285, 190, 35, color=(44, 44, 44), batch=batch)
r_outline1 = Arc(540, 240, 75, color=(44, 44, 44), batch=batch)
r_outline2 = Arc(435, 190, 35, color=(44, 44, 44), batch=batch)

buttons = {'a': Circle(435, 170, 9, color=(124, 178, 232), batch=batch),
'b': Circle(455, 190, 9, color=(255, 102, 102), batch=batch),
'x': Circle(415, 190, 9, color=(255, 105, 248), batch=batch),
'y': Circle(435, 210, 9, color=(64, 226, 160), batch=batch),
'leftshoulder': Rectangle(70, 290, 40, 10, batch=batch),
'rightshoulder': Rectangle(610, 290, 40, 10, batch=batch),
'start': Circle(390, 240, 9, batch=batch),
'guide': Circle(360, 240, 9, color=(255, 255, 100), batch=batch),
'back': Circle(330, 240, 9, batch=batch),
'leftstick': Circle(180, 240, 9, batch=batch),
'rightstick': Circle(540, 240, 9, batch=batch)}

for button in buttons.values():
button.visible = False
def __init__(self, batch):

self.label = pyglet.text.Label("No Controller connected.", x=10, y=window.height - 20,
multiline=True, width=720, batch=batch)

self.left_trigger = Rectangle(70, 310, 40, 10, batch=batch)
self.right_trigger = Rectangle(610, 310, 40, 10, batch=batch)
self.d_pad = Rectangle(280, 185, 10, 10, batch=batch)
self.left_stick = Arc(180, 240, 20, batch=batch)
self.right_stick = Arc(540, 240, 20, batch=batch)

self.l_outline1 = Arc(180, 240, 75, color=(44, 44, 44), batch=batch)
self.l_outline2 = Arc(285, 190, 35, color=(44, 44, 44), batch=batch)
self.r_outline1 = Arc(540, 240, 75, color=(44, 44, 44), batch=batch)
self.r_outline2 = Arc(435, 190, 35, color=(44, 44, 44), batch=batch)

self.buttons = {'a': Circle(435, 170, 9, color=(124, 178, 232), batch=batch),
'b': Circle(455, 190, 9, color=(255, 102, 102), batch=batch),
'x': Circle(415, 190, 9, color=(255, 105, 248), batch=batch),
'y': Circle(435, 210, 9, color=(64, 226, 160), batch=batch),
'leftshoulder': Rectangle(70, 290, 40, 10, batch=batch),
'rightshoulder': Rectangle(610, 290, 40, 10, batch=batch),
'start': Circle(390, 240, 9, batch=batch),
'guide': Circle(360, 240, 9, color=(255, 255, 100), batch=batch),
'back': Circle(330, 240, 9, batch=batch),
'leftstick': Circle(180, 240, 9, batch=batch),
'rightstick': Circle(540, 240, 9, batch=batch)}

for button in self.buttons.values():
button.visible = False

def on_button_press(self, controller, button_name):
button = self.buttons.get(button_name, None)
if button:
if button := self.buttons.get(button_name, None):
button.visible = True

controller.rumble_play_weak(1.0, 0.1)

def on_button_release(self, controller, button_name):
button = self.buttons.get(button_name, None)
if button:
if button := self.buttons.get(button_name, None):
button.visible = False

def on_dpad_motion(self, controller, dpleft, dpright, dpup, dpdown):
Expand Down Expand Up @@ -84,11 +84,12 @@ def on_trigger_motion(self, controller, trigger, value):
controller.rumble_play_strong(value, duration=5)


controller_display = ControllerDisplay()
controller_display = ControllerDisplay(batch=batch)


def on_connect(controller):
controller.open()
controller.rumble_play_weak(1.0, 0.1)
controller_display.label.text = f"Detected: {controller.name}\nController GUID: {controller.guid}"
controller.push_handlers(controller_display)

Expand Down

0 comments on commit bda9b85

Please sign in to comment.