//! The `map` and the panes' minimaps (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 each pane's //! column of it, small, in a `strip` inside the pane's frame at its right. //! 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, //! and a strip frames it. 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 }; /// The panes with a strip, in the order of their places in the strips' images. pub const stripped = [_]Panel{ .source, .disasm, .stack, .view }; fn slotOf(panel: Panel) ?usize { return std.mem.indexOfScalar(Panel, &stripped, panel); } /// `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`), each pane's column squeezing into its strip with it; the /// rest of the map gone by `gone`; the strips and the tags in from `lift`; /// then `still` from `land`. `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 its column's. pub const travel = 60; pub const gone = 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; } /// 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 map and /// its window on its strip glide with its scroll, and never jump. 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; } /// How much of a pane's strip shows as the pane fades through to another /// column (another file, another function) over the advance's `travel` /// seconds: out and in as the pane does, as late as the strip's own /// drawing is (`time.minimap_lag` and half its fade), so that the column it /// changes to comes in while the strip is out. pub fn through(now: @import("cursor.zig").Now, travel: f32) f32 { if (now.resting() or travel <= 0) return 1; const lag = theme.time.minimap_lag + @as(f32, theme.time.minimap_fade) / 2.0 / 60.0; return @abs(2 * std.math.clamp((now.time - now.plan.advance - lag) / travel, 0, 1) - 1); } /// Whether `panel`'s column on its strip has the rows of keys `keys[0]` to `keys[1]`. pub fn inColumn(panel: Panel, keys: [2]u64) bool { const col = &atlas.columns[slotOf(panel) orelse return false]; return col.span(.{ keys[0], keys[0] }) != null and col.span(.{ keys[1], keys[1] }) != null; } /// The width a pane's rows have: its box's, less its strip's in the working frame. pub fn content(w: f32) f32 { return if (ui.mapping) w else w - theme.strip.cost; } /// 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, band_h: f32, 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; // The drawing as high as the working frame's. const band: Rect = .{ .x = c[0], .y = st.y, .w = w, .h = band_h }; const lens = band_h > 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.most_lines, 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.most_lines, 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) { // A recording that counts its calls is read where it goes deeper; one of breakpoints (no calls counted: sqlite's) at every stop. if (seen != null and rec.depths[i] <= seen.? and rec.len() > 512) 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: [14]Part = undefined, n: usize = 0, /// 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, each pane's column in its strip). 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 strips' 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 }, /// The scene's `unroll`s, each a pane's column out of its strip for a beat (`Unroll` draws them). unrolls: [4]Unrolled = undefined, n_unrolls: usize = 0, const Unrolled = struct { panel: Panel, veil: anim.Value, grow: anim.Value }; /// `unroll`: the pane `panel`'s whole column out of its strip over the /// frame, for a beat ("the whole function", "the whole stack"): the /// panes it covers veiled and its strip's window bright (18 f), then the /// column growing (36 f, `glide`), its window's outline riding from the /// strip to the pane's rows in it; held until `roll`. pub fn unroll(m: *Map, s: *anim.Frame, panel: Panel) void { const u: Unrolled = .{ .panel = panel, .veil = s.value(0), .grow = s.value(0) }; m.unrolls[m.n_unrolls] = u; m.n_unrolls += 1; const t = theme.time.unroll; s.play(.{u.veil.to(1)}, .{ .seconds = t.veil, .ease = anim.ease.linear }); s.play(.{u.grow.to(1)}, .{ .seconds = t.grow, .ease = anim.ease.linear }); } /// `roll`: the last `unroll` back into its strip (36 f), the window's /// outline landing as the strip's window; the veil lifts (18 f). pub fn roll(m: *Map, s: *anim.Frame) void { const u = m.unrolls[m.n_unrolls - 1]; const t = theme.time.unroll; s.play(.{u.grow.to(0)}, .{ .seconds = t.roll, .ease = anim.ease.linear }); s.play(.{u.veil.to(0)}, .{ .seconds = t.veil, .ease = anim.ease.linear }); } /// Which drawings a part is in: the map's, the strips' (what they are /// drawn from: a pane's column, as the linear disassembly the map draws /// as a graph or a descent), or both. pub const In = enum { both, map, strips }; /// `panel`: the box it draws in (null: a lens's, its drawing and its view), for a column squeezing in a dive. const Part = struct { ctx: *const anyopaque, draw: *const fn (*const anyopaque, *sk.Frame, anim.Placed) void, in: In, panel: ?Panel }; /// Puts `component` (bound to `m.stage`; a lens, unless `addIn` says /// which panel it is) on the map, drawn in the order added. pub fn add(m: *Map, s: *anim.Frame, component: anytype) void { m.addIn(s, component, .both, null); } pub fn addIn(m: *Map, s: *anim.Frame, component: anytype, in: In, panel: ?Panel) 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, .in = in, .panel = panel }; 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, theme.layout.split.subject.band.h, if (m.cursor) |c| .{ c.first, c.last } else .{ 0, 0 }); m.add(s, component); } /// Whether the working frame is in place. 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), /// each pane's column squeezing into its strip with it; the rest of /// the map fades where it is; 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 { // A scene that did not open on it has not shown it yet: under the panels until it rises. s.play(.{ m.t.to(diving.land), m.obj.fadeIn() }, .{ .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 a dive plays (the scene's first /// object): filling the frame at half its size, the rest of it fading /// where it is as each pane's column squeezes into its strip. 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; // The columns' rows, where the strips have them. m.strips(f, placed); const fixed = @import("words.zig").fixed(f, placed, 0); fixed.fill(.{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }, theme.color.page); const at = half(fixed.at); // The rest of the map veiled from 6 to 24 f, then gone where it is by 72. var rest = at; rest.pose.opacity *= 1 - std.math.clamp((t - diving.move) / (diving.gone - diving.move), 0, 1); m.drawParts(f, rest); const veil = std.math.clamp((t - diving.bright) / (diving.move - diving.bright), 0, 1); (ui.Ui{ .f = f, .at = rest }).veil(theme.map.stage, veil); // The windows bright from 0 to 6 f, over the veil: a pane's with a strip rides on its column. const bright = std.math.clamp(t / diving.bright, 0, 1); m.outline(f, rest, bright, true); // 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); var ways: [7]f32 = @splat(0); // 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)); ways[i] = p; 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, and the strips, come in as the map lands. const chrome = std.math.clamp((t - diving.lift) / 12, 0, 1); // The registers arrive whole from the map and fold as they land: each quiet row glides into its cell. context.zoomed[i] = .{ .box = box, .to = .{ .x = x, .y = y, .w = box.w * scale, .h = box.h * scale }, .chrome = chrome, .fold = if (i == @intFromEnum(Panel.registers)) p else 1 }; }; } // Each pane's column squeezing into its strip: their places on the map give way first, then they go. for ([_]bool{ true, false }) |masking| for (stripped) |panel| { const box = switch (panel) { inline else => |tag| @field(working, @tagName(tag)), }; const gone = 1 - stripShows(panel, t); if (box.h > 0 and gone > 0 and windows[@intFromEnum(panel)] != null) m.squeeze(f, fixed, at, panel, box, ways[@intFromEnum(panel)], .{ .masking = masking, .shows = gone, .veil = veil, .bright = bright }); }; } /// How much of a pane's strip shows at dive frame `t`: it comes in as /// its pane lands (12 f), its column going into it. fn stripShows(panel: Panel, t: f32) f32 { // The panels' order setting off (`draw`). const order: f32 = switch (panel) { .band => 0, .view => 1, .disasm => 2, .stack => 3, .source => 4, .registers => 5, .backtrace => 6, }; const lands = diving.move + order * diving.apart + diving.travel; return std.math.clamp((t - lands + 12) / 12, 0, 1); } /// The map filling the frame: half its units. fn half(at: anim.Placed) anim.Placed { var out = at; out.xf = at.xf.mul(.scale(0.5, 0.5)); out.scale = at.scale * 0.5; return out; } const Squeeze = struct { masking: bool, shows: f32, veil: f32, bright: f32 }; /// A pane's column in a dive, `p` of its way (eased) from its place on /// the map (`at`: filling the frame) into the pane's strip: the rows the /// strip shows, its width and its height each on its logarithm (to 36 /// of 880; to 4 of 24 a row), the text thinning as it lands, the /// window's outline riding on it. With `masking`, only its place on the /// map given up (the page over it: the column goes from there). fn squeeze(m: *const Map, f: *sk.Frame, fixed: ui.Ui, at: anim.Placed, panel: Panel, box: Rect, p: f32, how: Squeeze) void { const col = &atlas.columns[slotOf(panel).?]; if (col.n == 0) return; const st = theme.strip; const cm = theme.metric.context; const r = stripRect(box); const cap = (r.h - st.top - st.foot) / st.pitch; const win = windowOn(panel, col) orelse return; const first = firstShown(col, win, cap); const k = [2]f32{ std.math.pow(f32, 0.5, 1 - p) * std.math.pow(f32, st.across, p), std.math.pow(f32, 0.5, 1 - p) * std.math.pow(f32, @as(f32, st.pitch) / cm.row, p) }; var offset: f32 = 0; for (m.column(at, panel)) |b| { if (b.h <= 0) continue; const n: f32 = @floatFromInt(theme.layout.rowsIn(b.h)); defer offset += n; const lo = @max(first, offset); const hi = @min(first + cap, @min(offset + n, col.rows)); if (hi <= lo) continue; const body = b.y + cm.tag + cm.pad; const rows_rect: Rect = .{ .x = b.x, .y = body + (lo - offset) * cm.row, .w = b.w, .h = (hi - lo) * cm.row }; if (how.masking) { (ui.Ui{ .f = f, .at = at }).fill(.{ .x = rows_rect.x, .y = rows_rect.y - cm.pad, .w = rows_rect.w, .h = rows_rect.h + 2 * cm.pad }, theme.color.page); continue; } // Its first row: where it is on the map, and where it goes in the strip. const from = [2]f32{ rows_rect.x / 2, rows_rect.y / 2 }; const to = [2]f32{ r.x + st.left, r.y + st.top + (lo - first) * st.pitch }; var moved = fixed.at; moved.xf = fixed.at.xf.mul(.translate(std.math.lerp(from[0], to[0], p), std.math.lerp(from[1], to[1], p))).mul(.scale(k[0], k[1])).mul(.translate(-rows_rect.x, -rows_rect.y)); moved.scale = fixed.at.scale * k[1]; moved.pose.opacity *= how.shows; const u: ui.Ui = .{ .f = f, .at = moved }; // Its parts clip to their boxes inside it. u.clip(rows_rect); const a0 = moved.xf.apply(.{ rows_rect.x, rows_rect.y }); const a1 = moved.xf.apply(.{ rows_rect.x + rows_rect.w, rows_rect.y + rows_rect.h }); ui.bound = .{ .x = @round(a0[0]), .y = @round(a0[1]), .w = @round(a1[0]) - @round(a0[0]), .h = @round(a1[1]) - @round(a0[1]) }; m.drawPanel(f, moved, panel); // Veiled with the map until it leaves. u.veil(rows_rect, how.veil * (1 - p)); ui.bound = null; u.unclip(); // The window's outline, where its rows are in this box: from the map's (dim, 2 and 4 out) to the strip's (primary, 2). const w0 = @max(win[0], lo); const w1 = @min(win[1] + 1, hi); if (w1 > w0) { const a = moved.xf.apply(.{ rows_rect.x, body + (w0 - offset) * cm.row }); const c = moved.xf.apply(.{ rows_rect.x + rows_rect.w, body + (w1 - offset) * cm.row }); const line = std.math.lerp(theme.map.outline / 2.0, st.window, p) * fixed.at.scale; const out = std.math.lerp(theme.map.outline_out / 2.0, 0, p) * fixed.at.scale; const ink = ui.mix(ui.mix(theme.color.text.dim, theme.color.text.bright, how.bright), theme.color.text.primary, p); var raw: ui.Ui = .{ .f = f, .at = moved }; raw.at.xf = .identity; raw.frame(.{ .x = a[0] - out - line, .y = a[1] - out - line, .w = c[0] - a[0] + 2 * (out + line), .h = c[1] - a[1] + 2 * (out + line) }, ink, line); } } } /// 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); fixed.fill(.{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }, theme.color.page); m.drawAt(f, half(fixed.at)); } /// 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. pub fn drawAt(m: *const Map, f: *sk.Frame, at: anim.Placed) void { m.drawParts(f, at); m.outline(f, at, 0, false); } /// The parts that draw `panel` (a dump: the lens's), as the map draws them. fn drawPanel(m: *const Map, f: *sk.Frame, at: anim.Placed, panel: Panel) void { ui.mapping = true; defer ui.mapping = false; for (m.parts[0..m.n]) |p| if (p.in != .strips and (p.panel == panel or (p.panel == null and panel == .view))) p.draw(p.ctx, f, at); } fn drawParts(m: *const Map, f: *sk.Frame, at: anim.Placed) void { ui.mapping = true; counts = @splat(0); for (m.parts[0..m.n]) |p| if (p.in != .strips) p.draw(p.ctx, f, at); if (m.cursor) |c| m.thread(f, at, c.now(at)); ui.mapping = 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. fn thread(m: *const Map, f: *sk.Frame, at: anim.Placed, now: @import("cursor.zig").Now) void { _ = m; const rec = now.rec; var raw: ui.Ui = .{ .f = f, .at = at }; raw.at.xf = .identity; const marked = now.marked(); const line = rows_of(.source, rec.shown[marked].line) orelse return; const pc = rows_of(.disasm, rec.register(marked, "rip")) orelse return; const lane = at.xf.apply(.{ 1008, 0 })[0]; raw.route(&.{ .{ line[2], (line[1] + line[3]) / 2 }, .{ lane, (line[1] + line[3]) / 2 }, .{ lane, (pc[1] + pc[3]) / 2 }, .{ pc[0], (pc[1] + pc[3]) / 2 } }, theme.mark(.current).solid, .{ .width = theme.stroke.line * at.scale, .head = null, .radius = 0 }); } /// 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 { const r = boxOf(i, keys) orelse return null; return .{ r[1], r[3] }; } /// The box round the map's rows of panel `i` with keys from `keys[0]` to `keys[1]`, in canvas pixels. fn boxOf(i: usize, keys: [2]u64) ?[4]f32 { var r: [4]f32 = .{ std.math.inf(f32), std.math.inf(f32), -std.math.inf(f32), -std.math.inf(f32) }; for (rows[i][0..counts[i]]) |rw| if (rw.key >= keys[0] and rw.key <= keys[1]) { r = .{ @min(r[0], rw.box[0]), @min(r[1], rw.box[1]), @max(r[2], rw.box[2]), @max(r[3], rw.box[3]) }; }; return if (r[1] < r[3]) r 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`). With /// `riding`, a pane's with a strip is not drawn: it rides on its column (`squeeze`). fn outline(m: *const Map, f: *sk.Frame, at: anim.Placed, bright: f32, riding: bool) 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; r[1] = std.math.lerp(from[0], to[0], win.p); r[3] = std.math.lerp(from[1], to[1], win.p); shown = true; } } window_px[i] = if (shown) r else null; const rides = riding and slotOf(panel) != null; if (shown and !rides) { const w = theme.map.outline * k; const out = 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 }; raw.frame(ring, ui.mix(theme.color.text.dim, theme.color.text.bright, bright), if (bright > 0) w * 1.5 else w); } } } } /// The boxes on the map of `panel`'s column, in order: the second where /// it goes on (`continued`), or none. fn column(m: *const Map, at: anim.Placed, panel: Panel) [2]Rect { const sp = m.stage.now(at); const none: Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }; return switch (panel) { .source => .{ sp.source, sp.source_more }, .disasm => .{ sp.disasm, none }, .stack => .{ sp.stack, sp.more }, .view => .{ sp.view, none }, else => .{ none, none }, }; } /// The strips, drawn once a frame: the map drawn for them (`collect`) /// six times, each from the cursor's mirror a frame later (6 to 11 f /// behind: `Cursor.lagInto`) and with the panes' windows as they were /// as long ago (`past`), each into an image of every strip as it shows /// (its rows round its window, the window framed), and those averaged: /// every change on a strip, a window gliding or its column scrolling /// too, fades in over 6 f from 6 f after its onset, and nothing on it /// moves a pixel in a frame. fn strips(m: *const Map, f: *sk.Frame, at: anim.Placed) void { if (atlas.frame == f.in.frame and atlas.map == m) return; atlas.frame = f.in.frame; atlas.map = m; f.clip(null); const st = theme.strip; const k = @import("words.zig").fixed(f, at, 0).at.scale; const w: u32 = @intFromFloat(@ceil(@as(f32, stripped.len * st.w) * k)); const h: u32 = @intFromFloat(@ceil(theme.canvas.stage.h * k)); if (atlas.w != w or atlas.h != h) { if (atlas.w != 0) for (atlas.images) |img| f.host.freeImage(img); for (&atlas.images) |*img| img.* = f.host.newImage(w, h); atlas.w = w; atlas.h = h; } if (atlas.scrap == null) atlas.scrap = f.host.newImage(4, 4); atlas.k = k; // After a seek the windows kept are of another time. if (f.in.jumped) past = @splat(.{}); past[f.in.frame % past.len] = .{ .frame = f.in.frame, .windows = windows }; // Each pane's strip as high as its box has it now (a relayout moves them). const working = m.frame.now(at); var heights: [stripped.len]f32 = undefined; for (stripped, &heights) |panel, *sh| sh.* = switch (panel) { inline else => |tag| stripRect(@field(working, @tagName(tag))).h, }; const values = f.scratch.dupe(f32, at.values) catch return; var placed = at; placed.values = values; placed.xf = .identity; placed.scale = 1; placed.pose.opacity = 1; const n = theme.time.minimap_fade; var weights: [n]f32 = @splat(0); var made: usize = 0; const chrome = ui.chrome; defer ui.chrome = chrome; var cols: [stripped.len]Column = undefined; for (0..n) |lag| { const then = pastWindows(f.in.frame, @as(u64, @intFromFloat(theme.time.minimap_lag * 60)) + lag); // A mirror the same as the one before draws the same (its windows too): its drawing counts twice. const moved = lag > 0 and !std.meta.eql(then, pastWindows(f.in.frame, @as(u64, @intFromFloat(theme.time.minimap_lag * 60)) + lag - 1)); const differs = (if (m.cursor) |c| c.lagInto(values, lag) else lag == 0) or moved; if (differs) { m.collect(f, placed); m.columns(placed, &cols); if (made == 0) atlas.columns = cols; f.target(atlas.images[made]); f.clear(ui.rgb(theme.color.well)); m.render(f, placed, k, &cols, then, heights); 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 = @floatFromInt(w), .h = @floatFromInt(h) }; f.target(atlas.images[n]); var so_far: f32 = 0; for (atlas.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); } /// The map drawn for the strips, into nothing that shows: its parts /// publish their rows and the text, marks and arcs they draw are /// collected (`ui.collected`), with the flashes of the moment. fn collect(m: *const Map, f: *sk.Frame, placed: anim.Placed) void { f.target(atlas.scrap.?); ui.mapping = true; ui.strokes = true; counts = @splat(0); ui.collected.reset(); for (m.parts[0..m.n]) |p| if (p.in != .map) p.draw(p.ctx, f, placed); f.clip(null); n_flashes = 0; if (m.cursor) |c| flash(c.now(placed)); ui.mapping = false; ui.strokes = false; } /// Each strip's column, from the map just collected: its rows' keys and places. fn columns(m: *const Map, at: anim.Placed, out: *[stripped.len]Column) void { for (stripped, out) |panel, *c| { c.* = .{}; const boxes = m.column(at, panel); const i = @intFromEnum(panel); for (rows[i][0..counts[i]]) |rw| { if (c.n == c.keys.len) break; const place = spotIn(boxes, rw.box[0] + 1, rw.box[1], false) orelse continue; c.keys[c.n] = rw.key; c.at[c.n] = place.r; c.n += 1; c.rows = @max(c.rows, @round(place.r) + 1); } } } /// Every strip, as it shows, into the image in hand, a strip's width /// each (`heights`: as high as each is): its column's rows round its /// window (`then`: the panes' windows as long ago as the drawing is), /// each run of text a stroke from where it starts, as long as it is, in /// its ink 65% of the way from the page; at a row's left its marks (the /// current row gold, a strike vermilion, a read in read's colour, held /// a vermilion 2 by 2); an arc with an end off its pane's window a /// 2-unit line in its ink down the left edge; a block's first row a /// tick; a flash's ring; three faint dots at an end where the column /// goes on; the window framed 2 in primary. fn render(m: *const Map, f: *sk.Frame, at: anim.Placed, k: f32, cols: *const [stripped.len]Column, then: [7]?Window, heights: [stripped.len]f32) void { const st = theme.strip; var boxes: [stripped.len][2]Rect = undefined; for (stripped, &boxes) |panel, *b| b.* = m.column(at, panel); // Each strip's window and the first row it shows. var wins: [stripped.len]?[2]f32 = undefined; var firsts: [stripped.len]f32 = undefined; var caps: [stripped.len]f32 = undefined; for (stripped, cols, &wins, &firsts, &caps, heights) |panel, *col, *win, *first, *cap, sh| { cap.* = (sh - st.top - st.foot) / st.pitch; win.* = spanOf2(then[@intFromEnum(panel)], col); // Whole rows at rest: its strokes on the image's pixels. first.* = firstShown(col, win.* orelse .{ 0, 0 }, @floor(cap.*)); } // Where row `r` of strip `slot` is drawn, and whether it shows. const Row = struct { fn y(fs: [stripped.len]f32, slot: usize, r: f32) f32 { return st.top + (r - fs[slot]) * st.pitch; } fn shows(fs: [stripped.len]f32, cs: [stripped.len]f32, slot: usize, r: f32) bool { return r > fs[slot] - 0.75 and r < fs[slot] + cs[slot] - 0.25; } }; const c = &ui.collected; const page = ui.rgb(theme.color.page); for (c.runs[0..c.n_runs]) |run| for (boxes, 0..) |pair, slot| { const spot = spotIn(pair, run.x0, run.y, true) orelse continue; if (Row.shows(firsts, caps, slot, spot.r)) { const ink = ui.mix(page, run.ink.alphaFloat(1), st.ink).alphaFloat(@as(f32, @floatFromInt(run.ink.a)) / 255); pixels(f, k, slot, st.left + spot.x * st.across, Row.y(firsts, slot, spot.r), (run.x1 - run.x0) * st.across, st.stroke, ink); } break; }; // The marks of each row: the most of each kind it has, side by side in the catalogue's order. var shows: [stripped.len][atlas_rows][4]f32 = @splat(@splat(@splat(0))); var place: [stripped.len][atlas_rows]f32 = undefined; for (c.marks[0..c.n_marks]) |mk| for (boxes, 0..) |pair, slot| { const spot = spotIn(pair, mk.x, mk.y, mk.kind != .tick) orelse continue; if (!Row.shows(firsts, caps, slot, spot.r)) break; if (mk.kind == .tick) { pixels(f, k, slot, 0, Row.y(firsts, slot, spot.r) - 1, st.left + st.mark, 2, ui.rgb(theme.color.rule).alphaFloat(mk.shows)); break; } const near = @round(spot.r); if (near < 0 or near >= atlas_rows) break; const i: usize = @intFromFloat(near); shows[slot][i][@intFromEnum(mk.kind)] = @max(shows[slot][i][@intFromEnum(mk.kind)], @min(mk.shows, 1)); place[slot][i] = spot.r; break; }; for (shows, place, 0..) |of_slot, places, slot| for (of_slot, places) |kinds, r| { var x: f32 = st.left; const y = Row.y(firsts, slot, r); const write = ui.rgb(theme.mark(.write).solid); if (kinds[0] > 0) { pixels(f, k, slot, x, y, st.mark, st.mark, ui.rgb(theme.mark(.current).solid).alphaFloat(kinds[0])); x += st.mark_apart; } if (kinds[1] > 0) { pixels(f, k, slot, x, y, st.mark, st.mark, write.alphaFloat(kinds[1])); x += st.mark_apart; } else if (kinds[3] > 0) { pixels(f, k, slot, x + 1, y + 1, st.held, st.held, write.alphaFloat(kinds[3])); x += st.mark_apart; } if (kinds[2] > 0) pixels(f, k, slot, x, y, st.mark, st.mark, ui.rgb(theme.mark(.read).solid).alphaFloat(kinds[2])); }; for (c.arcs[0..c.n_arcs]) |arc| for (boxes, 0..) |pair, slot| { const a = spotIn(pair, arc.x, arc.from, true) orelse continue; const b = spotIn(pair, arc.x, arc.to, true) orelse break; // An arc with both ends in its pane's window is there: only one with an end off it is here. const win = wins[slot] orelse break; const in = struct { fn of(r: f32, w: [2]f32) bool { return r > w[0] - 0.5 and r < w[1] + 0.5; } }.of; if (in(a.r, win) and in(b.r, win)) break; const y0 = @max(Row.y(firsts, slot, @min(a.r, b.r)) + 1, st.top); const y1 = @min(Row.y(firsts, slot, @max(a.r, b.r)) + 2, st.top + caps[slot] * st.pitch); if (y1 > y0) pixels(f, k, slot, 0, y0, 2, y1 - y0, arc.ink); break; }; // A flash: a ring 8 across drawing out to 14, and gone. var scaled = at; scaled.xf = sk.math.Affine.scale(k, k); scaled.scale = k; const u: ui.Ui = .{ .f = f, .at = scaled }; for (flashes[0..n_flashes]) |fl| { const slot = slotOf(fl.panel).?; const r = (cols[slot].span(.{ fl.key, fl.key }) orelse continue)[0]; if (!Row.shows(firsts, caps, slot, r)) continue; u.faded(1 - fl.p).disc(.{ @as(f32, @floatFromInt(slot)) * st.w + st.left + 7, Row.y(firsts, slot, r) + 1.5 }, std.math.lerp(@as(f32, 4), 7, fl.p), null, ui.rgb(fl.ink), 2); } for (cols, firsts, caps, wins, 0..) |*col, first, cap, win, slot| { const left = @as(f32, @floatFromInt(slot)) * st.w; // Where it goes on: three faint dots, 2 by 2, 6 apart, coming as the column's end leaves (over a row). const shown = @min(cap, col.rows - first); for ([2]f32{ std.math.clamp(first, 0, 1), std.math.clamp(col.rows - first - shown, 0, 1) }, [2]f32{ 0, st.top + cap * st.pitch + 1 }) |q, y| if (q > 0) { for (0..3) |i| pixels(f, k, slot, 13 + 6 * @as(f32, @floatFromInt(i)), y, 2, 2, ui.rgb(theme.color.text.faint).alphaFloat(q)); }; // Its window: the brightest thing on it. if (win) |w| { const top = st.top + (w[0] - first) * st.pitch - st.window; const bottom = st.top + (w[1] - first + 1) * st.pitch + 1; u.frame(.{ .x = left, .y = top, .w = st.w, .h = bottom - top }, theme.color.text.primary, st.window); } } } }; /// A pane's far jump in its own column at a walk, this frame (the pane /// publishes it as it draws; `Unroll` draws it and clears it): how far /// the column has grown out of its strip (0 to 1, before its `glide`) and /// how far the panes it covers are veiled. pub var jumping: ?struct { panel: Panel, grow: f32, veil: f32 } = null; /// `unroll` and `roll` drawn (design/README.md, "6. Verbs"; motion.md, /// "The map and the working frame"): the column grows out of the strip /// over the pane and the panes under it, a row of the map's 4 units /// scaling on its logarithm to as many as the stage holds (24 a row where /// the column is short enough to read, to fit where it is long), right on /// the strip's right edge, the pane's window in it level with the pane's /// rows where the stage allows; the window's outline rides from the strip /// to its rows in the column. Added last (`Context.ties`): it lies over /// everything in the frame but the caption and the clock. pub const Unroll = struct { map: *const Map, pub fn draw(un: *const Unroll, f: *sk.Frame, placed: anim.Placed) void { const m = un.map; defer jumping = null; if (ui.mapping or looking(f) or !m.settled(placed)) return; var panel: Panel = .source; var grow: f32 = 0; var veil: f32 = 0; for (m.unrolls[0..m.n_unrolls]) |u| { const v = placed.value(u.veil); if (v <= 0) continue; panel = u.panel; grow = placed.value(u.grow); veil = v; } if (jumping) |j| { panel = j.panel; grow = j.grow; veil = j.veil; } if (veil <= 0) return; const fixed = @import("words.zig").fixed(f, placed, 0); fixed.veil(theme.canvas.stage, veil); m.strips(f, fixed.at); const slot = slotOf(panel) orelse return; const col = &atlas.columns[slot]; if (col.n == 0 or col.rows <= 0) return; const working = m.frame.now(placed); const box = switch (panel) { inline else => |tag| @field(working, @tagName(tag)), }; // The strip's window goes bright over the veil's first third (6 f), never in a frame. unrolled(m, f, fixed, panel, box, col, theme.ease(.glide, grow), std.math.clamp(veil * 3, 0, 1)); } }; /// A pane's column `q` of its way (eased) from its strip to unrolled, in /// `fixed` (the canvas's units). fn unrolled(m: *const Map, f: *sk.Frame, fixed: ui.Ui, panel: Panel, box: Rect, col: *const Column, q: f32, bright: f32) void { const st = theme.strip; const cm = theme.metric.context; const stage = theme.canvas.stage; const r = stripRect(box); const cap = @floor((r.h - st.top - st.foot) / st.pitch); const win = windowOn(panel, col) orelse .{ 0, 0 }; const first = firstShown(col, win, cap); // Unrolled: as many rows as the stage holds, 24 a row at most. const a = @min(cm.row, stage.h / col.rows); const k1 = a / cm.row; const h1 = col.rows * a; const w1 = theme.map.column * k1; const pane_mid = box.y + cm.tag + cm.pad + @as(f32, @floatFromInt(theme.layout.rowsIn(box.h))) * cm.row / 2; const y1 = std.math.clamp(pane_mid - (win[0] + win[1] + 1) / 2 * a, stage.y, @max(stage.y + stage.h - h1, stage.y)); const x1 = @max(r.x + r.w - w1, stage.x); // Its scale on the logarithm, from the strip's (36 of 880 across, 4 of 24 a row) to the unrolled. const kx = std.math.pow(f32, st.across, 1 - q) * std.math.pow(f32, k1, q); const ky = std.math.pow(f32, @as(f32, st.pitch) / cm.row, 1 - q) * std.math.pow(f32, k1, q); // The window rides: its first row from its place on the strip to its place in the column. const wy = std.math.lerp(r.y + st.top + (win[0] - first) * st.pitch, y1 + win[0] * a, q); const oy = wy - win[0] * cm.row * ky; const ox = std.math.lerp(r.x + st.left, x1, q); // What shows: the strip's rows at first, the column whole at last. const top = std.math.lerp(r.y + st.top, y1, q); const bottom = std.math.lerp(r.y + st.top + @min(cap, col.rows - first) * st.pitch, y1 + h1, q); const left = std.math.lerp(r.x, x1, q); const right = std.math.lerp(r.x + r.w, x1 + w1, q); // Over the strip as it grows out of it, and gone into it as it lands: the strip's own drawing crossfades under it. const lifts = std.math.clamp(q * 8, 0, 1); if (q > 0) fixed.faded(lifts).fill(.{ .x = left - 4, .y = top - 4, .w = right - left + 8, .h = bottom - top + 8 }, theme.color.page); var offset: f32 = 0; for (m.column(fixed.at, panel)) |b| { if (q <= 0) break; if (b.h <= 0) continue; const n: f32 = @floatFromInt(theme.layout.rowsIn(b.h)); defer offset += n; const body = b.y + cm.tag + cm.pad; // The rows of this box inside what shows, in rows from the column's top. const lo = std.math.clamp((top - oy) / (cm.row * ky), offset, offset + n); const hi = std.math.clamp((bottom - oy) / (cm.row * ky), offset, offset + n); if (hi <= lo) continue; var moved = fixed.at; moved.xf = fixed.at.xf.mul(.translate(ox, oy)).mul(.scale(kx, ky)).mul(.translate(-b.x, -(body - offset * cm.row))); moved.scale = fixed.at.scale * ky; // The text comes in over the strip's strokes as it starts to grow. moved.pose.opacity *= lifts; const u: ui.Ui = .{ .f = f, .at = moved }; // Its parts clip to their own boxes inside this one's rows (`ui.bound`). const rows_rect: Rect = .{ .x = b.x, .y = body + (lo - offset) * cm.row, .w = b.w, .h = (hi - lo) * cm.row }; const a0 = moved.xf.apply(.{ rows_rect.x, rows_rect.y }); const a1 = moved.xf.apply(.{ rows_rect.x + rows_rect.w, rows_rect.y + rows_rect.h }); ui.bound = .{ .x = @round(a0[0]), .y = @round(a0[1]), .w = @round(a1[0]) - @round(a0[0]), .h = @round(a1[1]) - @round(a0[1]) }; u.clip(rows_rect); m.drawPanel(f, moved, panel); ui.bound = null; u.unclip(); } // The window's outline, the strip's framed bright as it begins (and as it ends), riding to round the pane's rows in the column. const wt = oy + win[0] * cm.row * ky; const wb = oy + (win[1] + 1) * cm.row * ky; // On the strip it is the strip's window's own frame (primary, `bright` of the way to bright). const ink = ui.mix(theme.color.text.primary, theme.color.text.bright, bright); fixed.frame(.{ .x = std.math.lerp(r.x, left - st.window, q), .y = wt - st.window, .w = std.math.lerp(st.w, right - left + 2 * st.window, q), .h = wb - wt + st.window + std.math.lerp(1, st.window, q) }, ink, st.window); } /// The most rows a column has in the strips' images. const atlas_rows = 160; /// A pane's column of the map as its strip draws it: each of its rows' /// key and place (rows from the column's top, continuous where the map's /// panel scrolls), and how many rows the column has. const Column = struct { n: usize = 0, keys: [atlas_rows]u64 = undefined, at: [atlas_rows]f32 = undefined, rows: f32 = 0, /// The first and last place of its rows with keys `keys[0]` to `keys[1]`. fn span(c: *const Column, keys: [2]u64) ?[2]f32 { var lo = std.math.inf(f32); var hi = -std.math.inf(f32); for (c.keys[0..c.n], c.at[0..c.n]) |key, at| if (key >= keys[0] and key <= keys[1]) { lo = @min(lo, at); hi = @max(hi, at); }; return if (lo <= hi) .{ lo, hi } else null; } }; /// The strips' images: one for each lagging drawing of the map /// (`time.minimap_fade`) and their average, last; each strip a column of /// them, its column whole from its top. /// shortcut: a process-wide cache of host images, for one player at a time; two at once want it in their Session. var atlas: struct { w: u32 = 0, h: u32 = 0, images: [theme.time.minimap_fade + 1]sk.Image = undefined, /// What a collecting drawing draws into: nothing of it shows. scrap: ?sk.Image = null, /// The frame they were made for, and of which map. frame: u64 = std.math.maxInt(u64), map: ?*const Map = null, /// Pixels a unit. k: f32 = 1, columns: [stripped.len]Column = @splat(.{}), } = .{}; /// The panes' windows in the frames before, for the strips' lagging drawings (`Map.strips`). var past: [16]struct { frame: u64 = std.math.maxInt(u64), windows: [7]?Window = @splat(null) } = @splat(.{}); /// The panes' windows `back` frames before frame `now` (where they were not kept, as they are). fn pastWindows(now: u64, back: u64) [7]?Window { const e = past[(now -% back) % past.len]; return if (e.frame == now -% back) e.windows else windows; } /// Where a window is on a column: its first and last row, between where it /// was and where it is, as far as its scroll had gone. fn spanOf2(maybe: ?Window, col: *const Column) ?[2]f32 { const win = maybe orelse return null; // A window going farther than the column holds (another place in a long // file, another function) is where the column is: where it was until the // column goes over with the pane, then where it is. const was = col.span(win.was); const is = col.span(win.now) orelse return was; const from = was orelse is; return .{ std.math.lerp(from[0], is[0], win.p), std.math.lerp(from[1], is[1], win.p) }; } /// The map whose columns the strips are (`context` sets it). /// shortcut: process-wide, as the drawing flags are: one scene drawn at a time. pub var current: ?*const Map = null; /// Every context's map (a scene in chapters has one a context, `context`): a /// pane's strip is drawn from its own context's, the one whose working /// frame has the pane's box; `current` where none has. pub var maps: [8]?*const Map = @splat(null); pub var maps_frame: u64 = std.math.maxInt(u64); fn ownMap(panel: Panel, at: anim.Placed, box: Rect) ?*const Map { for (maps) |maybe| if (maybe) |m| { const b = m.frame.box(at, @enumFromInt(@intFromEnum(panel))); if (@abs(b.x - box.x) < 0.5 and @abs(b.y - box.y) < 0.5 and @abs(b.w - box.w) < 0.5 and @abs(b.h - box.h) < 0.5) return m; }; return current; } /// A mark no working window shows, `flash`ing on its pane's strip. const Flash = struct { panel: Panel, key: u64, ink: u24, p: f32 }; var flashes: [16]Flash = undefined; var n_flashes: usize = 0; /// The flashes of the step, in the strips' time (6 f behind): a word /// struck or read on the stack or in the view's dump that its pane's /// window does not show; `p` of its 12 f. fn flash(now: @import("cursor.zig").Now) void { if (!now.walking()) return; const p = now.since(.memory, 0) / (12.0 / 60.0); if (p < 0 or p > 1) return; const rec = now.rec; 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 (Map.rows_of(.stack, word) != null) .{ .stack, word } else .{ .view, sixteen }; if (Map.rows_of(panel, key) == null) continue; if (windows[@intFromEnum(panel)]) |win| if (key >= win.now[0] and key <= win.now[1]) continue; if (n_flashes == flashes.len) return; flashes[n_flashes] = .{ .panel = panel, .key = key, .ink = mark.ink, .p = p }; n_flashes += 1; }; } const Spot = struct { r: f32, x: f32 }; /// Where a point of the map is in a column of `boxes`: its row, counted /// from the column's top (continuous: `middle`, a point on a row's middle, /// else on its top), and how far across its box. fn spotIn(boxes: [2]Rect, x: f32, y: f32, middle: bool) ?Spot { const cm = theme.metric.context; var offset: f32 = 0; for (boxes) |b| { if (b.h <= 0) continue; const body = b.y + cm.tag + cm.pad; const n: f32 = @floatFromInt(theme.layout.rowsIn(b.h)); if (x >= b.x - 1 and x <= b.x + b.w + 1 and y >= body - 1 and y <= body + n * cm.row + 1) return .{ .r = offset + (y - body - @as(f32, if (middle) cm.row / 2 else 0)) / cm.row, .x = x - b.x }; offset += n; } return null; } /// A rect of a strip's column, in strip units from its slot's corner, in the image's pixels (one at least). fn pixels(f: *sk.Frame, k: f32, slot: usize, x: f32, y: f32, w: f32, h: f32, color: sk.Color) void { const left = @as(f32, @floatFromInt(slot)) * theme.strip.w; const x0 = @round((left + x) * k); const y0 = @round(y * k); const x1 = @max(@round((left + x + w) * k), x0 + 1); const y1 = @max(@round((y + h) * k), y0 + 1); f.fill(sk.Rect{ .x = x0, .y = y0, .w = x1 - x0, .h = y1 - y0 }, color); } /// Where a pane's window is on its column now: its first and last row, /// between where it was and where it is, as far as its scroll has gone. fn windowOn(panel: Panel, col: *const Column) ?[2]f32 { return spanOf2(windows[@intFromEnum(panel)], col); } /// The first row a strip of `cap` rows shows: the column's top, or where /// it is longer, the window in the middle, clamped to the column's ends. fn firstShown(col: *const Column, win: [2]f32, cap: f32) f32 { if (col.rows <= cap) return 0; return std.math.clamp((win[0] + win[1] + 1) / 2 - cap / 2, 0, col.rows - cap); } /// A pane's strip, in the pane's units (`box` its box). fn stripRect(box: Rect) Rect { const st = theme.strip; return .{ .x = box.x + box.w - st.inset - st.w, .y = box.y + theme.metric.context.tag, .w = st.w, .h = box.h - theme.metric.context.tag - st.inset }; } /// A pane's minimap (design/README.md, "The map and the minimap"): its /// column of the map in a strip inside its frame at its right; where the /// column is longer than the strip, the rows round its window, with three /// faint dots at an end where it goes on; its window framed in primary, /// gliding with its scroll. `u` is the pane's `Ui` at its box's corner (a /// pane fading through passes its faded one); drawn last, over its rows. pub fn strip(panel: Panel, u: ui.Ui, box: Rect) void { if (ui.mapping) return; const m = ownMap(panel, u.at, box) orelse return; const f = u.f; if (looking(f)) return; const slot = slotOf(panel) orelse return; // In a dive it comes in as its pane lands. const v = u.faded(Map.stripShows(panel, u.at.value(m.t))); if (v.at.pose.opacity <= 0) return; m.strips(f, u.at); const col = &atlas.columns[slot]; if (col.n == 0) return; const st = theme.strip; const r = stripRect(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }); if (r.h <= st.top + st.foot + st.pitch) return; // The strip as its drawings have it, its rows, dots and window in place. const k = atlas.k; const a = v.at.xf.apply(.{ r.x, r.y }); const b = v.at.xf.apply(.{ r.x + r.w, r.y + r.h }); f.image(atlas.images[theme.time.minimap_fade], sk.Rect{ .x = a[0], .y = a[1], .w = b[0] - a[0], .h = b[1] - a[1] }, .{ .src = sk.Rect{ .x = @as(f32, @floatFromInt(slot)) * st.w * k, .y = 0, .w = st.w * k, .h = r.h * k }, .tint = sk.Color.white.alphaFloat(v.at.pose.opacity) }); } 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); } test "a point of the map is placed in its column, the second box going on from the first" { const cm = theme.metric.context; const boxes = [2]Rect{ .{ .x = 0, .y = 0, .w = 880, .h = theme.layout.panelH(10) }, .{ .x = 900, .y = 100, .w = 880, .h = theme.layout.panelH(5) } }; const first = spotIn(boxes, 40, cm.tag + cm.pad + 3 * cm.row + cm.row / 2, true).?; try std.testing.expectEqual(@as(f32, 3), first.r); const second = spotIn(boxes, 940, 100 + cm.tag + cm.pad + cm.row, false).?; try std.testing.expectEqual(@as(f32, 11), second.r); try std.testing.expectEqual(@as(f32, 40), second.x); try std.testing.expect(spotIn(boxes, 2000, 50, true) == null); }