summaryrefslogtreecommitdiff
path: root/src/sketch/camera.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/camera.zig')
-rw-r--r--src/sketch/camera.zig60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/sketch/camera.zig b/src/sketch/camera.zig
new file mode 100644
index 0000000..460fc8a
--- /dev/null
+++ b/src/sketch/camera.zig
@@ -0,0 +1,60 @@
+//! A 2D camera maps world points to canvas pixels. Zoom keeps the world point
+//! underneath the pointer stationary; panning is measured in screen pixels.
+//! Points and rects are duck-typed (see `shape.zig`) and come back in the
+//! caller's own type.
+const std = @import("std");
+const shape = @import("shape.zig");
+
+pub const Camera = struct {
+ x: f32 = 0,
+ y: f32 = 0,
+ zoom: f32 = 1,
+
+ pub fn toScreen(self: Camera, world: anytype) shape.Out(@TypeOf(world)) {
+ const T = shape.Out(@TypeOf(world));
+ const x = self.x + shape.get(world, "x") * self.zoom;
+ const y = self.y + shape.get(world, "y") * self.zoom;
+ return if (comptime shape.isRect(T))
+ shape.make(T, x, y, shape.get(world, "w") * self.zoom, shape.get(world, "h") * self.zoom)
+ else
+ shape.point(T, x, y);
+ }
+ pub fn toWorld(self: Camera, screen: anytype) shape.Out(@TypeOf(screen)) {
+ const T = shape.Out(@TypeOf(screen));
+ const x = (shape.get(screen, "x") - self.x) / self.zoom;
+ const y = (shape.get(screen, "y") - self.y) / self.zoom;
+ return if (comptime shape.isRect(T))
+ shape.make(T, x, y, shape.get(screen, "w") / self.zoom, shape.get(screen, "h") / self.zoom)
+ else
+ shape.point(T, x, y);
+ }
+ pub fn pan(self: *Camera, delta: anytype) void {
+ self.x += shape.get(delta, "x");
+ self.y += shape.get(delta, "y");
+ }
+ /// Zoom by `1.2^wheel` around `screen`, clamped to `[min, max]`.
+ pub fn zoomAt(self: *Camera, screen: anytype, wheel: f32, limits: struct { min: f32 = 0.04, max: f32 = 12 }) void {
+ if (!std.math.isFinite(wheel)) return;
+ const sx = shape.get(screen, "x");
+ const sy = shape.get(screen, "y");
+ const wx = (sx - self.x) / self.zoom;
+ const wy = (sy - self.y) / self.zoom;
+ self.zoom = std.math.clamp(self.zoom * std.math.pow(f32, 1.2, std.math.clamp(wheel, -40, 40)), limits.min, limits.max);
+ self.x = sx - wx * self.zoom;
+ self.y = sy - wy * self.zoom;
+ }
+};
+
+test "zooming keeps the point under the cursor fixed, in any shape" {
+ var camera: Camera = .{ .x = 10, .y = 20, .zoom = 0.5 };
+ const P = struct { x: f32, y: f32 };
+ const before = camera.toWorld(P{ .x = 300, .y = 200 });
+ camera.zoomAt(@Vector(2, f32){ 300, 200 }, 3, .{});
+ const after = camera.toWorld(P{ .x = 300, .y = 200 });
+ try std.testing.expectApproxEqAbs(before.x, after.x, 1e-3);
+ try std.testing.expectApproxEqAbs(before.y, after.y, 1e-3);
+ const r = camera.toScreen([4]f32{ 0, 0, 10, 10 });
+ try std.testing.expectApproxEqAbs(camera.zoom * 10, r[2], 1e-4);
+ // Literals come back as f32 points.
+ try std.testing.expectEqual(shape.Point{ .x = 12, .y = 21 }, (Camera{ .x = 10, .y = 20 }).toScreen(.{ .x = 2, .y = 1 }));
+}