class Cursor(Actor): """Block that takes input from the detector and passes this to the BlockManager""" def __init__(self, position, listener, activator, index=0): super(Cursor, self).__init__(position) self.listener = listener self.activator = activator self.index = index self.key = None self.set_image(game.get_image('cursor_block')) self.text = Line((64, 72), self.key, game.get_font('screen_large')) self.text.set_color(pg.Color(62, 255, 4)) self.start_pos = np.array(position) self.state = 'input' self.state_time = 0.0 self.progress_time = 0 self.load_icon = AnimatedActor((64, 64), mode='custom') self.load_icon.set_animation(game.get_animation('load'), (64, 64), 20) self.load_icon_rect = self.load_icon.surface.get_rect() game.key_down.subscribe(self.key_down) def update(self, delta): super(Cursor, self).update(delta) if self.state == 'input': if self.activator() == self.key: if self.progress_time < 1: self.progress_time += delta else: self.progress_time = 0 self.listener(self.index) else: if self.progress_time > 0: self.progress_time -= delta if self.progress_time < 0: self.progress_time = 0 elif self.progress_time > 1: self.progress_time = 1 elif self.state == 'change_mode': self.position[1] = self.start_pos[1] + 16 * np.sin( 2 * np.pi * self.state_time) if self.state_time >= 0.5: self.position = self.start_pos.copy() self.set_state('wait') self.load_icon.frame_time = self.progress_time self.load_icon.update(delta) self.text.update(delta) self.state_time += delta def draw(self, surface): if not self.is_active: return self.rect.center = self.position self.surface.fill(pg.Color(0, 0, 0, 0)) self.surface.blit(self.image, self.image_rect) self.text.draw(self.surface) self.load_icon.draw(self.surface) surface.blit(self.surface, self.rect) # TODO: Create detector activator def key_down(self, key): if self.state == 'input': if key == ord(self.key): self.listener(self.index) def set_key(self, key): self.key = key self.text.set_text(str(key)) def set_state(self, new_state): previous_state = self.state self.state = new_state self.state_time = 0
class HandScreen(Button): def __init__(self, position): super(Button, self).__init__(position) self.button_up_image = game.get_image('screen_empty') self.button_down_image = game.get_image('screen_active') self.set_image(self.button_up_image) self.is_pressed = False self.wait_time = 0 self.hand_surface = pg.Surface((256, 256)) self.hand_rect = self.hand_surface.get_rect() self.label_text = Line((315, 450), '?', game.get_font('screen_small')) self.label_text.set_color(pg.Color(50, 240, 0)) def update(self, delta): super(Button, self).update(delta) if self.cursor_over(): if not self.is_pressed: self.press() elif self.is_pressed: self.unpress() if self.is_pressed: self.label_text.set_text(detect.detector.get_current_pose()) self.label_text.update(delta) def draw(self, surface): if not self.is_active: return self.rect.center = self.position self.hand_rect.center = (256, 192) self.surface.fill(pg.Color(0, 0, 0, 0)) if self.is_pressed: frame = detect.detector.get_fixed_frame( (256, 256), self.position[::-1], (256, 256)) pg.surfarray.blit_array(self.hand_surface, frame) self.surface.blit(self.hand_surface, self.hand_rect) self.surface.blit(self.image, self.image_rect) for child in self.children: child.draw(surface) if self.is_pressed: self.label_text.draw(self.surface) surface.blit(self.surface, self.rect) def cursor_over(self): cursor_position = game.get_cursor_position() return ( cursor_position[0] >= self.position[0] - self.image.get_width() / 2 and cursor_position[0] <= self.position[0] + self.image.get_width() / 2 and cursor_position[1] >= self.position[1] - self.image.get_height() / 2 and cursor_position[1] <= self.position[1] + self.image.get_height() / 2) def press(self): self.is_pressed = True self.set_image(self.button_down_image) game.cursor_down(type(self)) def get_pose(self): if self.is_pressed: return detect.detector.get_current_pose() else: return None