def _update_gamepad(self): for i in range(2): if i == 0: states, count = glfw.get_joystick_buttons(glfw.JOYSTICK_1) offset = pyxel.GAMEPAD_1_A else: states, count = glfw.get_joystick_buttons(glfw.JOYSTICK_2) offset = pyxel.GAMEPAD_2_A for j in range(count): action = states[j] button = offset + j self._set_key_state(button, action)
def check_input(input: Optional[Input]) -> float: if input is None: return 0.0 elif isinstance(input, JoystickAxis): for joystick in get_joysticks_by_id(input.joystick): axes_ptr, length = glfw.get_joystick_axes(joystick) if input.index < length: axis_value = float(axes_ptr[input.index]) axis_value = min(max(axis_value * input.direction, 0.0), 1.0) if axis_value < 0.001: # TODO: Allow dead zone configuration axis_value = 0.0 if axis_value > 0.0: return axis_value return 0.0 elif isinstance(input, JoystickButton): for joystick in get_joysticks_by_id(input.joystick): buttons_ptr, length = glfw.get_joystick_buttons(joystick) if input.index < length and buttons_ptr[input.index]: return 1.0 return 0.0 elif isinstance(input, KeyboardKey): return 1.0 if ig.is_key_down(input.key) else 0.0
def get_joy_buttons(joy): buttons = glfw.get_joystick_buttons(joy) #print(buttons) toReturn = [0.] * buttons[1] for i in range(buttons[1]): toReturn[i] = buttons[0][i] return toReturn
def getNumButtons(self): """Return the number of digital buttons on the device.""" if backend == 'pyglet': return len(self._device.buttons) elif backend == 'glfw': _, count = glfw.get_joystick_buttons(self._device) return count else: return self._device.get_numbuttons()
def _update_gamepad(self): for i in range(2): if i == 0: states, count = glfw.get_joystick_buttons(glfw.JOYSTICK_1) offset = pyxel.GAMEPAD_1_A else: states, count = glfw.get_joystick_buttons(glfw.JOYSTICK_2) offset = pyxel.GAMEPAD_2_A for j in range(count): action = states[j] button = offset + j if action == glfw.PRESS: self._key_state[button] = pyxel.frame_count elif action == glfw.RELEASE: if self._key_state.get(button) == pyxel.frame_count: self._key_state[button] = -pyxel.frame_count - 1 else: self._key_state[button] = -pyxel.frame_count
def getAllButtons(self): """Get the state of all buttons as a list.""" if backend == 'pyglet': return self._device.buttons elif backend == 'glfw': bs, count = glfw.get_joystick_buttons(self._device) return [bs[i] for i in range(count)] else: bs = [] for id in range(self._device.get_numbuttons()): bs.append(self._device.get_button(id)) return bs
def getButton(self, buttonId): """Get the state of a given button. buttonId should be a value from 0 to the number of buttons-1 """ if backend == 'pyglet': return self._device.buttons[buttonId] elif backend == 'glfw': bs, _ = glfw.get_joystick_buttons(self._device) return bs[buttonId] else: return self._device.get_button(buttonId)
def _update_gamepad(self): for i in range(4): states, count = glfw.get_joystick_buttons(getattr(glfw, 'JOYSTICK_{0}'.format(i + 1))) offset = getattr(pyxel, 'GAMEPAD_{0}_UP'.format(i + 1)) for j in range(count): action = states[j] button = offset + j if action == glfw.PRESS: self._key_state[button] = pyxel.frame_count elif action == glfw.RELEASE: if self._key_state.get(button) == pyxel.frame_count: self._key_state[button] = -pyxel.frame_count - 1 else: self._key_state[button] = -pyxel.frame_count
def update_inputs(self, steering_type=SteeringType.TANK): # TODO docstring # Update GLFW input glfw.poll_events() # Read the C array of axes and transform to a list (axes_ptr, axis_count) = glfw.get_joystick_axes(glfw.JOYSTICK_1) axes = [axes_ptr[i] for i in range(axis_count)] # Update last and current x/y depending on steering type self.last_stick = self.stick self.stick = (-axes[steering_type.value], -axes[1]) # Update last and current trigger values self.last_trigger = self.trigger self.trigger = ((axes[5] + 1) / 2, (axes[4] + 1) / 2) # Get buttons (btns_ptr, btn_count) = glfw.get_joystick_buttons(glfw.JOYSTICK_1) self.buttons = [btns_ptr[i] for i in range(btn_count)]
def joystick_callback(self, jid): present = glfw.joystick_present(jid) if present == 1: ax, n_ax = glfw.get_joystick_axes(jid) # TODO: get better threshold for stick drift if abs(ax[0]) > 0.09: # right stick left/right self.action[1] = ax[0] else: self.action[0] = 0 if abs(ax[1]) > 0.09: # right stick up/down self.action[0] = ax[1] else: self.action[1] = 0 if abs(ax[4]) > 0.09: # left stick up/down self.action[2] = -ax[4] else: self.action[2] = 0 butt, n_butt = glfw.get_joystick_buttons(jid) if butt[0] == 1: # X button self.action[3] = 1. if butt[1] == 1: # O button self.action[3] = -1.
def detect_input() -> Optional[Input]: for key in KEY_NAMES: if ig.is_key_down(key): return KeyboardKey(key) for joystick_index, joystick_id in get_joysticks(): axes_ptr, length = glfw.get_joystick_axes(joystick_index) axes = tuple(axes_ptr[i] for i in range(length)) if len(axes) > 0: max_axis = max(range(len(axes)), key=lambda i: abs(axes[i])) if abs(axes[max_axis]) > 0.5: return JoystickAxis(joystick_id, max_axis, 1 if axes[max_axis] > 0 else -1) buttons_ptr, length = glfw.get_joystick_buttons(joystick_index) buttons = tuple(buttons_ptr[i] for i in range(length)) for i, pressed in enumerate(buttons): if pressed: return JoystickButton(joystick_id, i) return None