1
1
from __future__ import annotations
2
2
3
+ from typing import TYPE_CHECKING , Any
4
+
3
5
import moderngl_window as mglw
4
6
from moderngl_window .context .pyglet .window import Window as PygletWindow
5
7
from moderngl_window .timers .clock import Timer
6
- from screeninfo import get_monitors
8
+ from screeninfo import Monitor , get_monitors
7
9
8
10
from .. import __version__ , config
9
11
12
+ if TYPE_CHECKING :
13
+ from .opengl_renderer import OpenGLRenderer
14
+
10
15
__all__ = ["Window" ]
11
16
12
17
@@ -17,15 +22,19 @@ class Window(PygletWindow):
17
22
vsync = True
18
23
cursor = True
19
24
20
- def __init__ (self , renderer , size = config .window_size , ** kwargs ):
25
+ def __init__ (
26
+ self ,
27
+ renderer : OpenGLRenderer ,
28
+ window_size : str = config .window_size ,
29
+ ** kwargs : Any ,
30
+ ) -> None :
21
31
monitors = get_monitors ()
22
32
mon_index = config .window_monitor
23
33
monitor = monitors [min (mon_index , len (monitors ) - 1 )]
24
34
25
- if size == "default" :
35
+ if window_size == "default" :
26
36
# make window_width half the width of the monitor
27
37
# but make it full screen if --fullscreen
28
-
29
38
window_width = monitor .width
30
39
if not config .fullscreen :
31
40
window_width //= 2
@@ -35,8 +44,13 @@ def __init__(self, renderer, size=config.window_size, **kwargs):
35
44
window_width * config .frame_height // config .frame_width ,
36
45
)
37
46
size = (window_width , window_height )
47
+ elif len (window_size .split ("," )) == 2 :
48
+ (window_width , window_height ) = tuple (map (int , window_size .split ("," )))
49
+ size = (window_width , window_height )
38
50
else :
39
- size = tuple (size )
51
+ raise ValueError (
52
+ "Window_size must be specified as 'width,height' or 'default'." ,
53
+ )
40
54
41
55
super ().__init__ (size = size )
42
56
@@ -55,13 +69,13 @@ def __init__(self, renderer, size=config.window_size, **kwargs):
55
69
self .position = initial_position
56
70
57
71
# Delegate event handling to scene.
58
- def on_mouse_motion (self , x , y , dx , dy ) :
72
+ def on_mouse_motion (self , x : int , y : int , dx : int , dy : int ) -> None :
59
73
super ().on_mouse_motion (x , y , dx , dy )
60
74
point = self .renderer .pixel_coords_to_space_coords (x , y )
61
75
d_point = self .renderer .pixel_coords_to_space_coords (dx , dy , relative = True )
62
76
self .renderer .scene .on_mouse_motion (point , d_point )
63
77
64
- def on_mouse_scroll (self , x , y , x_offset : float , y_offset : float ):
78
+ def on_mouse_scroll (self , x : int , y : int , x_offset : float , y_offset : float ) -> None :
65
79
super ().on_mouse_scroll (x , y , x_offset , y_offset )
66
80
point = self .renderer .pixel_coords_to_space_coords (x , y )
67
81
offset = self .renderer .pixel_coords_to_space_coords (
@@ -71,28 +85,32 @@ def on_mouse_scroll(self, x, y, x_offset: float, y_offset: float):
71
85
)
72
86
self .renderer .scene .on_mouse_scroll (point , offset )
73
87
74
- def on_key_press (self , symbol , modifiers ) :
88
+ def on_key_press (self , symbol : int , modifiers : int ) -> bool :
75
89
self .renderer .pressed_keys .add (symbol )
76
- super ().on_key_press (symbol , modifiers )
90
+ event_handled : bool = super ().on_key_press (symbol , modifiers )
77
91
self .renderer .scene .on_key_press (symbol , modifiers )
92
+ return event_handled
78
93
79
- def on_key_release (self , symbol , modifiers ) :
94
+ def on_key_release (self , symbol : int , modifiers : int ) -> None :
80
95
if symbol in self .renderer .pressed_keys :
81
96
self .renderer .pressed_keys .remove (symbol )
82
97
super ().on_key_release (symbol , modifiers )
83
98
self .renderer .scene .on_key_release (symbol , modifiers )
84
99
85
- def on_mouse_drag (self , x , y , dx , dy , buttons , modifiers ):
100
+ def on_mouse_drag (
101
+ self , x : int , y : int , dx : int , dy : int , buttons : int , modifiers : int
102
+ ) -> None :
86
103
super ().on_mouse_drag (x , y , dx , dy , buttons , modifiers )
87
104
point = self .renderer .pixel_coords_to_space_coords (x , y )
88
105
d_point = self .renderer .pixel_coords_to_space_coords (dx , dy , relative = True )
89
106
self .renderer .scene .on_mouse_drag (point , d_point , buttons , modifiers )
90
107
91
- def find_initial_position (self , size , monitor ):
108
+ def find_initial_position (
109
+ self , size : tuple [int , int ], monitor : Monitor
110
+ ) -> tuple [int , int ]:
92
111
custom_position = config .window_position
93
112
window_width , window_height = size
94
- # Position might be specified with a string of the form
95
- # x,y for integers x and y
113
+ # Position might be specified with a string of the form x,y for integers x and y
96
114
if len (custom_position ) == 1 :
97
115
raise ValueError (
98
116
"window_position must specify both Y and X positions (Y/X -> UR). Also accepts LEFT/RIGHT/ORIGIN/UP/DOWN." ,
@@ -105,20 +123,21 @@ def find_initial_position(self, size, monitor):
105
123
elif custom_position == "ORIGIN" :
106
124
custom_position = "O" * 2
107
125
elif "," in custom_position :
108
- return tuple (map (int , custom_position .split ("," )))
126
+ pos_y , pos_x = tuple (map (int , custom_position .split ("," )))
127
+ return (pos_x , pos_y )
109
128
110
129
# Alternatively, it might be specified with a string like
111
130
# UR, OO, DL, etc. specifying what corner it should go to
112
131
char_to_n = {"L" : 0 , "U" : 0 , "O" : 1 , "R" : 2 , "D" : 2 }
113
- width_diff = monitor .width - window_width
114
- height_diff = monitor .height - window_height
132
+ width_diff : int = monitor .width - window_width
133
+ height_diff : int = monitor .height - window_height
115
134
116
135
return (
117
136
monitor .x + char_to_n [custom_position [1 ]] * width_diff // 2 ,
118
137
- monitor .y + char_to_n [custom_position [0 ]] * height_diff // 2 ,
119
138
)
120
139
121
- def on_mouse_press (self , x , y , button , modifiers ) :
140
+ def on_mouse_press (self , x : int , y : int , button : int , modifiers : int ) -> None :
122
141
super ().on_mouse_press (x , y , button , modifiers )
123
142
point = self .renderer .pixel_coords_to_space_coords (x , y )
124
143
mouse_button_map = {
0 commit comments