summaryrefslogtreecommitdiff
path: root/src/host/overlay.zig
blob: 5219a83a492705d1e2af1c4bb664141234e51f55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Debug overlays drawn by the host over any sketch: the live state's fields
//! and bytes, from the reflected layout, and the audio output waveform.
const std = @import("std");
const abi = @import("abi");
const memory = @import("memory.zig");
const fields = @import("fields.zig");

const List = std.ArrayList(abi.Command);
const shown_bytes = 0x100;

/// Each field hashes its path into a dark shade; bytes it owns wear the same
/// shade in the hex view below, and padding stays gray.
fn shade(path: []const u8) abi.Color {
    const hash = std.hash.Wyhash.hash(0, path);
    return .{ .r = @as(u8, @truncate(hash)) / 2, .g = @as(u8, @truncate(hash >> 8)) / 2, .b = @as(u8, @truncate(hash >> 16)) / 2 };
}

pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, state: []const u8, width: f32) !void {
    // Arrays show as wholes: their element leaves (`list[].x`) are left out.
    var top_level: usize = 0;
    for (layout.fields) |leaf| top_level += @intFromBool(leaf.dims.len == 0);
    const rows = (top_level + 1) / 2;
    const legend_top: f32 = 24;
    const hex_top = legend_top + @as(f32, @floatFromInt(rows)) * 14 + 16;
    const shown = @min(state.len, shown_bytes);
    const height = hex_top + @as(f32, @floatFromInt((shown + 15) / 16)) * 14 + 10;
    try out.append(arena, .{ .rect = .{ .dst = .{ .x = 12, .y = 4, .w = width - 24, .h = height }, .color = .{ .r = 230, .g = 254, .b = 242 } } });
    try out.append(arena, .{ .text = .{ .x = 30, .y = 8, .scale = 1, .color = .{ .r = 24, .g = 24, .b = 24 }, .bytes = "MEMORY  /  overlay memory to hide" } });

    // Legend: two fixed-width columns filled down then across.
    const column = (width - 60) / 2;
    var row: usize = 0;
    for (layout.fields) |leaf| {
        if (leaf.dims.len != 0) continue;
        const i = row;
        row += 1;
        var value: std.Io.Writer.Allocating = .init(arena);
        try fields.format(leaf, state, &value.writer);
        const name = if (leaf.path.len > 26) try std.fmt.allocPrint(arena, "{s}...", .{leaf.path[0..23]}) else leaf.path;
        const line = try std.fmt.allocPrint(arena, "{x:0>4} {s: <26} {s}", .{ leaf.offset, name, value.written() });
        try out.append(arena, .{ .text = .{
            .x = 30 + @as(f32, @floatFromInt(i / rows)) * column,
            .y = legend_top + @as(f32, @floatFromInt(i % rows)) * 14,
            .scale = 1,
            .color = shade(leaf.path),
            .bytes = line[0..@min(line.len, @as(usize, @intFromFloat(column / 8)) - 1)],
        } });
    }

    var colors: [shown_bytes]abi.Color = @splat(.{ .r = 0x9f, .g = 0x9f, .b = 0x9f });
    for (layout.fields) |leaf| {
        if (leaf.offset >= shown or leaf.dims.len != 0) continue;
        @memset(colors[leaf.offset..@min(shown, leaf.offset + leaf.size)], shade(leaf.path));
    }
    for (state[0..shown], 0..) |byte, i| {
        try out.append(arena, .{ .text = .{
            .x = 30 + @as(f32, @floatFromInt(i % 16)) * 24,
            .y = hex_top + @as(f32, @floatFromInt(i / 16)) * 14,
            .scale = 1,
            .color = colors[i],
            .bytes = try std.fmt.allocPrint(arena, "{X:0>2}", .{byte}),
        } });
    }
}

pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, canvas: [2]u32) !void {
    const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 };
    const x = @as(f32, @floatFromInt(canvas[0])) - 526;
    const top = @as(f32, @floatFromInt(canvas[1])) - 130;
    const mid = top + 72;
    try out.append(arena, .{ .rect = .{ .dst = .{ .x = x, .y = top, .w = 514, .h = 118 }, .color = .{ .r = 24, .g = 24, .b = 24 } } });
    try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1, .y = mid, .w = 512, .h = 1 }, .color = .{ .r = 60, .g = 85, .b = 65 } } });
    const ms = @as(f32, @floatFromInt(samples.len * 1000)) / @as(f32, @floatFromInt(@max(rate, 1)));
    try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / overlay sound to hide", .{ms}) } });
    for (samples, 0..) |sample, i| {
        const a = std.math.clamp(if (std.math.isFinite(sample)) sample else 0, -1, 1);
        const previous = samples[i -| 1];
        const b = std.math.clamp(if (std.math.isFinite(previous)) previous else 0, -1, 1);
        const y1 = mid - a * 36;
        const y2 = mid - b * 36;
        try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1 + @as(f32, @floatFromInt(i)), .y = @min(y1, y2), .w = 1, .h = @max(1, @abs(y2 - y1)) }, .color = green } });
    }
}