class LeapCrispyCursor: CURSOR_CHOICES = { 'path': CrispyPathCursor, 'pitch': CrispyPitchCursor, } def __init__(self, parsed_args, *args, **kwargs): self.parsed_args = parsed_args # Create a Leap controller and a listener self.controller = Controller() self.listener = self.CURSOR_CHOICES[self.parsed_args.mode]() self.listener.click_timeout = self.parsed_args.click self.listener.press_timeout = self.parsed_args.press def run(self): self.controller.add_listener(self.listener) try: while True: time.sleep(1000) except KeyboardInterrupt: pass finally: self.controller.remove_listener(self.listener) showmessage("Removing listener", Status.SUCCESS, Colors.GREEN) print ""
class Bounces(Application): def __init__(self, parser, rate): Application.__init__(self, parser) self.balls = [] self.rate = Rate(rate) def rand(): return choice([-1, 1]) * uniform(1. / rate, 10. / rate) for ball in range(4): self.balls.append( Ball(ball, randint(0, self.height - 1), randint(0, self.width - 1), self.height, self.width, Ball.colors[ball % len(Ball.colors)], rand(), rand())) # Motion control via Leap Motion self.swipe = [None] self.swipe_lock = RLock() if leapmotion: self.leap_listener = SampleListener(self.swipe, self.swipe_lock) self.controller = Controller() self.controller.add_listener(self.leap_listener) def close(self, reason='unknown'): Application.close(self, reason) if leapmotion: self.controller.remove_listener(self.leap_listener) def render(self): with self.model: self.model.set_all('black') with self.swipe_lock: for ball in self.balls: self.model.set_pixel(ball.x, ball.y, ball.color) if self.swipe[0] is not None: # mapping axes (height, width) of Arbalet on axes (x, z) of Leap Motion x_speed_boost = self.swipe[0].direction[ 0] * self.swipe[0].speed / 500. y_speed_boost = self.swipe[0].direction[ 2] * self.swipe[0].speed / 500. ball.x_speed += x_speed_boost ball.y_speed += y_speed_boost self.swipe[0] = None def run(self): while True: for ball in self.balls: ball.step_forward() self.render() self.rate.sleep()
class Bounces(Application): def __init__(self, parser, rate): Application.__init__(self, parser) self.balls = [] self.rate = Rate(rate) def rand(): return choice([-1, 1])*uniform(1./rate, 10./rate) for ball in range(4): self.balls.append(Ball(ball, randint(0, self.height-1), randint(0, self.width-1), self.height, self.width, Ball.colors[ball%len(Ball.colors)], rand(), rand())) # Motion control via Leap Motion self.swipe = [None] self.swipe_lock = RLock() if leapmotion: self.leap_listener = SampleListener(self.swipe, self.swipe_lock) self.controller = Controller() self.controller.add_listener(self.leap_listener) def close(self, reason='unknown'): Application.close(self, reason) if leapmotion: self.controller.remove_listener(self.leap_listener) def render(self): with self.model: self.model.set_all('black') with self.swipe_lock: for ball in self.balls: self.model.set_pixel(ball.x, ball.y, ball.color) if self.swipe[0] is not None: # mapping axes (height, width) of Arbalet on axes (x, z) of Leap Motion x_speed_boost = self.swipe[0].direction[0] * self.swipe[0].speed / 500. y_speed_boost = self.swipe[0].direction[2] * self.swipe[0].speed / 500. ball.x_speed += x_speed_boost ball.y_speed += y_speed_boost self.swipe[0] = None def run(self): while True: for ball in self.balls: ball.step_forward() self.render() self.rate.sleep()