Skip to content

Commit

Permalink
chore: add type hints to manimlib.window
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCrane committed Feb 15, 2022
1 parent 91ffdeb commit f085e6c
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions manimlib/window.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import numpy as np
import moderngl_window as mglw
from moderngl_window.context.pyglet.window import Window as PygletWindow
Expand All @@ -7,6 +9,10 @@
from manimlib.utils.config_ops import digest_config
from manimlib.utils.customization import get_customization

from typing import TYPE_CHECKING
if TYPE_CHECKING:
from manimlib.scene.scene import Scene


class Window(PygletWindow):
fullscreen = False
Expand All @@ -15,7 +21,12 @@ class Window(PygletWindow):
vsync = True
cursor = True

def __init__(self, scene, size=(1280, 720), **kwargs):
def __init__(
self,
scene: Scene,
size: tuple[int, int] = (1280, 720),
**kwargs
):
super().__init__(size=size)
digest_config(self, kwargs)

Expand All @@ -37,7 +48,7 @@ def __init__(self, scene, size=(1280, 720), **kwargs):
self.position = initial_position
self.position = initial_position

def find_initial_position(self, size):
def find_initial_position(self, size: tuple[int, int]) -> tuple[int, int]:
custom_position = get_customization()["window_position"]
monitors = get_monitors()
mon_index = get_customization()["window_monitor"]
Expand All @@ -59,7 +70,12 @@ def find_initial_position(self, size):
)

# Delegate event handling to scene
def pixel_coords_to_space_coords(self, px, py, relative=False):
def pixel_coords_to_space_coords(
self,
px: int,
py: int,
relative: bool = False
) -> np.ndarray:
pw, ph = self.size
fw, fh = self.scene.camera.get_frame_shape()
fc = self.scene.camera.get_frame_center()
Expand All @@ -72,59 +88,59 @@ def pixel_coords_to_space_coords(self, px, py, relative=False):
0
])

def on_mouse_motion(self, x, y, dx, dy):
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> None:
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):
def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int) -> None:
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)
self.scene.on_mouse_drag(point, d_point, buttons, modifiers)

def on_mouse_press(self, x: int, y: int, button, mods):
def on_mouse_press(self, x: int, y: int, button: int, mods: int) -> None:
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):
def on_mouse_release(self, x: int, y: int, button: int, mods: int) -> None:
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):
def on_mouse_scroll(self, x: int, y: int, x_offset: float, y_offset: float) -> None:
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):
def on_key_press(self, symbol: int, modifiers: int) -> None:
self.pressed_keys.add(symbol) # Modifiers?
super().on_key_press(symbol, modifiers)
self.scene.on_key_press(symbol, modifiers)

def on_key_release(self, symbol, modifiers):
def on_key_release(self, symbol: int, modifiers: int) -> None:
self.pressed_keys.difference_update({symbol}) # Modifiers?
super().on_key_release(symbol, modifiers)
self.scene.on_key_release(symbol, modifiers)

def on_resize(self, width: int, height: int):
def on_resize(self, width: int, height: int) -> None:
super().on_resize(width, height)
self.scene.on_resize(width, height)

def on_show(self):
def on_show(self) -> None:
super().on_show()
self.scene.on_show()

def on_hide(self):
def on_hide(self) -> None:
super().on_hide()
self.scene.on_hide()

def on_close(self):
def on_close(self) -> None:
super().on_close()
self.scene.on_close()

def is_key_pressed(self, symbol):
def is_key_pressed(self, symbol: int) -> bool:
return (symbol in self.pressed_keys)

0 comments on commit f085e6c

Please sign in to comment.