From 60e07feef401d9044db3a1c925cb9d1f6d20dac9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 23:46:02 -0300 Subject: canim: relations as one data-oriented subsystem; aim; the ASan pair; the introduction's chapters From the user. Relations between panes are one subsystem: per-frame tables of places and values in struct-of-arrays, owned by the frame; the kinds one table of data; a resolver of pure passes (join, filter, priority, cut) unit-tested on hand-written tables. Ported kinds draw byte-identical frames; a synthetic map of 4000 places and 6000 values resolves in 0.56 ms against the old table's 5.7. aim, a word holding a code address tied to its row, is the first new kind, off unless a scene turns it on. The ASan pair: one overflow built plain and with ASan, recorded by instruction; the frame as bytes eight a row; the plain run goes on alone in the column, its return address aimed at main's row until the letters reach it; the fault at ret. The introduction's three chapters are built and tested, not yet exported. Annotations, rows and scrolls that changed in one frame now fade; captions stand twelve seconds at most. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- anim/src/aside.zig | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 anim/src/aside.zig (limited to 'anim/src/aside.zig') diff --git a/anim/src/aside.zig b/anim/src/aside.zig new file mode 100644 index 0000000..7cf6867 --- /dev/null +++ b/anim/src/aside.zig @@ -0,0 +1,160 @@ +//! A text beside what it speaks of, for a scene that builds its parts in +//! place rather than in the column's panes (the introduction): the +//! column's `bubble` (design/README.md, "column"), standing alone. Go 40 +//! on 54, code in mono 35, at most 40 characters a line, 16 of padding; +//! the newest on `raised` paper, primary; an older one loses its paper and +//! dims to `dim` (`dim`, 18 f). It arrives rising 8 (its object's fade, +//! 18 f, `enter`) and leaves with its fade (12 f, `exit`). No stem: its +//! level is the tie. +//! shortcut: a second drawing of column.zig's bubble (wrap, paper); fold into one when the column takes custom panes. +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const ui = @import("ui.zig"); +const words = @import("words.zig"); +const theme = ui.theme; +const t = theme.color.text; + +const time = theme.time; +const pad = 16; +const chars = 40; +const first = 40; +const line_pitch = theme.size.sans.caption_leading; + +pub const Aside = struct { + text: []const u8, + /// The paper's left, and the middle of its first line. + x: f32, + level: f32, + /// 0 newest, 1 the account (no paper, `dim`). + dim: anim.Value, + + pub fn draw(a: *const Aside, f: *sk.Frame, placed: anim.Placed) void { + const u = (ui.Ui{ .f = f, .at = placed }).moved(0, (1 - placed.pose.opacity) * theme.time.caption_rise); + const wrapped = wrap(u, a.text); + const top = a.level - first; + const h = first * 2 + @as(f32, @floatFromInt(wrapped.len - 1)) * line_pitch; + var w: f32 = 0; + for (wrapped) |line| w = @max(w, width(u, line)); + const dim = std.math.clamp(placed.value(a.dim), 0, 1); + if (dim < 1) u.faded(1 - dim).fill(.{ .x = a.x, .y = top, .w = w + 2 * pad, .h = h }, theme.color.raised); + const ink = ui.mix(t.primary, t.dim, dim); + for (wrapped, 0..) |line, n| _ = words.runs(u, a.x + pad, top + first + @as(f32, @floatFromInt(n)) * line_pitch + 14, line, .sans, theme.size.sans.caption, theme.size.mono.value, ink); + } +}; + +/// A scene's texts beside their parts, said one after another: each waits +/// until the last has been read, arrives (18 f, `enter`), and is followed +/// by its lead (60 f); the one before it dims (18 f), or leaves (12 f, +/// `exit`) where the new one would stand on it. `leave`: all go together. +pub const Asides = struct { + shown: [8]?anim.Obj = @splat(null), + dims: [8]anim.Value = undefined, + spans: [8][2]f32 = undefined, + n: usize = 0, + read_by: f32 = 0, + + pub fn say(a: *Asides, s: *anim.Frame, text: []const u8, x: f32, level: f32) void { + s.wait(@max(a.read_by - s.cursor, 0)); + const dim = s.value(0); + const made = s.new(Aside{ .text = text, .x = x, .level = level, .dim = dim }); + const obj = s.custom(made, .{ .x = -640, .y = -360, .scale = ui.scale }); + const span = [2]f32{ level - first, level + first + @as(f32, @floatFromInt(lines(text) - 1)) * line_pitch }; + const at = s.cursor; + for (a.shown[0..a.n], a.dims[0..a.n], a.spans[0..a.n]) |*o, d, other| { + s.cursor = at; + if (o.* == null) continue; + if (other[0] < span[1] + 12 and span[0] < other[1] + 12) { + s.play(.{o.*.?.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit }); + o.* = null; + } else s.play(.{d.to(1)}, .{ .seconds = 18.0 / 60.0, .ease = easeGlide }); + } + s.cursor = at; + s.play(.{obj.fadeIn()}, .{ .seconds = time.caption_in, .ease = easeEnter }); + a.shown[a.n] = obj; + a.dims[a.n] = dim; + a.spans[a.n] = span; + a.n += 1; + a.read_by = s.cursor + time.caption(@intCast(std.unicode.utf8CountCodepoints(text) catch text.len)); + s.wait(time.caption_lead); + } + + /// All leave together (12 f), once the last is read. + pub fn leave(a: *Asides, s: *anim.Frame) void { + s.wait(@max(a.read_by - s.cursor, 0)); + const at = s.cursor; + for (a.shown[0..a.n]) |o| if (o) |obj| { + s.cursor = at; + s.play(.{obj.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit }); + }; + } +}; + +fn easeExit(x: f32) f32 { + return theme.ease(.exit, x); +} +fn easeEnter(x: f32) f32 { + return theme.ease(.enter, x); +} +fn easeGlide(x: f32) f32 { + return theme.ease(.glide, x); +} + +/// How many lines `wrap` makes of `text`. +fn lines(text: []const u8) usize { + var it = std.mem.splitScalar(u8, text, ' '); + var n: usize = 0; + var count: usize = 1; + while (it.next()) |word| { + const seen = (std.unicode.utf8CountCodepoints(word) catch word.len) - std.mem.count(u8, word, "`"); + if (n > 0 and n + 1 + seen > chars) { + count += 1; + n = 0; + } + if (n > 0) n += 1; + n += seen; + } + return count; +} + +fn width(u: ui.Ui, line: []const u8) f32 { + var w: f32 = 0; + var parts = std.mem.splitScalar(u8, line, '`'); + var code = false; + while (parts.next()) |part| : (code = !code) w += if (code) u.width(.mono, theme.size.mono.value, part) else u.width(.sans, theme.size.sans.caption, part); + return w; +} + +/// Lines of at most `chars` visible characters, broken at spaces; a code +/// run keeps its backticks on each line it spans. +fn wrap(u: ui.Ui, text: []const u8) []const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var it = std.mem.splitScalar(u8, text, ' '); + var line: std.ArrayList(u8) = .empty; + var n: usize = 0; + while (it.next()) |word| { + const seen = std.unicode.utf8CountCodepoints(word) catch word.len; + const ticks = std.mem.count(u8, word, "`"); + if (n > 0 and n + 1 + seen - ticks > chars) { + out.append(u.f.scratch, line.items) catch break; + line = .empty; + n = 0; + } + if (n > 0) { + line.append(u.f.scratch, ' ') catch break; + n += 1; + } + line.appendSlice(u.f.scratch, word) catch break; + n += seen - ticks; + } + if (line.items.len > 0) out.append(u.f.scratch, line.items) catch {}; + if (out.items.len == 0) out.append(u.f.scratch, "") catch {}; + return out.items; +} + +test "lines of at most forty characters, backticks not counted" { + try std.testing.expectEqual(@as(usize, 1), lines("Its address space, 128 TiB, to scale.")); + try std.testing.expectEqual(@as(usize, 2), lines("Its address space, 128 TiB, to scale: mapped only at each end.")); + // Four code runs of nine: 39 shown, 47 with their backticks. + try std.testing.expectEqual(@as(usize, 1), lines("`012345678` `012345678` `012345678` `012345678`")); +} -- cgit v1.3