summaryrefslogtreecommitdiff
path: root/src/sketch/camera.zig
blob: 460fc8a1f77b92ace10c1c3f02232c2db869057b (plain) (blame)
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
//! 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 }));
}