-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.py
362 lines (300 loc) · 12.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""Class for rendering things to a surface, relative to a camera-position.
worldspace == Coordinates in space
screenspace == Coordinates on the screen
"""
from __future__ import annotations
import pygame
import pygame.gfxdraw
from pygame import Color, Rect
from pygame.math import Vector2 as Vec2
class Camera:
"""A camera with dynamic position and zoom, drawing to a fixed Surface."""
def __init__(self, center: Vec2, zoom: float, surface: pygame.Surface) -> None:
"""Construct a new camera.
Args:
----
center (Vec2): Worldspace-coordinate at the center of the screen
zoom (float): Higher = Fewer objects fit on screen,
zoom==1 corresponds to 1 pixel per unit
surface (pygame.Surface): Surface to draw on
"""
self.zoom: float = zoom
self.surface: pygame.Surface = surface
# Convert `center` to topleft corner
self.pos: Vec2 = Vec2(center) - Vec2(surface.get_size()) / (2 * zoom)
def smoothly_transition_to(
self,
new_pos: Vec2,
new_zoom: float,
dt: float,
transition_time: float = 0.25,
) -> None:
"""Smoothly transition the camera to a new location.
Args:
----
new_pos (Vec2): New camera worldspace topleft corner
new_zoom (float): New zoom-factor
dt (float): Time-factor (for the smooth operation)
transition_time (float, optional): After this amount of dt has passed,
the camera will have fully transitioned. Defaults to 0.25
"""
dist = self.pos.distance_to(new_pos)
self.pos.move_towards_ip(new_pos, dist * dt / transition_time)
# This makes it easier to write, please don't judge me
zoomy = Vec2(self.zoom, 0)
new_zoomy = Vec2(new_zoom, 0)
dist = abs(self.zoom - new_zoom)
self.zoom = zoomy.move_towards(new_zoomy, dist * dt / transition_time).x
def smoothly_focus_rect(
self,
rect: Rect,
dt: float,
transition_time: float = 0.25,
) -> None:
"""Smoothly move the camera so that a worldspace-rectangle is
visible entirely, but not more.
Args:
----
rect (Rect): Worldspace-rectangle to fit to
dt (float): Time-factor (for the smooth operation)
transition_time (float, optional): After this amount of dt has passed,
the camera will have fully transitioned. Defaults to 0.25
"""
ratio = rect.width / rect.height
surface_width = self.surface.get_width()
surface_height = self.surface.get_height()
desired_ratio = surface_width / surface_height
if ratio > desired_ratio:
# Width dominates, height is too small
new_zoom = surface_width / rect.width
new_height = rect.width / desired_ratio
rect.inflate_ip(0, new_height - rect.height)
else:
# Height dominates, width is too small
new_zoom = surface_height / rect.height
new_width = rect.height * desired_ratio
rect.inflate_ip(new_width - rect.width, 0)
self.smoothly_transition_to(Vec2(rect.topleft), new_zoom, dt, transition_time)
def smoothly_focus_points(
self,
points: list[Vec2],
buff: float,
dt: float,
transition_time: float = 0.25,
) -> None:
"""Smoothly focus camera so that a list of worldspace-points is
visible, with an additional buffer.
Args:
----
points (list[Vec2]): Worldspace-points to focus on. Hopefully nonempty.
buff (float): Worldspace-buffer around the points
dt (float): Time-factor (for the smooth operation)
transition_time (float, optional): After this amount of dt has passed,
the camera will have fully transitioned. Defaults to 0.25
"""
enclosing_rect = _get_enclosing_rect(points)
buffed_rect = enclosing_rect.inflate(2 * buff, 2 * buff)
self.smoothly_focus_rect(buffed_rect, dt, transition_time)
def _rectangle_intersects_screen(self, rect: Rect) -> bool:
"""Determine whether a screenspace-rectangle intersects the camera's screen.
Args:
----
rect (Rect): Screenspace-rectangle
Returns:
-------
bool: True iff screenspace-rectangle intersects the screen
"""
own_rect = Rect((0, 0), self.surface.get_size())
# Inflate rect, to take care of edge-cases like zero width or height
return own_rect.colliderect(rect.inflate(1, 1))
def world_to_screen(self, vec: Vec2) -> Vec2:
"""Transform a worldspace-vector to screenspace.
Args:
----
vec (Vec2): Worldspace-vector
Returns:
-------
Vec2: Screenspace-vector
"""
return (vec - self.pos) * self.zoom
def start_drawing_new_frame(self) -> None:
"""Fill the camera's surface black to prepare for drawing a new frame."""
self.surface.fill(Color("black"))
def draw_circle(self, color: Color, center: Vec2, radius: float) -> None:
"""Draw an anti-aliased worldspace-circle on screen.
Args:
----
color (Color): Border- and fill-color
center (Vec2): Worldspace-center of the circle
radius (float): Worldspace-radius of the circle
"""
ccenter, cradius = self.world_to_screen(center), radius * self.zoom
# ??? Why only ints?
x, y, r = int(ccenter.x), int(ccenter.y), int(cradius)
# soft check for circle-screen-intersection:
enclosing_rect = Rect((x - r, y - r), (2 * r, 2 * r))
if self._rectangle_intersects_screen(enclosing_rect):
pygame.gfxdraw.aacircle(self.surface, x, y, r, color)
pygame.gfxdraw.filled_circle(self.surface, x, y, r, color)
def draw_polygon(self, color: Color, points: list[Vec2]) -> None:
"""Draw an anti-aliased worldspace-polygon on screen.
Args:
----
color (Color): Border- and fill-color
points (list[Vec2]): Worldspace-points
"""
cpoints = [self.world_to_screen(p) for p in points]
# Soft check for points-screen-intersection:
enclosing_rect = _get_enclosing_rect(cpoints)
if self._rectangle_intersects_screen(enclosing_rect):
pygame.gfxdraw.aapolygon(self.surface, cpoints, color)
pygame.gfxdraw.filled_polygon(self.surface, cpoints, color)
def draw_line(self, color: Color, start: Vec2, end: Vec2, thickness: float) -> None:
"""Draw an anti-aliased worldspace-line with a given thickness.
Args:
----
color (Color): Border- and fill-color
start (Vec2): Line's start-worldspace-point
end (Vec2): Line's end-worldspace-point
thickness (float): Line's worldspace-thickness
"""
delta = end - start
if delta == Vec2(0, 0):
return
orthogonal = Vec2(-delta.y, delta.x).normalize() * thickness / 2
points = [
start + orthogonal,
end + orthogonal,
end - orthogonal,
start - orthogonal,
]
# Need not check whether this is on-screen, as
# draw_polygon does it for us
self.draw_polygon(color, points)
def draw_hairline(self, color: Color, start: Vec2, end: Vec2) -> None:
"""Draw an anti-aliased worldspace-line of single-pixel-thickness.
Args:
----
color (Color): Line's color
start (Vec2): Line's start-worldspace-point
end (Vec2): Line's end-worldspace-point
"""
tstart, tend = self.world_to_screen(start), self.world_to_screen(end)
screen_rect = Rect((0, 0), self.surface.get_size())
clipped_line = screen_rect.clipline(tstart, tend)
if clipped_line:
((x1, y1), (x2, y2)) = clipped_line
pygame.gfxdraw.line(self.surface, x1, y1, x2, y2, color)
def draw_vertical_hairline(
self,
color: Color,
x: float,
starty: float,
endy: float,
) -> None:
"""Draw a vertical worldspace-line of single-pixel-thickness.
Args:
----
color (Color): Line's Color
x (float): Line's horizontal position
starty (float): Line's starting point
endy (float): Line's ending point
"""
tstart, tend = (
self.world_to_screen(Vec2(x, starty)),
self.world_to_screen(Vec2(x, endy)),
)
screen_rect = Rect((0, 0), self.surface.get_size())
clipped_line = screen_rect.clipline(tstart, tend)
if clipped_line:
((x, y1), (_, y2)) = clipped_line
pygame.gfxdraw.vline(self.surface, x, y1, y2, color)
def draw_horizontal_hairline(
self,
color: Color,
startx: float,
endx: float,
y: float,
) -> None:
"""Draw a horizontal worldspace-line of single-pixel-thickness.
Args:
----
color (Color): Line's Color
startx (float): Line's starting point
endx (float): Line's ending point
y (float): Line's vertical position
"""
tstart, tend = (
self.world_to_screen(Vec2(startx, y)),
self.world_to_screen(Vec2(endx, y)),
)
screen_rect = Rect((0, 0), self.surface.get_size())
clipped_line = screen_rect.clipline(tstart, tend)
if clipped_line:
((x1, y), (x2, _)) = clipped_line
pygame.gfxdraw.hline(self.surface, x1, x2, y, color)
def draw_rect(self, color: Color, rect: Rect) -> None:
"""Draw an anti-aliased worldspace-rectangle.
Args:
----
color (Color): Border- and fill-color
rect (Rect): Worldspace rectangle to draw
"""
ttopleft = self.world_to_screen(Vec2(rect.topleft))
tbottomright = self.world_to_screen(Vec2(rect.bottomright))
screen_rect = Rect(ttopleft, tbottomright - ttopleft)
if self._rectangle_intersects_screen(screen_rect):
pygame.gfxdraw.box(self.surface, screen_rect, color)
def draw_text(
self,
text: str,
pos: Vec2 | None,
font: pygame.font.Font,
color: Color,
) -> None:
"""Draw text on screen at screenspace-position, or centered on screen.
Args:
----
text (str): Text to render
pos (Vec2 | None): If Vec2, screenspace-position of text's top-left-corner,
if None, text will be centered on screen
font (pygame.font.Font): Font to be used
color (Color): Text's fill color
"""
rendered = font.render(text, True, color)
if pos is None:
width, height = self.surface.get_size()
pos = Vec2(
(width - rendered.get_width()) / 2,
(height - rendered.get_height()) / 2,
)
self.surface.blit(rendered, pos)
def draw_image(self, image: pygame.Surface, pos: Vec2) -> None:
"""Draw an image on screen at screenspace-position.
Args:
----
image (pygame.Surface): Image to draw
pos (Vec2): Screenspace-position of the image's top-left-corner
"""
zoomed_image = pygame.transform.scale(
image,
(int(image.get_width() * self.zoom), int(image.get_height() * self.zoom)),
)
self.surface.blit(zoomed_image, self.world_to_screen(pos))
def _get_enclosing_rect(points: list[Vec2]) -> Rect:
"""Get the smallest rectangle enclosing all points.
Args:
----
points (list[Vec2]): Points to enclose
Returns:
-------
Rect: Rectangle fitting all points snugly
"""
minx = miny = float("inf")
maxx = maxy = float("-inf")
for point in points:
minx = min(minx, point.x)
maxx = max(maxx, point.x)
miny = min(miny, point.y)
maxy = max(maxy, point.y)
return Rect((minx, miny), (maxx - minx, maxy - miny))