summaryrefslogtreecommitdiff
path: root/src/animation.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 11:50:43 -0300
committerGabriel Schneider <[email protected]>2026-11-05 15:17:05 -0300
commitecdb0996147f319a4427273bf276e1fc5bf89156 (patch)
tree0b1be457ce4b8ce99db4acd713ff2620cde2d015 /src/animation.zig
parent2bd4066a932ec8fc401e4b85e9f8441b17df3cfb (diff)
downloadpardes-ecdb0996147f319a4427273bf276e1fc5bf89156.tar.gz
pardes-ecdb0996147f319a4427273bf276e1fc5bf89156.zip
effects out, the panel transitions: the eleven Panel words in both tracks go, with the core's Presentation (tracks, the frozen previous grid, cell diffs, the acknowledged-geometry pending guard, and the pointer's mapping through a moving pane, so a pointer's cell is the cell it is over), draw.zig's core-composed character effects, animation.zig's transition half, the Surface's tracks and previous grid, the tty's compositor (src/tui/panel_compositor.zig: the glide's rows move into tui.zig) and the window's track groups, frozen old layers, last-frame image snapshot, transition instance fields and shader paths (ui.vert, ui.frag's crossfade, image.vert and image.frag, the transition-<n>.ppm capture); acknowledgePanelPresentation becomes `presented`, which a frontend calls after drawing a frame (a drag's moving end and a look-hover follow what is under the pointer now), and its tape site with it; the pointer tape site goes; GUI golden 18-mid-transition goes, ttyhelp's golden loses the `SPC a` rows (its wait is on `SPC Y` now), the window monkey loses PanelSlide, and the settings behaviour test's floor of on/off words is ten
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/animation.zig')
-rw-r--r--src/animation.zig691
1 files changed, 10 insertions, 681 deletions
diff --git a/src/animation.zig b/src/animation.zig
index 51164495..25abf421 100644
--- a/src/animation.zig
+++ b/src/animation.zig
@@ -1,664 +1,26 @@
-//! Animation curves and the panel transition records every shell reads:
-//! easing presets, transition kinds and their tracks, the fractional boxes
-//! they move through, and the character effects' per-cell sources; the
-//! springs that settle a view, a lift and the cursor (colors.zig fades the
-//! chrome).
+//! The springs that settle a view and the cursor, and the Motion flavours
+//! they move by (colors.zig fades the chrome).
const std = @import("std");
-pub const ascii_max_movement_frames: u16 = 12;
-
-pub const Easing = enum(u8) {
- linear,
- smooth,
- /// Quintic ease-in-out. It creeps at both ends and crosses the middle of
- /// the distance fast, inside the same frame count a linear walk would use.
- smoother,
- in_cubic,
- out_cubic,
- out_back,
-};
-
-pub const Transition = enum(u8) {
- // Numeric values are shared with the GUI shader ABI.
- off = 0,
- slide = 1,
- zoom = 2,
- dissolve = 3,
- ascii = 4,
- vertical = 5,
- edges = 6,
- fall = 7,
- wave = 8,
- curtain = 9,
- scramble = 10,
- typewriter = 11,
-
- pub fn easing(effect: Transition) Easing {
- return switch (effect) {
- .off, .dissolve, .wave => .smooth,
- .slide, .vertical, .edges => .out_cubic,
- .zoom => .out_back,
- // Character walks and per-cell locks read best with a slow start,
- // a fast middle, and a slow settle over their fixed frame count.
- .ascii, .fall, .scramble => .smoother,
- // A sweep and a typewriter are constant-rate by definition: easing
- // their head would make the pass visibly hesitate mid-pane.
- .curtain, .typewriter => .linear,
- };
- }
-
- pub fn frames(effect: Transition) u16 {
- return switch (effect) {
- .off => 0,
- .slide => 12,
- .zoom => 14,
- .dissolve => 10,
- .ascii => ascii_max_movement_frames + 1,
- .vertical => 12,
- .edges, .curtain, .scramble => 12,
- // Travelling motion needs a couple more samples than a lock or a
- // rigid slide before it stops reading as a jump.
- .fall, .wave, .typewriter => 14,
- };
- }
-
- pub fn composedByCore(effect: Transition) bool {
- return switch (effect) {
- .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => true,
- .off, .slide, .zoom, .dissolve, .vertical => false,
- };
- }
-
- pub fn needsPreviousGrid(effect: Transition) bool {
- return effect == .dissolve or effect == .vertical or effect.composedByCore();
- }
-
- pub fn lifecycleOnly(effect: Transition) bool {
- return effect == .vertical;
- }
-};
-
-pub const Phase = enum(u8) {
- opening = 0,
- moving = 1,
- /// Presentation-only content whose pane lifetime has already ended.
- /// It is never a valid input target.
- closing = 2,
-};
-
-pub const Box = extern struct {
- x: f32 = 0,
- y: f32 = 0,
- w: f32 = 0,
- h: f32 = 0,
-
- pub fn eql(a: Box, b: Box) bool {
- return a.x == b.x and a.y == b.y and a.w == b.w and a.h == b.h;
- }
-
- /// A cell belongs to a fractional box when its centre lies inside:
- /// drawing and hit testing both ask this.
- pub fn contains(box: Box, col: u16, row: u16) bool {
- const x = @as(f32, @floatFromInt(col)) + 0.5;
- const y = @as(f32, @floatFromInt(row)) + 0.5;
- return x >= box.x and x < box.x + box.w and y >= box.y and y < box.y + box.h;
- }
-};
-
-// Drawing and hit testing share this moving/opening/closing order.
-pub fn paintOrder(live: []const ?Track, closing: []const Track, out: []Track) usize {
- var len: usize = 0;
- for ([_]Phase{ .moving, .opening }) |phase| for (live) |maybe| {
- const track = maybe orelse continue;
- if (!track.active() or track.phase != phase) continue;
- if (len == out.len) return len;
- out[len] = track;
- len += 1;
- };
- for (closing) |track| {
- if (!track.active()) continue;
- if (len == out.len) return len;
- out[len] = track;
- len += 1;
- }
- return len;
-}
-
-/// One POD record is enough for every backend. `from` and `to` are logical
-/// cell boxes; frontends convert them to pixels only at their render edge.
-pub const Track = extern struct {
- serial: u32 = 0,
- pane: u8 = 0,
- phase: Phase = .moving,
- effect: Transition = .off,
- /// The Motion flavour a pane's move follows, plus one; 0 is the
- /// effect's own easing (Presentation.stamp).
- motion: u8 = 0,
- frame: u16 = 0,
- frame_count: u16 = 0,
- from: Box = .{},
- to: Box = .{},
- /// The move's speed as it starts, in its lengths per unit of progress:
- /// 0 sets off as the flavour does; a move retargeted mid-way carries
- /// the old one's speed on (Presentation.sync), so it never stops dead.
- launch: f32 = 0,
-
- pub fn active(track: Track) bool {
- return track.effect != .off and track.frame < track.frames();
- }
-
- pub fn frames(track: Track) u16 {
- return if (track.frame_count != 0) track.frame_count else track.effect.frames();
- }
-
- pub fn amount(track: Track) f32 {
- // Opening rises quickly and settles; closing reverses that motion and
- // accelerates down out of the fixed clip.
- if (track.phase == .closing and track.effect == .vertical)
- return progressEased(.in_cubic, track.frames(), track.frame);
- if (track.flavour()) |motion|
- return motion.easeFrom(track.progress(), track.launch);
- return progressEased(track.effect.easing(), track.frames(), track.frame);
- }
-
- /// The flavour an opening or moving pane follows; a closing one keeps
- /// its effect's own exit.
- fn flavour(track: Track) ?Motion {
- if (track.motion == 0 or track.phase == .closing) return null;
- const which = std.enums.fromInt(Motion.Flavour, track.motion - 1) orelse return null;
- return Motion.of(which);
- }
-
- /// How fast its presented box moves now, in cells per frame of its
- /// own (x, y, w, h); zero when it follows no flavour or has landed.
- pub fn velocity(track: Track) Box {
- const motion = track.flavour() orelse return .{};
- const u = track.progress();
- if (u >= 1 or track.frames() <= 1) return .{};
- const rate = motion.speedFrom(u, track.launch) / @as(f32, @floatFromInt(track.frames() - 1));
- return .{ .x = (track.to.x - track.from.x) * rate, .y = (track.to.y - track.from.y) * rate, .w = (track.to.w - track.from.w) * rate, .h = (track.to.h - track.from.h) * rate };
- }
-
- fn progress(track: Track) f32 {
- if (track.frames() <= 1 or track.frame >= track.frames() - 1) return 1;
- return @as(f32, @floatFromInt(track.frame)) / @as(f32, @floatFromInt(track.frames() - 1));
- }
-
- pub fn presented(track: Track) Box {
- // A flavour's wind-up goes back past where the move began.
- const t = track.amount();
- // Landed, it is exactly where it goes: no stretch or float residue.
- if (track.flavour() != null and track.progress() >= 1) return track.to;
- var box = if (t < 0) lerpBox(track.to, track.from, 1 - t) else lerpBox(track.from, track.to, t);
- // Squash and stretch: along its path a moving pane lengthens with
- // its speed and, landing past its mark and coming back, squashes
- // (the user's choice: its text with it; a pointer inverts the same
- // box, so a click lands on the glyph it is drawn as).
- const motion = track.flavour() orelse return box;
- if (motion.stretch == 0) return box;
- const factor = 1 + motion.stretch * std.math.clamp(motion.speedFrom(track.progress(), track.launch) / 4, -1, 1);
- if (@abs(track.to.x - track.from.x) >= @abs(track.to.y - track.from.y)) {
- const w = box.w * factor;
- box.x -= (w - box.w) / 2;
- box.w = w;
- } else {
- const h = box.h * factor;
- box.y -= (h - box.h) / 2;
- box.h = h;
- }
- return box;
- }
-
- pub fn visualBox(track: Track) Box {
- return switch (track.effect) {
- .slide, .zoom, .vertical => track.presented(),
- // A fade, a dissolve and every character effect stay inside the
- // pane's final rectangle.
- .off, .dissolve, .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => track.to,
- };
- }
-
- pub fn contentBox(track: Track) Box {
- return if (track.phase == .closing) track.from else track.to;
- }
-};
-
-pub fn openingBox(effect: Transition, target: Box, screen_width: u16) Box {
- return switch (effect) {
- .slide => blk: {
- var from = target;
- const middle = target.x + target.w * 0.5;
- from.x = if (middle < @as(f32, @floatFromInt(screen_width)) * 0.5)
- -target.w
- else
- @floatFromInt(screen_width);
- break :blk from;
- },
- .zoom => .{
- .x = target.x + target.w * 0.5,
- .y = target.y + target.h * 0.5,
- .w = 0,
- .h = 0,
- },
- .vertical => blk: {
- var from = target;
- from.y += target.h;
- break :blk from;
- },
- // A dissolve and the character effects own the glyphs inside a fixed
- // rectangle, so their panel opens at exactly its final geometry.
- .off, .dissolve, .ascii, .edges, .fall, .wave, .curtain, .scramble, .typewriter => target,
- };
-}
-
-pub fn closingBox(effect: Transition, source: Box) Box {
- return switch (effect) {
- .vertical => blk: {
- var to = source;
- to.y += source.h;
- break :blk to;
- },
- else => source,
- };
-}
+/// The curves a theme change's chrome fades on (colors.ChromeAnimation).
+pub const Easing = enum(u8) { smooth, out_cubic };
pub fn sample(easing: Easing, raw: f32) f32 {
const t = std.math.clamp(raw, 0.0, 1.0);
return switch (easing) {
- .linear => t,
.smooth => t * t * (3.0 - 2.0 * t),
- .smoother => t * t * t * (t * (6.0 * t - 15.0) + 10.0),
- .in_cubic => t * t * t,
.out_cubic => 1.0 - (1.0 - t) * (1.0 - t) * (1.0 - t),
- // Robert Penner's ease-out-back polynomial. It intentionally travels
- // a little past one before settling exactly on the endpoint.
- .out_back => blk: {
- const c1: f32 = 1.70158;
- const c3 = c1 + 1.0;
- const u = t - 1.0;
- break :blk 1.0 + c3 * u * u * u + c1 * u * u;
- },
};
}
-fn progressEased(easing: Easing, frames: u16, frame: u16) f32 {
- if (frames <= 1 or frame >= frames - 1) return 1.0;
- return sample(easing, @as(f32, @floatFromInt(frame)) / @as(f32, @floatFromInt(frames - 1)));
-}
-
-pub fn lerpBox(from: Box, to: Box, t: f32) Box {
- const u = @max(0.0, t);
- return .{
- .x = from.x + (to.x - from.x) * u,
- .y = from.y + (to.y - from.y) * u,
- .w = @max(0.0, from.w + (to.w - from.w) * u),
- .h = @max(0.0, from.h + (to.h - from.h) * u),
- };
-}
-
-/// Stable cell noise shared by the TTY reveal and shader ports. Integer-only
-/// hashing means resizing or repainting a frame does not make cells flicker.
-pub fn cellNoise(serial: u32, col: u16, row: u16) f32 {
- var x = serial ^ (@as(u32, col) *% 0x9e37_79b9) ^ (@as(u32, row) *% 0x85eb_ca6b);
- x ^= x >> 16;
- x *%= 0x7feb_352d;
- x ^= x >> 15;
- x *%= 0x846c_a68b;
- x ^= x >> 16;
- return @as(f32, @floatFromInt(x & 0xffff)) / 65535.0;
-}
-
-/// Whether a changed dissolve cell has crossed from the frozen old grid to
-/// the new one. Exact endpoints are part of the presentation contract.
-/// T4 (§9.2): an ordered swap, each cell at its place in a 4x4 Bayer
-/// matrix (shifted by the pane's serial so two panes differ), so the new
-/// content comes in as an even, patterned screen rather than noise, and
-/// each cell swaps once.
-pub fn dissolveRevealed(serial: u32, col: u16, row: u16, raw_progress: f32) bool {
- const t = std.math.clamp(raw_progress, 0.0, 1.0);
- if (t <= 0) return false;
- if (t >= 1) return true;
- const bayer = [4][4]u8{ .{ 0, 8, 2, 10 }, .{ 12, 4, 14, 6 }, .{ 3, 11, 1, 9 }, .{ 15, 7, 13, 5 } };
- const x = (@as(u32, col) +% serial) % 4;
- const y = (@as(u32, row) +% serial / 4) % 4;
- return (@as(f32, @floatFromInt(bayer[y][x])) + 0.5) / 16 < t;
-}
-
-pub const CellArea = struct {
- x0: u16 = 0,
- y0: u16 = 0,
- cols: u16 = 1,
- rows: u16 = 1,
-
- pub fn of(box: Box) CellArea {
- return .{
- .x0 = floorCell(box.x),
- .y0 = floorCell(box.y),
- .cols = ceilCell(box.w),
- .rows = ceilCell(box.h),
- };
- }
-};
-
-fn floorCell(value: f32) u16 {
- return @intFromFloat(std.math.clamp(@floor(value), 0.0, @as(f32, std.math.maxInt(u16))));
-}
-
-fn ceilCell(value: f32) u16 {
- return @intFromFloat(std.math.clamp(@ceil(value), 1.0, @as(f32, std.math.maxInt(u16))));
-}
-
-pub const CharSource = union(enum) {
- /// Nothing has arrived here yet: keep the frozen old cell.
- old,
- at: Offset,
- /// Paint this printable byte in the destination cell's own style, whatever
- /// that cell holds — a caret marching over empty space is still a caret.
- byte: u8,
- churn: u8,
- /// The destination cell with its colours still this far (0..1) toward
- /// the frozen old cell's ground: a soft edge between the two grids.
- edge: f32,
-
- pub const Offset = struct { cols: i32 = 0, rows: i32 = 0 };
-
- pub const settled: CharSource = .{ .at = .{} };
+/// A fractional box in grid cells: the cursor's quad aims at one.
+pub const Box = extern struct {
+ x: f32 = 0,
+ y: f32 = 0,
+ w: f32 = 0,
+ h: f32 = 0,
};
-pub fn charSource(track: Track, col: u16, row: u16, area: CellArea) CharSource {
- const t = track.amount();
- if (t >= 1.0) return .settled;
- const w: f32 = @floatFromInt(area.cols);
- const h: f32 = @floatFromInt(area.rows);
- const c: f32 = @floatFromInt(col);
- const r: f32 = @floatFromInt(row);
- const remaining = 1.0 - t;
- return switch (track.effect) {
- .edges => blk: {
- const travel = cellsOf(remaining * (w + 1.0));
- break :blk .{ .at = .{ .cols = if (row % 2 == 0) travel else -travel } };
- },
- // Columns rain down, each with its own stable head start, so the pane
- // fills from the top and the last glyphs land at the bottom.
- .fall => blk: {
- const local = staggered(t, cellNoise(track.serial, col, 0) * 0.4);
- if (local <= 0.0) break :blk .old;
- break :blk .{ .at = .{ .rows = cellsOf((1.0 - local) * (h + 1.0)) } };
- },
- // A vertical ripple travels left to right and its amplitude decays, so
- // the pane settles out of a wave instead of a fade.
- .wave => .{ .at = .{
- .rows = cellsOf(remaining * @min(4.0, h) * @sin(c * 0.55 - t * 9.0)),
- } },
- // A wipe, left to right: the new grid behind the head, the old one
- // ahead of it, and between them a soft edge two cells wide whose
- // colours step from the old ground to the new. Only the head's
- // columns change from one frame to the next.
- .curtain => blk: {
- const lead = t * (w + 2.0) - c;
- if (lead <= 0.0) break :blk .old;
- if (lead <= 1.0) break :blk .{ .edge = 2.0 / 3.0 };
- if (lead <= 2.0) break :blk .{ .edge = 1.0 / 3.0 };
- break :blk .settled;
- },
- // Every cell churns through printable ASCII and locks onto its final
- // glyph at its own stable threshold: the pane resolves out of noise.
- .scramble => blk: {
- if (t >= cellNoise(track.serial, col, row) * 0.8) break :blk .settled;
- const churn = cellNoise(
- track.serial ^ (@as(u32, track.frame) *% 0x27d4_eb2f),
- col,
- row,
- );
- break :blk .{ .churn = @intCast(33 + @min(93, @as(u32, @intFromFloat(churn * 94.0)))) };
- },
- // Reading-order reveal with a caret sitting on the write head.
- .typewriter => blk: {
- const head = t * w * h;
- const index = r * w + c;
- if (index + 1.0 <= head) break :blk .settled;
- if (index <= head) break :blk .{ .byte = '_' };
- break :blk .old;
- },
- // PanelAscii walks its own byte distance per cell, and the geometry
- // effects never reach this path at all.
- .off, .slide, .zoom, .dissolve, .vertical, .ascii => .settled,
- };
-}
-
-fn cellsOf(distance: f32) i32 {
- return @intFromFloat(@round(std.math.clamp(distance, -65535.0, 65535.0)));
-}
-
-/// Remap track progress into one cell's own window. A stagger delays a glyph
-/// without making the effect as a whole end after its last frame.
-fn staggered(t: f32, delay: f32) f32 {
- if (delay >= 1.0) return t;
- return (t - delay) / (1.0 - delay);
-}
-
-test "easing presets have exact endpoints and intended shapes" {
- inline for (std.enums.values(Easing)) |easing| {
- try std.testing.expectEqual(@as(f32, 0), sample(easing, 0));
- try std.testing.expectEqual(@as(f32, 1), sample(easing, 1));
- }
- try std.testing.expectEqual(@as(f32, 0.5), sample(.linear, 0.5));
- try std.testing.expect(sample(.in_cubic, 0.5) < sample(.linear, 0.5));
- try std.testing.expect(sample(.out_cubic, 0.5) > sample(.linear, 0.5));
- try std.testing.expect(sample(.out_back, 0.8) > 1.0);
- // Slow at both ends, fast through the middle, and symmetric about the
- // halfway point: the same curve the integer byte walk reproduces.
- try std.testing.expectEqual(@as(f32, 0.5), sample(.smoother, 0.5));
- try std.testing.expect(sample(.smoother, 0.15) < sample(.smooth, 0.15));
- try std.testing.expect(sample(.smoother, 0.85) > sample(.smooth, 0.85));
- try std.testing.expect(sample(.smoother, 0.6) - sample(.smoother, 0.4) >
- sample(.linear, 0.6) - sample(.linear, 0.4));
-}
-
-test "transition progress completes exactly" {
- inline for (std.enums.values(Transition)) |effect| {
- try std.testing.expectEqual(@as(f32, 1), progressEased(effect.easing(), effect.frames(), effect.frames()));
- if (effect.frames() > 0)
- try std.testing.expectEqual(@as(f32, 1), progressEased(effect.easing(), effect.frames(), effect.frames() - 1));
- try std.testing.expectEqual(@as(f32, 1), progressEased(effect.easing(), effect.frames(), std.math.maxInt(u16)));
- }
- try std.testing.expectEqual(@as(f32, 1), progressEased(Transition.off.easing(), Transition.off.frames(), 0));
-
- const shrinking = lerpBox(.{ .w = 100, .h = 40 }, .{}, sample(.out_back, 0.8));
- try std.testing.expectEqual(@as(f32, 0), shrinking.w);
- try std.testing.expectEqual(@as(f32, 0), shrinking.h);
- const opening = lerpBox(.{}, .{ .w = 100, .h = 40 }, sample(.out_back, 0.8));
- try std.testing.expect(opening.w > 100);
- try std.testing.expect(opening.h > 40);
-}
-
-test "character effects are core-composed and settle on the canonical glyph" {
- const box: Box = .{ .x = 4, .y = 2, .w = 20, .h = 6 };
- const area: CellArea = .of(box);
- try std.testing.expectEqual(@as(u16, 4), area.x0);
- try std.testing.expectEqual(@as(u16, 2), area.y0);
- try std.testing.expectEqual(@as(u16, 20), area.cols);
- try std.testing.expectEqual(@as(u16, 6), area.rows);
-
- inline for (std.enums.values(Transition)) |effect| {
- if (comptime !effect.composedByCore()) continue;
- // Core composition needs the frozen old grid for every glyph which has
- // not arrived, so no character effect may animate without it.
- try std.testing.expect(effect.needsPreviousGrid());
- if (comptime effect == .ascii) continue; // owns its own per-cell byte walk
-
- const last: Track = .{ .effect = effect, .frame = effect.frames() - 1, .to = box };
- const first: Track = .{ .effect = effect, .frame = 0, .to = box };
- var moving = false;
- var row: u16 = 0;
- while (row < area.rows) : (row += 1) {
- var col: u16 = 0;
- while (col < area.cols) : (col += 1) {
- // The last active sample is the exact canonical grid: no cell
- // is displaced, churning, or still frozen.
- try std.testing.expectEqual(CharSource.settled, charSource(last, col, row, area));
- if (!std.meta.eql(CharSource.settled, charSource(first, col, row, area)))
- moving = true;
- }
- }
- try std.testing.expect(moving);
- }
-}
-
-test "each character effect moves glyphs along its own axis" {
- const box: Box = .{ .w = 30, .h = 8 };
- const area: CellArea = .of(box);
-
- // Rows alternate which screen edge they come from, and every glyph in a row
- // travels as one rigid slide: one offset, no vertical component.
- var edges: Track = .{ .effect = .edges, .frame = 2, .to = box };
- const even = charSource(edges, 5, 0, area).at;
- const odd = charSource(edges, 5, 1, area).at;
- try std.testing.expect(even.cols > 0);
- try std.testing.expectEqual(-even.cols, odd.cols);
- try std.testing.expectEqual(@as(i32, 0), even.rows);
- try std.testing.expectEqual(even, charSource(edges, 17, 0, area).at);
- edges.frame = 5;
- try std.testing.expect(charSource(edges, 5, 0, area).at.cols < even.cols);
-
- // Falling columns are vertical only, staggered, and sample from below the
- // destination because the new text is still above the pane.
- const fall: Track = .{ .effect = .fall, .frame = 4, .to = box };
- var falling = false;
- var col: u16 = 0;
- while (col < area.cols) : (col += 1) switch (charSource(fall, col, 0, area)) {
- .old => {},
- .byte, .churn, .edge => return error.FallShouldNotChurn,
- .at => |offset| {
- try std.testing.expectEqual(@as(i32, 0), offset.cols);
- try std.testing.expect(offset.rows >= 0);
- if (offset.rows > 0) falling = true;
- },
- };
- try std.testing.expect(falling);
-
- // The wave displaces rows both ways as it travels, and only rows.
- const wave: Track = .{ .effect = .wave, .frame = 1, .to = box };
- var above = false;
- var below = false;
- col = 0;
- while (col < area.cols) : (col += 1) {
- const offset = charSource(wave, col, 3, area).at;
- try std.testing.expectEqual(@as(i32, 0), offset.cols);
- if (offset.rows < 0) above = true;
- if (offset.rows > 0) below = true;
- }
- try std.testing.expect(above and below);
-
- // The curtain wipes: columns ahead of its head hold the old grid,
- // columns it has passed are settled, and between them a soft edge of
- // exactly two columns steps from the old ground to the new.
- const curtain: Track = .{ .effect = .curtain, .frame = 5, .to = box };
- try std.testing.expectEqual(CharSource.settled, charSource(curtain, 0, 0, area));
- try std.testing.expectEqual(CharSource{ .old = {} }, charSource(curtain, 29, 0, area));
- var wiped: [2]f32 = undefined;
- var at_edge: usize = 0;
- col = 0;
- while (col < area.cols) : (col += 1) switch (charSource(curtain, col, 0, area)) {
- .edge => |weight| {
- wiped[at_edge] = weight;
- at_edge += 1;
- },
- .old, .at, .byte, .churn => {},
- };
- try std.testing.expectEqual(@as(usize, 2), at_edge);
- try std.testing.expect(wiped[0] < wiped[1]);
-
- var scramble: Track = .{ .effect = .scramble, .frame = 3, .to = box };
- var churning: usize = 0;
- var locked: usize = 0;
- var changed = false;
- col = 0;
- while (col < area.cols) : (col += 1) switch (charSource(scramble, col, 0, area)) {
- .churn => |byte| {
- try std.testing.expect(byte >= ' ' and byte <= '~');
- churning += 1;
- scramble.frame = 4;
- switch (charSource(scramble, col, 0, area)) {
- .churn => |next| changed = changed or next != byte,
- .old, .at, .byte, .edge => {},
- }
- scramble.frame = 3;
- },
- .at => locked += 1,
- .old, .byte, .edge => return error.ScrambleShouldNotFreeze,
- };
- try std.testing.expect(churning > 0 and locked > 0 and changed);
-
- // The typewriter writes in reading order with a caret on its head.
- const typewriter: Track = .{ .effect = .typewriter, .frame = 7, .to = box };
- try std.testing.expectEqual(CharSource.settled, charSource(typewriter, 0, 0, area));
- try std.testing.expectEqual(
- CharSource{ .old = {} },
- charSource(typewriter, area.cols - 1, area.rows - 1, area),
- );
- var carets: usize = 0;
- var row: u16 = 0;
- while (row < area.rows) : (row += 1) {
- col = 0;
- while (col < area.cols) : (col += 1) switch (charSource(typewriter, col, row, area)) {
- .byte => |byte| {
- try std.testing.expectEqual(@as(u8, '_'), byte);
- carets += 1;
- },
- .old, .at, .churn, .edge => {},
- };
- }
- try std.testing.expectEqual(@as(usize, 1), carets);
-}
-
-test "opening presets separate geometry and content transitions" {
- const target: Box = .{ .x = 30, .y = 2, .w = 20, .h = 8 };
- try std.testing.expectEqual(target, openingBox(.ascii, target, 80));
- try std.testing.expectEqual(@as(f32, 0), openingBox(.zoom, target, 80).w);
- try std.testing.expectEqual(@as(f32, 80), openingBox(.slide, target, 80).x);
- try std.testing.expectEqual(@as(f32, target.y + target.h), openingBox(.vertical, target, 80).y);
- try std.testing.expectEqual(@as(f32, target.y + target.h), closingBox(.vertical, target).y);
-
- var track: Track = .{ .effect = .slide, .from = target, .to = target };
- try std.testing.expect(track.active());
- track.frame = track.effect.frames();
- try std.testing.expect(!track.active());
-
- track = .{ .effect = .dissolve, .frame = 3, .from = .{}, .to = target };
- try std.testing.expectEqual(target, track.visualBox());
-
- var closing: Track = .{
- .phase = .closing,
- .effect = .vertical,
- .from = target,
- .to = closingBox(.vertical, target),
- };
- try std.testing.expectEqual(target, closing.contentBox());
- closing.frame = 2;
- try std.testing.expect(closing.amount() < progressEased(Transition.vertical.easing(), Transition.vertical.frames(), closing.frame));
-}
-
-test "the ordered dissolve reveals a 4x4 block evenly, each cell once" {
- for ([_]f32{ 0.25, 0.5, 0.75 }) |t| {
- var revealed: usize = 0;
- for (0..4) |y| for (0..4) |x| {
- if (dissolveRevealed(7, @intCast(x), @intCast(y), t)) revealed += 1;
- };
- try std.testing.expectEqual(@as(usize, @intFromFloat(t * 16)), revealed);
- }
-}
-
-test "dissolve has exact stable endpoints" {
- for (0..64) |col| {
- const x: u16 = @intCast(col);
- try std.testing.expect(!dissolveRevealed(42, x, 7, 0));
- try std.testing.expect(dissolveRevealed(42, x, 7, 1));
- if (dissolveRevealed(42, x, 7, 0.25))
- try std.testing.expect(dissolveRevealed(42, x, 7, 0.75));
- }
-}
-
pub const frame_ms: u32 = 16;
pub const frame_ns: u64 = frame_ms * std.time.ns_per_ms;
pub const transition_steps: u16 = 10;
@@ -910,39 +272,6 @@ test "each flavour is its own motion: off lands at once, bouncy overshoots, play
for (std.enums.values(Motion.Flavour)) |flavour| try std.testing.expectEqual(@as(f32, 1), Motion.of(flavour).ease(1));
}
-test "a flavour's stretched pane lands exactly on its box, and a click finds the glyph drawn there" {
- const from: Box = .{ .x = 0, .y = 2, .w = 30, .h = 10 };
- const to: Box = .{ .x = 37.3, .y = 2, .w = 30, .h = 10 };
- for (std.enums.values(Motion.Flavour)) |flavour| {
- var track: Track = .{ .effect = .slide, .motion = @backingInt(flavour) + 1, .frame_count = 20, .from = from, .to = to };
- track.frame = track.frames() - 1;
- const landed = track.presented();
- // Bit for bit: no stretch or float residue once it lands.
- try std.testing.expectEqual(to, landed);
- }
- // Playful at its most stretched: the pane is drawn wider than its box,
- // and the drawn place of a cell maps back to that cell (the pointer
- // inverts the same box: target + (x - shown.x) / shown.w * target.w).
- var track: Track = .{ .effect = .slide, .motion = @backingInt(Motion.Flavour.playful) + 1, .frame_count = 20, .from = from, .to = to };
- var most: u16 = 0;
- var widest: f32 = 0;
- while (track.frame < track.frames()) : (track.frame += 1) {
- if (track.presented().w > widest) {
- widest = track.presented().w;
- most = track.frame;
- }
- }
- track.frame = most;
- const shown = track.presented();
- try std.testing.expect(shown.w > to.w * 1.05);
- for (0..30) |k| {
- const cell = to.x + @as(f32, @floatFromInt(k)) + 0.5;
- const drawn = shown.x + (cell - to.x) / to.w * shown.w;
- const back = to.x + (drawn - shown.x) / shown.w * to.w;
- try std.testing.expectApproxEqAbs(cell, back, 1e-3);
- }
-}
-
/// A view that follows its input (anim 9, 11): the picture's offset from
/// the core's position now (in the view's own unit: rows of text, pixels of
/// a PDF strip; negative, the picture still shows what is above), its speed