class StillCanvas(app.Canvas): def __init__(self, *args, **kwargs): self.exit = kwargs.pop('exit', False) self.inputs_path = kwargs.pop('inputs_path', None) super(StillCanvas, self).__init__(*args, **kwargs) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.inputs = None if self.inputs_path is not None: self.input_manager = FileStoredInput(self.inputs_path) self.time = self.input_manager.stored_time definition = Definitions[self.input_manager.stored_definition_name] else: self.input_manager = RandomInput() self.time = float(random.randint(0, 345212312)) definition = Definitions[random.choice(Definitions.keys())] self.fractal = FractalProgram(definition, mask=False) self.apply_zoom() self._timer = app.Timer(1.0 / 5, connect=self.update, start=True) if self.exit: app.Timer(1, connect=self.write_and_exit, start=True, iterations=1) # self.update(None) # self.update(None) self.show() # self.write_and_exit() def write(self): image = _screenshot(alpha=True) write_png('covers2/%s.png' % self.time, image) data = {} data.update(self.input_manager.smoothed_inputs) data['time'] = self.time data['definition'] = self.fractal.definition['name'] with open('covers2/%s.yml' % self.time, 'w') as f: yaml.dump(data, stream=f) print "Wrote image and data for %s" % self.time def write_and_exit(self, event=None): self.write() sys.exit(0) def on_draw(self, event): self.input_manager.time = self.time self.fractal['time'] = self.time self.fractal.draw() self.inputs = self.input_manager.inputs(self.time) self.fractal.adjust(self.inputs) def on_resize(self, event): self.apply_zoom() def apply_zoom(self): width, height = self.physical_size gloo.set_viewport(0, 0, width, height) self.fractal['resolution'] = [width, height] def on_key_press(self, event): if event.text == 'w': self.write() else: self.input_manager.on_key_press(event) print self.inputs self.update()
class MainCanvas(app.Canvas): def __init__(self, *args, **kwargs): self.fake_inputs = kwargs.pop('fake_inputs', False) self.draw_bones = kwargs.pop('draw_bones', False) self.kiosk_interval = kwargs.pop('kiosk_interval', 0) self.start_definition = kwargs.pop('start_definition', 0) self.start_input = kwargs.pop('start_input', 0) self.show_mask = kwargs.pop('mask', False) super(MainCanvas, self).__init__(*args, **kwargs) gloo.set_state(clear_color='black', blend=True, blend_func=('src_alpha', 'one_minus_src_alpha')) self.skeleton_bones = SkeletonBonesProgram() self.mask = MaskProgram() self.inputs = None self.input_manager = None self._starttime = time.time() self.definition_position = self.start_definition self.input_position = self.start_input self.rotate() if self.kiosk_interval > 0: self.kiosk_timer = app.Timer(self.kiosk_interval, connect=self.rotate, start=True) self._timer = app.Timer('auto', connect=self.update, start=True) self.show() def rotate(self, event=None): definition = Definitions[Definitions.keys()[self.definition_position % len(Definitions.keys())]] self.fractal = FractalProgram(definition, mask=self.show_mask) if self.inputs: self.fractal.adjust(self.inputs) self.apply_zoom() # Only rotate the user tracker if there aren't users actively interacting if (not self.input_manager) or not self.input_manager.tracking_users: if self.fake_inputs: self.input_manager = FakeInput() else: input_manager_index = (self.input_position / 2) % len(KIOSK_INPUTS) # random.choice(range(len(KIOSK_INPUTS))) self.input_manager = KIOSK_INPUTS[input_manager_index]() self.mask['tipColorSelector'] = input_manager_index / float(len(KIOSK_INPUTS)) print "Rotated to %s feeding %s" % (self.input_manager, definition['name']) self.definition_position += 1 self.input_position += 1 def on_draw(self, event): elapsed = time.time() - self._starttime self.input_manager.time = elapsed self.fractal['time'] = elapsed self.fractal.draw() self.inputs = self.input_manager.inputs(elapsed) self.fractal.adjust(self.inputs) if not self.fake_inputs and self.draw_bones and hasattr(self.input_manager, 'user_tracker'): self.skeleton_bones.draw(self.input_manager.user_tracker.read_frame()) if self.show_mask: self.mask.draw() def on_resize(self, event): self.apply_zoom() def apply_zoom(self): width, height = self.physical_size gloo.set_viewport(0, 0, width, height) self.fractal['resolution'] = [width, height] self.skeleton_bones['resolution'] = [width, height] self.mask['resolution'] = [width, height] def on_key_press(self, event): self.input_manager.on_key_press(event)