Skip to content

Commit d872e5c

Browse files
committed
Docs: BaseWindow
1 parent bfbc3de commit d872e5c

File tree

3 files changed

+156
-12
lines changed

3 files changed

+156
-12
lines changed

demosys/context/base.py

+118-12
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@
1010

1111

1212
class BaseKeys:
13-
"""Generic constants here"""
13+
"""
14+
Namespace for generic key constants
15+
working across all window types.
16+
"""
1417
ACTION_PRESS = 'ACTION_PRESS'
1518
ACTION_RELEASE = 'ACTION_RELEASE'
1619

1720

1821
class BaseWindow:
19-
keys = None
22+
"""
23+
The base window we extend when adding new window types to the system.
24+
"""
25+
keys = None #: The key class/namespace used by the window defining keyboard constants
2026

2127
def __init__(self):
2228
"""
23-
Base window intializer
29+
Base window intializer reading values from ``settings``.
30+
If you need an initializer in your own window, always call
31+
this methods using ``super().__init__()``
2432
"""
2533
self.frames = 0
2634
self.width = settings.WINDOW['size'][0]
@@ -55,18 +63,43 @@ def __init__(self):
5563

5664
@property
5765
def size(self):
66+
"""
67+
(width, height) tuple containing the window size.
68+
69+
Note that for certain displays we rely on :py:func:`buffer_size`
70+
to get the actual window buffer size. This is fairly common
71+
for retina and 4k displays where the UI scale is > 1.0
72+
"""
5873
return (self.width, self.height)
5974

6075
@property
6176
def buffer_size(self):
77+
"""
78+
(width, heigh) buffer size of the window.
79+
80+
This is the actual buffer size of the window
81+
taking UI scale into account. A 1920 x 1080
82+
window running in an environment with UI scale 2.0
83+
would have a 3840 x 2160 window buffer.
84+
"""
6285
return (self.buffer_width, self.buffer_height)
6386

6487
def draw(self, current_time, frame_time):
88+
"""
89+
Draws a frame. Internally it calls the
90+
configured timeline's draw method.
91+
92+
Args:
93+
current_time (float): The current time (preferrably always from the configured timer class)
94+
frame_time (float): The duration of the previous frame in seconds
95+
"""
6596
self.set_default_viewport()
6697
self.timeline.draw(current_time, frame_time, self.fbo)
6798

6899
def clear(self):
69-
"""Clear the scren"""
100+
"""
101+
Clear the window buffer
102+
"""
70103
self.ctx.fbo.clear(
71104
red=self.clear_color[0],
72105
green=self.clear_color[1],
@@ -76,34 +109,91 @@ def clear(self):
76109
)
77110

78111
def clear_values(self, red=0.0, green=0.0, blue=0.0, alpha=0.0, depth=1.0):
112+
"""
113+
Sets the clear values for the window buffer.
114+
115+
Args:
116+
red (float): red compoent
117+
green (float): green compoent
118+
blue (float): blue compoent
119+
alpha (float): alpha compoent
120+
depth (float): depth value
121+
"""
79122
self.clear_color = (red, green, blue, alpha)
80123
self.clear_depth = depth
81124

82125
def use(self):
83-
"""Render to this window"""
126+
"""
127+
Set the window buffer as the current render target
128+
129+
Raises:
130+
NotImplementedError
131+
"""
84132
raise NotImplementedError()
85133

86134
def swap_buffers(self):
87-
"""Swap frame buffer"""
135+
"""
136+
Swap the buffers. Most windows have at least support for double buffering
137+
cycling a back and front buffer.
138+
139+
Raises:
140+
NotImplementedError
141+
"""
88142
raise NotImplementedError()
89143

90144
def resize(self, width, height):
91-
"""Resize window"""
145+
"""
146+
Resize the window. Should normallty be overriden
147+
when implementing a window as most window libraries need additional logic here.
148+
149+
Args:
150+
width (int): Width of the window
151+
height: (int): Height of the window
152+
"""
92153
self.set_default_viewport()
93154

94155
def close(self):
95-
"""Set the close state"""
156+
"""
157+
Set the window in close state. This doesn't actually close the window,
158+
but should make :py:func:`should_close` return ``True`` so the
159+
main loop can exit gracefully.
160+
161+
Raises:
162+
NotImplementedError
163+
"""
96164
raise NotImplementedError()
97165

98166
def should_close(self):
99-
"""Check if window should close"""
167+
"""
168+
Check if window should close. This should always be checked in the main draw loop.
169+
170+
Raises:
171+
NotImplementedError
172+
"""
100173
raise NotImplementedError()
101174

102175
def terminate(self):
103-
"""Cleanup after close"""
176+
"""
177+
The actual teardown of the window.
178+
179+
Raises:
180+
NotImplementedError
181+
"""
104182
raise NotImplementedError()
105183

106184
def keyboard_event(self, key, action, modifier):
185+
"""
186+
Handles the standard keyboard events such as camera movements,
187+
taking a screenshot, closing the window etc.
188+
189+
Can be overriden add new keyboard events. Ensure this method
190+
is also called if you want to keep the standard features.
191+
192+
Arguments:
193+
key: The key that was pressed or released
194+
action: The key action. Can be `ACTION_PRESS` or `ACTION_RELEASE`
195+
modifier: Modifiers such as holding shift or ctrl
196+
"""
107197
# The well-known standard key for quick exit
108198
if key == self.keys.ESCAPE:
109199
self.close()
@@ -170,10 +260,23 @@ def keyboard_event(self, key, action, modifier):
170260
self.timeline.key_event(key, action, modifier)
171261

172262
def cursor_event(self, x, y, dx, dy):
263+
"""
264+
The standard mouse movement event method.
265+
Can be overriden to add new functionality.
266+
By default this feeds the system camera with new values.
267+
268+
Args:
269+
x: The current mouse x position
270+
y: The current mouse y position
271+
dx: Delta x postion (x position difference from the previous event)
272+
dy: Delta y postion (y position difference from the previous event)
273+
"""
173274
self.sys_camera.rot_state(x, y)
174275

175276
def print_context_info(self):
176-
"""Prints out context info"""
277+
"""
278+
Prints moderngl context info.
279+
"""
177280
print("Context Version:")
178281
print('ModernGL:', moderngl.__version__)
179282
print('vendor:', self.ctx.info['GL_VENDOR'])
@@ -184,7 +287,10 @@ def print_context_info(self):
184287
print('code:', self.ctx.version_code)
185288

186289
def set_default_viewport(self):
187-
"""Calculate viewport with correct aspect ratio"""
290+
"""
291+
Calculates the viewport based on the configured aspect ratio in settings.
292+
Will add black borders if the window do not match the viewport.
293+
"""
188294
# The expected height with the current viewport width
189295
expected_height = int(self.buffer_width / self.aspect_ratio)
190296

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
.. py:module:: demosys.context.base
3+
.. py:currentmodule:: demosys.context.base
4+
5+
demosys.context.base.BaseWindow
6+
===============================
7+
8+
.. autodata:: BaseWindow
9+
:annotation:
10+
11+
.. autodata:: BaseKeys
12+
:annotation:
13+
14+
Methods
15+
-------
16+
17+
.. automethod:: BaseWindow.__init__
18+
.. automethod:: BaseWindow.draw
19+
.. automethod:: BaseWindow.clear
20+
.. automethod:: BaseWindow.clear_values
21+
.. automethod:: BaseWindow.use
22+
.. automethod:: BaseWindow.swap_buffers
23+
.. automethod:: BaseWindow.resize
24+
.. automethod:: BaseWindow.close
25+
.. automethod:: BaseWindow.should_close
26+
.. automethod:: BaseWindow.terminate
27+
.. automethod:: BaseWindow.keyboard_event
28+
.. automethod:: BaseWindow.cursor_event
29+
.. automethod:: print_context_info
30+
.. automethod:: set_default_viewport
31+
32+
Attributes
33+
----------
34+
35+
.. autoattribute:: BaseWindow.size
36+
.. autoattribute:: BaseWindow.buffer_size
37+
.. autoattribute:: BaseWindow.keys

docs/reference/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ Reference
1515
demosys.timers.rocket
1616
demosys.timers.rocketmusic
1717
demosys.timers.vlc
18+
demosys.context.base

0 commit comments

Comments
 (0)