diff options
Diffstat (limited to 'anim/src/words.zig')
| -rw-r--r-- | anim/src/words.zig | 216 |
1 files changed, 216 insertions, 0 deletions
diff --git a/anim/src/words.zig b/anim/src/words.zig new file mode 100644 index 0000000..ff9c5c1 --- /dev/null +++ b/anim/src/words.zig @@ -0,0 +1,216 @@ +//! 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 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 }, + + 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 stage = c.stage; + // The margins are the page's, whatever the camera has brought under them. + u.fill(.{ .x = -c.width, .y = -c.height, .w = 3 * c.width, .h = c.height + stage.y }, theme.color.page); + u.fill(.{ .x = -c.width, .y = stage.y + stage.h, .w = 3 * c.width, .h = 2 * c.height }, theme.color.page); + u.fill(.{ .x = -c.width, .y = 0, .w = c.width + stage.x - 8, .h = c.height }, theme.color.page); + u.fill(.{ .x = stage.x + stage.w + 8, .y = 0, .w = c.width, .h = c.height }, theme.color.page); + u.fill(.{ .x = stage.x, .y = c.clock_y - 1, .w = stage.w, .h = theme.stroke.rule }, theme.color.rule); + u.fill(.{ .x = stage.x, .y = c.clock_y - 2, .w = stage.w * now.place, .h = 4 }, theme.mark(.current).solid); + const size = theme.size.mono.context; + const base = c.clock_y + 28; + const count = u.print("moment {d} of {d}", .{ now.stops[0] -| now.way[0], now.way[1] -| now.way[0] }); + 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); + } + } +}; + +/// 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); + } + } +}; + +/// 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, + entries: []const Entry = &.{ .current, .written, .changed, .read, .pointer, .allocated, .freed, .unknown, .kernel }, + + pub const Entry = enum { current, written, changed, read, pointer, allocated, 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" }, + .pointer => .{ "pointer", "holds an address" }, + .allocated => .{ "allocated", "handed out" }, + .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.written(cell, if (entry == .written) .{ .level = 1, .solid = 1 } else .ember, null); + u.label(ink.face, size, cell, .center, digits, ink.ink); + }, + .read => for ([_]Rect{ half, other }, [_][]const u8{ "2a", "00" }) |cell, digits| { + u.readMark(cell, 1); + u.label(.mono, size, cell, .center, digits, t.primary); + }, + .pointer => { + u.fill(half, theme.color.raised); + u.frame(half, theme.mark(.pointer).solid, theme.stroke.arrow); + u.arrow(&.{ .{ r.x + 46, r.y + 23 }, .{ r.x + r.w, r.y + 23 } }, theme.mark(.pointer).solid, .{}); + }, + .allocated => { + u.fill(r, theme.mark(.allocated).ember); + u.frame(r, theme.mark(.allocated).solid, theme.stroke.line); + }, + .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 }; +} |
