summaryrefslogtreecommitdiff
path: root/src/animation.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/animation.zig')
-rw-r--r--src/animation.zig53
1 files changed, 41 insertions, 12 deletions
diff --git a/src/animation.zig b/src/animation.zig
index a4887148..63a40cbf 100644
--- a/src/animation.zig
+++ b/src/animation.zig
@@ -952,8 +952,12 @@ test "a flavour's stretched pane lands exactly on its box, and a click finds the
/// The focused cursor's quad (docs/render-pipeline.md §5.4), in grid cells:
/// four corners, each on its own springs, so a jump smears along its path
/// (the leading corners stiffer than the trailing ones, Motion.follow) and
-/// collapses at rest; a long jump bows (Motion.arc). A move of a cell or
-/// less, or any the caller calls a snap, lands at once.
+/// collapses at rest; a long jump bows (Motion.arc). A step of a cell or
+/// less (a key held, typing) eases there whole, its corners together, on a
+/// spring `step_pace` times stiffer still: in ~60 ms on the default, so at
+/// a keyboard's repeat it trails by half a cell and never smears a caret.
+/// Any move the caller calls a snap, and every move under Motion off,
+/// lands at once.
pub const CursorGlide = struct {
/// Top left, top right, bottom right, bottom left; x then y.
corners: [4][2]Spring = @splat(@splat(.{})),
@@ -966,6 +970,8 @@ pub const CursorGlide = struct {
pub const pace: f32 = 2.4;
/// A jump longer than this, in cells, bows.
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;
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 } };
@@ -985,7 +991,7 @@ pub const CursorGlide = struct {
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 or !jump;
+ const land = !glide.placed or snap or motion.instant;
glide.from_centre = from_c;
glide.target = box;
glide.placed = true;
@@ -993,7 +999,12 @@ 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 *= pace * (if (ahead > 0) 1 else motion.follow);
+ 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.zeta = 1;
+ own.anticipate = 0;
+ }
if (land) {
for (corner, point) |*axis, value| axis.* = .{ .omega = own.omega, .zeta = own.zeta, .target = value, .from = value };
continue;
@@ -1105,32 +1116,50 @@ pub const Blink = struct {
}
};
-test "a cursor snaps a cell at a time and glides a jump, smearing, bowing and landing exactly" {
+test "a cursor eases a cell at a time, keeps up with typing, and glides a jump, smearing, bowing and landing exactly" {
const ms = std.time.ns_per_ms;
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);
try std.testing.expect(!glide.moving());
- // One cell over: lands at once.
+ // 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);
- try std.testing.expect(!glide.moving());
+ try std.testing.expect(glide.moving());
+ var step_t: u64 = ms;
+ while (glide.step(step_t)) : (step_t += ms) {
+ const q = glide.sample(step_t, smooth_motion);
+ 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.
+ 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);
+ 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);
+ const j0 = key_t;
const far: Box = .{ .x = 34, .y = 4, .w = 1, .h = 1 };
- glide.aim(far, 2 * ms, smooth_motion, false);
+ glide.aim(far, j0 + 2 * ms, smooth_motion, false);
try std.testing.expect(glide.moving());
- const mid = glide.sample(40 * ms, smooth_motion);
+ const mid = glide.sample(j0 + 40 * ms, smooth_motion);
const width = mid[1][0] - mid[0][0];
try std.testing.expect(width > 1.5);
// Bowed off the straight line on a long jump.
try std.testing.expect(@abs(mid[0][1] - 4) > 0.05);
- var t: u64 = 2 * ms;
+ var t: u64 = j0 + 2 * ms;
while (glide.step(t)) t += ms;
// Arrives within §8.1's 90-150 ms on the default, within a twentieth of
// a cell (under a pixel); settles to rest just after, landing exactly.
- const arrived = glide.sample(152 * ms, smooth_motion);
+ const arrived = glide.sample(j0 + 152 * ms, smooth_motion);
try std.testing.expect(@abs(arrived[3][0] - 34) < 0.05 and @abs(arrived[1][0] - 35) < 0.05);
- try std.testing.expect(t <= 220 * ms);
+ try std.testing.expect(t - j0 <= 220 * ms);
const rest = glide.sample(t, smooth_motion);
try std.testing.expectEqual([2]f32{ 34, 4 }, rest[0]);
try std.testing.expectEqual([2]f32{ 35, 5 }, rest[2]);