summaryrefslogtreecommitdiff
path: root/anim/src/stage.zig
blob: bbf5887ee10616aaf41a31cffc51d52e41c4e604 (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
//! The `stage` (design/README.md, "5. The frame"): where every panel is,
//! by the scene's split, and `relayout`, the one move of the layout: a
//! change of split, 48 f on `glide`, with nothing else moving under it.
//! Every panel of the context reads its box here each frame, so a
//! relayout moves them all together.
const std = @import("std");
const anim = @import("anim");
const ui = @import("ui.zig");
const theme = ui.theme;
const Split = theme.layout.Split;
const Rect = theme.Rect;

pub const Part = enum { band, view, source, disasm, registers, stack, backtrace, more, source_more };

pub const Stage = struct {
    first: Split,
    moves: [32]Move = undefined,
    count: usize = 0,
    /// Seconds into the band's lens rising from its bytes (`Context.establish`,
    /// `ties.rise`); far past it, for a scene that does not raise one.
    rise: anim.Value,
    /// 1, or less while a relayout fades the drawing out and in round its move.
    drawing: anim.Value,
    /// 1, or less while the camera visits the drawing and its view is cleared.
    view: anim.Value,
    /// While the camera visits the stack: the moment whose window it holds
    /// still (negative: none), and how many `free` words above its rsp it shows.
    hold: anim.Value,
    hold_free: anim.Value,
    /// 0 to 1 as the window goes from where it was to where it is held (and back).
    hold_in: anim.Value,
    /// The registers panel's rows (the scene's, `layout.fit`): every split it changes to keeps them. 0: as the split has it.
    registers_rows: f32 = 0,
    /// 0 the working frame, 1 the column (`column.zig`): the clock under the text column.
    column: anim.Value,
    /// How much larger the band's drawing is drawn than its lens's size (a
    /// column's `on`, `above`: the drawing across both columns), its tag not.
    zoom: anim.Value,

    const Move = struct { from: Split, to: Split, p: anim.Value };

    /// A stage at `split`, made where a scene's components can keep a pointer to it.
    pub fn init(s: *anim.Frame, split: Split) *Stage {
        return s.new(Stage{ .first = split, .rise = s.value(std.math.floatMax(f32)), .drawing = s.value(1), .view = s.value(1), .hold = s.value(-1), .hold_free = s.value(0), .hold_in = s.value(0), .column = s.value(0), .zoom = s.value(1) });
    }

    /// Every panel to its box in `to`, as it is (no fit), over `seconds` on
    /// `glide`, the drawing not faded: the column's moves (`column.zig`).
    pub fn glide(st: *Stage, s: *anim.Frame, to: Split, seconds: f32) void {
        const p = s.value(0);
        st.moves[st.count] = .{ .from = st.last(), .to = to, .p = p };
        st.count += 1;
        s.play(.{p.to(1)}, .{ .seconds = seconds, .ease = anim.ease.linear });
    }

    /// The split the stage ends in, after every move so far.
    pub fn last(st: *const Stage) Split {
        return if (st.count == 0) st.first else st.moves[st.count - 1].to;
    }

    /// A change of split: every panel glides to its place in `to`.
    pub fn relayout(st: *Stage, s: *anim.Frame, to: Split) void {
        const p = s.value(0);
        st.moves[st.count] = .{ .from = st.last(), .to = if (st.registers_rows > 0) theme.layout.fit(to, st.registers_rows) else to, .p = p };
        st.count += 1;
        // The drawing fades out (12 f), the panels move, it fades in at its new size.
        s.play(.{st.drawing.to(0)}, .{ .seconds = theme.time.relayout_fade, .ease = anim.ease.linear });
        s.play(.{p.to(1)}, .{ .seconds = theme.time.relayout, .ease = anim.ease.linear });
        s.play(.{st.drawing.to(1)}, .{ .seconds = theme.time.relayout_fade, .ease = anim.ease.linear });
    }

    /// Holds the stack's window still as it is at moment `at`, with `free`
    /// words above its rsp for pushes to fill: for the camera's visit.
    pub fn holdStack(st: *Stage, s: *anim.Frame, at: u64, free: u32) void {
        s.play(.{ st.hold.to(@floatFromInt(at)), st.hold_free.to(@floatFromInt(free)) }, .{ .seconds = 0 });
        // It scrolls there by whole rows, as a window does.
        s.play(.{st.hold_in.to(1)}, .{ .seconds = theme.time.move, .ease = anim.ease.linear });
    }

    pub fn releaseStack(st: *Stage, s: *anim.Frame) void {
        s.play(.{st.hold_in.to(0)}, .{ .seconds = theme.time.move, .ease = anim.ease.linear });
        s.play(.{st.hold.to(-1)}, .{ .seconds = 0 });
    }

    /// The split as it stands at this instant.
    pub fn now(st: *const Stage, at: anim.Placed) Split {
        var split = st.first;
        for (st.moves[0..st.count]) |m| {
            const p = at.value(m.p);
            if (p <= 0) continue;
            split = between(m.from, m.to, theme.ease(.glide, p));
        }
        return split;
    }

    /// Whether a relayout is under way: nothing else moves under it.
    pub fn moving(st: *const Stage, at: anim.Placed) bool {
        for (st.moves[0..st.count]) |m| {
            const p = at.value(m.p);
            if (p > 0 and p < 1) return true;
        }
        return false;
    }

    pub fn box(st: *const Stage, at: anim.Placed, part: Part) Rect {
        const split = st.now(at);
        return switch (part) {
            inline else => |tag| @field(split, @tagName(tag)),
        };
    }
};

fn between(a: Split, b: Split, p: f32) Split {
    var out = b;
    inline for (.{ "band", "view", "source", "disasm", "registers", "stack", "backtrace" }) |name| {
        const x = @field(a, name);
        const y = @field(b, name);
        @field(out, name) = .{ .x = std.math.lerp(x.x, y.x, p), .y = std.math.lerp(x.y, y.y, p), .w = std.math.lerp(x.w, y.w, p), .h = std.math.lerp(x.h, y.h, p) };
    }
    out.dump_rows = if (p < 0.5) a.dump_rows else b.dump_rows;
    return out;
}

test "a relayout passes through the splits it joins" {
    const sp = theme.layout.split;
    const half = between(sp.beside, sp.subject, 0.5);
    try std.testing.expectEqual(@as(f32, 224), half.band.h);
    try std.testing.expectEqual(sp.subject.stack, between(sp.beside, sp.subject, 1).stack);
}