summaryrefslogtreecommitdiff
path: root/src/animation.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/animation.zig')
-rw-r--r--src/animation.zig157
1 files changed, 3 insertions, 154 deletions
diff --git a/src/animation.zig b/src/animation.zig
index ce0b382a..1738ba7d 100644
--- a/src/animation.zig
+++ b/src/animation.zig
@@ -1,7 +1,8 @@
//! Animation curves and the panel transition records every shell reads:
//! easing presets, transition kinds and their tracks, the fractional boxes
-//! they move through, the character effects' per-cell sources, and the
-//! generic displayed-value Transition the chrome colours use.
+//! 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).
const std = @import("std");
pub const ascii_max_movement_frames: u16 = 12;
@@ -1324,155 +1325,3 @@ test "a blink holds while typing, eases at its edges and stops when idle" {
try std.testing.expectEqual(@as(?u64, null), Blink.at(Blink.hold_ns + Blink.idle_ns + ms, 0).next_ns);
try std.testing.expectEqual(@as(f32, 1), Blink.at(Blink.hold_ns + Blink.idle_ns + ms, 0).alpha);
}
-
-/// A displayed value that fades from one target to the next over
-/// `transition_steps` frames; the chrome colours are one.
-pub fn Fade(comptime Value: type) type {
- return struct {
- const Self = @This();
-
- from: Value,
- to: Value,
- displayed: Value,
- step: u16 = transition_steps,
- /// Set off from partway through another fade: it eases out (moving
- /// at once) instead of in and out, so it never stalls (§7.2).
- retargeted: bool = false,
-
- pub fn init(value: Value) Self {
- return .{ .from = value, .to = value, .displayed = value };
- }
-
- pub fn isActive(a: *const Self) bool {
- return a.step < transition_steps;
- }
-
- pub fn retarget(a: *Self, target: Value) void {
- a.retargeted = a.isActive();
- a.from = a.displayed;
- a.to = target;
- a.step = if (std.meta.eql(a.from, target)) transition_steps else 0;
- if (a.step == transition_steps) a.displayed = target;
- }
-
- /// A state change eases in and out (§8.1); a retargeted one out.
- const resolution: u16 = 1000;
-
- pub fn advance(a: *Self) void {
- if (!a.isActive()) return;
- a.step += 1;
- const t = @as(f32, @floatFromInt(a.step)) / @as(f32, @floatFromInt(transition_steps));
- const eased = sample(if (a.retargeted) .out_cubic else .smooth, t);
- a.displayed = if (a.step == transition_steps)
- a.to
- else
- Value.interpolate(a.from, a.to, @intFromFloat(@round(eased * resolution)), resolution);
- }
-
- pub fn snap(a: *Self, value: Value) void {
- a.* = init(value);
- }
- };
-}
-
-pub fn Immediate(comptime Value: type) type {
- return struct {
- const Self = @This();
-
- displayed: Value,
-
- pub fn init(value: Value) Self {
- return .{ .displayed = value };
- }
-
- pub fn isActive(_: *const Self) bool {
- return false;
- }
-
- pub fn retarget(a: *Self, target: Value) void {
- a.displayed = target;
- }
-
- pub fn advance(_: *Self) void {}
-
- pub fn snap(a: *Self, value: Value) void {
- a.displayed = value;
- }
- };
-}
-
-/// A Fade's value for its tests: a colour stepped channel by channel.
-const TestColor = struct {
- rgb: [3]u8,
-
- pub fn interpolate(from: TestColor, to: TestColor, step: u16, steps: u16) TestColor {
- var out = from;
- for (&out.rgb, to.rgb) |*c, b| c.* = @intCast((@as(u32, c.*) * (steps - step) + @as(u32, b) * step + steps / 2) / steps);
- return out;
- }
-};
-
-test "Immediate lands where a completed Transition lands" {
- const from: TestColor = .{ .rgb = .{ 240, 3, 90 } };
- const to: TestColor = .{ .rgb = .{ 5, 222, 90 } };
-
- var faded = Fade(TestColor).init(from);
- faded.retarget(to);
- for (0..transition_steps) |_| faded.advance();
-
- var instant = Immediate(TestColor).init(from);
- try std.testing.expect(!instant.isActive());
- instant.retarget(to);
- try std.testing.expectEqual(faded.displayed, instant.displayed);
-
- // Never active, so a frontend that renders only while something is animating stops immediately
- // rather than spending ten frames discovering there is nothing to draw.
- try std.testing.expect(!instant.isActive());
- instant.advance();
- try std.testing.expectEqual(to, instant.displayed);
-
- instant.snap(from);
- try std.testing.expectEqual(from, instant.displayed);
-}
-
-test "fixed-step interpolation has exact monotonic endpoints" {
- const Tween = Fade(TestColor);
- const from: TestColor = .{ .rgb = .{ 240, 3, 90 } };
- const to: TestColor = .{ .rgb = .{ 5, 222, 90 } };
- var tween = Tween.init(from);
- tween.retarget(to);
- try std.testing.expectEqual(from, tween.displayed);
-
- var previous = tween.displayed;
- for (0..transition_steps) |_| {
- tween.advance();
- try std.testing.expect(tween.displayed.rgb[0] <= previous.rgb[0]);
- try std.testing.expect(tween.displayed.rgb[1] >= previous.rgb[1]);
- try std.testing.expectEqual(@as(u8, 90), tween.displayed.rgb[2]);
- previous = tween.displayed;
- }
- try std.testing.expect(!tween.isActive());
- try std.testing.expectEqual(to, tween.displayed);
- tween.advance();
- try std.testing.expectEqual(to, tween.displayed);
-}
-
-test "retarget starts at the currently displayed value" {
- const Tween = Fade(TestColor);
- const first: TestColor = .{ .rgb = .{ 0, 40, 200 } };
- const second: TestColor = .{ .rgb = .{ 200, 140, 0 } };
- const third: TestColor = .{ .rgb = .{ 20, 10, 250 } };
- var tween = Tween.init(first);
- tween.retarget(second);
- tween.advance();
- tween.advance();
- tween.advance();
- const on_screen = tween.displayed;
-
- tween.retarget(third);
- try std.testing.expectEqual(on_screen, tween.from);
- try std.testing.expectEqual(on_screen, tween.displayed);
- try std.testing.expect(tween.isActive());
- for (0..transition_steps) |_| tween.advance();
- try std.testing.expectEqual(third, tween.displayed);
-}