def set_cursor_position(self, x, y, cursor, limit): """ Moves cursor image. """ w = limit[2] - (cursor.get_allocation().width * 0.5) h = limit[3] - (cursor.get_allocation().height * 0.5) x = x / float(STICK_PAD_MAX) y = y / float(STICK_PAD_MAX) * -1.0 x, y = circle_to_square(x, y) x = clamp( cursor.get_allocation().width * 0.5, (limit[0] + w * 0.5) + x * w * 0.5, self.get_allocation().width - cursor.get_allocation().width ) - cursor.get_allocation().width * 0.5 y = clamp( cursor.get_allocation().height * 0.5, (limit[1] + h * 0.5) + y * h * 0.5, self.get_allocation().height - cursor.get_allocation().height ) - cursor.get_allocation().height * 0.5 cursor.position = int(x), int(y) self.f.move(cursor, *cursor.position) for a in self.background.areas: if a.contains(x, y): if a != self._hovers[cursor]: self._hovers[cursor] = a if self._pressed[cursor] is not None: self.mapper.keyboard.releaseEvent([ self._pressed[cursor] ]) self.key_from_cursor(cursor, True) if not self.timer_active('redraw'): self.timer('redraw', 0.01, self.redraw_background) break
def set_cursor_position(self, x, y, cursor, limit): """ Moves cursor image. """ w = limit[2] - (cursor.get_allocation().width * 0.5) h = limit[3] - (cursor.get_allocation().height * 0.5) x = x / float(STICK_PAD_MAX) y = y / float(STICK_PAD_MAX) * -1.0 x, y = circle_to_square(x, y) x = clamp(cursor.get_allocation().width * 0.5, (limit[0] + w * 0.5) + x * w * 0.5, self.get_allocation().width - cursor.get_allocation().width ) - cursor.get_allocation().width * 0.5 y = clamp( cursor.get_allocation().height * 0.5, (limit[1] + h * 0.5) + y * h * 0.5, self.get_allocation().height - cursor.get_allocation().height ) - cursor.get_allocation().height * 0.5 cursor.position = int(x), int(y) self.f.move(cursor, *cursor.position) for a in self.background.areas: if a.contains(x, y): if a != self._hovers[cursor]: self._hovers[cursor] = a if self._pressed[cursor] is not None: self.mapper.keyboard.releaseEvent( [self._pressed[cursor]]) self.key_from_cursor(cursor, True) if not self.timer_active('redraw'): self.timer('redraw', 0.01, self.redraw_background) break
def _move_cursor(self, cursor, x, y): if (x, y) == (0, 0): cursor.set_visible(False) return cursor.set_visible(True) pad_w = cursor.get_allocation().width * 0.5 pad_h = cursor.get_allocation().height * 0.5 max_w = self.grid.get_allocation().width - 2 * pad_w max_h = self.grid.get_allocation().height - 2 * pad_h x, y = circle_to_square(x / (STICK_PAD_MAX * 2.0), y / (STICK_PAD_MAX * 2.0)) x = clamp(pad_w, (pad_w + max_w) * 0.5 + x * max_w, max_w - pad_w) y = clamp(pad_h, (pad_h + max_h) * 0.5 + y * max_h * -1, max_h - pad_h) x += self.grid.get_allocation().x y += self.grid.get_allocation().y self.f.move(cursor, int(x), int(y)) for i in self._buttons: if point_in_gtkrect(i.get_allocation(), x, y): if cursor.selected: cursor.selected.set_name("osd-key-buton") cursor.selected = i cursor.selected.set_name("osd-key-buton-hilight") break
def set_cursor_position(self, x, y, cursor, limit): """ Moves cursor image. """ if cursor not in self._hovers: return w = limit[2] - (cursor.get_allocation().width * 0.5) h = limit[3] - (cursor.get_allocation().height * 0.5) x = x / float(STICK_PAD_MAX) y = y / float(STICK_PAD_MAX) * -1.0 x, y = circle_to_square(x, y) x = clamp(cursor.get_allocation().width * 0.5, (limit[0] + w * 0.5) + x * w * 0.5, self.get_allocation().width - cursor.get_allocation().width) y = clamp( cursor.get_allocation().height * 0.5, (limit[1] + h * 0.5) + y * h * 0.5, self.get_allocation().height - cursor.get_allocation().height) cursor.position = int(x), int(y) self.f.move(cursor, x - cursor.get_allocation().width * 0.5, y - cursor.get_allocation().height * 0.5) for button in self.background.buttons: if button.contains(x, y): if button != self._hovers[cursor]: self._hovers[cursor] = button if self._pressed[cursor] is not None: self.mapper.keyboard.releaseEvent( [self._pressed[cursor]]) self.key_from_cursor(cursor, True) if not self.timer_active('update'): self.timer('update', 0.01, self.update_background) break
def whole(self, mapper, x, y, what): if self._enabled: if (x, y) == (0, 0): # Pad was released self._enabled = False self._on_finished(self, "".join(self._result)) return else: # Convert positions on pad to position on grid if what == CPAD: x = clamp(0, float(x) / (CPAD_X_MAX - CPAD_MIN), 1.0) y = clamp(0, float(y) / (CPAD_Y_MAX - CPAD_MIN), 1.0) x *= self._resolution y *= self._resolution else: x -= STICK_PAD_MIN y = STICK_PAD_MAX - y x = float(x) / (float(STICK_PAD_MAX - STICK_PAD_MIN) / self._resolution) y = float(y) / (float(STICK_PAD_MAX - STICK_PAD_MIN) / self._resolution) # Check for deadzones around grid lines for i in xrange(1, self._resolution): if x > i - self._deadzone and x < i + self._deadzone: return if y > i - self._deadzone and y < i + self._deadzone: return # Round x = clamp(0, int(x), self._resolution - 1) y = clamp(0, int(y), self._resolution - 1) if self._positions: ox, oy = self._positions[-1] if (x, y) != (ox, oy): self._positions.append((x, y)) while (x, y) != (ox, oy): if x < ox: self._result.append(self.LEFT) x += 1 elif x > ox: self._result.append(self.RIGHT) x -= 1 elif y < oy: self._result.append(self.UP) y += 1 elif y > oy: self._result.append(self.DOWN) y -= 1 else: self._positions.append((x, y))
def mode_LINEAR(self, x, y, range): """ Input value is scaled, so entire output range is covered by reduced input range of deadzone. """ if y == 0: # Small optimalization for 1D input, for example trigger return copysign( clamp(0, ((x - self.lower) / (self.upper - self.lower)) * range, range), x), 0 distance = clamp(self.lower, sqrt(x * x + y * y), self.upper) distance = (distance - self.lower) / (self.upper - self.lower) * range angle = atan2(x, y) return distance * sin(angle), distance * cos(angle)
def gyro(self, mapper, pitch, yaw, roll, q1, q2, q3, q4): pyr = list(quat2euler(q1 / 32768.0, q2 / 32768.0, q3 / 32768.0, q4 / 32768.0)) if self.ir is None: # TODO: Move this to controller and allow some way to reset it self.ir = pyr[2] # Covert what quat2euler returns to what controller can use for i in (0, 1): pyr[i] = pyr[i] * (2**15) * self.speed[i] * 2 / PI pyr[2] = anglediff(self.ir, pyr[2]) * (2**15) * self.speed[2] * 2 / PI # Restrict to acceptablle range if self.haptic: oor = False # oor - Out Of Range for i in (0, 1, 2): pyr[i] = int(pyr[i]) if pyr[i] > STICK_PAD_MAX: pyr[i] = STICK_PAD_MAX oor = True elif pyr[i] < STICK_PAD_MIN: pyr[i] = STICK_PAD_MIN oor = True if oor: if not self._was_oor: mapper.send_feedback(self.haptic) self._was_oor = True else: self._was_oor = False else: for i in (0, 1, 2): pyr[i] = int(clamp(STICK_PAD_MIN, pyr[i], STICK_PAD_MAX)) # print "% 12.0f, % 12.0f, % 12.5f" % (p,y,r) for i in (0, 1, 2): axis = self.axes[i] if axis in Axes: mapper.gamepad.axisEvent(axis, AxisAction.clamp_axis(axis, pyr[i] * self.speed[i])) mapper.syn_list.add(mapper.gamepad)
def gyro(self, mapper, pitch, yaw, roll, q1, q2, q3, q4): pyr = list(quat2euler(q1 / 32768.0, q2 / 32768.0, q3 / 32768.0, q4 / 32768.0)) if self.ir is None: # TODO: Move this to controller and allow some way to reset it self.ir = pyr[2] # Covert what quat2euler returns to what controller can use for i in (0, 1): pyr[i] = pyr[i] * (2**15) * self.speed[i] * 2 / PI pyr[2] = anglediff(self.ir, pyr[2]) * (2**15) * self.speed[2] * 2 / PI # Restrict to acceptablle range if self.haptic: oor = False # oor - Out Of Range for i in (0, 1, 2): pyr[i] = int(pyr[i]) if pyr[i] > STICK_PAD_MAX: pyr[i] = STICK_PAD_MAX oor = True elif pyr[i] < STICK_PAD_MIN: pyr[i] = STICK_PAD_MIN oor = True if oor: if not self._was_oor: mapper.send_feedback(self.haptic) self._was_oor = True else: self._was_oor = False else: for i in (0, 1, 2): pyr[i] = int(clamp(STICK_PAD_MIN, pyr[i], STICK_PAD_MAX)) # print "% 12.0f, % 12.0f, % 12.5f" % (p,y,r) for i in (0, 1, 2): axis = self.axes[i] if axis in Axes: mapper.gamepad.axisEvent(axis, clamp_axis(axis, pyr[i] * self.speed[i])) mapper.syn_list.add(mapper.gamepad)
def mode_LINEAR(self, x, y, range): """ Input value is scaled, so entire output range is covered by reduced input range of deadzone. """ if y == 0: # Small optimalization for 1D input, for example trigger return copysign( clamp( 0, ((x - self.lower) / (self.upper - self.lower)) * range, range), x ), 0 distance = clamp(self.lower, sqrt(x*x + y*y), self.upper) distance = (distance - self.lower) / (self.upper - self.lower) * range angle = atan2(x, y) return distance * sin(angle), distance * cos(angle)
def on_event(self, daemon, what, data): if self._submenu: return self._submenu.on_event(daemon, what, data) if what == self._control_with: x, y = data if self._use_cursor: # Special case, both confirm_with and cancel_with # can be set to STICK if self._cancel_with == STICK and self._control_with == STICK: if self._control_equals_cancel(daemon, x, y): return pad_w = self.cursor.get_allocation().width * 0.5 pad_h = self.cursor.get_allocation().height * 0.5 max_w = self.get_allocation().width - 2 * pad_w max_h = self.get_allocation().height - 2 * pad_h x, y = circle_to_square(x / (STICK_PAD_MAX * 2.0), y / (STICK_PAD_MAX * 2.0)) x = clamp(pad_w, (pad_w + max_w) * 0.5 + x * max_w, max_w - pad_w) y = clamp(pad_h, (pad_h + max_h) * 0.5 + y * max_h * -1, max_h - pad_h) self.f.move(self.cursor, int(x), int(y)) for i in self.items: if point_in_gtkrect(i.widget.get_allocation(), x, y): self.select(self.items.index(i)) else: self._scon.set_stick(x, y) elif what == self._cancel_with: if data[0] == 0: # Button released self.quit(-1) elif what == self._confirm_with: if data[0] == 0: # Button released if self._selected and self._selected.callback: self._selected.callback(self, self.daemon, self.controller, self._selected) elif self._selected: self.quit(0) else: self.quit(-1)
def on_event(self, daemon, what, data): if self._submenu: return self._submenu.on_event(daemon, what, data) if what == self._control_with or what == "LEFT" and self._control_with_dpad: x, y = data if self._use_cursor: # Special case, both confirm_with and cancel_with # can be set to STICK if self._cancel_with == STICK and self._control_with == STICK: if self._control_equals_cancel(daemon, x, y): return pad_w = self.cursor.get_allocation().width * 0.5 pad_h = self.cursor.get_allocation().height * 0.5 max_w = self.get_allocation().width - 2 * pad_w max_h = self.get_allocation().height - 2 * pad_h x, y = circle_to_square(x / (STICK_PAD_MAX * 2.0), y / (STICK_PAD_MAX * 2.0)) x = clamp(pad_w, (pad_w + max_w) * 0.5 + x * max_w, max_w - pad_w) y = clamp(pad_h, (pad_h + max_h) * 0.5 + y * max_h * -1, max_h - pad_h) self.f.move(self.cursor, int(x), int(y)) for i in self.items: if point_in_gtkrect(i.widget.get_allocation(), x, y): self.select(self.items.index(i)) else: self._scon.set_stick(x, y) elif what == self._cancel_with: if data[0] == 0: # Button released self.quit(-1) elif what == self._confirm_with: if data[0] == 0: # Button released if self._selected and self._selected.callback: self._selected.callback(self, self.daemon, self.controller, self._selected) elif self._selected: self.quit(0) else: self.quit(-1)
def set_cursor_position(self, x, y, cursor, limit): """ Moves cursor image. """ if cursor not in self._hovers: return w = limit[2] - (cursor.get_allocation().width * 0.5) h = limit[3] - (cursor.get_allocation().height * 0.5) x = x / float(STICK_PAD_MAX) y = y / float(STICK_PAD_MAX) * -1.0 x, y = circle_to_square(x, y) x = clamp( cursor.get_allocation().width * 0.5, (limit[0] + w * 0.5) + x * w * 0.5, self.get_allocation().width - cursor.get_allocation().width ) y = clamp( cursor.get_allocation().height * 0.5, (limit[1] + h * 0.5) + y * h * 0.5, self.get_allocation().height - cursor.get_allocation().height ) cursor.position = int(x), int(y) self.f.move(cursor, x - cursor.get_allocation().width * 0.5, y - cursor.get_allocation().height * 0.5) for button in self.background.buttons: if button.contains(x, y): if button != self._hovers[cursor]: self._hovers[cursor] = button if self._pressed[cursor] is not None: self.mapper.keyboard.releaseEvent([ self._pressed[cursor] ]) self.key_from_cursor(cursor, True) if not self.timer_active('update'): self.timer('update', 0.01, self.update_background) break
def _move_cursor(self, cursor, x, y): if (x, y) == (0, 0): cursor.set_visible(False) return cursor.set_visible(True) pad_w = cursor.get_allocation().width * 0.5 pad_h = cursor.get_allocation().height * 0.5 max_w = self.grid.get_allocation().width - 2 * pad_w max_h = self.grid.get_allocation().height - 2 * pad_h x, y = circle_to_square(x / (STICK_PAD_MAX * 2.0), y / (STICK_PAD_MAX * 2.0)) x = clamp(pad_w, (pad_w + max_w) * 0.5 + x * max_w, max_w - pad_w) y = clamp(pad_h, (pad_h + max_h) * 0.5 + y * max_h * -1, max_h - pad_h) x += self.grid.get_allocation().x y += self.grid.get_allocation().y self.f.move(cursor, int(x), int(y)) for i in self._buttons: if point_in_gtkrect(i.get_allocation(), x, y): if cursor.selected: cursor.selected.set_name("osd-key-buton") cursor.selected = i cursor.selected.set_name("osd-key-buton-hilight") break
def __init__(self, *stuff): OSDEnabledAction.__init__(self) Action.__init__(self, *stuff) self.gestures = {} self.precision = self.DEFAULT_PRECISION gstr = None if len(stuff) > 0 and type(stuff[0]) in (int, float): self.precision = clamp(0.0, float(stuff[0]), 1.0) stuff = stuff[1:] for i in stuff: if gstr is None and type(i) in (str, unicode): gstr = i elif gstr is not None and isinstance(i, Action): self.gestures[gstr] = i gstr = None else: raise ValueError("Invalid parameter for '%s': unexpected %s" % (self.COMMAND, i))
def input(self, *a): new_state = self._state need_cancel_padpressemu = False try: for event in self.device.read(): if event.type == ecodes.EV_KEY and event.code in self._dpad_map: cal = self._calibrations[event.code] if event.value: if self._dpad_map[event.code]: # Positive value = STICK_PAD_MAX else: value = STICK_PAD_MIN cal = self._calibrations[event.code] value = int(value * cal.scale * STICK_PAD_MAX) else: value = 0 axis = self._axis_map[event.code] if not new_state.buttons & SCButtons.LPADTOUCH and axis in ( "lpad_x", "lpad_y"): b = new_state.buttons | SCButtons.LPAD | SCButtons.LPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{axis: value}) elif not new_state.buttons & SCButtons.RPADTOUCH and axis in ( "rpad_x", "rpad_y"): b = new_state.buttons | SCButtons.RPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{axis: value}) else: new_state = new_state._replace(**{axis: value}) elif event.type == ecodes.EV_KEY and event.code in self._button_map: if event.value: b = new_state.buttons | self._button_map[event.code] new_state = new_state._replace(buttons=b) else: b = new_state.buttons & ~self._button_map[event.code] new_state = new_state._replace(buttons=b) elif event.type == ecodes.EV_KEY and event.code in self._axis_map: axis = self._axis_map[event.code] if event.value: new_state = new_state._replace(**{axis: TRIGGER_MAX}) else: new_state = new_state._replace(**{axis: TRIGGER_MIN}) elif event.type == ecodes.EV_ABS and event.code in self._axis_map: cal = self._calibrations[event.code] value = (float(event.value) * cal.scale) + cal.offset if value >= -cal.deadzone and value <= cal.deadzone: value = 0 else: value = clamp(cal.clamp_min, int(value * cal.clamp_max), cal.clamp_max) axis = self._axis_map[event.code] if not new_state.buttons & SCButtons.LPADTOUCH and axis in ( "lpad_x", "lpad_y"): b = new_state.buttons | SCButtons.LPAD | SCButtons.LPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{axis: value}) elif not new_state.buttons & SCButtons.RPADTOUCH and axis in ( "rpad_x", "rpad_y"): b = new_state.buttons | SCButtons.RPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{axis: value}) else: new_state = new_state._replace(**{axis: value}) except IOError, e: # TODO: Maybe check e.errno to determine exact error # all of them are fatal for now log.error(e) _evdevdrv.device_removed(self.device)
def change(self, mapper, dx, dy): """ Called from BallModifier """ self.axis(mapper, clamp(STICK_PAD_MIN, dx, STICK_PAD_MAX), None)
def __init__(self, brightness): Action.__init__(self, brightness) self.brightness = clamp(0, int(brightness), 100)
def change(self, mapper, dx, dy): """ Called from BallModifier """ self.axis(mapper, clamp(STICK_PAD_MIN, dx, STICK_PAD_MAX), None)
def input(self, *a): new_state = self._state need_cancel_padpressemu = False try: for event in self.device.read(): if event.type == ecodes.EV_KEY and event.code in self._dpad_map: cal = self._calibrations[event.code] if event.value: if self._dpad_map[event.code]: # Positive value = STICK_PAD_MAX else: value = STICK_PAD_MIN cal = self._calibrations[event.code] value = int(value * cal.scale * STICK_PAD_MAX) else: value = 0 axis = self._axis_map[event.code] if not new_state.buttons & SCButtons.LPADTOUCH and axis in ("lpad_x", "lpad_y"): b = new_state.buttons | SCButtons.LPAD | SCButtons.LPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{ axis : value }) elif not new_state.buttons & SCButtons.RPADTOUCH and axis in ("rpad_x", "rpad_y"): b = new_state.buttons | SCButtons.RPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{ axis : value }) else: new_state = new_state._replace(**{ axis : value }) elif event.type == ecodes.EV_KEY and event.code in self._button_map: if event.value: b = new_state.buttons | self._button_map[event.code] new_state = new_state._replace(buttons=b) else: b = new_state.buttons & ~self._button_map[event.code] new_state = new_state._replace(buttons=b) elif event.type == ecodes.EV_KEY and event.code in self._axis_map: axis = self._axis_map[event.code] if event.value: new_state = new_state._replace(**{ axis : TRIGGER_MAX }) else: new_state = new_state._replace(**{ axis : TRIGGER_MIN }) elif event.type == ecodes.EV_ABS and event.code in self._axis_map: cal = self._calibrations[event.code] value = (float(event.value) * cal.scale) + cal.offset if value >= -cal.deadzone and value <= cal.deadzone: value = 0 else: value = clamp(cal.clamp_min, int(value * cal.clamp_max), cal.clamp_max) axis = self._axis_map[event.code] if not new_state.buttons & SCButtons.LPADTOUCH and axis in ("lpad_x", "lpad_y"): b = new_state.buttons | SCButtons.LPAD | SCButtons.LPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{ axis : value }) elif not new_state.buttons & SCButtons.RPADTOUCH and axis in ("rpad_x", "rpad_y"): b = new_state.buttons | SCButtons.RPADTOUCH need_cancel_padpressemu = True new_state = new_state._replace(buttons=b, **{ axis : value }) else: new_state = new_state._replace(**{ axis : value }) except IOError, e: # TODO: Maybe check e.errno to determine exact error # all of them are fatal for now log.error(e) _evdevdrv.device_removed(self.device.fn)