summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/next-anim.md25
-rw-r--r--src/gui/gui.zig175
2 files changed, 164 insertions, 36 deletions
diff --git a/docs/next-anim.md b/docs/next-anim.md
index 9f43a232..1acdeacf 100644
--- a/docs/next-anim.md
+++ b/docs/next-anim.md
@@ -163,6 +163,31 @@ keeps the rows that left for the gap, and kicks the spring.
test checks).
- The caret and the glide quad ride the picture (`glideLift`).
+### The view's motion is one system (anim 9)
+
+The author watched sample 3 and saw a bounce a line under a held j: every
+repeat restarted the spring's ease to its new target, so the picture sped
+up and slowed down once an input. Now the picture's motion is one system
+with its own state (`Glide`: its lag behind the text, its speed, and the
+inputs that have been feeding it), integrated in one place every frame
+(`Glide.advance`), and every scroll is an input into it (`Glide.kick`),
+absorbed by what the system is doing:
+
+| the system is | an input comes | it does |
+|---|---|---|
+| at rest | any move (a j, a notch, a page, a jump) | the spring starts an ease from rest |
+| easing | the same way, within 150 ms | the inputs are a stream now: it moves at their rate (rows an input over their spacing), a lag of an input or so behind, never easing to each |
+| fed by a stream | another of the stream | the target moves on; the speed stays the stream's |
+| fed by a stream | the inputs stop (none for 1.5 spacings) | the spring settles it, from the speed it has, never past its mark: the only ease-out |
+| moving | an input the other way | the stream ends; the spring turns it through zero from its speed |
+| moving | a jump past what two frames share | its rows are added (the stitched slide); the speed carries on |
+
+So a held j, a spun wheel and a held Ctrl-d are the same case (a stream),
+a single j or notch the same ease as before. Every input, a repeat too,
+is still drawn at once: in a stream the picture is on the stream's
+straight line wherever a frame samples it (tried: holding repeats to the
+next tick cost a held key 7 ms of latency for no evenness one could see).
+
### The cursor (anim 4)
The thin caret glides as the block does. A step of a cell or less eases
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 9c3a1256..8163581a 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -4175,7 +4175,10 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void {
// Paced: what came before the tick is in; wait out the rest of it,
// unless an input came: its frame is drawn at once (the paced loop
// never adds a tick to an input's latency), and the ticks after it
- // run from it.
+ // run from it. A stream's repeat too: the picture it moves is on
+ // the stream's straight line wherever it is sampled, so drawing it
+ // between two ticks costs no evenness, and holding it to the tick
+ // cost a held key 7 ms of latency.
if (paced and g.input_seen) tick = c.SDL_GetTicksNS();
// The tick is waited out polling, not asleep, as a game's frame
// limiter spins: asleep between 60 Hz frames, the CPU's clock fell
@@ -5044,13 +5047,30 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a
/// draw in the gap. A move past what the two frames share (a page and more,
/// G) has no rows between them to show: the picture comes from the last
/// frame's body as if it lay just past the new one, one body away, so a
-/// jump of any length reads as one body's slide. A move during a glide
-/// adds to it, its speed carried: it retargets, it never queues.
+/// jump of any length reads as one body's slide.
+///
+/// The picture's motion is one system with its own state, integrated in
+/// one place every frame (`advance`), and every scroll, whatever made it,
+/// is an input into it (`kick`): it moves the target by the rows the core
+/// moved and leaves the motion as it is. What the system was doing decides
+/// how it is absorbed: inputs coming as a stream (each like the last, at a
+/// steady spacing: a held key, a spun wheel) feed it, and it moves at the
+/// stream's own rate, a lag of an input or so behind, never easing toward
+/// each target (that was a bounce a line); an input into a resting view,
+/// or against the way it moves, starts the spring from the speed it has
+/// (an ease, a turn through zero); a jump mid-glide adds its rows with the
+/// speed kept. The spring's settle only comes once the inputs stop.
const Glide = struct {
- spring: pardes.animation.Spring = .{},
/// The picture's offset from the core's rows now (rows; negative, the
- /// picture still shows what is above).
+ /// picture still shows what is above), its speed (offset's rows a
+ /// second), and when it was last integrated.
offset: f32 = 0,
+ speed: f32 = 0,
+ at_ns: u64 = 0,
+ /// What has been feeding it: the rows of the last inputs (down
+ /// positive), their spacing, the last's time, and how many came as one
+ /// stream.
+ feed: struct { rows: f32 = 0, gap_ns: u64 = 0, last_ns: u64 = 0, count: u16 = 0 } = .{},
now_ns: u64 = 0,
pane: usize = 0,
rect: pardes.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 },
@@ -5078,21 +5098,22 @@ const Glide = struct {
/// Each pane's view as the last loop step saw it (stepGlide): its
/// serial, first line and file, so a view that moved since is found
/// whatever moved it.
- /// When the picture was last sampled (sample).
- shown_ns: u64 = 0,
seen: [pardes.MAX_PANES]struct { serial: u32 = 0, line: i64 = 0, file: u64 = 0, revision: u32 = 0 } = @splat(.{}),
/// How far the picture may lag, in bodies: the rows kept past each
/// edge, so a jump made while one glides carries on from where it is.
const reach = 2;
const bodies_per_s: f32 = 15;
+ /// Inputs this close, each the way the last went, are one stream.
+ const stream_ns = 150 * std.time.ns_per_ms;
fn moving(glide: *const Glide) bool {
- return !glide.spring.settled or glide.pending;
+ return glide.offset != 0 or glide.speed != 0 or glide.pending;
}
fn stop(glide: *Glide) void {
- glide.spring = .{};
+ glide.speed = 0;
+ glide.feed = .{};
glide.offset = 0;
glide.pending = false;
glide.nabove = 0;
@@ -5119,32 +5140,70 @@ const Glide = struct {
return glide.rect.w -| config.GUTTER;
}
- /// The core's rows moved by `rows` (down positive) since the last
- /// frame: the picture now lags by as many, carrying its speed.
+ /// An input: the core's rows moved by `rows` (down positive) since the
+ /// last frame. The picture now lags by as many more, its speed as it was.
fn kick(glide: *Glide, rows: f32, now_ns: u64, motion: pardes.animation.Motion) void {
- // Follows input: the flavour's pace, half again, never overshooting.
- var pace = motion;
- pace.omega = motion.omega * 1.5;
- glide.spring.carry(-rows, now_ns, pace);
+ glide.advance(now_ns, motion);
// Never more than `reach` bodies behind: a flick past that jumps.
const limit: f32 = @floatFromInt(@as(u32, glide.body_h) * reach);
- const now = glide.spring.value(now_ns);
- if (@abs(now) > limit) glide.spring.carry(std.math.clamp(now, -limit, limit) - now, now_ns, pace);
- glide.offset = glide.spring.value(now_ns);
- glide.shown_ns = now_ns;
+ glide.offset = std.math.clamp(glide.offset - rows, -limit, limit);
+ const f = &glide.feed;
+ const like = f.count > 0 and (rows > 0) == (f.rows > 0) and now_ns -| f.last_ns < stream_ns;
+ if (like) {
+ const gap = now_ns -| f.last_ns;
+ f.gap_ns = if (f.count == 1) gap else (f.gap_ns + gap) / 2;
+ f.rows = (f.rows + rows) / 2;
+ f.count +|= 1;
+ } else f.* = .{ .rows = rows, .count = 1 };
+ f.last_ns = now_ns;
}
- /// The picture at `now_ns`: the spring's, but never more than
- /// `bodies_per_s` of the body a second from the last frame's (a body in
- /// four frames at 60 Hz), so text that moves stays text one can read;
- /// the spring slows below that before it lands, and the picture is on
- /// it again by then.
- fn sample(glide: *Glide, now_ns: u64) void {
- const want = glide.spring.value(now_ns);
- const dt: f32 = @floatCast(@as(f64, @floatFromInt(@min(now_ns -| glide.shown_ns, 2 * pardes.animation.frame_ns))) / std.time.ns_per_s);
- glide.shown_ns = now_ns;
- const cap = @as(f32, @floatFromInt(glide.body_h)) * bodies_per_s * dt;
- glide.offset = std.math.clamp(want, glide.offset - cap, glide.offset + cap);
+ /// The system moved on to `now_ns`. Fed by a stream, at the stream's
+ /// rate (rows an input over their spacing), held a lag `lead` behind
+ /// the text so a late input never finds it stopped and the stream's end
+ /// never runs it past its mark; else on the flavour's spring at 1.5x
+ /// its pace, critically damped, from the speed it has. Never more than
+ /// `bodies_per_s` of the body a second, so moving text stays readable.
+ fn advance(glide: *Glide, now_ns: u64, motion: pardes.animation.Motion) void {
+ const dt: f32 = @floatCast(@as(f64, @floatFromInt(@min(now_ns -| glide.at_ns, 2 * pardes.animation.frame_ns))) / std.time.ns_per_s);
+ glide.at_ns = now_ns;
+ if (glide.offset == 0 and glide.speed == 0) return;
+ const omega = motion.omega * 1.5;
+ const cap = @as(f32, @floatFromInt(glide.body_h)) * bodies_per_s;
+ const toward: f32 = if (glide.offset < 0) 1 else -1;
+ const f = glide.feed;
+ if (f.count >= 2 and f.gap_ns > 0 and now_ns -| f.last_ns < f.gap_ns * 3 / 2) {
+ const gap: f32 = @floatCast(@as(f64, @floatFromInt(f.gap_ns)) / std.time.ns_per_s);
+ const rate = @abs(f.rows) / gap;
+ const lead = @max(@abs(f.rows), rate / omega);
+ // ponytail: a gentle pull to the lead (a tenth an input), so the
+ // per-frame step varies by a tenth; a stream's own jitter beyond
+ // that shows. An estimate of the input clock if it ever matters.
+ const v = std.math.clamp(rate + 0.1 / gap * (@abs(glide.offset) - lead), 0, cap);
+ const step = @min(v * dt, @abs(glide.offset));
+ glide.offset += toward * step;
+ glide.speed = toward * v;
+ return;
+ }
+ const x = glide.offset;
+ // Coming to rest from a stream: never faster than the spring's own
+ // pace from here (omega a row of lag a second), so it eases in and
+ // does not pass its mark.
+ if (glide.speed * x < 0 and @abs(glide.speed) > omega * @abs(x)) glide.speed = std.math.sign(glide.speed) * omega * @abs(x);
+ const lead_v = glide.speed + omega * x;
+ const decay = @exp(-omega * dt);
+ var next = (x + lead_v * dt) * decay;
+ var speed = (glide.speed - omega * dt * lead_v) * decay;
+ if (@abs(next - x) > cap * dt) {
+ next = x + std.math.sign(next - x) * cap * dt;
+ speed = std.math.sign(next - x) * cap;
+ }
+ glide.offset = next;
+ glide.speed = speed;
+ if (@abs(next) < 5e-3 and @abs(speed) < 5e-2) {
+ glide.offset = 0;
+ glide.speed = 0;
+ }
}
/// Finds how far the pane's body rows moved, `dir` rows at a time, by
@@ -5248,10 +5307,10 @@ fn stepGlide(g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator) void {
defer core.shell_continuous = glide.moving() or g.sel_glow.fading or g.hover_glow.fading;
glide.now_ns = core.now_ns;
glide.motion = pardes.animation.Motion.of(core.settings.motion);
- if (!glide.spring.settled) {
- _ = glide.spring.step(core.now_ns);
+ if (glide.offset != 0 or glide.speed != 0) {
+ glide.advance(core.now_ns, glide.motion);
core.needs_frame = true;
- if (glide.spring.settled) {
+ if (glide.offset == 0) {
glide.offset = 0;
glide.nabove = 0;
glide.nbelow = 0;
@@ -5465,7 +5524,7 @@ test "a notch scrolls the core at once and the picture glides after it, a wrappe
now += 7 * ms;
core.advance(now);
stepGlide(&g, core, gpa);
- try std.testing.expect(g.glide.spring.value(now) <= 1e-4);
+ try std.testing.expect(g.glide.offset <= 1e-4);
if (frames > 200) return error.NeverSettled;
}
try std.testing.expect(frames > 5);
@@ -5499,7 +5558,7 @@ const GlideRun = struct {
_ = arena.reset(.retain_capacity);
const s = try core.render(arena.allocator());
if (g.glide.pending) g.glide.measure(gpa, s);
- if (g.glide.moving()) g.glide.sample(g.glide.now_ns);
+ if (g.glide.moving()) g.glide.advance(g.glide.now_ns, g.glide.motion);
seq.offsets[frame] = g.glide.offset;
seq.lines[frame] = @intCast(core.panes[core.active].?.file.?.scroll);
}
@@ -5594,6 +5653,50 @@ test "every scroll glides on one law: a half page, a page, the far jumps, a run
for (off.offsets[0..off.frames]) |offset| try std.testing.expectEqual(@as(f32, 0), offset);
}
+test "a held key moves the view at the stream's own rate, never a bounce a line, and settles once it stops" {
+ const gpa = std.testing.allocator;
+ const core = try pardes.Pardes.init(gpa, .{ .cols = 100, .rows = 40 });
+ defer core.deinit();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(gpa);
+ for (0..3000) |i| try text.print(gpa, "{d:0>5} line {d} of the long file\n", .{ i, i * 7 % 13 });
+ _ = try core.setTestFile(text.items);
+ core.settings.motion = .smooth;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ core.advance(std.time.ns_per_s);
+ _ = try core.render(arena.allocator());
+ var g: Gui = undefined;
+ g.glide = .{};
+ defer g.glide.deinit(gpa);
+ g.scroll_notch = 0;
+ g.scroll_pane = null;
+ g.scroll_delta = 0;
+ g.scroll_lag = 0;
+ g.scroll_edge_len = 0;
+ g.sel_glow = .{};
+ g.hover_glow = .{};
+ // j held from the top: a repeat every second frame, the view following
+ // a line a repeat once the caret is at its last rows.
+ var script: [48]GlideRun.Step = undefined;
+ for (&script, 0..) |*step, i| step.* = .{ .frame = 20 + 2 * i, .key = .{ .cp = 'j', .text = "j" } };
+ var seq: GlideRun = .{};
+ try seq.play(&g, core, gpa, &arena, &script, 200);
+ // Inside the stream the picture moves every frame by about the same
+ // step: an input's rows over its spacing, not a spring's ease to each.
+ var least: f32 = std.math.inf(f32);
+ var most: f32 = 0;
+ for (90..110) |k| {
+ const step = (@as(f32, @floatFromInt(seq.lines[k])) + seq.offsets[k]) - (@as(f32, @floatFromInt(seq.lines[k - 1])) + seq.offsets[k - 1]);
+ least = @min(least, step);
+ most = @max(most, step);
+ }
+ try std.testing.expect(seq.lines[89] > 0 and least > 0.4 and most < 0.6);
+ // It comes to rest once the keys stop, never past its mark.
+ for (110..200) |k| try std.testing.expect(seq.offsets[k] <= 0);
+ try std.testing.expectEqual(@as(f32, 0), seq.offsets[199]);
+}
+
fn resetScroll(g: *Gui) void {
g.scroll_pane = null;
g.scroll_delta = 0;
@@ -6291,7 +6394,7 @@ fn renderFrame(
if (surface.chrome.fx & 1 == 0 or surface.panelTracks().len != 0) g.sel_glow = .{ .now_ns = g.sel_glow.now_ns };
g.hover_glow.now_ns = g.sel_glow.now_ns;
if (surface.chrome.fx & 2 == 0 or surface.panelTracks().len != 0) g.hover_glow = .{ .now_ns = g.sel_glow.now_ns };
- if (g.glide.moving()) g.glide.sample(g.glide.now_ns);
+ if (g.glide.moving()) g.glide.advance(g.glide.now_ns, g.glide.motion);
cell_decor = CellDecor.of(chrome);
plaque_through_len = 0;
// G13: a body's dots follow its text at a quarter of its speed: its