2020-02-11 19:51:19 -08:00
|
|
|
import moderngl_window as mglw
|
|
|
|
from moderngl_window.context.pyglet.window import Window as PygletWindow
|
2020-02-13 10:50:38 -08:00
|
|
|
from moderngl_window.timers.clock import Timer
|
2020-02-11 19:51:19 -08:00
|
|
|
|
2020-02-13 10:50:38 -08:00
|
|
|
from manimlib.utils.config_ops import digest_config
|
2020-02-11 19:51:19 -08:00
|
|
|
|
|
|
|
|
|
|
|
class Window(PygletWindow):
|
|
|
|
fullscreen = False
|
|
|
|
resizable = True
|
|
|
|
gl_version = (3, 3)
|
|
|
|
vsync = True
|
|
|
|
samples = 1
|
|
|
|
cursor = True
|
|
|
|
|
|
|
|
def __init__(self, scene, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
2021-01-02 20:47:51 -08:00
|
|
|
digest_config(self, kwargs)
|
2020-02-11 19:51:19 -08:00
|
|
|
self.scene = scene
|
2020-02-13 10:50:38 -08:00
|
|
|
self.title = str(scene)
|
2021-01-02 20:47:51 -08:00
|
|
|
if "position" in kwargs:
|
|
|
|
self.position = kwargs["position"]
|
2020-02-13 10:50:38 -08:00
|
|
|
|
2021-01-06 12:47:13 -08:00
|
|
|
self.pressed_keys = set()
|
|
|
|
|
2020-02-11 19:51:19 -08:00
|
|
|
mglw.activate_context(window=self)
|
2020-02-13 10:50:38 -08:00
|
|
|
self.timer = Timer()
|
|
|
|
self.config = mglw.WindowConfig(ctx=self.ctx, wnd=self, timer=self.timer)
|
|
|
|
self.timer.start()
|
2020-02-11 19:51:19 -08:00
|
|
|
|
|
|
|
# Delegate event handling to scene
|
|
|
|
def pixel_coords_to_space_coords(self, px, py, relative=False):
|
|
|
|
return self.scene.camera.pixel_coords_to_space_coords(px, py, relative)
|
|
|
|
|
|
|
|
def on_mouse_motion(self, x, y, dx, dy):
|
|
|
|
super().on_mouse_motion(x, y, dx, dy)
|
|
|
|
point = self.pixel_coords_to_space_coords(x, y)
|
|
|
|
d_point = self.pixel_coords_to_space_coords(dx, dy, relative=True)
|
|
|
|
self.scene.on_mouse_motion(point, d_point)
|
|
|
|
|
|
|
|
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
|
|
|
|
super().on_mouse_drag(x, y, dx, dy, buttons, modifiers)
|
|
|
|
point = self.pixel_coords_to_space_coords(x, y)
|
|
|
|
d_point = self.pixel_coords_to_space_coords(dx, dy, relative=True)
|
2020-02-14 10:52:39 -08:00
|
|
|
self.scene.on_mouse_drag(point, d_point, buttons, modifiers)
|
2020-02-11 19:51:19 -08:00
|
|
|
|
|
|
|
def on_mouse_press(self, x: int, y: int, button, mods):
|
|
|
|
super().on_mouse_press(x, y, button, mods)
|
|
|
|
point = self.pixel_coords_to_space_coords(x, y)
|
|
|
|
self.scene.on_mouse_press(point, button, mods)
|
|
|
|
|
|
|
|
def on_mouse_release(self, x: int, y: int, button, mods):
|
|
|
|
super().on_mouse_release(x, y, button, mods)
|
|
|
|
point = self.pixel_coords_to_space_coords(x, y)
|
|
|
|
self.scene.on_mouse_release(point, button, mods)
|
|
|
|
|
|
|
|
def on_mouse_scroll(self, x, y, x_offset: float, y_offset: float):
|
|
|
|
super().on_mouse_scroll(x, y, x_offset, y_offset)
|
|
|
|
point = self.pixel_coords_to_space_coords(x, y)
|
|
|
|
offset = self.pixel_coords_to_space_coords(x_offset, y_offset, relative=True)
|
|
|
|
self.scene.on_mouse_scroll(point, offset)
|
|
|
|
|
|
|
|
def on_key_press(self, symbol, modifiers):
|
2021-01-06 12:47:13 -08:00
|
|
|
self.pressed_keys.add(symbol) # Modifiers?
|
2020-02-11 19:51:19 -08:00
|
|
|
super().on_key_press(symbol, modifiers)
|
|
|
|
self.scene.on_key_press(symbol, modifiers)
|
|
|
|
|
2021-01-06 12:47:13 -08:00
|
|
|
def on_key_release(self, symbol, modifiers):
|
|
|
|
self.pressed_keys.difference_update({symbol}) # Modifiers?
|
|
|
|
super().on_key_release(symbol, modifiers)
|
|
|
|
self.scene.on_key_release(symbol, modifiers)
|
|
|
|
|
2020-02-11 19:51:19 -08:00
|
|
|
def on_resize(self, width: int, height: int):
|
|
|
|
super().on_resize(width, height)
|
|
|
|
self.scene.on_resize(width, height)
|
|
|
|
|
|
|
|
def on_show(self):
|
|
|
|
super().on_show()
|
|
|
|
self.scene.on_show()
|
|
|
|
|
|
|
|
def on_hide(self):
|
|
|
|
super().on_hide()
|
|
|
|
self.scene.on_hide()
|
|
|
|
|
|
|
|
def on_close(self):
|
|
|
|
super().on_close()
|
|
|
|
self.scene.on_close()
|
2021-01-06 12:47:13 -08:00
|
|
|
|
|
|
|
def is_key_pressed(self, symbol):
|
|
|
|
return (symbol in self.pressed_keys)
|