Skip to content

Commit 5124ddd

Browse files
Add _enter and _exit methods to support 'with'
This lets you apply view matrix, camera model view and viewport operations by using the 'with' keyword. Ie: with viewport: render_scene()
1 parent f9c28cd commit 5124ddd

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

pygly/camera_node.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ def __init__( self, name, view_matrix ):
3232
#: the camer's view matrix
3333
self.view_matrix = view_matrix
3434

35+
def _enter(self):
36+
# apply our view matrix
37+
# and push our model view
38+
self.view_matrix.push_view_matrix()
39+
self.push_model_view()
40+
41+
def _exit(self):
42+
# pop our model view
43+
# and our view matrix
44+
self.pop_model_view()
45+
self.view_matrix.pop_view_matrix()
46+
3547
@property
3648
def model_view( self ):
3749
"""Property for the camera's model view matrix.

pygly/view_matrix.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def __init__( self, aspect_ratio, near_clip, far_clip ):
3939
self._matrix = None
4040
self.dirty = True
4141

42+
def _enter(self):
43+
self.push_view_matrix()
44+
45+
def _exit(self):
46+
self.pop_view_matrix()
47+
4248
@property
4349
def matrix( self ):
4450
"""The current view matrix.

pygly/viewport.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ def switch_to( self ):
112112
# update our viewport size
113113
gl.set_viewport( self.rect )
114114

115+
def _enter(self):
116+
# activate our viewport
117+
# scissor to the viewport
118+
# and set our gl state
119+
self.switch_to()
120+
self.scissor_to_viewport()
121+
self.push_viewport_attributes()
122+
123+
def _exit(self):
124+
# pop our gl state
125+
# reset our scissor
126+
# and reset our viewport to full size
127+
self.pop_viewport_attributes()
128+
window_rect = window.create_rectangle( self.window )
129+
gl.set_scissor( window_rect )
130+
gl.set_viewport( window_rect )
131+
115132
@property
116133
def aspect_ratio( self ):
117134
"""Returns the aspect ratio of the viewport.

0 commit comments

Comments
 (0)