| 1 |
import pyglet |
| 2 |
|
| 3 |
"""Camera class for easy scrolling and zooming. |
| 4 |
|
| 5 |
A simple example of a Camera class that can be used to easily scroll and |
| 6 |
zoom when rendering. For example, you might have a playfield that needs |
| 7 |
to scroll and/or zoom, and a GUI layer that will remain static. For that |
| 8 |
scenario, you can create two Camera instances. You can optionally set |
| 9 |
the minimum allowed zoom, maximum allowed zoom, and scrolling speed:: |
| 10 |
|
| 11 |
world_camera = Camera(scroll_speed=5, min_zoom=1, max_zoom=4) |
| 12 |
gui_camera = Camera() |
| 13 |
|
| 14 |
After creating Camera instances, the zoom can be easily updated. It will |
| 15 |
clamp to the `max_zoom` parameter (default of 4):: |
| 16 |
|
| 17 |
world_camera.zoom += 1 |
| 18 |
|
| 19 |
The scrolling can be set in two different ways. Directly with the |
| 20 |
`Camera.position attribute, which can be set with a tuple of absolute |
| 21 |
x, y values:: |
| 22 |
|
| 23 |
world_camera.position = 50, 0 |
| 24 |
|
| 25 |
Or, it can be updated incrementally with the `Camera.move(x, y)` method. |
| 26 |
This will update the camera position by multiplying the passed vector by |
| 27 |
the `Camera.scroll_speed` parameter, which can be set on instantiation. |
| 28 |
|
| 29 |
world_camera.move(1, 0) |
| 30 |
# If the world_camera.scroll_speed is "5", this will move the camera |
| 31 |
# by 5 pixels right on the x axis. |
| 32 |
|
| 33 |
|
| 34 |
During your `Window.on_draw` event, you can set the Camera, and draw the |
| 35 |
appropriate objects. For convenience, the Camera class can act as a context |
| 36 |
manager, allowing easy use of "with":: |
| 37 |
|
| 38 |
@window.event |
| 39 |
def on_draw(): |
| 40 |
window.clear() |
| 41 |
|
| 42 |
# Draw your world scene using the world camera |
| 43 |
with world_camera: |
| 44 |
batch.draw() |
| 45 |
|
| 46 |
# Can also be written as: |
| 47 |
# camera.begin() |
| 48 |
# batch.draw() |
| 49 |
# camera.end() |
| 50 |
|
| 51 |
# Draw your GUI elements with the GUI camera. |
| 52 |
with gui_camera: |
| 53 |
label.draw() |
| 54 |
|
| 55 |
""" |
| 56 |
|
| 57 |
|
| 58 |
class Camera: |
| 59 |
""" A simple 2D camera that contains the speed and offset.""" |
| 60 |
|
| 61 |
def __init__(self, window: pyglet.window.Window, scroll_speed=1, min_zoom=1, max_zoom=4): |
| 62 |
assert min_zoom <= max_zoom, "Minimum zoom must not be greater than maximum zoom" |
| 63 |
self._window = window |
| 64 |
self.scroll_speed = scroll_speed |
| 65 |
self.max_zoom = max_zoom |
| 66 |
self.min_zoom = min_zoom |
| 67 |
self.offset_x = 0 |
| 68 |
self.offset_y = 0 |
| 69 |
self._zoom = max(min(1, self.max_zoom), self.min_zoom) |
| 70 |
|
| 71 |
@property |
| 72 |
def zoom(self): |
| 73 |
return self._zoom |
| 74 |
|
| 75 |
@zoom.setter |
| 76 |
def zoom(self, value): |
| 77 |
""" Here we set zoom, clamp value to minimum of min_zoom and max of max_zoom.""" |
| 78 |
self._zoom = max(min(value, self.max_zoom), self.min_zoom) |
| 79 |
|
| 80 |
@property |
| 81 |
def position(self): |
| 82 |
"""Query the current offset.""" |
| 83 |
return self.offset_x, self.offset_y |
| 84 |
|
| 85 |
@position.setter |
| 86 |
def position(self, value): |
| 87 |
"""Set the scroll offset directly.""" |
| 88 |
self.offset_x, self.offset_y = value |
| 89 |
|
| 90 |
def move(self, axis_x, axis_y): |
| 91 |
""" Move axis direction with scroll_speed. |
| 92 |
Example: Move left -> move(-1, 0) |
| 93 |
""" |
| 94 |
self.offset_x += self.scroll_speed * axis_x |
| 95 |
self.offset_y += self.scroll_speed * axis_y |
| 96 |
|
| 97 |
def begin(self): |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
view_matrix = self._window.view.translate((-self.offset_x * self._zoom, -self.offset_y * self._zoom, 0)) |
| 102 |
|
| 103 |
view_matrix = view_matrix.scale((self._zoom, self._zoom, 1)) |
| 104 |
|
| 105 |
self._window.view = view_matrix |
| 106 |
|
| 107 |
def end(self): |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
view_matrix = self._window.view.scale((1 / self._zoom, 1 / self._zoom, 1)) |
| 114 |
|
| 115 |
view_matrix = view_matrix.translate((self.offset_x * self._zoom, self.offset_y * self._zoom, 0)) |
| 116 |
|
| 117 |
self._window.view = view_matrix |
| 118 |
|
| 119 |
def __enter__(self): |
| 120 |
self.begin() |
| 121 |
|
| 122 |
def __exit__(self, exception_type, exception_value, traceback): |
| 123 |
self.end() |
| 124 |
|
| 125 |
|
| 126 |
class CenteredCamera(Camera): |
| 127 |
"""A simple 2D camera class. 0, 0 will be the center of the screen, as opposed to the bottom left.""" |
| 128 |
|
| 129 |
def begin(self): |
| 130 |
x = -self._window.width // 2 / self._zoom + self.offset_x |
| 131 |
y = -self._window.height // 2 / self._zoom + self.offset_y |
| 132 |
|
| 133 |
view_matrix = self._window.view.translate((-x * self._zoom, -y * self._zoom, 0)) |
| 134 |
view_matrix = view_matrix.scale((self._zoom, self._zoom, 1)) |
| 135 |
self._window.view = view_matrix |
| 136 |
|
| 137 |
def end(self): |
| 138 |
x = -self._window.width // 2 / self._zoom + self.offset_x |
| 139 |
y = -self._window.height // 2 / self._zoom + self.offset_y |
| 140 |
|
| 141 |
view_matrix = self._window.view.scale((1 / self._zoom, 1 / self._zoom, 1)) |
| 142 |
view_matrix = view_matrix.translate((x * self._zoom, y * self._zoom, 0)) |
| 143 |
self._window.view = view_matrix |
| 144 |
|