def getNumJoysticks(): """Return a count of the number of joysticks available.""" if backend == 'pyglet': return len(pyglet_input.get_joysticks()) else: pygame.joystick.init() return pygame.joystick.get_count()
def __init__(self, game): self.game = game self.joysticks = input.get_joysticks() for joystick in self.joysticks: joystick.open() joystick.push_handlers(self)
def on_enter(self): super(GamepadController, self).on_enter() self.joy = input.get_joysticks()[0] self.joy.on_joyaxis_motion = self.on_joyaxis_motion self.joy.on_joyhat_motion = self.on_joyhat_motion self.joy.on_joybutton_press = self.on_joybutton_press self.joy.on_joybutton_release = self.on_joybutton_release self.joy.open()
def __init__(self, user_control, gamepad_num=0): super().__init__(user_control) gamepads = get_joysticks() # pylint: disable=assignment-from-no-return if not gamepads: raise RuntimeError("No gamepad found!") self.gamepad = gamepads[gamepad_num] self.gamepad.open() self.gamepad.set_handler("on_joybutton_press", self.on_joybutton_press) self.gamepad.set_handler("on_joyaxis_motion", self.on_joyaxis_motion)
def getNumJoysticks(): """Return a count of the number of joysticks available.""" if backend == 'pyglet': return len(pyglet_input.get_joysticks()) elif backend == 'glfw': n_joys = 0 for joy in range(glfw.JOYSTICK_1, glfw.JOYSTICK_LAST): if glfw.joystick_present(joy): n_joys += 1 return n_joys else: pygame.joystick.init() return pygame.joystick.get_count()
def connect_joystick(*args): if EventHandle().joystick_device is not None: devices = listdir('/dev/input') if 'js0' not in devices: print("%s disconnected " % (EventHandle().joystick_device.name)) input.evdev._devices = {} EventHandle().joystick = None EventHandle().joystick_device = None return if 'js1' not in devices: print("Only 1 joystick is supported for now") try: # Check if joystick is connected EventHandle().joystick = input.get_joysticks()[0] EventHandle().joystick.open() EventHandle().joystick.z = EventHandle().joystick.rz = -1 EventHandle().joystick_device = input.get_devices()[0] print "%s connected" % EventHandle().joystick_device.name except Exception: pass
def __init__(self, id): """An object to control a multi-axis joystick or gamepad .. note: You do need to be flipping frames (or dispatching events manually) in order for the values of the joystick to be updated. :Known issues: Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis. """ self.id = id if backend == 'pyglet': joys = pyglet_input.get_joysticks() if id >= len(joys): logging.error("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") else: self._device = joys[id] self._device.open() self.name = self._device.device.name if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: win()._eventDispatchers.append(self._device.device) else: pygame.joystick.init() self._device = pygame.joystick.Joystick(id) self._device.init() self.name = self._device.get_name()
def __init__(self, id): """An object to control a multi-axis joystick or gamepad. .. note: You do need to be flipping frames (or dispatching events manually) in order for the values of the joystick to be updated. :Known issues: Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis. """ self.id = id if backend == 'pyglet': joys = pyglet_input.get_joysticks() if id >= len(joys): logging.error("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") else: self._device = joys[id] self._device.open() self.name = self._device.device.name if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: win()._eventDispatchers.append(self._device.device) elif backend == 'glfw': # We can create a joystick anytime after glfwInit() is called, but # there should be a window open first. # Joystick events are processed when flipping the associated window. if not glfw.init(): logging.error("GLFW could not be initialized. Exiting.") # get all available joysticks, GLFW supports up to 16. joys = [] for joy in range(glfw.JOYSTICK_1, glfw.JOYSTICK_LAST): if glfw.joystick_present(joy): joys.append(joy) # error checks if not joys: # if the list is empty, no joysticks were found error_msg = ("No joysticks were found by the GLFW runtime. " "Check connections and try again.") logging.error(error_msg) raise RuntimeError(error_msg) elif id not in joys: error_msg = ("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") logging.error(error_msg) raise RuntimeError(error_msg) self._device = id # just need the ID for GLFW self.name = glfw.get_joystick_name(self._device).decode("utf-8") if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: # sending the raw ID to the window. win()._eventDispatchers.append(self._device) else: pygame.joystick.init() self._device = pygame.joystick.Joystick(id) self._device.init() self.name = self._device.get_name()
def __init__(self, id): """An object to control a multi-axis joystick or gamepad. .. note: You do need to be flipping frames (or dispatching events manually) in order for the values of the joystick to be updated. :Known issues: Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis. """ self.id = id if backend == 'pyglet': joys = pyglet_input.get_joysticks() if id >= len(joys): logging.error("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") else: self._device = joys[id] self._device.open() self.name = self._device.device.name if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: win()._eventDispatchers.append(pyglet_dispatcher) elif backend == 'glfw': # We can create a joystick anytime after glfwInit() is called, but # there should be a window open first. # Joystick events are processed when flipping the associated window. if not glfw.init(): logging.error("GLFW could not be initialized. Exiting.") # get all available joysticks, GLFW supports up to 16. joys = [] for joy in range(glfw.JOYSTICK_1, glfw.JOYSTICK_LAST): if glfw.joystick_present(joy): joys.append(joy) # error checks if not joys: # if the list is empty, no joysticks were found error_msg = ("No joysticks were found by the GLFW runtime. " "Check connections and try again.") logging.error(error_msg) raise RuntimeError(error_msg) elif id not in joys: error_msg = ("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") logging.error(error_msg) raise RuntimeError(error_msg) self._device = id # just need the ID for GLFW self.name = glfw.get_joystick_name(self._device).decode("utf-8") if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: # sending the raw ID to the window. win()._eventDispatchers.append(self._device) else: pygame.joystick.init() self._device = pygame.joystick.Joystick(id) self._device.init() self.name = self._device.get_name()
def get_joysticks(): return input.get_joysticks()
def __init__(self, joystick_id): super().__init__() joystick = get_joysticks()[joystick_id] joystick.open() joystick.push_handlers(self) self.joystick = joystick
class JoystickEvents(object): def on_joybutton_release(self, joystick, button): if button in buttons: buttons[button]() else: ao2.speak(str(button)) def on_joyhat_motion(self, joystick, hat_x, hat_y): """Joystick hat was pressed.""" ao2.speak('%s, %s.' % (x, y)) joysticks = [] for j in input.get_joysticks(): logger.info('Found joystick %s.', j.device.name) joysticks.append(j) j.open() j.push_handlers(JoystickEvents()) @window.event def on_close(): for j in joysticks: logger.info('Closing joystick %s.', j.device.name) j.close() for p in keyboard.pressed: logger.info('Releasing key %s.', p) keyboard.toggle(p)
from pyglet.input import get_joysticks import config from game_scene import GameScene joysticks = get_joysticks() game_name = "Robot Warz" import patch_director patch_director.exec() from cocos.director import director window = director.init( width=config.screen_size[0], height=config.screen_size[1], caption=game_name, resizable=True ) director._usable_width = config.screen_size[0] * 2 director._usable_height = config.screen_size[1] * 2 director.show_FPS = True print("Window config: {0}".format(window.config)) director.run(GameScene())