summaryrefslogtreecommitdiff
path: root/anim/src/loupe.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/src/loupe.zig')
-rw-r--r--anim/src/loupe.zig181
1 files changed, 181 insertions, 0 deletions
diff --git a/anim/src/loupe.zig b/anim/src/loupe.zig
new file mode 100644
index 0000000..99dae71
--- /dev/null
+++ b/anim/src/loupe.zig
@@ -0,0 +1,181 @@
+//! `loupe` (design/README.md, "3. Panels"): the telescope's other form. A
+//! chain says, flat, `rax → 407030 ◂ 12`; a loupe opens the place it points
+//! to: a panel held over the context, lifted (its own surface, the one
+//! shadow there is), with five `bytes` rows round the target, the target
+//! framed bright, its pointers to its own rows as `arc`s at full ink. In
+//! the working frame it hangs from its pointer: its tag's top on the foot
+//! of the pointer's row, its left edge 92 left of the pointer's digits (the
+//! tag's address under them), a bracket under the digits its stem. One
+//! opened from a pointer field of a loupe hangs the same way from that
+//! field, 120 to the right where the stage has room, deeper (its shadow
+//! 20 and 26). Its verbs are `open` and `close`.
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+const zt = @import("ztrace");
+const ui = @import("ui.zig");
+const rows = @import("rows.zig");
+const cursors = @import("cursor.zig");
+const Recording = @import("Recording.zig");
+const objects = @import("objects.zig");
+const hexdump = @import("hexdump.zig");
+const ties = @import("ties.zig");
+const context = @import("context.zig");
+const Stage = @import("stage.zig").Stage;
+const theme = ui.theme;
+const Rect = ui.Rect;
+const t = theme.color.text;
+const m = theme.metric.context;
+const k = theme.metric.loupe;
+const time = theme.time.loupe;
+
+/// The rows each open loupe shows, and how much it shows: a dump showing
+/// the same rows withholds their arcs while it is open (the loupe carries
+/// them). Set as each draws, read the next frame by the dumps drawn before it.
+/// shortcut: process-wide, a frame behind; two players at once want it on the frame.
+pub var holding: [2]?struct { lo: u64, hi: u64, shows: f32 } = @splat(null);
+
+pub const Loupe = struct {
+ cursor: cursors.Cursor,
+ stage: *const Stage,
+ /// What it opens from: a register, by name; or a pointer field (its
+ /// address) of the loupe it hangs under.
+ from: union(enum) { register: []const u8, field: struct { loupe: *const Loupe, at: u64 } },
+ /// 0 to 1 as it opens (`time.loupe.open`) and as it closes (`close`).
+ opening: anim.Value = undefined,
+ closing: anim.Value = undefined,
+
+ /// `open`: its pointer framed as read; the stem draws and the loupe
+ /// rises with it, its shadow growing; its target's frame lands last.
+ pub fn open(l: *const Loupe, s: *anim.Frame) void {
+ s.play(.{l.opening.to(1)}, .{ .seconds = time.open, .ease = anim.ease.linear });
+ }
+
+ /// `close`: the stem draws back and the loupe sinks, fading as it falls.
+ pub fn close(l: *const Loupe, s: *anim.Frame) void {
+ s.play(.{l.closing.to(1)}, .{ .seconds = time.close, .ease = anim.ease.linear });
+ }
+
+ /// Where it is now (in the working frame's units) and what it shows:
+ /// null while it is shut, or where its pointer is not on screen.
+ const Placed = struct { box: Rect, pointer: Rect, target: u64, first: u64, ink: u24 };
+
+ fn place(l: *const Loupe, u: ui.Ui, now: cursors.Now) ?Placed {
+ const moment = now.moment();
+ var pointer: Rect = undefined;
+ var target: u64 = undefined;
+ var right: f32 = 0;
+ switch (l.from) {
+ .register => |name| {
+ const i = context.index(name) orelse return null;
+ const px = ties.find(.{ .register = i }) orelse return null;
+ const back = u.at.xf.invert();
+ const a = back.apply(.{ px[0], px[1] });
+ const b = back.apply(.{ px[2], px[3] });
+ pointer = .{ .x = a[0], .y = a[1], .w = b[0] - a[0], .h = b[1] - a[1] };
+ const regs = (moment.regs(l.cursor.rec.tid) catch null) orelse return null;
+ target = regs.named(name) orelse return null;
+ },
+ .field => |field| {
+ const parent = field.loupe.place(u, now) orelse return null;
+ const row = @as(f32, @floatFromInt((field.at -| parent.first) / 16));
+ const shift = rows.addressRoom(parent.first + 16 * (k.rows - 1));
+ const at: u8 = @intCast(field.at % 16);
+ pointer = .{ .x = parent.box.x + shift + rows.byteX(at), .y = parent.box.y + m.tag + m.pad + row * m.row, .w = rows.byteX(at + 7) + 24 - rows.byteX(at), .h = m.row };
+ target = (Recording.word(moment, field.at, 8) catch null) orelse return null;
+ right = parent.box.x + 120;
+ },
+ }
+ if (target == 0) return null;
+ // shortcut: it keeps to the stage and does not yet move past the current marks or a changed row; the scene picks a pointer whose loupe covers neither.
+ const stage = theme.canvas.stage;
+ const h = theme.layout.panelH(k.rows);
+ const x = std.math.clamp(@max(pointer.x - k.hang, right), stage.x, stage.x + stage.w - k.width);
+ const y = @min(pointer.y + pointer.h - 1, stage.y + stage.h - h);
+ return .{ .box = .{ .x = x, .y = y, .w = k.width, .h = h }, .pointer = pointer, .target = target, .first = (target & ~@as(u64, 15)) - 16 * (k.rows / 2), .ink = rows.ink(rows.regionOf(moment, target)) };
+ }
+
+ pub fn draw(l: *const Loupe, f: *sk.Frame, placed: anim.Placed) void {
+ const deep = l.from == .field;
+ holding[@intFromBool(deep)] = null;
+ const opened = placed.value(l.opening) * time.open;
+ const closed = placed.value(l.closing);
+ if (opened <= 0 or closed >= 1 or ui.mapping) return;
+ const u: ui.Ui = .{ .f = f, .at = placed };
+ const now = l.cursor.now(placed);
+ const p = l.place(u, now) orelse return;
+ holding[@intFromBool(deep)] = .{ .lo = p.first, .hi = p.first + 16 * k.rows, .shows = std.math.clamp((opened - time.read) / time.rise, 0, 1) * (1 - closed) };
+ // Its pointer framed as read, first.
+ u.read(p.pointer, std.math.clamp(opened / time.read, 0, 1) * (1 - closed));
+ // It rises with the stem: in over 12 f from 8 lower, its shadow growing; it sinks as it closes.
+ const since = opened - time.read;
+ if (since <= 0) return;
+ const rise = std.math.clamp(since / time.rise, 0, 1) * (1 - closed);
+ const lower = (1 - theme.ease(.enter, std.math.clamp(since / time.rise, 0, 1))) * theme.time.caption_rise + closed * theme.time.caption_rise;
+ const grow = cursors.standard(std.math.clamp(since / time.shadow, 0, 1)) * (1 - closed);
+ const off = k.shadow[@intFromBool(deep)];
+ const b = p.box;
+ const v = u.faded(rise).moved(b.x, b.y + lower);
+ v.fill(.{ .x = off[0] * grow, .y = off[1] * grow, .w = b.w, .h = b.h }, theme.color.shade);
+ v.fill(.{ .x = 0, .y = 0, .w = b.w, .h = b.h }, theme.color.lift);
+ v.fill(.{ .x = 0, .y = 0, .w = b.w, .h = m.tag }, theme.color.lift_tag);
+ v.frame(.{ .x = 0, .y = 0, .w = b.w, .h = b.h }, t.dim, theme.stroke.rule);
+ // The tag: the pointer it opens from, and the target in its region's ink; the region and the type.
+ const size = theme.size.mono.context;
+ const base = m.tag - 6;
+ const moment = now.moment();
+ const things = objects.reach(f.scratch, l.cursor.rec, moment, null);
+ const object: ?objects.Object = for (things) |o| {
+ if (o.holds(p.target)) break o;
+ } else null;
+ var x: f32 = m.side;
+ switch (l.from) {
+ .register => |name| {
+ v.text(.mono, size, x, base, name, t.dim);
+ x += v.width(.mono, size, name) + 6;
+ },
+ .field => |field| x = v.address(x, base, field.at, .{ .ink = ui.rgb(rows.ink(rows.regionOf(moment, field.at))) }) + 6,
+ }
+ x = v.arrow(x, m.tag / 2, .points) + 8;
+ _ = v.address(x, base, p.target, .{ .ink = ui.rgb(p.ink) });
+ const region = switch (rows.regionOf(moment, p.target)) {
+ .stack => "stack",
+ .heap => "heap",
+ .code => "code",
+ .data, .rwx => "data",
+ .rodata => "read-only",
+ .none => "",
+ };
+ const note = if (object) |o| v.print("{s} \u{00b7} {s}, {d} bytes", .{ region, if (o.name.len > 0) o.name else "a struct", o.type.record.size }) else region;
+ v.text(.sans, theme.size.sans.micro, b.w - 10 - v.width(.sans, theme.size.sans.micro, note), base, note, t.dim);
+ // Its rows, typed where the recording types them, its own pointers as arcs at full ink.
+ const top = m.tag + m.pad;
+ v.clip(.{ .x = 0, .y = m.tag, .w = b.w, .h = b.h - m.tag });
+ hexdump.drawArcs(v, now, 0, top, p.first, k.rows, .{ .things = things }, true, .{ m.tag, b.h - m.pad });
+ v.unclip();
+ // Its target, framed bright: each field of the struct, or the word.
+ const framed = std.math.clamp((since - time.stem - time.frame) / time.frame, 0, 1);
+ if (framed > 0) {
+ const row_y = top + @as(f32, @floatFromInt((p.target - p.first) / 16)) * m.row;
+ const shift = rows.addressRoom(p.first + 16 * (k.rows - 1));
+ const w = v.faded(framed);
+ if (object) |o| {
+ for (o.type.record.fields) |field| frameRun(w, row_y, shift, o.addr + field.offset - (p.target & ~@as(u64, 15)), field.type.size() orelse 8);
+ } else frameRun(w, row_y, shift, p.target % 16, 8);
+ }
+ // The stem: a bracket under the pointer's digits, lying on the tag, drawn head first.
+ const drawn = cursors.standard(std.math.clamp(since / time.stem, 0, 1)) * (1 - closed);
+ u.faded(@min(rise * 2, 1)).fill(.{ .x = p.pointer.x - 2, .y = p.pointer.y + p.pointer.h - 3, .w = (p.pointer.w + 4) * drawn, .h = k.stem }, p.ink);
+ }
+};
+
+/// `attention` on the bytes `at .. at + len` of a row (from its first
+/// byte, up to its end): a 2-unit bright frame round them.
+fn frameRun(u: ui.Ui, y: f32, shift: f32, at: u64, len: u64) void {
+ if (at >= 16 or len == 0) return;
+ const first: u8 = @intCast(at);
+ const last: u8 = @intCast(@min(at + len, 16) - 1);
+ const x0 = shift + rows.byteX(first) - 4;
+ const x1 = shift + rows.byteX(last) + 28;
+ u.frame(.{ .x = x0 - 3, .y = y - 1, .w = x1 - x0 + 6, .h = m.row + 2 }, theme.mark(.attention).solid, theme.stroke.rule);
+}