summaryrefslogtreecommitdiff
path: root/anim/src/map.zig
blob: e22db9f09ffd1c62cb149bda2dfe412bf4681b54 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//! The `map` and the `minimap` (design/README.md, "The map and the
//! minimap"): the working frame unfolded, every panel at its full length on
//! a stage of 3840 by 2160 at the context's type size, and the same at a
//! twentieth in the caption band's right end. Its panels are the working
//! frame's components bound to a stage of their own (`split`), drawn with
//! `ui.mapping` set: then they publish their rows (`row`), and in the
//! working frame their windows (`window`); the map outlines each working
//! window round the rows of its own that it shows.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
const ui = @import("ui.zig");
const Recording = @import("Recording.zig");
const Stage = @import("stage.zig").Stage;
const theme = ui.theme;
const Rect = theme.Rect;
const Split = theme.layout.Split;

pub const Panel = enum { band, view, source, disasm, registers, stack, backtrace };

/// `dive`'s frames: the windows bright by `bright`, the rest veiled by
/// `move`, when the panels set off (each `apart` after the one before, for
/// `travel`); the veil lifts from `lift` as the map lands at `land`; then
/// `still`. `surface` takes `surface` back.
pub const diving = struct {
    pub const bright = 6;
    pub const move = 24;
    pub const apart = 6;
    /// A panel's way, and the map's.
    pub const travel = 60;
    pub const fall = 72;
    pub const lift = 84;
    /// The last of seven panels in place: 24 + 6 * 6 + 60.
    pub const land = 120;
    pub const still = 30;
    pub const surface = 84;
};

/// Where each working window is on the map this frame, in canvas pixels.
var window_px: [7]?[4]f32 = @splat(null);

/// The working frame's windows this frame: the first and last key of the
/// rows each shows (a line number, an address).
var windows: [7]?[2]u64 = @splat(null);
/// The map's rows this frame, in canvas pixels: each one's key and box.
var rows: [7][160]struct { key: u64, box: [4]f32 } = undefined;
var counts: [7]usize = @splat(0);

/// A working panel shows the rows of keys `first` to `last`.
pub fn window(panel: Panel, first: u64, last: u64) void {
    if (!ui.mapping) windows[@intFromEnum(panel)] = .{ first, last };
}

/// A map panel's row of key `key` is at `r`, in `u`'s units.
pub fn row(panel: Panel, key: u64, u: ui.Ui, r: Rect) void {
    if (!ui.mapping) return;
    const i = @intFromEnum(panel);
    if (counts[i] == rows[i].len) return;
    const a = u.at.xf.apply(.{ r.x, r.y });
    const b = u.at.xf.apply(.{ r.x + r.w, r.y + r.h });
    rows[i][counts[i]] = .{ .key = key, .box = .{ a[0], a[1], b[0], b[1] } };
    counts[i] += 1;
}

/// The map's boxes for a recording: the drawing (with a lens) and the
/// source in the first column, the disassembly in the second, the lens's
/// view (its region whole), the registers and the backtrace in the third,
/// the stack in the fourth. Each as long as its content where that is
/// shorter than the column.
// shortcut: a panel longer than its column is a window of what fits; continuing it in the next column's free space (`continued`), and the key in a column's foot, are not built.
pub fn split(rec: *const Recording, lens: bool) Split {
    const st = theme.map.stage;
    const c = theme.map.columns;
    const w = theme.map.column;
    const gap = theme.canvas.row_gap;
    const panelH = theme.layout.panelH;
    const foot = st.y + st.h;
    const band: Rect = .{ .x = c[0], .y = st.y, .w = w, .h = if (lens) theme.layout.split.subject.band.h else 0 };
    const source_y = if (lens) band.y + band.h + gap else st.y;
    const source_rows: f32 = @floatFromInt(@min(rec.line_count, theme.layout.rowsIn(foot - source_y)));
    const view: Rect = .{ .x = c[2], .y = st.y, .w = w, .h = if (lens) panelH(theme.map.memory_rows) else 0 };
    const registers: Rect = .{ .x = c[2], .y = if (lens) view.y + view.h + gap else st.y, .w = w, .h = panelH(17) };
    const bt_y = registers.y + registers.h + gap;
    return .{
        .band = band,
        .view = view,
        .source = .{ .x = c[0], .y = source_y, .w = w, .h = panelH(source_rows) },
        .disasm = .{ .x = c[1], .y = st.y, .w = w, .h = panelH(theme.map.disasm_rows) },
        .registers = registers,
        .stack = .{ .x = c[3], .y = st.y, .w = w, .h = st.h },
        .backtrace = .{ .x = c[2], .y = bt_y, .w = w, .h = @min(panelH(12), foot - bt_y) },
        .dump_rows = theme.map.memory_rows,
    };
}

/// The map: its stage, and the components drawn on it, each a working
/// frame's component bound to that stage.
pub const Map = struct {
    stage: *Stage,
    parts: [8]Part = undefined,
    n: usize = 0,
    /// The panel the narration is in: its window bright on the minimap.
    focus: ?Panel = null,
    /// The working frame, whose panels dive out of their windows.
    frame: *const Stage,
    /// Frames into a `dive` (0: the map fills the frame; `diving.land` and
    /// on: the working frame, the map its minimap).
    t: anim.Value,
    /// The map as an object of the scene, under the panels: it shows from `show`.
    obj: anim.Obj = undefined,

    const Part = struct { ctx: *const anyopaque, draw: *const fn (*const anyopaque, *sk.Frame, anim.Placed) void };

    /// Puts `component` (bound to `m.stage`) on the map, drawn in the order added.
    pub fn add(m: *Map, s: *anim.Frame, component: anytype) void {
        const T = @TypeOf(component);
        const kept = s.new(component);
        m.parts[m.n] = .{ .ctx = kept, .draw = struct {
            fn draw(ctx: *const anyopaque, f: *sk.Frame, at: anim.Placed) void {
                @as(*const T, @ptrCast(@alignCast(ctx))).draw(f, at);
            }
        }.draw };
        m.n += 1;
    }

    /// Whether the working frame is in place (the minimap is the map).
    pub fn settled(m: *const Map, at: anim.Placed) bool {
        return at.value(m.t) >= diving.land;
    }

    /// The map filling the frame, before a scene's first `dive`: it fades
    /// in (18 f) with `with` (the working panels, at their windows on it).
    pub fn show(m: *const Map, s: *anim.Frame, with: anytype) void {
        s.play(.{m.t.to(0)}, .{ .seconds = 0 });
        var all: [with.len + 1]anim.Animation = undefined;
        all[0] = m.obj.fadeIn();
        inline for (with, 1..) |o, i| all[i] = o.fadeIn();
        s.play(all, .{ .seconds = theme.time.cascade, .ease = anim.ease.linear });
    }

    /// `dive`: the windows go bright, the rest of the map is veiled; each
    /// window travels to its panel's place, growing from the map's size to
    /// the frame's on a logarithm (`glide`, 6 f apart in reading order),
    /// as the map falls to the minimap's place; the veil lifts as it lands;
    /// then the frame is still.
    pub fn dive(m: *const Map, s: *anim.Frame) void {
        s.play(.{m.t.to(diving.land + diving.still)}, .{ .seconds = @as(f32, diving.land + diving.still) / 60.0, .ease = anim.ease.linear });
    }

    /// `surface`: the dive backwards, a little quicker; then the map is
    /// still before anything plays on it.
    pub fn surface(m: *const Map, s: *anim.Frame) void {
        s.play(.{m.t.to(diving.land)}, .{ .seconds = 0 });
        s.play(.{m.t.to(0)}, .{ .seconds = @as(f32, diving.surface) / 60.0, .ease = anim.ease.linear });
        s.wait(@as(f32, diving.still) / 60.0);
    }

    /// The map while it is shown or travelling (the scene's first object):
    /// filling the frame at half its size, or on its way to the minimap.
    /// It sets where each working panel is drawn on the way (`context.zoomed`).
    pub fn draw(m: *const Map, f: *sk.Frame, placed: anim.Placed) void {
        const context = @import("context.zig");
        context.zoomed = @splat(null);
        const t = placed.value(m.t);
        if (t >= diving.land) return;
        const fixed = @import("words.zig").fixed(f, placed, 0);
        // The map: from filling the frame (half its units) to the minimap's rect (a twentieth).
        const q = theme.ease(.glide, std.math.clamp((t - diving.move) / diving.fall, 0, 1));
        const mini = theme.minimap.box;
        const k = std.math.pow(f32, 0.5, 1 - q) * std.math.pow(f32, theme.minimap.scale, q);
        const origin = [2]f32{ q * mini.x, q * mini.y };
        var at = fixed.at;
        at.xf = at.xf.mul(.translate(origin[0], origin[1])).mul(.scale(k, k));
        at.scale = fixed.at.scale * k;
        fixed.fill(.{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }, theme.color.page);
        m.drawAt(f, at, k < 0.12);
        // The rest of the map veiled from 6 f to 24 f, lifting as it lands.
        const veil = std.math.clamp((t - diving.bright) / (diving.move - diving.bright), 0, 1) * (1 - std.math.clamp((t - diving.lift) / (diving.land - diving.lift), 0, 1));
        (ui.Ui{ .f = f, .at = at }).veil(theme.map.stage, veil);
        // The windows bright from 0 to 6 f, over the veil.
        m.outline(f, at, k < 0.12, std.math.clamp(t / diving.bright, 0, 1));
        // Each working panel: from its window on the map at the start to its place, 6 f after the one before.
        const back = at.xf.invert();
        const working = m.frame.now(placed);
        inline for (.{ "band", "view", "source", "registers", "disasm", "stack", "backtrace" }, 0..) |name, order| {
            const i = @intFromEnum(@field(Panel, name));
            const box = @field(working, name);
            if (box.h > 0) if (window_px[i]) |w| {
                const a = back.apply(.{ w[0], w[1] });
                const b = back.apply(.{ w[2], w[3] });
                // On the map as it fills the frame (half its units): the panel's rows on the window's rows, its tag above them.
                const s0 = (b[0] - a[0]) / 2 / box.w;
                const lead = (if (i == @intFromEnum(Panel.band)) 0 else theme.metric.context.tag + theme.metric.context.pad) * s0;
                const from: Rect = .{ .x = a[0] / 2, .y = a[1] / 2 - lead, .w = box.w * s0, .h = box.h * s0 };
                const p = theme.ease(.glide, std.math.clamp((t - diving.move - @as(f32, @floatFromInt(order)) * diving.apart) / diving.travel, 0, 1));
                const scale = std.math.pow(f32, s0, 1 - p);
                const x = std.math.lerp(from.x, box.x, p);
                const y = std.math.lerp(from.y, box.y, p);
                // The tags, rules and lanes come in as the map lands.
                const chrome = std.math.clamp((t - diving.lift) / 12, 0, 1);
                context.zoomed[i] = .{ .box = box, .to = .{ .x = x, .y = y, .w = box.w * scale, .h = box.h * scale }, .chrome = chrome };
            };
        }
    }

    /// The map in `at`'s transform (the map's units), and the working
    /// frame's windows outlined on it: a 2-unit `dim` frame 4 out; on the
    /// minimap (`mini`) a pixel of primary, the focused one 1.5.
    pub fn drawAt(m: *const Map, f: *sk.Frame, at: anim.Placed, mini: bool) void {
        m.drawParts(f, at, mini);
        m.outline(f, at, mini, 0);
    }

    fn drawParts(m: *const Map, f: *sk.Frame, at: anim.Placed, mini: bool) void {
        ui.mapping = true;
        ui.strokes = mini;
        counts = @splat(0);
        for (m.parts[0..m.n]) |p| p.draw(p.ctx, f, at);
        ui.mapping = false;
        ui.strokes = false;
    }

    /// The working windows' outlines on the map in `at`, `bright` of the way
    /// from `dim` to bright; where they are is kept (`window_px`).
    fn outline(m: *const Map, f: *sk.Frame, at: anim.Placed, mini: bool, bright: f32) void {
        var raw: ui.Ui = .{ .f = f, .at = at };
        raw.at.xf = .identity;
        const k = at.scale;
        const split_ = m.stage.now(at);
        inline for (.{ "band", "view", "source", "disasm", "registers", "stack", "backtrace" }) |name| {
            const panel = @field(Panel, name);
            const i = @intFromEnum(panel);
            const box = @field(split_, name);
            if (box.h > 0) {
                const a = at.xf.apply(.{ box.x, box.y });
                const b = at.xf.apply(.{ box.x + box.w, box.y + box.h });
                var r: [4]f32 = .{ a[0], a[1], b[0], b[1] };
                var shown = panel == .registers or panel == .band;
                if (windows[i]) |win| {
                    var top: f32 = std.math.inf(f32);
                    var bottom: f32 = -std.math.inf(f32);
                    for (rows[i][0..counts[i]]) |rw| if (rw.key >= win[0] and rw.key <= win[1]) {
                        top = @min(top, rw.box[1]);
                        bottom = @max(bottom, rw.box[3]);
                    };
                    if (top < bottom) {
                        r[1] = top;
                        r[3] = bottom;
                        shown = true;
                    }
                }
                window_px[i] = if (shown) r else null;
                if (shown) {
                    const focus = m.focus == panel;
                    // On the minimap a pixel of the frame: `k` is the canvas's pixels to a map unit.
                    const w = if (mini) @as(f32, if (focus) 1.5 else 1) * k / theme.minimap.scale else theme.map.outline * k;
                    const out = if (mini) w else theme.map.outline_out * k;
                    raw.frame(.{ .x = r[0] - out, .y = r[1] - out, .w = r[2] - r[0] + 2 * out, .h = r[3] - r[1] + 2 * out }, ui.mix(if (mini) theme.color.text.primary else theme.color.text.dim, theme.color.text.bright, bright), if (bright > 0 and !mini) w * 1.5 else w);
                }
            }
        }
    }
};

/// The minimap: `m` at a twentieth in the caption band's right end, still
/// while the camera moves; the page under it, framed a pixel in `rule`.
pub fn minimap(m: *const Map, f: *sk.Frame, at: anim.Placed) void {
    const fixed = @import("words.zig").fixed(f, at, 0);
    const box = theme.minimap.box;
    fixed.fill(box, theme.color.page);
    var placed = fixed.at;
    placed.xf = placed.xf.mul(.translate(box.x, box.y)).mul(.scale(theme.minimap.scale, theme.minimap.scale));
    placed.scale = fixed.at.scale * theme.minimap.scale;
    const clip = fixed.at.xf.apply(.{ box.x, box.y });
    const end = fixed.at.xf.apply(.{ box.x + box.w, box.y + box.h });
    f.clip(sk.Rect{ .x = @round(clip[0]), .y = @round(clip[1]), .w = @round(end[0]) - @round(clip[0]), .h = @round(end[1]) - @round(clip[1]) });
    m.drawAt(f, placed, true);
    f.clip(null);
    fixed.frame(.{ .x = box.x - 1, .y = box.y - 1, .w = box.w + 2, .h = box.h + 2 }, theme.color.rule, 1);
}

test "the map's columns hold their panels" {
    try std.testing.expectEqual(@as(f32, 1688), theme.layout.panelH(theme.map.disasm_rows));
    const st = theme.map.stage;
    try std.testing.expectEqual(@as(f32, 3840), st.x + st.w + st.x);
}