-
Notifications
You must be signed in to change notification settings - Fork 295
/
camera.py
142 lines (102 loc) · 4.67 KB
/
camera.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
import pyglet
"""Camera class for easy scrolling and zooming.
A simple example of a Camera class that can be used to easily scroll and
zoom when rendering. For example, you might have a playfield that needs
to scroll and/or zoom, and a GUI layer that will remain static. For that
scenario, you can create two Camera instances. You can optionally set
the minimum allowed zoom, maximum allowed zoom, and scrolling speed::
world_camera = Camera(scroll_speed=5, min_zoom=1, max_zoom=4)
gui_camera = Camera()
After creating Camera instances, the zoom can be easily updated. It will
clamp to the `max_zoom` parameter (default of 4)::
world_camera.zoom += 1
The scrolling can be set in two different ways. Directly with the
`Camera.position attribute, which can be set with a tuple of absolute
x, y values::
world_camera.position = 50, 0
Or, it can be updated incrementally with the `Camera.move(x, y)` method.
This will update the camera position by multiplying the passed vector by
the `Camera.scroll_speed` parameter, which can be set on instantiation.
world_camera.move(1, 0)
# If the world_camera.scroll_speed is "5", this will move the camera
# by 5 pixels right on the x axis.
During your `Window.on_draw` event, you can set the Camera, and draw the
appropriate objects. For convenience, the Camera class can act as a context
manager, allowing easy use of "with"::
@window.event
def on_draw():
window.clear()
# Draw your world scene using the world camera
with world_camera:
batch.draw()
# Can also be written as:
# camera.begin()
# batch.draw()
# camera.end()
# Draw your GUI elements with the GUI camera.
with gui_camera:
label.draw()
"""
class Camera:
""" A simple 2D camera that contains the speed and offset."""
def __init__(self, scroll_speed=1, min_zoom=1, max_zoom=4):
assert min_zoom <= max_zoom, "Minimum zoom must not be greater than maximum zoom"
self.scroll_speed = scroll_speed
self.max_zoom = max_zoom
self.min_zoom = min_zoom
self.offset_x = 0
self.offset_y = 0
self._zoom = max(min(1, self.max_zoom), self.min_zoom)
@property
def zoom(self):
return self._zoom
@zoom.setter
def zoom(self, value):
""" Here we set zoom, clamp value to minimum of min_zoom and max of max_zoom."""
self._zoom = max(min(value, self.max_zoom), self.min_zoom)
@property
def position(self):
"""Query the current offset."""
return self.offset_x, self.offset_y
@position.setter
def position(self, value):
"""Set the scroll offset directly."""
self.offset_x, self.offset_y = value
def move(self, axis_x, axis_y):
""" Move axis direction with scroll_speed.
Example: Move left -> move(-1, 0)
"""
self.offset_x += self.scroll_speed * axis_x
self.offset_y += self.scroll_speed * axis_y
def begin(self):
# Set the current camera offset so you can draw your scene.
# Translate using the zoom and the offset.
pyglet.gl.glTranslatef(-self.offset_x * self._zoom, -self.offset_y * self._zoom, 0)
# Scale by zoom level.
pyglet.gl.glScalef(self._zoom, self._zoom, 1)
def end(self):
# Since this is a matrix, you will need to reverse the translate after rendering otherwise
# it will multiply the current offset every draw update pushing it further and further away.
# Reverse scale, since that was the last transform.
pyglet.gl.glScalef(1 / self._zoom, 1 / self._zoom, 1)
# Reverse translate.
pyglet.gl.glTranslatef(self.offset_x * self._zoom, self.offset_y * self._zoom, 0)
def __enter__(self):
self.begin()
def __exit__(self, exception_type, exception_value, traceback):
self.end()
class CenteredCamera(Camera):
"""A simple 2D camera class. 0, 0 will be the centre of the screen, as opposed to the bottom left."""
def __init__(self, window: pyglet.window.Window, *args, **kwargs):
self.window = window
super().__init__(*args, **kwargs)
def begin(self):
x = -self.window.width//2/self._zoom + self.offset_x
y = -self.window.height//2/self._zoom + self.offset_y
pyglet.gl.glTranslatef(-x * self._zoom, -y * self._zoom, 0)
pyglet.gl.glScalef(self._zoom, self._zoom, 1)
def end(self):
x = -self.window.width//2/self._zoom + self.offset_x
y = -self.window.height//2/self._zoom + self.offset_y
pyglet.gl.glScalef(1 / self._zoom, 1 / self._zoom, 1)
pyglet.gl.glTranslatef(x * self._zoom, y * self._zoom, 0)