diff options
Diffstat (limited to 'anim/src/stage.zig')
| -rw-r--r-- | anim/src/stage.zig | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/anim/src/stage.zig b/anim/src/stage.zig new file mode 100644 index 0000000..31a149a --- /dev/null +++ b/anim/src/stage.zig @@ -0,0 +1,84 @@ +//! 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, source, disasm, registers, stack, backtrace }; + +pub const Stage = struct { + first: Split, + moves: [8]Move = undefined, + count: usize = 0, + + 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 }); + } + + /// 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 = to, .p = p }; + st.count += 1; + s.play(.{p.to(1)}, .{ .seconds = theme.time.relayout, .ease = anim.ease.linear }); + } + + /// 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", "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, 200), half.band.h); + try std.testing.expectEqual(sp.subject.stack, between(sp.beside, sp.subject, 1).stack); +} |
