diff options
Diffstat (limited to 'anim/src/ui.zig')
| -rw-r--r-- | anim/src/ui.zig | 598 |
1 files changed, 598 insertions, 0 deletions
diff --git a/anim/src/ui.zig b/anim/src/ui.zig new file mode 100644 index 0000000..9d92e28 --- /dev/null +++ b/anim/src/ui.zig @@ -0,0 +1,598 @@ +//! What every component draws with, in the design's units (1920 by 1080; a +//! component's origin is its own top left corner): rects and rules snapped +//! to pixels, text in the design's faces set on a baseline, hex in groups +//! of four, the panel, the marks (written, read, unknown), and arrows. +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +pub const theme = @import("theme.zig"); + +pub const Rect = theme.Rect; +pub const Color = sk.Color; +pub const Face = sk.Face; +pub const Vec = sk.math.Vec; + +/// Scene units to a design unit: `anim` frames 1280 by 720. +pub const scale = 1280.0 / @as(comptime_float, theme.canvas.width); + +/// Adds `data` (a component: something with `draw(f, at)`) to the scene, +/// its origin at design `x, y` and its units the design's. Like any +/// object, it shows once something plays it in (`fadeIn`). +pub fn place(s: *anim.Frame, data: anytype, x: f32, y: f32) anim.Obj { + return s.custom(s.new(data), .{ .x = x * scale - 640, .y = y * scale - 360, .scale = scale }); +} + +/// A camera move that puts the middle of design rect `r` in the middle of +/// the stage (the band between the head and the caption, which stay put), +/// at `zoom` (the design allows 1, 2 and 4). +pub fn lookAt(r: Rect, zoom: f32) anim.Animation { + const to = framing(r, zoom); + return anim.camera(.{ .x = to.x, .y = to.y, .zoom = to.zoom }); +} + +/// Where the camera is when the middle of design rect `r` is in the middle +/// of the stage, at `zoom`. +pub fn framing(r: Rect, zoom: f32) anim.Camera { + const stage = theme.canvas.stage; + const off = (theme.canvas.height / 2.0 - (stage.y + stage.h / 2)) / zoom; + return .{ .x = (r.x + r.w / 2) * scale - 640, .y = (r.y + r.h / 2 + off) * scale - 360, .zoom = zoom }; +} + +/// The camera `p` of the way from `a` to `b`. Arcs: it moves in a straight +/// line and zooms by ratio (the logarithm of the scale is what is +/// interpolated), about the one point that stays where it is on screen, +/// or the first half of a move to twice the size looks twice as fast as +/// the second. +pub fn between(a: anim.Camera, b: anim.Camera, p: f32) anim.Camera { + if (@abs(a.zoom - b.zoom) < 1e-4) return .{ .x = std.math.lerp(a.x, b.x, p), .y = std.math.lerp(a.y, b.y, p), .zoom = b.zoom }; + const zoom = a.zoom * std.math.pow(f32, b.zoom / a.zoom, p); + const still = [2]f32{ (b.x * b.zoom - a.x * a.zoom) / (b.zoom - a.zoom), (b.y * b.zoom - a.y * a.zoom) / (b.zoom - a.zoom) }; + return .{ .x = still[0] - (still[0] - a.x) * a.zoom / zoom, .y = still[1] - (still[1] - a.y) * a.zoom / zoom, .zoom = zoom }; +} + +/// A theme colour (`0xRRGGBB`) or a `Color` already. +pub fn rgb(c: anytype) Color { + return if (@TypeOf(c) == Color) c else .hex(c); +} + +pub fn mix(a: anytype, b: anytype, t: f32) Color { + return sk.tween.lerp(rgb(a), rgb(b), std.math.clamp(t, 0, 1)); +} + +/// The face a text is set in: every monospace is JetBrains Mono NL, and +/// under 30 its medium weight (light letters on a dark ground lose their +/// thin strokes first in a video stream). +fn set(face: Face, size: f32) Face { + return switch (face) { + .mono => if (size < 30) .code_medium else .code, + .mono_bold => .code_bold, + .mono_italic => .code_italic, + else => face, + }; +} + +/// `n` in hex, a space between groups of four from the right, into `out` +/// (20 bytes is enough): nobody reads twelve digits unbroken. +pub fn grouped(out: []u8, n: u64) []const u8 { + var buf: [16]u8 = undefined; + const digits = std.fmt.bufPrint(&buf, "{x}", .{n}) catch unreachable; + var at: usize = 0; + for (digits, 0..) |d, i| { + if (i > 0 and (digits.len - i) % 4 == 0) { + out[at] = ' '; + at += 1; + } + out[at] = d; + at += 1; + } + return out[0..at]; +} + +/// Width of one mono character at `size`: the mono faces advance 0.6 em, a +/// whole number of pixels at the design's sizes. +pub fn advance(size: f32) f32 { + return size * 3 / 5; +} + +/// The space between groups of four hex digits: half a character, a whole unit. +pub fn hexGap(size: f32) f32 { + return @ceil(advance(size) / 2); +} + +/// Width of `digits` hex digits in groups of four, half a character apart. +pub fn hexWidth(digits: usize, size: f32) f32 { + const groups: f32 = @floatFromInt((digits + 3) / 4); + return @as(f32, @floatFromInt(digits)) * advance(size) + (groups - 1) * hexGap(size); +} + +pub const Align = enum { left, center, right }; + +/// What digits are set in: leading zeros in `lead`, the rest in `ink`. +pub const Ink = struct { + ink: Color = rgb(theme.color.text.primary), + lead: Color = rgb(theme.color.text.faint), + face: Face = .mono, + + /// One colour for every digit: on a solid fill. + pub fn all(c: anytype, face: Face) Ink { + return .{ .ink = rgb(c), .lead = rgb(c), .face = face }; + } +}; + +pub const Ui = struct { + f: *sk.Frame, + at: anim.Placed, + + /// The same, at `opacity` of what it had: a thing arriving or leaving. + pub fn faded(u: Ui, opacity: f32) Ui { + var out = u; + out.at.pose.opacity *= std.math.clamp(opacity, 0, 1); + return out; + } + + fn point(u: Ui, x: f32, y: f32) Vec { + return u.at.xf.apply(.{ x, y }); + } + + /// A filled rect, its edges on whole pixels. + pub fn fill(u: Ui, r: Rect, c: anytype) void { + const a = u.point(r.x, r.y); + const b = u.point(r.x + r.w, r.y + r.h); + const x = @round(a[0]); + const y = @round(a[1]); + u.f.fill(sk.Rect{ .x = x, .y = y, .w = @round(b[0]) - x, .h = @round(b[1]) - y }, u.at.faded(rgb(c))); + } + + /// A stroke `w` wide, wholly inside `r`. + pub fn frame(u: Ui, r: Rect, c: anytype, w: f32) void { + u.fill(.{ .x = r.x, .y = r.y, .w = r.w, .h = w }, c); + u.fill(.{ .x = r.x, .y = r.y + r.h - w, .w = r.w, .h = w }, c); + u.fill(.{ .x = r.x, .y = r.y + w, .w = w, .h = r.h - 2 * w }, c); + u.fill(.{ .x = r.x + r.w - w, .y = r.y + w, .w = w, .h = r.h - 2 * w }, c); + } + + /// A dashed frame inside `r`: nobody's bytes. + pub fn dashedFrame(u: Ui, r: Rect, c: anytype, w: f32) void { + const h = w / 2; + const pts = [_]Vec{ .{ r.x + h, r.y + h }, .{ r.x + r.w - h, r.y + h }, .{ r.x + r.w - h, r.y + r.h - h }, .{ r.x + h, r.y + r.h - h }, .{ r.x + h, r.y + h } }; + sk.draw.dashed(u.f, &pts, theme.stroke.dash, u.at.pen(.{ .stroke = rgb(c), .width = w, .cap = .butt })); + } + + /// Text on a baseline. The host places a line by the top of its box, + /// which is the font's ascent above the baseline. + pub fn text(u: Ui, face: Face, size: f32, x: f32, baseline: f32, bytes: []const u8, c: anytype) void { + if (bytes.len == 0) return; + // Go's ascent is 0.945 em; JetBrains Mono's, which every monospace is set in, 1.02. + const at = u.point(x, baseline - @as(f32, if (set(face, size) == face) 0.945 else 1.02) * size); + const drawn = size / 13.0 * u.at.scale; + // The camera rests at 1, 2 and 4: there text is shaped for its + // size, sharp. On the way between, it is the text of the nearer + // rest scaled, so letters do not shift against each other. + const one = @min(@floor(u.f.in.window_width) / theme.canvas.width, @floor(u.f.in.window_height) / theme.canvas.height); + const zoom = u.at.scale / one; + const rest: f32 = for (theme.camera.scales) |s| { + if (zoom <= s * 1.002) break s; + } else theme.camera.scales[theme.camera.scales.len - 1]; + if (@abs(zoom - rest) <= rest * 0.002) { + u.f.textIn(set(face, size), at[0], at[1], drawn, bytes, u.at.faded(rgb(c))); + } else u.f.textScaled(set(face, size), at[0], at[1], drawn, size / 13.0 * one * rest, bytes, u.at.faded(rgb(c))); + } + + pub fn width(u: Ui, face: Face, size: f32, bytes: []const u8) f32 { + return switch (face) { + .mono, .mono_bold, .mono_italic => @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(bytes) catch bytes.len)) * advance(size), + else => u.at.textSizeIn(u.f, face, size / 13.0, bytes)[0], + }; + } + + /// Text with its capitals centred in `r`'s height, ranged in its width. + pub fn label(u: Ui, face: Face, size: f32, r: Rect, how: Align, bytes: []const u8, c: anytype) void { + const x = switch (how) { + .left => r.x, + .center => r.x + (r.w - u.width(face, size, bytes)) / 2, + .right => r.x + r.w - u.width(face, size, bytes), + }; + u.text(face, size, x, theme.baseline(r.y, r.h, size), bytes, c); + } + + /// Formats into the frame's scratch memory. + pub fn print(u: Ui, comptime fmt: []const u8, args: anytype) []const u8 { + return u.f.format(fmt, args); + } + + /// `value` as `digits` hex digits, in groups of four half a character + /// apart, from `x` rightwards. + pub fn hex(u: Ui, size: f32, x: f32, baseline: f32, v: u64, digits: usize, ink: Ink) void { + var buf: [16]u8 = undefined; + const all = std.fmt.bufPrint(&buf, "{x:0>16}", .{v}) catch unreachable; + const shown = all[16 - digits ..]; + // Leading zeros, but never the last digit. + var zeros: usize = 0; + while (zeros < digits - 1 and shown[zeros] == '0') zeros += 1; + const adv = advance(size); + var at_x = x; + var i: usize = 0; + while (i < digits) { + // A group ends where the digits left are a multiple of four. + const end = i + ((digits - i - 1) % 4) + 1; + const split = std.math.clamp(zeros, i, end); + u.text(ink.face, size, at_x, baseline, shown[i..split], ink.lead); + u.text(ink.face, size, at_x + @as(f32, @floatFromInt(split - i)) * adv, baseline, shown[split..end], ink.ink); + at_x += @as(f32, @floatFromInt(end - i)) * adv + hexGap(size); + i = end; + } + } + + /// An address in a gutter: its last group is the end that changes from + /// row to row, so only that one is in full ink. + pub fn address(u: Ui, x: f32, baseline: f32, addr: u64, digits: usize) void { + const size = theme.size.mono.small; + const adv = advance(size); + var buf: [16]u8 = undefined; + const all = std.fmt.bufPrint(&buf, "{x:0>16}", .{addr}) catch unreachable; + const shown = all[16 - digits ..]; + var i: usize = 0; + var at_x = x; + while (i < digits) : (i += 4) { + u.text(.mono, size, at_x, baseline, shown[i .. i + 4], if (i + 4 == digits) theme.color.text.primary else theme.color.text.faint); + at_x += 4 * adv + hexGap(size); + } + } + + /// An acme window `w` by `h` at the origin: a tag one row high (a box, + /// gold where the narration is; the title; a note ranged right, or the + /// legend of the regions), and a rule round the body. Returns the body. + pub fn panel(u: Ui, w: f32, h: f32, title: []const u8, options: struct { focus: bool = false, note: []const u8 = "", legend: bool = false }) Rect { + u.region(title, .{ .x = 0, .y = 0, .w = w, .h = h }); + const m = theme.metric.context; + const t = theme.color.text; + u.fill(.{ .x = 0, .y = 0, .w = w, .h = m.tag }, theme.color.raised); + u.frame(.{ .x = 0, .y = 0, .w = w, .h = h }, theme.color.rule, theme.stroke.rule); + const box: Rect = .{ .x = 8, .y = 6, .w = 12, .h = 12 }; + if (options.focus) u.fill(box, theme.mark(.current).solid) else u.frame(box, t.faint, theme.stroke.rule); + const base = theme.baseline(0, m.tag, theme.size.sans.tag); + u.text(.sans_medium, theme.size.sans.tag, 28, base, title, if (options.focus) t.primary else t.dim); + const micro = theme.size.sans.micro; + if (options.legend) { + // What an address's ink says, always on screen, as pwndbg's legend is. + const c = theme.color.region; + var x = w - 10; + inline for (.{ .{ "read-only", c.rodata }, .{ "rwx", c.code_ }, .{ "data", c.data }, .{ "code", c.code_ }, .{ "heap", c.heap }, .{ "stack", c.stack } }) |entry| { + x -= u.width(.sans_medium, micro, entry[0]); + u.text(.sans_medium, micro, x, base, entry[0], entry[1]); + if (comptime std.mem.eql(u8, entry[0], "rwx")) u.fill(.{ .x = x, .y = base + 3, .w = u.width(.sans_medium, micro, entry[0]), .h = theme.stroke.rule }, entry[1]); + x -= 16; + } + } else u.text(.sans, micro, w - 10 - u.width(.sans, micro, options.note), base, options.note, t.faint); + return .{ .x = 0, .y = m.tag, .w = w, .h = h - m.tag }; + } + + /// For tools/motion.py, which plots how much of each panel changes + /// from frame to frame: with CANIM_PANELS in the environment, every + /// frame tells where on the canvas (as fractions of it) `r` is. + pub fn region(u: Ui, name: []const u8, r: Rect) void { + if (u.f.host.env("CANIM_PANELS") == null) return; + const a = u.point(r.x, r.y); + const b = u.point(r.x + r.w, r.y + r.h); + const w = @floor(u.f.in.window_width); + const h = @floor(u.f.in.window_height); + std.debug.print("canim-panel {d:.4} {d:.4} {d:.4} {d:.4} {s}\n", .{ a[0] / w, a[1] / h, b[0] / w, b[1] / h, name }); + } + + /// The ground of a thing a step wrote, as hot as `h` says: armed (a + /// frame, the old value still in it), struck (the fill coming in, + /// its edges pulsing out and back), solid, cooling to its ember with a + /// bar along the bottom, `ground` (null: nothing) once that has gone. + /// Returns what to set its digits in. + pub fn written(u: Ui, r: Rect, h: Heat, ground: ?u24) Ink { + const w = theme.mark(.write); + const under: Color = rgb(ground orelse theme.color.page); + var body = if (h.level < 1) mix(under, w.ember, h.level) else mix(w.ember, w.solid, h.solid); + // Timing: a strike arrives over a tenth of a second, not in a frame. + if (h.in < 1) body = mix(if (h.from == .ember) rgb(w.ember) else under, body, h.in); + // The pulse is an outset, the same on a byte and on a register, + // and the digits do not move with it. + // shortcut: a neighbour drawn after covers its side of the outset; struck cells in two passes when that shows. + if (h.level > 0 or ground != null) u.fill(.{ .x = r.x - h.out, .y = r.y - h.out, .w = r.w + 2 * h.out, .h = r.h + 2 * h.out }, body); + const bar = theme.metric.ember_bar; + if (h.level >= 1 and (h.in >= 1 or h.from == .ember)) u.fill(.{ .x = r.x, .y = r.y + r.h - bar, .w = r.w, .h = bar }, w.solid); + // Anticipation: where the write will land, before it does. + if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, theme.stroke.line); + // Bold ink on the solid. The face changes at the middle of the + // cool, where the colour is moving fastest and hides it. + const heat = h.solid * h.in; + if (h.level < 1 or heat <= 0) return .{}; + const cooled: Ink = .{}; + return .{ .ink = mix(cooled.ink, w.on_solid, heat), .lead = mix(cooled.lead, w.on_solid, heat), .face = if (heat >= 0.5) .mono_bold else .mono }; + } + + /// A thing a step read: its ember inside a frame, `shows` of it (0 to + /// 1: it fades in and out, and is never armed or pulsed). + pub fn readMark(u: Ui, r: Rect, shows: f32) void { + if (shows <= 0) return; + const m = theme.mark(.read); + const v = u.faded(shows); + v.fill(r, m.ember); + v.frame(r, m.solid, theme.stroke.line); + } + + /// The same in the context, at mono 20, where a value has no cell of + /// its own: armed is a thin frame; a strike is solid behind the value + /// (its edges out a little and back), and for as long as it is solid + /// "written now" is all the value says: the ink this returns, bold on + /// the solid. It cools to nothing, and what is left for three steps is + /// a tick in the gap at the value's left: a held change never sits + /// under the digits it marks, whose ink is where they point. + pub fn struck(u: Ui, r: Rect, h: Heat) ?Ink { + const w = theme.mark(.write); + const m = theme.metric.context; + if (h.level > 0) u.fill(.{ .x = r.x - m.tick - 2, .y = r.y + 1, .w = m.tick, .h = r.h - 2 }, mix(theme.color.page, w.solid, h.level)); + if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, m.read_frame); + const solid = if (h.level >= 1) h.solid * h.in else 0; + if (solid <= 0) return null; + const out = @min(h.out, m.pulse_outset); + u.fill(.{ .x = r.x - out, .y = r.y - out, .w = r.w + 2 * out, .h = r.h + 2 * out }, mix(theme.color.page, w.solid, solid)); + return if (solid >= 0.5) Ink.all(w.on_solid, .mono_bold) else null; + } + + /// A thing a step read, in the context: a thin frame outside the digits, and no fill. + pub fn readFrame(u: Ui, r: Rect, shows: f32) void { + if (shows > 0) u.faded(shows).frame(r, theme.mark(.read).solid, theme.metric.context.read_frame); + } + + /// One arrow of a chain, `metric.context.arrow_w` wide from `x`, on the + /// row whose middle is `y`: pointing right ("points to") or back + /// ("holds"). Faint: it is grammar, not data. + pub fn chainArrow(u: Ui, x: f32, y: f32, back: bool) void { + const m = theme.metric.context; + const c = theme.color.text.faint; + const a = x + 5; + const b = x + m.arrow_w - 5; + const head = m.arrow_head; + const pen = u.at.pen(.{ .fill = rgb(c) }); + if (back) { + u.fill(.{ .x = a + head[0] - 1, .y = y - 1, .w = b - a - head[0] + 1, .h = 2 }, c); + sk.draw.polygon(u.f, &.{ .{ a, y }, .{ a + head[0], y - head[1] / 2 }, .{ a + head[0], y + head[1] / 2 } }, pen); + } else { + u.fill(.{ .x = a, .y = y - 1, .w = b - a - head[0] + 1, .h = 2 }, c); + sk.draw.polygon(u.f, &.{ .{ b, y }, .{ b - head[0], y - head[1] / 2 }, .{ b - head[0], y + head[1] / 2 } }, pen); + } + } + + /// A value as its class is set (design/README.md, "What a value shows + /// as"), from `x` on `baseline` at mono 20: an address (`pointer`) as + /// long as it is, in `ink`; a number under 10 as the digit; up to + /// 0xffff as hex with its decimal after it, faint; larger, hex with + /// `0x`; within 4096 of 2^64, a small negative. Returns where it ends (the faint + /// decimal apart: `Value.width` is what a mark goes behind). + pub fn value(u: Ui, x: f32, baseline: f32, v: u64, pointer: bool, ink: Ink) f32 { + const size = theme.size.mono.context; + const adv = advance(size); + const shown = Value.of(v, pointer); + var at = x; + if (shown.prefix.len > 0) { + u.text(ink.face, size, at, baseline, shown.prefix, ink.ink); + at += @as(f32, @floatFromInt(shown.prefix.len)) * adv; + } + var i: usize = 0; + const digits = shown.digits(); + while (i < digits.len) { + // A group ends where the digits left are a multiple of four. + const end = if (shown.grouped) i + ((digits.len - i - 1) % 4) + 1 else digits.len; + u.text(ink.face, size, at, baseline, u.print("{s}", .{digits[i..end]}), ink.ink); + at += @as(f32, @floatFromInt(end - i)) * adv + @as(f32, if (end < digits.len) theme.metric.context.group_gap else 0); + i = end; + } + if (shown.decimal) { + u.text(.mono, size, at + theme.metric.context.group_gap, baseline, u.print("{d}", .{v}), theme.color.text.faint); + at += theme.metric.context.group_gap + @as(f32, @floatFromInt(std.fmt.count("{d}", .{v}))) * adv; + } + return at; + } + + /// The veil over what cannot be read at the pace: `amount` of it, 0 to 1. + pub fn veil(u: Ui, r: Rect, amount: f32) void { + if (amount > 0) u.fill(r, rgb(theme.color.veil).alphaFloat(theme.color.veil_alpha * std.math.clamp(amount, 0, 1))); + } + + /// Draws only inside `r` until `unclip`: text scrolling in its panel. + pub fn clip(u: Ui, r: Rect) void { + const a = u.point(r.x, r.y); + const b = u.point(r.x + r.w, r.y + r.h); + u.f.clip(sk.Rect{ .x = @round(a[0]), .y = @round(a[1]), .w = @round(b[0]) - @round(a[0]), .h = @round(b[1]) - @round(a[1]) }); + } + + pub fn unclip(u: Ui) void { + u.f.clip(null); + } + + /// Never observed: two dots where the digits would be, centred in `r`. + pub fn unknown(u: Ui, r: Rect) void { + const d = theme.stroke.dot; + const y = r.y + (r.h - d) / 2; + u.fill(.{ .x = r.x + r.w / 2 - d - 3, .y = y, .w = d, .h = d }, theme.color.text.faint); + u.fill(.{ .x = r.x + r.w / 2 + 3, .y = y, .w = d, .h = d }, theme.color.text.faint); + } + + /// Bytes that are there and not shown: three dots and what they are. + pub fn elision(u: Ui, x: f32, y: f32, note: []const u8) void { + const d = theme.stroke.dot; + const h = theme.metric.elision_h; + for (0..3) |i| u.fill(.{ .x = x + @as(f32, @floatFromInt(i)) * 14, .y = y + (h - d) / 2, .w = d, .h = d }, theme.color.text.faint); + u.text(.sans, theme.size.sans.micro, x + 56, theme.baseline(y, h, theme.size.sans.micro), note, theme.color.text.faint); + } + + /// A name in a box 40 tall at `x, y`: hollow (a value) with `fill` + /// null, else filled (a place, a tracked object). Returns its width. + pub fn plaque(u: Ui, x: f32, y: f32, face: Face, size: f32, bytes: []const u8, ink: anytype, fill_color: ?u24, stroke: ?u24) f32 { + const m = theme.metric.plaque; + const w = u.width(face, size, bytes) + 2 * m.pad; + const r: Rect = .{ .x = x, .y = y, .w = w, .h = m.h }; + if (fill_color) |c| u.fill(r, c); + if (stroke) |c| u.frame(r, c, theme.stroke.rule); + u.text(face, size, x + m.pad, theme.baseline(y, m.h, size), bytes, ink); + return w; + } + + /// A path through `points` with filleted corners, `progress` of its + /// length drawn (an arrow growing to its target), dashed or whole, with + /// a filled head at its end or none. + pub fn arrow(u: Ui, points: []const Vec, c: anytype, options: struct { width: f32 = theme.stroke.arrow, head: ?[2]f32 = theme.stroke.head, dashed: bool = false, progress: f32 = 1 }) void { + if (points.len < 2 or options.progress <= 0) return; + var path: std.ArrayList(Vec) = .empty; + const gpa = u.f.scratch; + path.append(gpa, points[0]) catch return; + for (1..points.len - 1) |i| { + // The corner as a quadratic through its fillet's ends. + const a = points[i - 1]; + const b = points[i]; + const z = points[i + 1]; + const r = @min(theme.stroke.elbow_radius, @min(sk.math.distance(a, b), sk.math.distance(b, z)) / 2); + const from = b + sk.math.withLength(a - b, r); + const to = b + sk.math.withLength(z - b, r); + for (0..7) |k| { + const t = @as(f32, @floatFromInt(k)) / 6; + path.append(gpa, sk.math.mix(sk.math.mix(from, b, t), sk.math.mix(b, to, t), t)) catch return; + } + } + path.append(gpa, points[points.len - 1]) catch return; + + // Cut to `progress` of its length, and short of the head. + var total: f32 = 0; + for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| total += sk.math.distance(a, b); + const head_len: f32 = if (options.head) |h| h[0] else 0; + const tip_at = total * std.math.clamp(options.progress, 0, 1); + var keep = @max(tip_at - @max(head_len - 2, 0), 0); + var tip = path.items[0]; + var dir: Vec = sk.math.normalize(path.items[1] - path.items[0]); + var body: std.ArrayList(Vec) = .empty; + body.append(gpa, path.items[0]) catch return; + var walked: f32 = 0; + for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| { + const len = sk.math.distance(a, b); + if (len == 0) continue; + if (keep > 0) body.append(gpa, if (keep >= len) b else sk.math.mix(a, b, keep / len)) catch return; + keep -= len; + if (walked <= tip_at) { + dir = sk.math.normalize(b - a); + tip = if (tip_at - walked >= len) b else sk.math.mix(a, b, (tip_at - walked) / len); + } + walked += len; + } + const pen = u.at.pen(.{ .stroke = rgb(c), .fill = rgb(c), .width = options.width, .join = .round, .cap = .butt }); + var stroke = pen; + stroke.fill = null; + if (body.items.len > 1) { + if (options.dashed) sk.draw.dashed(u.f, body.items, theme.stroke.dash, stroke) else sk.draw.polyline(u.f, body.items, stroke); + } + if (options.head) |h| { + const back = tip - sk.math.scale(dir, h[0]); + const side = sk.math.scale(sk.math.perp(dir), h[1] / 2); + var solid = pen; + solid.stroke = null; + sk.draw.polygon(u.f, &.{ tip, back + side, back - side }, solid); + } + } + + /// A straight line between two points (a tree's edge). + pub fn line(u: Ui, a: Vec, b: Vec, c: anytype, w: f32) void { + sk.draw.line(u.f, a, b, u.at.pen(.{ .stroke = rgb(c), .width = w, .cap = .butt })); + } + + pub fn disc(u: Ui, center: Vec, radius: f32, fill_color: ?Color, stroke: ?Color, w: f32) void { + sk.draw.circle(u.f, center, radius, u.at.pen(.{ .fill = fill_color, .stroke = stroke, .width = w })); + } +}; + +/// How a value is set, by its class. +pub const Value = struct { + buf: [20]u8 = undefined, + len: usize = 0, + prefix: []const u8 = "", + /// In groups of four from the right. + grouped: bool = false, + /// Its decimal follows, faint: a small number that may be either. + decimal: bool = false, + + pub fn of(v: u64, pointer: bool) Value { + var shown: Value = .{}; + const negative = !pointer and v > std.math.maxInt(u64) - 4096; + if (negative) { + shown.len = (std.fmt.bufPrint(&shown.buf, "-{d}", .{0 -% v}) catch unreachable).len; + } else if (!pointer and v < 10) { + shown.len = (std.fmt.bufPrint(&shown.buf, "{d}", .{v}) catch unreachable).len; + } else { + shown.len = (std.fmt.bufPrint(&shown.buf, "{x}", .{v}) catch unreachable).len; + // The user's direction: hex unbroken, as a debugger prints it (the design had groups of four). + shown.grouped = false; + shown.decimal = !pointer and v <= 0xffff; + if (!pointer) shown.prefix = "0x"; + } + return shown; + } + + pub fn digits(shown: *const Value) []const u8 { + return shown.buf[0..shown.len]; + } + + /// Units its digits take at mono 20 (the faint decimal apart). + pub fn width(shown: *const Value) f32 { + const adv = advance(theme.size.mono.context); + const groups: f32 = if (shown.grouped) @floatFromInt((shown.len + 3) / 4) else 1; + return @as(f32, @floatFromInt(shown.prefix.len + shown.len)) * adv + (groups - 1) * theme.metric.context.group_gap; + } +}; + +/// How hot a written thing is, from `cursor.Now.heat`. +pub const Heat = struct { + /// 0 no mark, up to 1 the mark in full (less while it leaves). + level: f32 = 0, + /// 1 solid, 0 cooled to the ember. + solid: f32 = 0, + /// 0 to 1 as the strike's fill comes in over what was there (`from`). + in: f32 = 1, + /// What it strikes over: the mark the steps before left. + from: enum { nothing, ember } = .nothing, + /// 0 to 1: armed, a frame round the cell before it is struck. + arm: f32 = 0, + /// The strike's pulse: how far out its edges are, in units. + out: f32 = 0, + + pub const none: Heat = .{}; + pub const ember: Heat = .{ .level = 1 }; +}; + +test "the camera zooms by ratio and ends where it was sent" { + const a: anim.Camera = .{}; + const b = framing(.{ .x = 64, .y = 528, .w = 728, .h = 360 }, 2); + try std.testing.expectApproxEqAbs(@sqrt(2.0), between(a, b, 0.5).zoom, 1e-4); + try std.testing.expectApproxEqAbs(a.x, between(a, b, 0).x, 1e-3); + try std.testing.expectApproxEqAbs(b.x, between(a, b, 1).x, 1e-3); + try std.testing.expectApproxEqAbs(b.y, between(a, b, 1).y, 1e-3); +} + +test "a value is set by its class" { + const expectEqualStrings = std.testing.expectEqualStrings; + try expectEqualStrings("4", Value.of(4, false).digits()); + try std.testing.expect(Value.of(0x21, false).decimal and std.mem.eql(u8, Value.of(0x21, false).prefix, "0x")); + try expectEqualStrings("-1", Value.of(std.math.maxInt(u64), false).digits()); + // An address: as long as it is, no `0x`, unbroken. + const address = Value.of(0x7ffe970f17b0, true); + try expectEqualStrings("7ffe970f17b0", address.digits()); + try std.testing.expectEqual(@as(f32, 12 * 12), address.width()); + try std.testing.expect(address.prefix.len == 0 and !address.decimal); +} + +test "hex is grouped in fours" { + var buf: [20]u8 = undefined; + try std.testing.expectEqualStrings("7fff ffff d8c0", grouped(&buf, 0x7fffffffd8c0)); + try std.testing.expectEqualStrings("8000", grouped(&buf, 0x8000)); + try std.testing.expectEqualStrings("1 0000", grouped(&buf, 0x10000)); +} + +test "hex widths are whole units at the design's sizes" { + try std.testing.expectEqual(@as(f32, 18), advance(theme.size.mono.data)); + try std.testing.expectEqual(@as(f32, 315), hexWidth(16, theme.size.mono.data)); + try std.testing.expectEqual(@as(f32, 196), hexWidth(12, theme.size.mono.small)); + try std.testing.expectEqual(@as(f32, 36), hexWidth(2, theme.size.mono.data)); +} |
