//! JumpTrail: a jump of the cursor, three cells or more between two frames, //! leaves a trail of three to five cells behind it along its path, most //! beside the cursor, all fading out over 120 ms. A frontend's own: it sees //! the cursor of the Surface it shows, frame after frame, and times the fade //! on its own clock; the tty tints those cells' ground toward the text, the //! window draws over them. Nothing of it reaches the core, which keeps the //! setting alone (config.Runtime.jump_trail). const std = @import("std"); pub const life_ns: u64 = 120 * std.time.ns_per_ms; /// A cell of the trail, and how far toward the text it is drawn (0 to 1). pub const Cell = struct { x: i32, y: i32, strength: f32 }; last: ?[2]u16 = null, from: [2]u16 = .{ 0, 0 }, to: [2]u16 = .{ 0, 0 }, start_ns: u64 = 0, /// Fading: a frontend draws frames until it is not. active: bool = false, const JumpTrail = @This(); /// A frame shows the cursor at `cursor` (null: none) at `now_ns`: the /// trail's cells in it, the one beside the cursor first, or none. pub fn see(trail: *JumpTrail, cursor: ?[2]u16, on: bool, now_ns: u64, out: *[5]Cell) []const Cell { const here = cursor orelse { trail.* = .{}; return out[0..0]; }; if (trail.last) |was| { const dx = @as(i32, here[0]) - was[0]; const dy = @as(i32, here[1]) - was[1]; if (on and @max(@abs(dx), @abs(dy)) >= 3) trail.* = .{ .from = was, .to = here, .start_ns = now_ns, .active = true }; } trail.last = here; const age = now_ns -| trail.start_ns; if (!on or !trail.active or age >= life_ns or !std.meta.eql(trail.to, here)) { trail.active = false; return out[0..0]; } const t = @as(f32, @floatFromInt(age)) / @as(f32, @floatFromInt(life_ns)); const fade = (1 - t) * (1 - t); const dx: f32 = @floatFromInt(@as(i32, trail.from[0]) - trail.to[0]); const dy: f32 = @floatFromInt(@as(i32, trail.from[1]) - trail.to[1]); const span = @max(@abs(dx), @abs(dy)); const n: usize = @intFromFloat(std.math.clamp(@round(span / 4), 3, 5)); var len: usize = 0; var k: usize = 1; var last_cell: ?[2]i32 = null; var step: f32 = 1; while (k <= n and step <= span) : (step += 1) { const x: i32 = @intFromFloat(@round(@as(f32, @floatFromInt(trail.to[0])) + dx * step / span)); const y: i32 = @intFromFloat(@round(@as(f32, @floatFromInt(trail.to[1])) + dy * step / span)); if (last_cell) |seen| if (seen[0] == x and seen[1] == y) continue; last_cell = .{ x, y }; defer k += 1; const strength = 0.55 * fade * (1 - @as(f32, @floatFromInt(k - 1)) / @as(f32, @floatFromInt(n))); if (strength < 0.02) continue; out[len] = .{ .x = x, .y = y, .strength = strength }; len += 1; } return out[0..len]; } test "a jump leaves a few cells behind the cursor, fading in 120 ms; a step, or off, leaves none" { const ms = std.time.ns_per_ms; var trail: JumpTrail = .{}; var out: [5]Cell = undefined; try std.testing.expectEqual(@as(usize, 0), trail.see(.{ 2, 1 }, true, 1000 * ms, &out).len); // Ten cells right: a trail back toward where it was, on its row, behind // it, never its own cell, strongest beside it. const lit = trail.see(.{ 12, 1 }, true, 1016 * ms, &out); try std.testing.expect(trail.active and lit.len >= 3 and lit.len <= 5); for (lit, 0..) |cell, i| { try std.testing.expectEqual(@as(i32, 1), cell.y); try std.testing.expect(cell.x > 2 and cell.x < 12); if (i > 0) try std.testing.expect(cell.strength < lit[i - 1].strength and cell.x < lit[i - 1].x); } // Gone by 120 ms. try std.testing.expectEqual(@as(usize, 0), trail.see(.{ 12, 1 }, true, 1150 * ms, &out).len); try std.testing.expect(!trail.active); // A step of a cell leaves none; nor does a jump with it off. try std.testing.expectEqual(@as(usize, 0), trail.see(.{ 13, 1 }, true, 1200 * ms, &out).len); try std.testing.expectEqual(@as(usize, 0), trail.see(.{ 2, 1 }, false, 1216 * ms, &out).len); try std.testing.expect(!trail.active); }