-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.py
executable file
·450 lines (368 loc) · 12.7 KB
/
window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import pygame
import simplegui
from typing import List, Tuple
from constants import HIDPI_FACTOR
from geom import Vector, BoundingBox
__all__ = ['Window', 'Renderable', 'RenderableParent', 'WindowHandler']
class Window(object):
"""
The game window, only one may be created.
"""
def __init__(self, title: str, size: Tuple[int, int], fullscreen: bool = False):
"""
Creates a new game window.
"""
self.hidpi_factor = HIDPI_FACTOR # HiDPI scale factor.
self.handler = WindowHandler(self) # A noop window handler.
self._hide_control_panel() # Hide control panel.
# self.fullscreen = self._set_fullscreen(fullscreen) # Set fullscreen state.
# Get appropriate window size.
width, height = self._get_window_size(size, fullscreen)
# Create the frame.
self.frame = self._create_frame(title, width, height)
# Set the frame event handlers.
self.frame.set_draw_handler(self._render)
self.frame.set_mouseclick_handler(self._on_click)
self.frame.set_mousedrag_handler(self._on_drag)
self.frame.set_keydown_handler(self._on_key_down)
self.frame.set_keyup_handler(self._on_key_up)
self._mouse_down = False
def show(self):
"""
Shows the window.
"""
try:
self.frame.start()
except KeyboardInterrupt:
print('Received keyboard interrupt closing')
def destroy(self):
"""
Destroys the window.
"""
self.frame.stop()
# noinspection PyMethodMayBeStatic
def get_title(self) -> str:
"""
Returns the title of the window.
"""
return pygame.display.get_caption()
# noinspection PyMethodMayBeStatic
def set_title(self, title: str):
"""
Sets the title of the window.
"""
return pygame.display.set_caption(title)
# noinspection PyMethodMayBeStatic
def get_cursor_pos(self) -> Vector:
x, y = pygame.mouse.get_pos()
return Vector(x, y)
# noinspection PyMethodMayBeStatic
def set_cursor_pos(self, pos: Vector):
return pygame.mouse.set_pos((pos.x, pos.y))
def get_size(self) -> Tuple[int, int]:
"""
Returns the window size.
"""
# noinspection PyProtectedMember
surf: pygame.Surface = self.frame._pygame_surface
w, h = surf.get_size()
return int(round(w)), int(round(h))
# noinspection PyProtectedMember
def set_size(self, size: Tuple[int, int]):
"""
Sets the size of the window.
"""
assert size[0] > 0 and size[1] > 0
self.frame._pygame_surface: pygame.Surface = \
pygame.display.set_mode((size[0], size[0]),
simplegui.Frame._pygame_mode_flags,
simplegui.Frame._pygame_mode_depth)
self.frame._pygame_surface.fill(simplegui.Frame._background_pygame_color)
# update display
pygame.display.update()
pygame.display.flip()
# Event handlers
def _render(self, canvas: simplegui.Canvas):
"""
Called by the window draw handler every game tick.
Passes the render to the handler.
"""
if self.handler is not None:
self.handler.render(canvas)
def _on_click(self, pos: Tuple[int, int]):
"""
Called whenever the window receives a mouse click event.
Passes the event to the handler.
"""
if self.handler is not None:
self.handler.on_click(Vector(*pos))
if self._mouse_down:
self._mouse_down = False
if self.handler is not None:
self.handler.on_mouse_up(Vector(*pos))
def _on_drag(self, pos: Tuple[int, int]):
"""
Called whenever the window receives a mouse drag event.
Passes the event to the handler.
"""
if not self._mouse_down:
self._mouse_down = True
self._last_mouse_pos = pos
if self.handler is not None:
self.handler.on_mouse_down(Vector(*pos))
else:
if self.handler is not None:
self.handler.on_drag(Vector(*self._last_mouse_pos), Vector(*pos))
def _on_key_down(self, key: int):
"""
Called whenever the window receives a key down event.
Passes the event to the handler.
"""
if self.handler is not None:
self.handler.on_key_down(key)
def _on_key_up(self, key: int):
"""
Called whenever the window receives a key up event.
Passes the event to the handler.
"""
if self.handler is not None:
self.handler.on_key_up(key)
# Internal
# noinspection PyProtectedMember
@staticmethod
def _create_frame(title: str, width: int, height: int) -> simplegui.Frame:
"""
Creates the window frame, magic included.
"""
offset = 4
fake_width = width - offset
fake_height = height - offset
frame: simplegui.Frame = simplegui.create_frame(title, fake_width, fake_height)
frame._canvas._frame_parent = None
frame._canvas._pygame_surface = None
frame._canvas = simplegui.Canvas(frame, width, height)
# Magical fixes in the pygame implementation.
frame._control_width = 0
frame._border_size = 0
frame._canvas_border_size = 0
frame._canvas_x_offset = 0
frame._canvas_y_offset = 0
frame._statusmouse_x_offset = 0
frame._statusmouse_y_offset = 0
frame._statuskey_x_offset = 0
frame._statuskey_y_offset = 0
simplegui.Frame._statuskey_height = 0
simplegui.Frame._statusmouse_height = 0
# redraw frame to correct size
frame._pygame_surface: pygame.Surface = \
pygame.display.set_mode((width, height),
simplegui.Frame._pygame_mode_flags,
simplegui.Frame._pygame_mode_depth)
frame._pygame_surface.fill(simplegui.Frame._background_pygame_color)
# update display
pygame.display.update()
pygame.display.flip()
return frame
@staticmethod
def _hide_control_panel():
"""
Attempts to hide the control panel.
"""
simplegui.Frame._hide_controlpanel = True
@staticmethod
def _set_fullscreen(value: bool) -> bool:
"""
Attempts to set the fullscreen status.
"""
# noinspection PyProtectedMember
flags = simplegui.Frame._pygame_mode_flags
enable = pygame.FULLSCREEN | pygame.HWSURFACE
if value:
flags |= enable
else:
flags &= ~enable
simplegui.Frame._pygame_mode_flags = flags
return value
@staticmethod
def _get_window_size(size: Tuple[int, int], fullscreen: bool) -> Tuple[int, int]:
"""
Returns the appropriate window size.
"""
import os
width = size[0]
height = size[1]
display_info = pygame.display.Info()
if fullscreen:
# fullscreen should be truly fullscreen
width = display_info.current_w
height = display_info.current_h
width *= HIDPI_FACTOR
height *= HIDPI_FACTOR
# width and height should be ints
width = int(round(width))
height = int(round(height))
if not fullscreen:
# center the window
pos = (display_info.current_w / 2 - width / 2, display_info.current_h / 2 - height / 2)
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % pos
return width, height
class Renderable(object):
"""
A renderable object to be drawn on the canvas.
"""
def __init__(self, window: Window):
self.window = window
self.parent: 'RenderableParent' = None
def get_bounds(self) -> BoundingBox:
"""
The bounds of the rendered object.
Used to handle mouse events.
"""
raise NotImplementedError
def render(self, canvas: simplegui.Canvas):
"""
Called to render the object.
"""
pass
def is_mouse_over(self) -> bool:
"""
Returns `true` if the mouse is over the object.
"""
return self.get_bounds().contains(self.window.get_cursor_pos())
def on_click(self, pos: Vector):
"""
Called whenever renderable receives a mouse click event.
"""
pass
def on_mouse_down(self, pos: Vector):
"""
Called whenever renderable receives a mouse down event.
"""
pass
def on_mouse_up(self, pos: Vector):
"""
Called whenever renderable receives a mouse up event.
"""
pass
def on_drag(self, last: Vector, new: Vector):
"""
Called whenever renderable receives a mouse drag event.
"""
pass
def on_key_down(self, key: int):
"""
Called whenever renderable receives a key down event.
"""
pass
def on_key_up(self, key: int):
"""
Called whenever renderable receives a key up event.
"""
pass
class RenderableParent(Renderable):
"""
A renderable object to be drawn on the canvas, that can have children.
"""
def __init__(self, window: Window):
super().__init__(window)
self.children: List[Renderable] = []
def add_child(self, child: Renderable):
"""
Adds a child to this parent.
"""
child.parent = self
self.children.append(child)
def render(self, canvas: simplegui.Canvas):
"""
Called to render the object and render it's children.
"""
for child in self.children:
child.render(canvas)
def get_bounds(self) -> BoundingBox:
raise NotImplementedError
def on_click(self, pos: Vector):
"""
Called whenever parent receives a mouse click event.
Also checks and passes event to children.
"""
for child in self.children:
if child.get_bounds().contains(pos):
child.on_click(pos)
def on_mouse_down(self, pos: Vector):
"""
Called whenever parent receives a mouse down event.
Also checks and passes event to children.
"""
for child in self.children:
if child.get_bounds().contains(pos):
child.on_mouse_down(pos)
def on_mouse_up(self, pos: Vector):
"""
Called whenever parent receives a mouse up event.
Also checks and passes event to children.
"""
for child in self.children:
if child.get_bounds().contains(pos):
child.on_mouse_up(pos)
def on_drag(self, last: Vector, new: Vector):
"""
Called whenever parent receives a mouse drag event.
Also checks and passes event to children.
"""
for child in self.children:
if child.get_bounds().contains(last):
child.on_drag(last, new)
def on_key_down(self, key: int):
"""
Called whenever the window receives a key down event.
"""
for child in self.children:
child.on_key_down(key)
def on_key_up(self, key: int):
"""
Called whenever the window receives a key up event.
"""
for child in self.children:
child.on_key_up(key)
class WindowHandler(RenderableParent):
"""
A WindowHandler handles the rendering of the window and receives all events.
"""
def __init__(self, window: Window):
"""
Creates a window handler.
"""
super().__init__(window)
def render(self, canvas: simplegui.Canvas):
"""
Called by the window draw handler every game tick.
"""
super().render(canvas)
def get_bounds(self) -> BoundingBox:
size = self.window.get_size()
return BoundingBox(
Vector(0, 0),
Vector(size[0], 0),
Vector(size[0], size[1]),
Vector(0, size[1]),
)
def on_click(self, pos: Vector):
"""
Called whenever the window receives a mouse click event.
"""
super().on_click(pos)
def on_mouse_down(self, pos: Vector):
"""
Called whenever the window receives a mouse down event.
"""
super().on_mouse_down(pos)
def on_mouse_up(self, pos: Vector):
"""
Called whenever the window receives a mouse up event.
"""
super().on_mouse_up(pos)
def on_drag(self, last: Vector, new: Vector):
"""
Called whenever the window receives a mouse drag event.
"""
super().on_drag(last, new)