summaryrefslogtreecommitdiff
path: root/anim/src/words.zig
blob: 9bec54ca24b3883a4bea7d28193bbdf18861ec3f (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
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
//! Words on the frame, and what frames them: the clock over the caption,
//! the narrator's caption, labels and titles, the legend, and the veil
//! that dims everything but the subject. The clock and the caption stay
//! where they are while the camera moves.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
const ui = @import("ui.zig");
const Cursor = @import("cursor.zig").Cursor;
const cursors = @import("cursor.zig");
const theme = ui.theme;
const Rect = ui.Rect;
const t = theme.color.text;

/// The clock: a rule the width of the stage, over the caption, gold along
/// it as far as the recording has run (linear: time itself; still at a
/// walk, sweeping at a sprint, the one thing on screen that says "fast"),
/// and under its right end the count of moments; while steps are hurried
/// it counts them too. Add it after the panels and before the first
/// caption: it also covers the margins round the stage, so that a camera
/// gone nearer shows nothing outside it.
pub const Clock = struct {
    cursor: Cursor,
    box: Rect = .{ .x = theme.canvas.stage.x, .y = theme.canvas.clock_y - 12, .w = theme.canvas.stage.w, .h = 24 },
    /// The map: with `CANIM_MAP`, drawn over everything (`Map.wholeMap`).
    map: ?*const @import("map.zig").Map = null,
    /// The stage: in the column (`Stage.column`) the clock is under the text column alone.
    stage: ?*const @import("stage.zig").Stage = null,

    pub fn draw(k: *const Clock, f: *sk.Frame, at: anim.Placed) void {
        const u = fixed(f, at, 0);
        const c = theme.canvas;
        const now = k.cursor.now(at);
        const in_column = if (k.stage) |st| at.value(st.column) else 0;
        const whole = c.stage;
        // The margins are the page's, whatever the camera has brought under them
        // (in the column the panes run on under the floor, 888 to 1032).
        u.fill(.{ .x = -c.width, .y = -c.height, .w = 3 * c.width, .h = c.height + whole.y }, theme.color.page);
        const floor_x: f32 = if (in_column > 0) theme.layout.state.x - c.gutter / 2 else -c.width;
        u.fill(.{ .x = floor_x, .y = whole.y + whole.h, .w = 3 * c.width, .h = 2 * c.height }, theme.color.page);
        u.fill(.{ .x = -c.width, .y = 0, .w = c.width + whole.x - 8, .h = c.height }, theme.color.page);
        u.fill(.{ .x = whole.x + whole.w + 8, .y = 0, .w = c.width, .h = c.height }, theme.color.page);
        // The rule: the stage's width, or the text column's.
        var stage = whole;
        stage.x = std.math.lerp(whole.x, theme.layout.state.x, theme.ease(.glide, in_column));
        stage.w = whole.x + whole.w - stage.x;
        u.fill(.{ .x = stage.x, .y = c.clock_y - 1, .w = stage.w, .h = theme.stroke.rule }, theme.color.rule);
        // Time itself: still while a step plays at a walk or a run; once its
        // mark has landed, the step's piece fades in (12 f, 8 at a run) and
        // the count changes as it starts. At a sprint it grows with the recording.
        const gold = theme.mark(.current).solid;
        const at_stop = struct {
            fn of(n: cursors.Now, m: u64) f32 {
                return std.math.clamp(@as(f32, @floatFromInt(m -| n.way[0])) / @as(f32, @floatFromInt(@max(n.way[1] -| n.way[0], 1))), 0, 1);
            }
        }.of;
        var counted = now.marked();
        if (now.plan.gait == .sprint or now.resting()) {
            u.fill(.{ .x = stage.x, .y = c.clock_y - 2, .w = stage.w * now.place, .h = 4 }, gold);
        } else {
            const landed = now.plan.advance + now.plan.travel;
            const fade = std.math.clamp((now.time - landed) / @as(f32, if (now.plan.gait == .run) theme.time.run_ease else theme.time.quick), 0, 1);
            const from = at_stop(now, now.stops[1]);
            const to = at_stop(now, now.stops[0]);
            u.fill(.{ .x = stage.x, .y = c.clock_y - 2, .w = stage.w * from, .h = 4 }, gold);
            if (fade > 0) u.fill(.{ .x = stage.x + stage.w * from, .y = c.clock_y - 2, .w = stage.w * (to - from), .h = 4 }, ui.mix(theme.color.page, gold, fade));
            counted = if (now.time >= landed) now.stops[0] else now.stops[1];
        }
        const size = theme.size.mono.context;
        const base = c.clock_y + 28;
        const count = u.print("moment {d} of {d}", .{ counted -| now.way[0], now.way[1] -| now.way[0] });
        // Ranged right at the stage's right edge, 1856.
        const count_x = stage.x + stage.w - u.width(.mono, size, count);
        u.text(.mono, size, count_x, base, count, t.faint);
        // Secondary action: the pace, said quietly. It only fades.
        if (now.hurried > 0) {
            const steps = if (now.of > 0) u.print("step {d} of {d}", .{ now.count, now.of }) else u.print("step {d}", .{now.count});
            u.faded(now.hurried).text(.mono, size, count_x - u.width(.mono, size, steps) - 3 * ui.advance(size), base, steps, t.dim);
        }
        if (k.map) |m| if (@import("map.zig").looking(f)) m.wholeMap(f, at);
    }
};

/// A bright frame round `box`: where the camera is about to go.
pub const Outline = struct {
    box: Rect,

    pub fn draw(o: *const Outline, f: *sk.Frame, at: anim.Placed) void {
        const out = theme.metric.pointer_frame_outset;
        (ui.Ui{ .f = f, .at = at }).frame(.{ .x = -out, .y = -out, .w = o.box.w + 2 * out, .h = o.box.h + 2 * out }, theme.mark(.attention).solid, theme.stroke.line);
    }
};

/// The narrator's sentence: one or two lines (split at a newline), sans,
/// with `code` between backticks in mono. It fades in rising, and stays
/// where it is while the camera moves.
pub const Caption = struct {
    text: []const u8,
    box: Rect = theme.canvas.caption,

    pub fn draw(c: *const Caption, f: *sk.Frame, at: anim.Placed) void {
        const u = fixed(f, at, (1 - at.pose.opacity) * theme.time.caption_rise);
        const two = std.mem.indexOfScalar(u8, c.text, '\n') != null;
        var lines = std.mem.splitScalar(u8, c.text, '\n');
        var n: usize = if (two) 1 else 0;
        while (lines.next()) |line| : (n += 1) {
            if (n > 2) break;
            _ = runs(u, theme.canvas.margin_x, theme.canvas.caption_baseline[n], line, .sans, theme.size.sans.caption, theme.size.mono.value, t.primary);
        }
    }
};

/// A chapter's title (design/README.md, `chapter`, `title`): said, not
/// carded, in the caption's place, one line of Go Medium 40, its numeral
/// faint and its name bright (`II  Registers`); it arrives rising as a
/// caption does, over the picture it grows from.
pub const Title = struct {
    numeral: []const u8,
    name: []const u8,
    box: Rect = theme.canvas.caption,

    pub fn draw(c: *const Title, f: *sk.Frame, at: anim.Placed) void {
        const u = fixed(f, at, (1 - at.pose.opacity) * theme.time.caption_rise);
        const size = theme.size.sans.caption;
        const base = theme.canvas.caption_baseline[0];
        u.text(.sans_medium, size, theme.canvas.margin_x, base, c.numeral, t.faint);
        const x = theme.canvas.margin_x + u.width(.sans_medium, size, c.numeral) + 2 * u.width(.sans_medium, size, " ");
        u.text(.sans_medium, size, x, base, c.name, t.bright);
    }
};

/// The clock's label (design/README.md, `clock`): the chapter, Go 18,
/// faint, ranged right 24 left of the clock's count (its widest, so that it
/// stands still as the count grows), or alone at 1856 where there is no
/// clock yet. `which` is the chapter (between two, their names
/// crossfading); `docked` 0 alone, 1 beside the count (out over its first
/// half, in over its second, with the clock: it is secondary, it only fades).
pub const ChapterLabel = struct {
    names: []const []const u8,
    which: anim.Value,
    docked: anim.Value,
    /// The count at its widest, `moment 193 of 193`.
    widest: []const u8,

    pub fn draw(l: *const ChapterLabel, f: *sk.Frame, at: anim.Placed) void {
        const u = fixed(f, at, 0);
        const size = theme.size.sans.micro;
        const base = theme.canvas.clock_y + 28;
        const right = theme.canvas.stage.x + theme.canvas.stage.w;
        const ends = [2]f32{ right, right - u.width(.mono, theme.size.mono.context, l.widest) - 24 };
        const which = std.math.clamp(at.value(l.which), 0, @as(f32, @floatFromInt(l.names.len - 1)));
        const lo: usize = @intFromFloat(@floor(which));
        const hi = @min(lo + 1, l.names.len - 1);
        const k = which - @floor(which);
        const docked = std.math.clamp(at.value(l.docked), 0, 1);
        for ([2]usize{ lo, hi }, [2]f32{ 1 - k, k }) |n, shows| {
            if (shows <= 0) continue;
            const name = l.names[n];
            const w = u.width(.sans, size, name);
            // Gone from where it stood alone before it comes beside the count: never over it.
            for (ends, [2]f32{ std.math.clamp(1 - 2 * docked, 0, 1), std.math.clamp(2 * docked - 1, 0, 1) }) |end, here| if (here > 0) u.faded(shows * here).text(.sans, size, end - w, base, name, t.faint);
        }
    }
};

/// Text with `code` between backticks set in mono, from `x` on `baseline`.
/// Returns where it ends.
pub fn runs(u: ui.Ui, x: f32, baseline: f32, text: []const u8, face: ui.Face, size: f32, mono_size: f32, ink: anytype) f32 {
    var at_x = x;
    var parts = std.mem.splitScalar(u8, text, '`');
    var code = false;
    while (parts.next()) |part| : (code = !code) {
        const part_face: ui.Face = if (code) .mono else face;
        const part_size = if (code) mono_size else size;
        u.text(part_face, part_size, at_x, baseline, part, ink);
        at_x += u.width(part_face, part_size, part);
    }
    return at_x;
}

/// A line of text anywhere: a label (sans medium 28), a note, a title.
pub const Label = struct {
    text: []const u8,
    box: Rect,
    face: ui.Face = .sans_medium,
    size: f32 = theme.size.sans.label,
    color: u24 = t.primary,
    how: ui.Align = .left,

    pub fn draw(l: *const Label, f: *sk.Frame, at: anim.Placed) void {
        const u: ui.Ui = .{ .f = f, .at = at };
        var lines = std.mem.splitScalar(u8, l.text, '\n');
        var y: f32 = 0;
        while (lines.next()) |line| : (y += l.box.h) u.label(l.face, l.size, .{ .x = 0, .y = y, .w = l.box.w, .h = l.box.h }, l.how, line, l.color);
    }
};

/// Dims everything added before it except `box`, which gets a bright
/// frame: what the narration is about now.
pub const Veil = struct {
    /// The subject, in the design's units.
    box: Rect,

    pub fn draw(v: *const Veil, f: *sk.Frame, at: anim.Placed) void {
        const u: ui.Ui = .{ .f = f, .at = at };
        const far = 4 * theme.canvas.width;
        const shade = ui.rgb(theme.color.veil).alphaFloat(theme.color.veil_alpha);
        const b = v.box;
        // Four rects round the subject, in its own units (the origin is its corner).
        u.fill(.{ .x = -far, .y = -far, .w = 2 * far + b.w, .h = far }, shade);
        u.fill(.{ .x = -far, .y = b.h, .w = 2 * far + b.w, .h = far }, shade);
        u.fill(.{ .x = -far, .y = 0, .w = far, .h = b.h }, shade);
        u.fill(.{ .x = b.w, .y = 0, .w = far, .h = b.h }, shade);
        const out = theme.metric.pointer_frame_outset;
        u.frame(.{ .x = -out, .y = -out, .w = b.w + 2 * out, .h = b.h + 2 * out }, theme.mark(.attention).solid, theme.stroke.line);
    }
};

/// The marks, each drawn as the components draw it, with its name and what
/// it means: the same in every picture.
pub const Legend = struct {
    box: Rect,
    /// The marks the scenes use.
    entries: []const Entry = &.{ .current, .written, .changed, .read, .waiting, .unknown, .link, .pin, .arc, .loupe, .freed, .kernel },

    pub const Entry = enum { current, written, changed, read, link, pin, arc, loupe, waiting, freed, unknown, kernel };

    pub fn draw(l: *const Legend, f: *sk.Frame, at: anim.Placed) void {
        const u: ui.Ui = .{ .f = f, .at = at };
        const body = u.panel(l.box.w, l.box.h, "legend", .{ .note = "the same marks in every picture" });
        const m = theme.metric.legend;
        const per_row = @max(1, (l.entries.len + 1) / 2);
        const pitch = (body.w - 2 * theme.metric.panel.pad_x) / @as(f32, @floatFromInt(per_row));
        for (l.entries, 0..) |entry, i| {
            const x = theme.metric.panel.pad_x + @as(f32, @floatFromInt(i % per_row)) * pitch;
            const y = body.y + 24 + @as(f32, @floatFromInt(i / per_row)) * m.row_h;
            const sample: Rect = .{ .x = x, .y = y, .w = m.sample_w, .h = 46 };
            const words: [2][]const u8 = switch (entry) {
                .current => .{ "current", "execution is here" },
                .written => .{ "written", "by this step" },
                .changed => .{ "changed", "a moment ago" },
                .read => .{ "read", "by this step" },
                .link => .{ "link", "now points at that" },
                .pin => .{ "pin", "who points here" },
                .arc => .{ "arc", "a pointer in its panel" },
                .loupe => .{ "loupe", "where it points" },
                .waiting => .{ "waiting", "for the call it made" },
                .freed => .{ "freed", "handed back" },
                .unknown => .{ "not seen", "never observed" },
                .kernel => .{ "kernel", "the other side" },
            };
            drawSample(u, entry, sample);
            u.text(.sans_medium, theme.size.sans.label, x + m.text_x, y + 22, words[0], t.primary);
            u.text(.sans, theme.size.sans.micro, x + m.text_x, y + 50, words[1], t.dim);
        }
    }

    fn drawSample(u: ui.Ui, entry: Entry, r: Rect) void {
        const size = theme.size.mono.data;
        const half: Rect = .{ .x = r.x, .y = r.y, .w = 46, .h = 46 };
        const other: Rect = .{ .x = r.x + 48, .y = r.y, .w = 46, .h = 46 };
        switch (entry) {
            .current => {
                u.fill(r, theme.mark(.current).solid);
                u.label(.mono_bold, size, r, .center, "rip", theme.mark(.current).on_solid);
            },
            .written, .changed => for ([_]Rect{ half, other }, [_][]const u8{ "2a", "00" }) |cell, digits| {
                const ink = u.cellWritten(cell, if (entry == .written) .struck else .ember, theme.color.raised);
                u.label(ink.face, size, cell, .center, digits, ink.ink);
            },
            .read => for ([_]Rect{ half, other }, [_][]const u8{ "2a", "00" }) |cell, digits| {
                u.cellRead(cell, 1);
                u.label(.mono, size, cell, .center, digits, t.primary);
            },
            .link => u.route(&.{ .{ r.x, r.y + 23 }, .{ r.x + r.w, r.y + 23 } }, theme.color.region.heap, .{ .width = theme.stroke.tie.link, .head = .{ 10, 10 } }),
            .pin => u.pinRow(r.x, r.y + 11, &.{"rsp"}, theme.color.region.stack),
            .arc => {
                const k = theme.metric.arc;
                u.route(&.{ .{ r.x + r.w, r.y + 8 }, .{ r.x + 20, r.y + 8 }, .{ r.x + 20, r.y + 38 }, .{ r.x + r.w, r.y + 38 } }, theme.color.region.heap, .{ .width = k.width, .head = k.head, .radius = k.radius });
            },
            .loupe => {
                const k = theme.metric.loupe.shadow[0];
                u.fill(.{ .x = r.x + k[0] / 2, .y = r.y + k[1] / 2, .w = r.w - k[0] / 2, .h = r.h - k[1] / 2 }, theme.color.shade);
                const lifted: Rect = .{ .x = r.x, .y = r.y, .w = r.w - k[0] / 2, .h = r.h - k[1] / 2 };
                u.fill(lifted, theme.color.lift);
                u.fill(.{ .x = r.x, .y = r.y, .w = lifted.w, .h = 10 }, theme.color.lift_tag);
                u.frame(lifted, t.dim, theme.stroke.rule);
            },
            .waiting => {
                u.frame(.{ .x = r.x + 1, .y = r.y + 1, .w = r.w - 2, .h = r.h - 2 }, t.primary, theme.stroke.rule);
                u.label(.mono, size, r, .center, "call", t.primary);
            },
            .freed => u.dashedFrame(r, theme.mark(.freed).solid, theme.stroke.line),
            .unknown => {
                u.unknown(half);
                u.unknown(other);
            },
            .kernel => {
                u.fill(r, theme.color.kernel);
                u.label(.mono_bold, size, r, .center, "sys", theme.color.on_kernel.ink);
            },
        }
    }
};

/// A `Ui` in the design's units that the camera does not move: the frame
/// fitted into the canvas, as at zoom 1. `drop` lowers it by that many units.
pub fn fixed(f: *sk.Frame, at: anim.Placed, drop: f32) ui.Ui {
    const w = @max(@floor(f.in.window_width), 1);
    const h = @max(@floor(f.in.window_height), 1);
    const k = @min(w / theme.canvas.width, h / theme.canvas.height);
    var still = at;
    still.scale = k;
    still.xf = sk.math.Affine.translate(w / 2, h / 2).mul(.scale(k, k)).mul(.translate(-theme.canvas.width / 2.0, -theme.canvas.height / 2.0 + drop));
    return .{ .f = f, .at = still };
}