Debian Codegraph @

examples/window/camera_group.py

raw symbols SCIP
1 """ Camera class similar to that of camera.py, this time implemented as a graphics Group.
2 Interface is much more simplified, with only a position and zoom implemented, but is easily
3 extended to add other features such as autoscroll.
4
5 camera = CameraGroup(x=0, y=0, zoom=1)
6
7 world_object = pyglet.some_renderable(batch=batch, group=camera)
8 ui_object = pyglet.some_renderable(batch=batch) # <-- Using the same batch here
9
10 @window.event
11 def on_draw():
12 window.clear()
13 batch.draw() # Only one batch necessary
14
15 A centered camera class is also provided, where the position of the camera is the center of
16 the screen instead of the bottom left.
17
18 centered_camera = CenteredCameraGroup(window, x=0, y=0, zoom=1)
19
20 Demo:
21
22 Use arrow keys to move the camera around the scene.
23 Note that everything in the window can be added to the same batch, as a group is used to
24 separate things in world space from things in "UI" space.
25 """
26
27 import pyglet
28 from pyglet.graphics import Group
29 from pyglet.math import Vec2
30
31
32
33 class CameraGroup(Group):
34 """ Graphics group emulating the behaviour of a camera in 2D space. """
35
36 def __init__(self, window, x, y, zoom=1.0, order=0, parent=None):
37 super().__init__(order, parent)
38 self._window = window
39 self.x = x
40 self.y = y
41 self.zoom = zoom
42
43 @property
44 def position(self) -> Vec2:
45 """Query the current offset."""
46 return Vec2(self.x, self.y)
47
48 @position.setter
49 def position(self, new_position: Vec2):
50 """Set the scroll offset directly."""
51 self.x, self.y = new_position
52
53 def set_state(self):
54 """ Apply zoom and camera offset to view matrix. """
55
56 # Translate using the offset.
57 view_matrix = self._window.view.translate(-self.x * self.zoom, -self.y * self.zoom, 0)
58 # Scale by zoom level.
59 view_matrix = view_matrix.scale(self.zoom, self.zoom, 1)
60
61 self._window.view = view_matrix
62
63 def unset_state(self):
64 """ Revert zoom and camera offset from view matrix. """
65 # Since this is a matrix, you will need to reverse the translate after rendering otherwise
66 # it will multiply the current offset every draw update pushing it further and further away.
67
68 # Use inverse zoom to reverse zoom
69 view_matrix = self._window.view.scale(1 / self.zoom, 1 / self.zoom, 1)
70 # Reverse translate.
71 view_matrix = view_matrix.translate(self.x * self.zoom, self.y * self.zoom, 0)
72
73 self._window.view = view_matrix
74
75
76 class CenteredCameraGroup(CameraGroup):
77 """ Alternative centered camera group.
78
79 (0, 0) will be the center of the screen, as opposed to the bottom left.
80 """
81
82 def set_state(self):
83 # Translate almost the same as normal, but add the center offset
84 x = -self._window.width // 2 / self.zoom + self.x
85 y = -self._window.height // 2 / self.zoom + self.y
86
87 view_matrix = self._window.view.translate((-x * self.zoom, -y * self.zoom, 0))
88 view_matrix = view_matrix.scale((self.zoom, self.zoom, 1))
89 self._window.view = view_matrix
90
91 def unset_state(self):
92
93 x = -self._window.width // 2 / self.zoom + self.x
94 y = -self._window.height // 2 / self.zoom + self.y
95
96 view_matrix = self._window.view.scale((1 / self.zoom, 1 / self.zoom, 1))
97 view_matrix = view_matrix.translate((x * self.zoom, y * self.zoom, 0))
98 self._window.view = view_matrix
99
100
101 if __name__ == "__main__":
102 from pyglet.window import key
103
104 # Create a window and a batch
105 window = pyglet.window.Window(resizable=True)
106 batch = pyglet.graphics.Batch()
107
108 # Key handler for movement
109 keys = key.KeyStateHandler()
110 window.push_handlers(keys)
111
112 # Use centered
113 camera = CenteredCameraGroup(window, 0, 0)
114 # Use un-centered
115 # camera = CameraGroup(0, 0)
116
117 # Create a scene
118 rect = pyglet.shapes.Rectangle(-25, -25, 50, 50, batch=batch, group=camera)
119 text = pyglet.text.Label("Text works too!", x=0, y=-50, anchor_x="center", batch=batch, group=camera)
120
121 # Create some "UI"
122 ui_text = pyglet.text.Label(
123 "Simply don't add to the group to make UI static (like this)",
124 anchor_y="bottom", batch=batch,
125 )
126 position_text = pyglet.text.Label(
127 "",
128 x=window.width,
129 anchor_x="right", anchor_y="bottom",
130 batch=batch,
131 )
132
133 @window.event
134 def on_draw():
135 # Draw our scene
136 window.clear()
137 batch.draw()
138
139 @window.event
140 def on_resize(width: float, height: float):
141 # Keep position text label to the right
142 position_text.x = width
143
144 def on_update(dt: float):
145 # Move camera with arrow keys
146 if keys[key.UP]:
147 camera.y += 50*dt
148 if keys[key.DOWN]:
149 camera.y -= 50*dt
150 if keys[key.LEFT]:
151 camera.x -= 50*dt
152 if keys[key.RIGHT]:
153 camera.x += 50*dt
154
155 # Update position text label
156 position_text.text = repr(round(camera.position))
157
158 # Start the demo
159 pyglet.clock.schedule(on_update)
160 pyglet.app.run()