diff options
Diffstat (limited to 'anim/src')
| -rw-r--r-- | anim/src/rows.zig | 59 | ||||
| -rw-r--r-- | anim/src/ui.zig | 25 |
2 files changed, 84 insertions, 0 deletions
diff --git a/anim/src/rows.zig b/anim/src/rows.zig index 4666ffd..0e4f58c 100644 --- a/anim/src/rows.zig +++ b/anim/src/rows.zig @@ -40,6 +40,65 @@ pub fn baseline(y: f32) f32 { return theme.baseline(y, row_h, size); } +pub const NumberFormat = enum { hex, unsigned, signed, float }; + +pub fn numberText(buf: []u8, octets: []const u8, format: NumberFormat) []const u8 { + var value: u64 = 0; + std.debug.assert(octets.len == 1 or octets.len == 2 or octets.len == 4 or octets.len == 8); + for (octets, 0..) |byte, i| value |= @as(u64, byte) << @intCast(i * 8); + return switch (format) { + .hex => switch (octets.len) { + 1 => std.fmt.bufPrint(buf, "{x:0>2}", .{value}), + 2 => std.fmt.bufPrint(buf, "{x:0>4}", .{value}), + 4 => std.fmt.bufPrint(buf, "{x:0>8}", .{value}), + else => std.fmt.bufPrint(buf, "{x:0>16}", .{value}), + }, + .unsigned => std.fmt.bufPrint(buf, "{d}", .{value}), + .signed => blk: { + const shift: u6 = @intCast(64 - octets.len * 8); + const signed: i64 = @bitCast(value << shift); + break :blk std.fmt.bufPrint(buf, "{d}", .{signed >> shift}); + }, + .float => if (octets.len == 4) std.fmt.bufPrint(buf, "{e}", .{@as(f32, @bitCast(@as(u32, @truncate(value))))}) else if (octets.len == 8) std.fmt.bufPrint(buf, "{e}", .{@as(f64, @bitCast(value))}) else return "—", + } catch "?"; +} + +/// Sixteen octets, grouped as little-endian values; pointer hits use the same geometry as hex rows. +pub fn wordsAt(u: ui.Ui, y: f32, addr: u64, values: *const [16]u8, known: *const [16]bool, unit: u8, format: NumberFormat, moment: Recording.Moment) f32 { + std.debug.assert(unit == 1 or unit == 2 or unit == 4 or unit == 8); + _ = u.address(theme.metric.bytes.address, baseline(y), addr, .{ .ink = ui.rgb(ink(regionOf(moment, addr))) }); + var x = theme.metric.bytes.first + addressRoom(addr); + var at_byte: usize = 0; + while (at_byte < 16) : (at_byte += unit) { + const octets = values[at_byte..][0..unit]; + const captured = std.mem.indexOfScalar(bool, known[at_byte..][0..unit], false) == null; + var buf: [64]u8 = undefined; + const text = if (captured) numberText(&buf, octets, format) else "?"; + const digits: usize = switch (format) { + .hex => unit * 2, + .unsigned => if (unit == 8) 20 else if (unit == 4) 10 else if (unit == 2) 5 else 3, + .signed => if (unit == 8) 20 else if (unit == 4) 11 else if (unit == 2) 6 else 4, + .float => if (unit == 8) 25 else 16, + }; + const width = @as(f32, @floatFromInt(digits)) * adv; + const value = if (unit == 8) std.mem.readInt(u64, octets[0..8], .little) else 0; + const pointer = captured and unit == 8 and format != .float and regionOf(moment, value) != .none; + u.text(.mono, size, x, baseline(y), text, if (pointer) ink(regionOf(moment, value)) else t.primary); + if (pointer) u.hitAddress(value, .{ .x = x, .y = y, .w = width, .h = row_h }); + x += width + adv * 2; + } + return x; +} + +test "memory scalar formats preserve byte order, sign and width" { + var buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("1234", numberText(&buf, &.{ 0x34, 0x12 }, .hex)); + try std.testing.expectEqualStrings("-1", numberText(&buf, &.{ 255, 255 }, .signed)); + try std.testing.expectEqualStrings("18446744073709551615", numberText(&buf, &@as([8]u8, @splat(255)), .unsigned)); + try std.testing.expectEqualStrings("-9223372036854775808", numberText(&buf, &.{ 0, 0, 0, 0, 0, 0, 0, 128 }, .signed)); + try std.testing.expectEqualStrings("1e0", numberText(&buf, &.{ 0, 0, 128, 63 }, .float)); +} + /// The marks on a chain's hops. pub const Marks = struct { /// The first hop: a register (or a word) written: armed, struck, held. diff --git a/anim/src/ui.zig b/anim/src/ui.zig index 282a24f..2c78c04 100644 --- a/anim/src/ui.zig +++ b/anim/src/ui.zig @@ -57,6 +57,17 @@ pub const Collected = struct { }; pub var collected: Collected = .{}; +/// A selected occurrence leads, then its matches arrive in reading order. +pub fn matchProgress(seconds: f32, rank: usize, delay: f32, duration: f32) f32 { + return theme.ease(.standard, std.math.clamp((seconds - @as(f32, @floatFromInt(rank)) * delay) / @max(0.001, duration), 0, 1)); +} + +test "matching highlights reveal the selection before its peers" { + try std.testing.expect(matchProgress(0.05, 0, 0.08, 0.16) > 0); + try std.testing.expectEqual(@as(f32, 0), matchProgress(0.05, 1, 0.08, 0.16)); + try std.testing.expectEqual(@as(f32, 1), matchProgress(1, 3, 0.08, 0.16)); +} + /// A mark on the row whose middle is `y` (in `u`), for the strips; `shows` of it. pub fn collectMark(u: Ui, x: f32, y: f32, kind: Collected.Kind, shows: f32) void { if (!strokes or shows <= 0 or collected.n_marks == collected.marks.len) return; @@ -635,6 +646,20 @@ pub const Ui = struct { if (bound) |b| u.f.clip(b) else u.f.clip(null); } + pub fn matchHighlight(u: Ui, r: Rect, seconds: f32, rank: usize, options: struct { + color: Color = .hex(0x65c9d8), + selected_color: Color = .hex(0xe6bd70), + delay: f32 = 0.075, + duration: f32 = 0.16, + opacity: f32 = 0.23, + }) void { + const progress = matchProgress(seconds, rank, options.delay, options.duration); + const c = if (rank == 0) options.selected_color else options.color; + const box: Rect = .{ .x = r.x, .y = r.y, .w = r.w * progress, .h = r.h }; + u.faded(options.opacity * progress).fill(box, c); + u.faded(progress * 0.6).line(.{ r.x, r.y + r.h - 1 }, .{ r.x + box.w, r.y + r.h - 1 }, c, 1); + } + pub fn hitAddress(u: Ui, addr: u64, r: Rect) void { const hook = address_hit orelse return; if (strokes or mapping or u.at.pose.opacity < 0.1) return; |
