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_axes(joy): axes = glfw.get_joystick_axes(joy) #print(axes) toReturn = [0.] * axes[1] for i in range(axes[1]): toReturn[i] = axes[0][i] return toReturn
def getNumAxes(self): """Return the number of joystick axes found. """ if backend == 'pyglet': return len(self.getAllAxes()) elif backend == 'glfw': _, count = glfw.get_joystick_axes(self._device) return count else: return self._device.get_numaxes()
def find_joysticks(): joylist = [] for joy in range(15): if glfw.joystick_present(joy): axes = glfw.get_joystick_axes(joy) sum = 2. for i in range(axes[1]): sum += axes[0][i] if abs(sum) > .000001: print('Found Joy %d' % (joy + 1)) joylist.append(Joystick(joy)) return joylist
def getAxis(self, axisId): """Get the value of an axis by an integer id. (from 0 to number of axes - 1) """ if backend == 'pyglet': val = self.getAllAxes()[axisId] if val is None: val = 0 return val elif backend == 'glfw': val, _ = glfw.get_joystick_axes(self._device) return val[axisId] else: return self._device.get_axis(axisId)
def getAllAxes(self): """Get a list of all current axis values.""" axes = [] if backend == 'pyglet': names = ['x', 'y', 'z', 'rx', 'ry', 'rz', ] for axName in names: if hasattr(self._device, axName): axes.append(getattr(self._device, axName)) elif backend == 'glfw': _axes, count = glfw.get_joystick_axes(self._device) for i in range(count): axes.append(_axes[i]) else: for id in range(self._device.get_numaxes()): axes.append(self._device.get_axis(id)) return axes
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
glViewport(0, 0, width, height) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ms.setToIdentityMatrix(ms.MatrixStack.model) ms.setToIdentityMatrix(ms.MatrixStack.view) ms.setToIdentityMatrix(ms.MatrixStack.projection) # render scene width, height = glfw.get_framebuffer_size(window) glViewport(0, 0, width, height) glClearColor(0.0, 0.0, 0.0, 1.0) # r # g # b # a glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) handle_inputs() axes_list = glfw.get_joystick_axes(glfw.JOYSTICK_1) if len(axes_list) >= 1 and axes_list[0]: if math.fabs(float(axes_list[0][0])) > 0.19: camera.x += 10.0 * axes_list[0][0] * math.cos(camera.rot_y) camera.z -= 10.0 * axes_list[0][0] * math.sin(camera.rot_y) if math.fabs(float(axes_list[0][1])) > 0.19: camera.x += 10.0 * axes_list[0][1] * math.sin(camera.rot_y) camera.z += 10.0 * axes_list[0][1] * math.cos(camera.rot_y) if math.fabs(axes_list[0][3]) > 0.19: camera.rot_y -= 3.0 * axes_list[0][3] * 0.01 if math.fabs(axes_list[0][4]) > 0.19: camera.rot_x += axes_list[0][4] * 0.01 # note - opengl matricies use degrees ms.rotate_x(ms.MatrixStack.view, -camera.rot_x)