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
|
//! 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, drawn small under the clock's right end: the minimap.
map: ?*const @import("map.zig").Map = 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 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);
// 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 left of the minimap.
const count_x = (if (k.map != null) theme.minimap.count_end else 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 (m.settled(at)) @import("map.zig").minimap(m, 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);
}
}
};
/// 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, .link, .allocated, .freed, .unknown, .kernel },
pub const Entry = enum { current, written, changed, read, link, 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" },
.link => .{ "link", "now points at that" },
.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.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 } }),
.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 };
}
|