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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
|
//! The `map` and the `minimap` (design/README.md, "The map and the
//! minimap"): the working frame unfolded, every panel at its full length on
//! a stage of 3840 by 2160 at the context's type size, and the same at a
//! twentieth in the caption band's right end. Its panels are the working
//! frame's components bound to a stage of their own (`split`), drawn with
//! `ui.mapping` set: then they publish their rows (`row`), and in the
//! working frame their windows (`window`); the map outlines each working
//! window round the rows of its own that it shows.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
const ui = @import("ui.zig");
const Recording = @import("Recording.zig");
const Stage = @import("stage.zig").Stage;
const theme = ui.theme;
const Rect = theme.Rect;
const Split = theme.layout.Split;
pub const Panel = enum { band, view, source, disasm, registers, stack, backtrace };
/// `dive`'s frames: the windows bright by `bright`, the rest veiled by
/// `move`, when the panels set off (each `apart` after the one before, for
/// `travel`); the veil lifts from `lift` as the map lands at `land`; then
/// `still`. `surface` takes `surface` back.
pub const diving = struct {
pub const bright = 6;
pub const move = 24;
pub const apart = 6;
/// A panel's way, and the map's.
pub const travel = 60;
pub const fall = 72;
pub const lift = 84;
/// The last of seven panels in place: 24 + 6 * 6 + 60.
pub const land = 120;
pub const still = 30;
pub const surface = 84;
};
/// Whether every frame is the map (`Map.wholeMap`): `CANIM_MAP` in the environment.
pub fn looking(f: *sk.Frame) bool {
return f.host.env("CANIM_MAP") != null;
}
/// The dive's frames over which the falling map crossfades into the minimap's drawing.
pub const minimap_lands = [2]f32{ 90, 96 };
/// How much of the minimap shows at dive frame `t`: none while the map falls, crossfading in as it lands.
pub fn minimapShows(t: f32) f32 {
return std.math.clamp((t - minimap_lands[0]) / (minimap_lands[1] - minimap_lands[0]), 0, 1);
}
/// Where each working window is on the map this frame, in canvas pixels.
var window_px: [7]?[4]f32 = @splat(null);
/// The working frame's windows this frame: the first and last key of the
/// rows each shows (a line number, an address), those it shows at the
/// step's start, and how far (0 to 1, eased) it has gone from them.
var windows: [7]?Window = @splat(null);
const Window = struct { now: [2]u64, was: [2]u64, p: f32 };
/// The map's rows this frame, in canvas pixels: each one's key and box.
var rows: [7][160]struct { key: u64, box: [4]f32 } = undefined;
var counts: [7]usize = @splat(0);
/// The most rows each map panel has wanted so far (`measure`): its box is
/// as long as that, its content, and no longer.
/// shortcut: process-wide and a frame behind, as the drawing flags are; a scene's first frame packs by the defaults.
var held: [7]usize = @splat(0);
/// While the map draws: `panel` wants `n` rows for what it shows now.
pub fn measure(panel: Panel, n: usize) void {
if (ui.mapping) held[@intFromEnum(panel)] = @max(held[@intFromEnum(panel)], n);
}
/// A working panel shows the rows of keys `now` (first and last), going
/// there from `was`, `p` of the way (eased): its outline on the minimap
/// glides with its scroll, and never jumps.
pub fn window(panel: Panel, now: [2]u64, was: [2]u64, p: f32) void {
if (!ui.mapping) windows[@intFromEnum(panel)] = .{ .now = now, .was = was, .p = p };
}
/// A map panel's row of key `key` is at `r`, in `u`'s units.
pub fn row(panel: Panel, key: u64, u: ui.Ui, r: Rect) void {
if (!ui.mapping) return;
const i = @intFromEnum(panel);
if (counts[i] == rows[i].len) return;
const a = u.at.xf.apply(.{ r.x, r.y });
const b = u.at.xf.apply(.{ r.x + r.w, r.y + r.h });
rows[i][counts[i]] = .{ .key = key, .box = .{ a[0], a[1], b[0], b[1] } };
counts[i] += 1;
}
/// The map's boxes for a recording, packed by content: each panel as long
/// as its rows (`measure`), the next under it 12 apart (design/README.md,
/// "The map and the minimap"). The drawing and the source in the first
/// column, the backtrace at its foot; the disassembly in the second; the
/// lens's view, the registers, and in the rest the stack going on
/// (`continued`), in the third; the stack in the fourth, whole.
// shortcut: a column's rest after the disassembly is page: the 5-row dumps round what the registers point at, the open loupes and the key are not built.
pub fn split(rec: *const Recording, lens: bool, way: [2]u64) Split {
const st = theme.map.stage;
const c = theme.map.columns;
const w = theme.map.column;
const gap = theme.canvas.row_gap;
const panelH = theme.layout.panelH;
const foot = st.y + st.h;
const want = struct {
fn of(panel: Panel, default: usize) f32 {
const n = held[@intFromEnum(panel)];
return @floatFromInt(if (n > 0) n else default);
}
}.of;
const band: Rect = .{ .x = c[0], .y = st.y, .w = w, .h = if (lens) theme.layout.split.subject.band.h else 0 };
const source_y = if (lens) band.y + band.h + gap else st.y;
// The backtrace as long as the call chain gets over the scene's way: packed once, never repacked under the eye.
const bt_rows = @max(want(.backtrace, 0), deepest(rec, way));
const source_rows: f32 = @floatFromInt(@min(rec.line_count, theme.layout.rowsIn(foot - source_y - gap - panelH(bt_rows))));
const source: Rect = .{ .x = c[0], .y = source_y, .w = w, .h = panelH(source_rows) };
const bt_y = source.y + source.h + gap;
// The view as long as what draws there has wanted: none where nothing does (a lens drawn alone).
const view_rows = @min(want(.view, 0), theme.map.memory_rows);
const view: Rect = .{ .x = c[2], .y = st.y, .w = w, .h = if (lens and view_rows > 0) panelH(view_rows) else 0 };
const registers: Rect = .{ .x = c[2], .y = if (view.h > 0) view.y + view.h + gap else st.y, .w = w, .h = panelH(17) };
const more_y = registers.y + registers.h + gap;
const more_h = foot - more_y;
const disasm: Rect = .{ .x = c[1], .y = st.y, .w = w, .h = panelH(@min(want(.disasm, theme.map.disasm_rows), theme.map.disasm_rows)) };
// The source longer than its column (69 lines round the current line, or the file whole if shorter)
// goes on at the top of the next column's free space, under its tag, `continued`.
const lines_left: f32 = @as(f32, @floatFromInt(@min(rec.line_count, theme.map.disasm_rows))) - source_rows;
const under_disasm = disasm.y + disasm.h + gap;
const more_rows = @min(lines_left, @as(f32, @floatFromInt(theme.layout.rowsIn(foot - under_disasm))));
const source_more: Rect = .{ .x = c[1], .y = under_disasm, .w = w, .h = if (more_rows >= 3) panelH(more_rows) else 0 };
const backtrace: Rect = .{ .x = c[0], .y = bt_y, .w = w, .h = @min(panelH(bt_rows), foot - bt_y) };
const rest = struct {
fn under(r: Rect, bottom: f32) Rect {
const y = r.y + r.h + theme.canvas.row_gap;
return .{ .x = r.x, .y = y, .w = r.w, .h = @max(bottom - y, 0) };
}
}.under;
return .{
.band = band,
.view = view,
.source = source,
.disasm = disasm,
.registers = registers,
.stack = .{ .x = c[3], .y = st.y, .w = w, .h = st.h },
.backtrace = backtrace,
.dump_rows = theme.map.memory_rows,
.more = .{ .x = c[2], .y = more_y, .w = w, .h = if (more_h >= panelH(3)) theme.layout.panelH(@floor((more_h - 32) / 24)) else 0 },
.source_more = source_more,
.spare = .{ rest(backtrace, foot), if (source_more.h > 0) rest(source_more, foot) else rest(disasm, foot) },
};
}
/// The most frames the call chain has from moment `way[0]` to `way[1]` (5 where it knows none).
fn deepest(rec: *const Recording, way: [2]u64) f32 {
const context = @import("context.zig");
var most: usize = 0;
var seen: ?@TypeOf(rec.depths[0]) = null;
var i = way[0];
while (i <= @min(way[1], rec.end())) : (i += 1) {
if (seen != null and rec.depths[i] <= seen.?) continue;
seen = rec.depths[i];
var buf: [24]context.Frame = undefined;
most = @max(most, context.frames(rec, i, &buf).len);
}
return @floatFromInt(if (most > 0) most else 5);
}
/// What the map gives the rest of its first two columns, in the
/// catalogue's order once the stack has had more (its own column, and the
/// registers' column's rest, `stack, continued`): a short dump round each
/// place the registers and the stack's words point at, outside the stack
/// and the code (five rows, the target's row in the middle, its region's
/// ink: registers first, in pwndbg's order, then the words up from rsp,
/// none within another's rows); the loupes open, each a lifted panel of
/// nine rows; and last the key. Only then page.
pub const Spare = struct {
cursor: @import("cursor.zig").Cursor,
stage: *const Stage,
map: *const Map,
pub fn draw(sp: *const Spare, f: *sk.Frame, placed: anim.Placed) void {
if (!ui.mapping) return;
const hexdump = @import("hexdump.zig");
const context = @import("context.zig");
const rows_ = @import("rows.zig");
const m = theme.metric.context;
const panelH = theme.layout.panelH;
var free = sp.stage.now(placed).spare;
const now = sp.cursor.now(placed);
const rec = sp.cursor.rec;
const moment = now.moment();
const regs = (moment.regs(rec.tid) catch null) orelse return;
const things = @import("objects.zig").reach(f.scratch, rec, moment, null);
// The next box of height `h` in what is left: from the top of the first column with room.
const take = struct {
fn of(left: *[2]Rect, h: f32) ?Rect {
for (left) |*r| if (r.h >= h) {
const out: Rect = .{ .x = r.x, .y = r.y, .w = r.w, .h = h };
r.y += h + theme.canvas.row_gap;
r.h = @max(r.h - h - theme.canvas.row_gap, 0);
return out;
};
return null;
}
}.of;
const Target = struct { name: []const u8, at: u64 };
var targets: [24]Target = undefined;
var n: usize = 0;
const add = struct {
fn of(list: []Target, count: *usize, mo: Recording.Moment, name: []const u8, at: u64) void {
if (count.* == list.len) return;
switch (rows_.regionOf(mo, at)) {
.heap, .data, .rwx, .rodata => {},
.none, .stack, .code => return,
}
var byte: [8]u8 = undefined;
var known: [8]bool = undefined;
mo.read(at & ~@as(u64, 7), &byte, &known) catch return;
if (!known[0]) return;
// None within another's five rows.
for (list[0..count.*]) |t_| if (@abs(@as(i64, @bitCast((at & ~@as(u64, 15)) -% (t_.at & ~@as(u64, 15))))) < 5 * 16) return;
list[count.*] = .{ .name = name, .at = at };
count.* += 1;
}
}.of;
for (theme.metric.all_registers[0..16]) |name| {
if (std.mem.eql(u8, name, "rsp") or std.mem.eql(u8, name, "rbp")) continue;
add(&targets, &n, moment, name, regs.named(name) orelse continue);
}
var word: u64 = 0;
while (word < 64) : (word += 1) {
const addr = regs.rsp + 8 * word;
const points = (Recording.word(moment, addr, 8) catch null) orelse continue;
add(&targets, &n, moment, f.format("rsp+{x}", .{8 * word}), points);
}
for (targets[0..n]) |target| {
const box = take(&free, panelH(5)) orelse break;
const u = context.at(f, placed, box);
const region = rows_.regionOf(moment, target.at);
_ = u.panel(box.w, box.h, target.name, .{ .note = f.format("\u{2192} {x} \u{00b7} {s}", .{ target.at, regionName(region) }) });
hexdump.drawArcs(u, now, 0, m.tag + m.pad, (target.at & ~@as(u64, 15)) -| 32, 5, .{ .things = things, .region = rows_.ink(region) }, false, .{ m.tag, box.h - m.pad });
}
// The loupes open: lifted, nine rows round their target.
for (sp.map.loupes) |maybe| {
const l = maybe orelse continue;
const shows = std.math.clamp(placed.value(l.opening) * theme.time.loupe.open / theme.time.loupe.rise, 0, 1) * (1 - placed.value(l.closing));
if (shows <= 0) continue;
const aim = l.aim(now) orelse continue;
const box = take(&free, panelH(9)) orelse break;
const u = context.at(f, placed, box).faded(shows);
u.fill(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }, theme.color.lift);
_ = u.panel(box.w, box.h, "loupe", .{ .note = f.format("{s} \u{2192} {x}", .{ aim.name, aim.target }) });
hexdump.drawArcs(u, now, 0, m.tag + m.pad, (aim.target & ~@as(u64, 15)) -| 64, 9, .{ .things = things, .region = rows_.ink(rows_.regionOf(moment, aim.target)) }, true, .{ m.tag, box.h - m.pad });
}
// The key: the recording and the moment, the marks, the inks.
if (take(&free, panelH(3))) |box| key(context.at(f, placed, box), box, rec, now);
}
fn regionName(region: @import("ztrace").telescope.Region) []const u8 {
return switch (region) {
.stack => "stack",
.heap => "heap",
.code => "code",
.data, .rwx => "data",
.rodata => "read-only",
.none => "",
};
}
/// The map's key, three rows: what ran and where in it the map is; the marks, each drawn as it is; the inks.
fn key(u: ui.Ui, box: Rect, rec: *const Recording, now: @import("cursor.zig").Now) void {
const m = theme.metric.context;
const t = theme.color.text;
const size = theme.size.mono.context;
const body = u.panel(box.w, box.h, "key", .{ .note = "the marks and inks of every picture", .legend = &.{} });
const rows_ = @import("rows.zig");
var y = body.y + m.pad;
u.text(.mono, size, m.side, rows_.baseline(y), u.print("{s} \u{00b7} moment {d} of {d}", .{ rec.file, now.marked() -| now.way[0], now.way[1] -| now.way[0] }), t.primary);
y += m.row;
var x: f32 = m.side;
u.current(.{ .x = x, .y = y, .w = 120, .h = m.row }, 40, 0);
u.text(.mono, size, x + 48, rows_.baseline(y), "current", t.primary);
x += 160;
_ = u.written(.{ .x = x + 6, .y = y, .w = 24, .h = m.row }, .struck);
u.text(.mono_bold, size, x + 6, rows_.baseline(y), "2a", t.bright);
u.text(.mono, size, x + 48, rows_.baseline(y), "written", t.primary);
x += 160;
u.corner(x + 2, y + 2, m.corner, 1);
u.text(.mono, size, x + 6, rows_.baseline(y), "2a", t.primary);
u.text(.mono, size, x + 48, rows_.baseline(y), "changed", t.primary);
x += 160;
u.read(.{ .x = x + 6, .y = y, .w = 24, .h = m.row }, 1);
u.text(.mono, size, x + 6, rows_.baseline(y), "2a", t.primary);
u.text(.mono, size, x + 48, rows_.baseline(y), "read", t.primary);
y += m.row;
x = m.side;
for (@import("context.zig").legend) |entry| {
u.text(.mono, size, x, rows_.baseline(y), entry.text, entry.ink);
if (entry.under) u.fill(.{ .x = x, .y = rows_.baseline(y) + 3, .w = u.width(.mono, size, entry.text), .h = theme.stroke.rule }, entry.ink);
x += u.width(.mono, size, entry.text) + 24;
}
}
};
/// The map: its stage, and the components drawn on it, each a working
/// frame's component bound to that stage.
pub const Map = struct {
stage: *Stage,
parts: [12]Part = undefined,
n: usize = 0,
/// The panel the narration is in: its window bright on the minimap.
focus: ?Panel = null,
/// The working frame, whose panels dive out of their windows.
frame: *const Stage,
/// Frames into a `dive` (0: the map fills the frame; `diving.land` and
/// on: the working frame, the map its minimap).
t: anim.Value,
/// The map as an object of the scene, under the panels: it shows from `show`.
obj: anim.Obj = undefined,
/// Where the program is: for the thread across the gutter, and the minimap's flashes.
cursor: ?@import("cursor.zig").Cursor = null,
/// The scene's loupes: open, each is a panel of its own on the map (`Spare`).
loupes: [2]?*const @import("loupe.zig").Loupe = .{ null, null },
const Part = struct { ctx: *const anyopaque, draw: *const fn (*const anyopaque, *sk.Frame, anim.Placed) void };
/// Puts `component` (bound to `m.stage`) on the map, drawn in the order added.
pub fn add(m: *Map, s: *anim.Frame, component: anytype) void {
const T = @TypeOf(component);
const kept = s.new(component);
m.parts[m.n] = .{ .ctx = kept, .draw = struct {
fn draw(ctx: *const anyopaque, f: *sk.Frame, at: anim.Placed) void {
@as(*const T, @ptrCast(@alignCast(ctx))).draw(f, at);
}
}.draw };
m.n += 1;
}
/// Puts a drawing on the map, in a band of its own over the source,
/// for a scene whose working frame has none (its map is split anew).
pub fn drawing(m: *Map, s: *anim.Frame, rec: *const Recording, component: anytype) void {
m.stage.first = split(rec, true, if (m.cursor) |c| .{ c.first, c.last } else .{ 0, 0 });
m.add(s, component);
}
/// Whether the working frame is in place (the minimap is the map).
pub fn settled(m: *const Map, at: anim.Placed) bool {
return at.value(m.t) >= diving.land;
}
/// The map filling the frame, before a scene's first `dive`: it fades
/// in (18 f) with `with` (the working panels, at their windows on it).
pub fn show(m: *const Map, s: *anim.Frame, with: anytype) void {
s.play(.{m.t.to(0)}, .{ .seconds = 0 });
var all: [with.len + 1]anim.Animation = undefined;
all[0] = m.obj.fadeIn();
inline for (with, 1..) |o, i| all[i] = o.fadeIn();
s.play(all, .{ .seconds = theme.time.cascade, .ease = anim.ease.linear });
}
/// `dive`: the windows go bright, the rest of the map is veiled; each
/// window travels to its panel's place, growing from the map's size to
/// the frame's on a logarithm (`glide`, 6 f apart in reading order),
/// as the map falls to the minimap's place; the veil lifts as it lands;
/// then the frame is still.
pub fn dive(m: *const Map, s: *anim.Frame) void {
s.play(.{m.t.to(diving.land + diving.still)}, .{ .seconds = @as(f32, diving.land + diving.still) / 60.0, .ease = anim.ease.linear });
}
/// `surface`: the dive backwards, a little quicker; then the map is
/// still before anything plays on it.
pub fn surface(m: *const Map, s: *anim.Frame) void {
s.play(.{m.t.to(diving.land)}, .{ .seconds = 0 });
s.play(.{m.t.to(0)}, .{ .seconds = @as(f32, diving.surface) / 60.0, .ease = anim.ease.linear });
s.wait(@as(f32, diving.still) / 60.0);
}
/// The map while it is shown or travelling (the scene's first object):
/// filling the frame at half its size, or on its way to the minimap.
/// It sets where each working panel is drawn on the way (`context.zoomed`).
pub fn draw(m: *const Map, f: *sk.Frame, placed: anim.Placed) void {
const context = @import("context.zig");
context.zoomed = @splat(null);
const t = placed.value(m.t);
context.thread_shows = std.math.clamp((t - diving.land) / 12, 0, 1);
if (t >= diving.land) return;
const fixed = @import("words.zig").fixed(f, placed, 0);
const at = m.falling(f, placed, t);
// Large, it is under the panels diving out of it; small (`falling`), over them, into the corner.
if (at.scale >= small * fixed.at.scale) m.copy(f, at, fixed, t) else fixed.fill(.{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }, theme.color.page);
// Each working panel: from its window on the map at the start to its place, 6 f after the one before.
const back = at.xf.invert();
const working = m.frame.now(placed);
// In the frame's reading order, except that a panel drawn over another
// sets off first: a panel in its place is never crossed by one still
// on its way (the disassembly over the source's foot, the stack over
// the registers' lower rows were, for a third of a second).
inline for (.{ "band", "view", "disasm", "stack", "source", "registers", "backtrace" }, 0..) |name, order| {
const i = @intFromEnum(@field(Panel, name));
const box = @field(working, name);
if (box.h > 0) if (window_px[i]) |w| {
const a = back.apply(.{ w[0], w[1] });
const b = back.apply(.{ w[2], w[3] });
// On the map as it fills the frame (half its units): the panel's rows on the window's rows, its tag above them;
// a window that is the panel whole (the band, the registers) has its tag in it.
const s0 = (b[0] - a[0]) / 2 / box.w;
const whole = i == @intFromEnum(Panel.band) or i == @intFromEnum(Panel.registers);
const lead = (if (whole) 0 else theme.metric.context.tag + theme.metric.context.pad) * s0;
const from: Rect = .{ .x = a[0] / 2, .y = a[1] / 2 - lead, .w = box.w * s0, .h = box.h * s0 };
const p = theme.ease(.glide, std.math.clamp((t - diving.move - @as(f32, @floatFromInt(order)) * diving.apart) / diving.travel, 0, 1));
const scale = std.math.pow(f32, s0, 1 - p);
const x = std.math.lerp(from.x, box.x, p);
const y = std.math.lerp(from.y, box.y, p);
// The tags, rules and lanes come in as the map lands.
const chrome = std.math.clamp((t - diving.lift) / 12, 0, 1);
context.zoomed[i] = .{ .box = box, .to = .{ .x = x, .y = y, .w = box.w * scale, .h = box.h * scale }, .chrome = chrome };
};
}
}
/// Frames into the dive now; 0 (the map filling the frame, as a scene
/// that opens on it shows it) with `CANIM_MAP` in the environment.
fn diveAt(m: *const Map, f: *sk.Frame, placed: anim.Placed) f32 {
return if (looking(f)) 0 else placed.value(m.t);
}
/// With `CANIM_MAP` in the environment every frame is the map filling
/// it, as a scene that opens on it shows it (the clock draws it, `wholeMap`; the
/// working panels, the ties and the loupes stand aside): for looking at
/// any scene's map at 1:1 in a 4K export.
pub fn wholeMap(m: *const Map, f: *sk.Frame, placed: anim.Placed) void {
const fixed = @import("words.zig").fixed(f, placed, 0);
m.copy(f, m.falling(f, placed, 0), fixed, 0);
}
/// Below this scale (of the frame's) the falling map is drawn over the
/// panels, on its way into the corner, and not under them.
const small = 0.12;
/// Where the map is at dive frame `t`: from filling the frame (half
/// its units) to the minimap's rect (a twentieth), its scale on the
/// logarithm, its corner going toward the minimap's as the scale does,
/// so that it falls into the corner.
fn falling(m: *const Map, f: *sk.Frame, placed: anim.Placed, t: f32) anim.Placed {
_ = m;
const fixed = @import("words.zig").fixed(f, placed, 0);
const q = theme.ease(.glide, std.math.clamp((t - diving.move) / diving.fall, 0, 1));
const mini = theme.minimap.box;
const k = std.math.pow(f32, 0.5, 1 - q) * std.math.pow(f32, theme.minimap.scale, q);
const toward = (0.5 - k) / (0.5 - theme.minimap.scale);
var at = fixed.at;
at.xf = at.xf.mul(.translate(toward * mini.x, toward * mini.y)).mul(.scale(k, k));
at.scale = fixed.at.scale * k;
return at;
}
/// The falling map, veiled, its windows outlined, and its edge as the
/// minimap's; it crossfades into the minimap's own drawing as it lands.
fn copy(m: *const Map, f: *sk.Frame, at: anim.Placed, fixed: ui.Ui, t: f32) void {
const shows = 1 - minimapShows(t);
if (shows <= 0) return;
const mini = at.scale < small * fixed.at.scale;
if (!mini) fixed.fill(.{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }, theme.color.page);
var faded = at;
faded.pose.opacity *= shows;
m.drawAt(f, faded, mini);
// The rest of the map veiled from 6 f to 24 f, lifting as it lands.
const veil = std.math.clamp((t - diving.bright) / (diving.move - diving.bright), 0, 1) * (1 - std.math.clamp((t - diving.lift) / (minimap_lands[1] - diving.lift), 0, 1));
(ui.Ui{ .f = f, .at = faded }).veil(theme.map.stage, veil);
// The windows bright from 0 to 6 f, over the veil.
m.outline(f, faded, mini, std.math.clamp(t / diving.bright, 0, 1));
var edge: ui.Ui = .{ .f = f, .at = faded };
edge.at.xf = .identity;
const a = at.xf.apply(.{ 0, 0 });
const b = at.xf.apply(.{ 2 * theme.canvas.width, 2 * theme.canvas.height });
const q = std.math.clamp((0.5 * fixed.at.scale - at.scale) / ((0.5 - theme.minimap.scale) * fixed.at.scale), 0, 1);
edge.faded(q).frame(.{ .x = a[0] - 1, .y = a[1] - 1, .w = b[0] - a[0] + 2, .h = b[1] - a[1] + 2 }, theme.color.rule, @max(1, fixed.at.scale));
}
/// Small, the falling map over the working panels (the minimap's drawer calls it).
pub fn fallingOver(m: *const Map, f: *sk.Frame, placed: anim.Placed) void {
const t = placed.value(m.t);
if (t >= minimap_lands[1]) return;
const fixed = @import("words.zig").fixed(f, placed, 0);
const at = m.falling(f, placed, t);
if (at.scale >= small * fixed.at.scale) return;
// Its ground, so that it reads over what it passes.
var ground: ui.Ui = .{ .f = f, .at = at };
ground.at.pose.opacity *= 1 - minimapShows(t);
ground.fill(.{ .x = 0, .y = 0, .w = 2 * theme.canvas.width, .h = 2 * theme.canvas.height }, theme.color.page);
m.copy(f, at, fixed, t);
}
/// The map in `at`'s transform (the map's units), and the working
/// frame's windows outlined on it: a 2-unit `dim` frame 4 out; on the
/// minimap (`mini`) a pixel of primary, the focused one 1.5.
pub fn drawAt(m: *const Map, f: *sk.Frame, at: anim.Placed, mini: bool) void {
m.drawParts(f, at, mini);
m.outline(f, at, mini, 0);
}
fn drawParts(m: *const Map, f: *sk.Frame, at: anim.Placed, mini: bool) void {
ui.mapping = true;
ui.strokes = mini;
counts = @splat(0);
for (m.parts[0..m.n]) |p| p.draw(p.ctx, f, at);
if (m.cursor) |c| m.thread(f, at, c.now(at), mini);
ui.mapping = false;
ui.strokes = false;
}
/// The thread on the map: the current line and the pc's row, joined
/// across the gutter between source and disassembly (x 1008), gold; on
/// the minimap a stroke a pixel wide. And there, a mark no working
/// window shows `flash`es: a ring 8 across drawing out to 14 and gone
/// (12 f), with the strike (the minimap's own time, 6 f behind).
fn thread(m: *const Map, f: *sk.Frame, at: anim.Placed, now: @import("cursor.zig").Now, mini: bool) void {
_ = m;
const rec = now.rec;
const k = at.scale;
var raw: ui.Ui = .{ .f = f, .at = at };
raw.at.xf = .identity;
const marked = now.marked();
const line = rows_of(.source, rec.own[marked]);
const pc = rows_of(.disasm, rec.register(marked, "rip"));
if (line != null and pc != null) {
const a = line.?;
const b = pc.?;
const lane = at.xf.apply(.{ 1008, 0 })[0];
const w = if (mini) @max(1, k / theme.minimap.scale) else theme.stroke.line * k;
const gold = theme.mark(.current).solid;
raw.route(&.{ .{ a[2], (a[1] + a[3]) / 2 }, .{ lane, (a[1] + a[3]) / 2 }, .{ lane, (b[1] + b[3]) / 2 }, .{ b[0], (b[1] + b[3]) / 2 } }, gold, .{ .width = w, .head = null, .radius = 0 });
}
if (!mini or !now.walking()) return;
// A mark no working window shows: a word struck or read, on the
// stack (its own column or continued) or in memory the map draws
// (the view's dump, the dumps round the pointers), flashes where the
// map has it, in the mark's colour.
const since = now.since(.memory, 0);
const p = since / (12.0 / 60.0);
if (p < 0 or p > 1) return;
const marks = [2]struct { spans: []const Recording.Span, ink: u24 }{
.{ .spans = rec.written(now.stops[1], now.stops[0]), .ink = theme.color.struck },
.{ .spans = rec.read(now.stops[1], now.stops[0]), .ink = theme.mark(.read).solid },
};
for (marks) |mark| for (mark.spans) |span| {
// Where the map has it: the stack's word, or the 16 bytes of a dump's row.
const word = span.addr & ~@as(u64, 7);
const sixteen = span.addr & ~@as(u64, 15);
const panel: Panel, const key: u64 = if (rows_of(.stack, word) != null) .{ .stack, word } else .{ .view, sixteen };
const box = rows_of(panel, key) orelse continue;
if (windows[@intFromEnum(panel)]) |win| if (key >= win.now[0] and key <= win.now[1]) continue;
const c = ui.Vec{ (box[0] + box[2]) / 2, (box[1] + box[3]) / 2 };
const r = std.math.lerp(@as(f32, 4), 7, p) * k / theme.minimap.scale;
raw.faded(1 - p).disc(c, r, null, ui.rgb(mark.ink), @max(1, k / theme.minimap.scale));
};
}
/// The top and bottom of the map's rows of panel `i` with keys from `keys[0]` to `keys[1]`, in canvas pixels.
fn spanOf(i: usize, keys: [2]u64) ?[2]f32 {
var top: f32 = std.math.inf(f32);
var bottom: f32 = -std.math.inf(f32);
for (rows[i][0..counts[i]]) |rw| if (rw.key >= keys[0] and rw.key <= keys[1]) {
top = @min(top, rw.box[1]);
bottom = @max(bottom, rw.box[3]);
};
return if (top < bottom) .{ top, bottom } else null;
}
/// Where the map's row of key `key` in `panel` is this frame, in canvas pixels.
fn rows_of(panel: Panel, key: u64) ?[4]f32 {
const i = @intFromEnum(panel);
for (rows[i][0..counts[i]]) |rw| if (rw.key == key) return rw.box;
return null;
}
/// The working windows' outlines on the map in `at`, `bright` of the way
/// from `dim` to bright; where they are is kept (`window_px`).
fn outline(m: *const Map, f: *sk.Frame, at: anim.Placed, mini: bool, bright: f32) void {
var raw: ui.Ui = .{ .f = f, .at = at };
raw.at.xf = .identity;
const k = at.scale;
const split_ = m.stage.now(at);
inline for (.{ "band", "view", "source", "disasm", "registers", "stack", "backtrace" }) |name| {
const panel = @field(Panel, name);
const i = @intFromEnum(panel);
const box = @field(split_, name);
if (box.h > 0) {
const a = at.xf.apply(.{ box.x, box.y });
const b = at.xf.apply(.{ box.x + box.w, box.y + box.h });
var r: [4]f32 = .{ a[0], a[1], b[0], b[1] };
var shown = panel == .registers or panel == .band;
if (windows[i]) |win| {
// Where the window was and where it is, and between them as far as its scroll has gone.
const is = spanOf(i, win.now);
const was = spanOf(i, win.was) orelse is;
if (is orelse was) |to| {
const from = was orelse to;
// On the minimap each end on whole pixels: crisp at rest, between them only while it glides.
const snap = struct {
fn of(v: f32, on: bool) f32 {
return if (on) @round(v) else v;
}
}.of;
r[1] = std.math.lerp(snap(from[0], mini), snap(to[0], mini), win.p);
r[3] = std.math.lerp(snap(from[1], mini), snap(to[1], mini), win.p);
shown = true;
}
}
// On the minimap on whole pixels (a window's ends already are, at rest).
if (mini) {
r[0] = @round(r[0]);
r[2] = @round(r[2]);
if (windows[i] == null) {
r[1] = @round(r[1]);
r[3] = @round(r[3]);
}
}
window_px[i] = if (shown) r else null;
if (shown) {
const focus = m.focus == panel;
// On the minimap a pixel of the frame: `k` is the canvas's pixels to a map unit.
const w = if (mini) @as(f32, if (focus) 1.5 else 1) * k / theme.minimap.scale else theme.map.outline * k;
const out = if (mini) w else theme.map.outline_out * k;
const ring: Rect = .{ .x = r[0] - out, .y = r[1] - out, .w = r[2] - r[0] + 2 * out, .h = r[3] - r[1] + 2 * out };
const ink = ui.mix(if (mini) theme.color.text.primary else theme.color.text.dim, theme.color.text.bright, bright);
// On the minimap not snapped to pixels: a window that glides moves a pixel over many frames, not in one.
if (mini) {
const pen = raw.at.pen(.{ .fill = ink });
for ([4]sk.math.Rect{ .{ .x = ring.x, .y = ring.y, .w = ring.w, .h = w }, .{ .x = ring.x, .y = ring.y + ring.h - w, .w = ring.w, .h = w }, .{ .x = ring.x, .y = ring.y + w, .w = w, .h = ring.h - 2 * w }, .{ .x = ring.x + ring.w - w, .y = ring.y + w, .w = w, .h = ring.h - 2 * w } }) |edge| sk.draw.rect(f, edge, pen);
} else raw.frame(ring, ink, if (bright > 0) w * 1.5 else w);
}
}
}
}
};
/// The minimap's drawings, a frame apart, and what they are averaged into:
/// the host's images, made once for a size.
/// shortcut: a process-wide cache of host images, for one player at a time; two at once want it in their Session.
var fades: struct { w: u32 = 0, h: u32 = 0, images: [theme.time.minimap_fade + 1]sk.Image = undefined } = .{};
/// The minimap: `m` at a twentieth in the caption band's right end, still
/// while the camera moves; the page under it, framed a pixel in `rule`.
/// Every change on it fades in over `time.minimap_fade` frames from
/// `time.minimap_lag` after its onset: it is the average of that many
/// drawings, each from the cursor's mirror a frame later (`Cursor.lagInto`),
/// so a stroke that changes, a mark that comes and the gold that moves on
/// all fade, and a window's glide is smoothed, never stepped.
pub fn minimap(m: *const Map, f: *sk.Frame, at: anim.Placed) void {
var shown = at;
shown.pose.opacity *= minimapShows(m.diveAt(f, at));
if (shown.pose.opacity <= 0) return;
const fixed = @import("words.zig").fixed(f, shown, 0);
const box = theme.minimap.box;
const a = fixed.at.xf.apply(.{ box.x, box.y });
const b = fixed.at.xf.apply(.{ box.x + box.w, box.y + box.h });
const px: sk.Rect = .{ .x = @round(a[0]), .y = @round(a[1]), .w = @round(b[0]) - @round(a[0]), .h = @round(b[1]) - @round(a[1]) };
const w: u32 = @intFromFloat(@max(px.w, 1));
const h: u32 = @intFromFloat(@max(px.h, 1));
if (fades.w != w or fades.h != h) {
if (fades.w != 0) for (fades.images) |img| f.host.freeImage(img);
for (&fades.images) |*img| img.* = f.host.newImage(w, h);
fades.w = w;
fades.h = h;
}
// Each drawing in an image of its own, the page under it, in the image's pixels.
var placed = fixed.at;
placed.pose.opacity = 1;
placed.xf = sk.math.Affine.translate(-px.x, -px.y).mul(fixed.at.xf).mul(.translate(box.x, box.y)).mul(.scale(theme.minimap.scale, theme.minimap.scale));
placed.scale = fixed.at.scale * theme.minimap.scale;
const values = f.scratch.dupe(f32, at.values) catch return;
placed.values = values;
// Its drawings outline the windows in their images' pixels: where the
// falling map has them (a dive's panels set off from there) is kept.
const falling_px = window_px;
defer window_px = falling_px;
const n = theme.time.minimap_fade;
var weights: [n]f32 = @splat(0);
var made: usize = 0;
for (0..n) |k| {
// A mirror the same as the one before draws the same: its drawing counts twice.
const differs = if (m.cursor) |c| c.lagInto(values, k) else k == 0;
if (differs) {
f.target(fades.images[made]);
f.clear(ui.rgb(theme.color.page));
m.drawAt(f, placed, true);
made += 1;
}
weights[made - 1] += 1;
}
// Averaged: each drawing over those before it at its share of the drawings so far.
const whole: sk.Rect = .{ .x = 0, .y = 0, .w = px.w, .h = px.h };
f.target(fades.images[n]);
var so_far: f32 = 0;
for (fades.images[0..made], weights[0..made]) |img, weight| {
so_far += weight;
f.image(img, whole, .{ .tint = sk.Color.white.alphaFloat(weight / so_far) });
}
f.target(.none);
f.image(fades.images[n], px, .{ .tint = sk.Color.white.alphaFloat(shown.pose.opacity) });
fixed.frame(.{ .x = box.x - 1, .y = box.y - 1, .w = box.w + 2, .h = box.h + 2 }, theme.color.rule, 1);
}
test "the map's columns hold their panels" {
try std.testing.expectEqual(@as(f32, 1688), theme.layout.panelH(theme.map.disasm_rows));
const st = theme.map.stage;
try std.testing.expectEqual(@as(f32, 3840), st.x + st.w + st.x);
}
|