Move window operations to Scene

This commit is contained in:
Grant Sanderson 2020-02-13 10:42:07 -08:00
parent c591954fc3
commit 183bae0825

View file

@ -38,7 +38,9 @@ class Scene(Container):
Container.__init__(self, **kwargs) Container.__init__(self, **kwargs)
if self.preview: if self.preview:
self.window = Window(self, **self.window_config) self.window = Window(self, **self.window_config)
self.camera_config["window"] = self.window self.camera_config["ctx"] = self.window.ctx
else:
self.window = None
self.camera = self.camera_class(**self.camera_config) self.camera = self.camera_class(**self.camera_config)
self.file_writer = SceneFileWriter(self, **self.file_writer_config) self.file_writer = SceneFileWriter(self, **self.file_writer_config)
@ -59,10 +61,6 @@ class Scene(Container):
pass pass
self.tear_down() self.tear_down()
# Is this what we want?
if self.preview:
self.update_until_closed()
def setup(self): def setup(self):
""" """
This is meant to be implement by any scenes which This is meant to be implement by any scenes which
@ -76,22 +74,29 @@ class Scene(Container):
pass pass
def tear_down(self): def tear_down(self):
self.skip_animations = False
if self.file_writer.save_last_frame:
self.update_frame()
self.file_writer.finish() self.file_writer.finish()
self.print_end_message() self.print_end_message()
def update_until_closed(self): if self.window:
self.interact()
def interact(self):
# If there is a window, enter a loop
# which updates the frame while under
# the hood calling the pyglet event loop
while not self.window.is_closing: while not self.window.is_closing:
now = time.time()
self.update_frame() self.update_frame()
time.sleep( self.window.destroy()
max(1 / 30 - (time.time() - now), 0)
)
def __str__(self): def __str__(self):
return self.__class__.__name__ return self.__class__.__name__
def print_end_message(self): def print_end_message(self):
print("Played {} animations".format(self.num_plays)) print(f"Played {self.num_plays} animations")
def set_variables_as_attrs(self, *objects, **newly_named_objects): def set_variables_as_attrs(self, *objects, **newly_named_objects):
""" """
@ -125,19 +130,22 @@ class Scene(Container):
mobjects = self.mobjects mobjects = self.mobjects
## ##
if self.window:
self.window.clear()
self.camera.clear() self.camera.clear()
self.camera.capture_mobjects(mobjects, excluded_mobjects=excluded_mobjects) self.camera.capture_mobjects(mobjects, excluded_mobjects=excluded_mobjects)
if self.window:
self.window.swap_buffers()
def emit_frame(self, dt): def emit_frame(self, dt):
self.increment_time(dt) self.increment_time(dt)
if not self.skip_animations: if not self.skip_animations:
self.file_writer.write_frame(self.camera) self.file_writer.write_frame(self.camera)
if self.preview: if self.window:
min_time_between_frames = 1 / self.camera.frame_rate frame_duration = 1 / self.camera.frame_rate
time_since_last = time.time() - self.time_of_last_frame t, dt = self.window.timer.next_frame()
time.sleep(max(0, min_time_between_frames - time_since_last)) time.sleep(np.clip(0, frame_duration - dt, frame_duration))
self.time_of_last_frame = time.time()
### ###
@ -344,12 +352,12 @@ class Scene(Container):
def handle_play_like_call(func): def handle_play_like_call(func):
def wrapper(self, *args, **kwargs): def wrapper(self, *args, **kwargs):
self.update_skipping_status() self.update_skipping_status()
allow_write = not self.skip_animations if not self.skip_animations:
if allow_write:
self.file_writer.begin_animation() self.file_writer.begin_animation()
func(self, *args, **kwargs) func(self, *args, **kwargs)
if allow_write:
self.file_writer.end_animation() self.file_writer.end_animation()
else:
func(self, *args, **kwargs)
self.num_plays += 1 self.num_plays += 1
return wrapper return wrapper
@ -500,7 +508,10 @@ class Scene(Container):
pass pass
def on_mouse_scroll(self, point, offset): def on_mouse_scroll(self, point, offset):
self.camera.frame.scale(1 + np.arctan(offset[1])) self.camera.frame.scale(
1 + np.arctan(3 * offset[1]),
about_point=point,
)
self.camera.refresh_shader_uniforms() self.camera.refresh_shader_uniforms()
def on_key_release(self, symbol, modifiers): def on_key_release(self, symbol, modifiers):