diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-24 21:30:52 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-24 22:33:46 -0300 |
| commit | 0dc2dc053af20d6811bfbe09ab31b762862a53a6 (patch) | |
| tree | 220c02b7d3e6af91ac5eda4f84b9ed965de638b1 /src/animation.zig | |
| parent | 408412838ce7bf3156ba8aa6f95057a72ebdd284 (diff) | |
| download | pardes-0dc2dc053af20d6811bfbe09ab31b762862a53a6.tar.gz pardes-0dc2dc053af20d6811bfbe09ab31b762862a53a6.zip | |
anim 10: the caret is its own body: drawn from its own springs only, never carried by the text's glide (the ride-along deleted); its target is its cell as the view will settle, so a page leaves it still, a scroll that carries its line moves it there whole at the view's pace, a jump is one ease, a stream of steps lands each
Diffstat (limited to 'src/animation.zig')
| -rw-r--r-- | src/animation.zig | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/src/animation.zig b/src/animation.zig index 63a40cbf..ae6f9ca0 100644 --- a/src/animation.zig +++ b/src/animation.zig @@ -964,6 +964,8 @@ pub const CursorGlide = struct { target: Box = .{}, from_centre: [2]f32 = .{ 0, 0 }, placed: bool = false, + /// When the last step (a move of a cell or less) came. + step_ns: u64 = 0, /// How much faster than the flavour's own pace a cursor moves: it /// follows input (ยง8.1: settles in 90-150 ms on the default). @@ -972,6 +974,9 @@ pub const CursorGlide = struct { pub const arc_from: f32 = 8; /// How much stiffer a step's spring is than a jump's. pub const step_pace: f32 = 1.5; + /// Steps this close are a stream (a held key, typing): each lands, the + /// caret at the keys' own rate, as an eased step a key would bounce. + pub const stream_ns: u64 = 150 * std.time.ns_per_ms; fn cornersOf(box: Box) [4][2]f32 { return .{ .{ box.x, box.y }, .{ box.x + box.w, box.y }, .{ box.x + box.w, box.y + box.h }, .{ box.x, box.y + box.h } }; @@ -982,16 +987,27 @@ pub const CursorGlide = struct { } /// Heads for `box` at `now_ns`. - pub fn aim(glide: *CursorGlide, box: Box, now_ns: u64, motion: Motion, snap: bool) void { + /// How a new mark is gone to: eased (a step, or a jump's smear and + /// bow), landed at once, or moved whole at a scroll's pace (the text + /// moved under it, not the caret). + pub const How = enum { ease, land, rigid }; + + pub fn aim(glide: *CursorGlide, box: Box, now_ns: u64, motion: Motion, how: How) void { // The same mark: nothing to do, unless a snap lands a glide under way. - if (glide.placed and std.meta.eql(box, glide.target) and !(snap and glide.moving())) return; + if (glide.placed and std.meta.eql(box, glide.target) and !(how == .land and glide.moving())) return; const to = cornersOf(box); const from_c = if (glide.placed) glide.centreAt(now_ns) else centre(box); const to_c = centre(box); const dx = to_c[0] - from_c[0]; const dy = to_c[1] - from_c[1]; const jump = @abs(dx) > 1.01 or @abs(dy) > 1.01; - const land = !glide.placed or snap or motion.instant; + // A step is measured mark to mark, not from where an ease under way + // has got to (a held key's steps read as jumps from there). + const last = centre(glide.target); + const one_step = glide.placed and @abs(to_c[0] - last[0]) <= 1.01 and @abs(to_c[1] - last[1]) <= 1.01; + const streaming = one_step and glide.step_ns != 0 and now_ns -| glide.step_ns < stream_ns; + if (one_step) glide.step_ns = now_ns; + const land = !glide.placed or how == .land or motion.instant or streaming; glide.from_centre = from_c; glide.target = box; glide.placed = true; @@ -999,9 +1015,11 @@ pub const CursorGlide = struct { // Leading: the corner ahead of the move, on the side it goes. const ahead = (point[0] - to_c[0]) * dx + (point[1] - to_c[1]) * dy; var own = motion; - own.omega *= if (jump) pace * (if (ahead > 0) 1 else motion.follow) else pace * step_pace; - // A step neither winds up nor overshoots: it is a caret moving. - if (!jump) { + own.omega *= if (how == .rigid) 1.5 else if (jump) pace * (if (ahead > 0) 1 else motion.follow) else pace * step_pace; + // A step neither winds up nor overshoots: it is a caret moving; + // nor a whole move with the text (rigid: at the view's pace, + // Glide's 1.5x, its corners together). + if (!jump or how == .rigid) { own.zeta = 1; own.anticipate = 0; } @@ -1121,10 +1139,10 @@ test "a cursor eases a cell at a time, keeps up with typing, and glides a jump, const smooth_motion = Motion.of(.smooth); var glide: CursorGlide = .{}; const cell: Box = .{ .x = 3, .y = 4, .w = 1, .h = 1 }; - glide.aim(cell, 0, smooth_motion, false); + glide.aim(cell, 0, smooth_motion, .ease); try std.testing.expect(!glide.moving()); // One cell over: eases there whole, no wider than a cell, in ~60 ms. - glide.aim(.{ .x = 4, .y = 4, .w = 1, .h = 1 }, ms, smooth_motion, false); + glide.aim(.{ .x = 4, .y = 4, .w = 1, .h = 1 }, ms, smooth_motion, .ease); try std.testing.expect(glide.moving()); var step_t: u64 = ms; while (glide.step(step_t)) : (step_t += ms) { @@ -1132,21 +1150,21 @@ test "a cursor eases a cell at a time, keeps up with typing, and glides a jump, try std.testing.expect(q[1][0] - q[0][0] <= 1.0 + 1e-3 and q[0][0] >= 3 - 1e-3 and q[0][0] <= 4 + 1e-3); } try std.testing.expect(step_t - ms >= 30 * ms and step_t - ms <= 90 * ms); - // At a keyboard's repeat it keeps up: about half a cell behind. + // At a keyboard's repeat the steps are a stream: each lands. var typed: f32 = 4; var key_t: u64 = step_t; for (0..20) |_| { typed += 1; - glide.aim(.{ .x = typed, .y = 4, .w = 1, .h = 1 }, key_t, smooth_motion, false); + glide.aim(.{ .x = typed, .y = 4, .w = 1, .h = 1 }, key_t, smooth_motion, .ease); key_t += 33 * ms; try std.testing.expect(typed - glide.sample(key_t, smooth_motion)[0][0] <= 0.6); } while (glide.step(key_t)) key_t += ms; // A jump of 30 cells: glides, the leading edge ahead of the trailing one. - glide.aim(cell, key_t, smooth_motion, true); + glide.aim(cell, key_t, smooth_motion, .land); const j0 = key_t; const far: Box = .{ .x = 34, .y = 4, .w = 1, .h = 1 }; - glide.aim(far, j0 + 2 * ms, smooth_motion, false); + glide.aim(far, j0 + 2 * ms, smooth_motion, .ease); try std.testing.expect(glide.moving()); const mid = glide.sample(j0 + 40 * ms, smooth_motion); const width = mid[1][0] - mid[0][0]; |
