diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 22:55:10 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 7e3475a779f9e8dcb14b96cfc59e2b92a20c0cda (patch) | |
| tree | fe1111672776bb131b7698d0aa3d69b94a082fff | |
| parent | 0c10f9668b3ef67ce1eb83db435aaa94b35df012 (diff) | |
| download | sketchbook-7e3475a779f9e8dcb14b96cfc59e2b92a20c0cda.tar.gz sketchbook-7e3475a779f9e8dcb14b96cfc59e2b92a20c0cda.zip | |
anim: the stock player runs on the Deck
The player's State is an anim.Deck plus the viewer's settings; keys and
clicks become an Ask: click, space and page down step (space plays or
pauses a scene with no pause), page up and backspace go back a step, ] and
[ go a slide, P plays or pauses, left/right/home/end/,/. seek through
Ask.to and Ask.end. A compiled scene is a talk without gates. The HUD
shows the slide and step, and when the talk waits. Exported, each pause
holds the scene file's hold (1 s). Its frozen hold (held, stopBetween,
proceed) is gone. Scripts read deck.t and deck.playing where they read t
and playing.
| -rw-r--r-- | examples/vecanim/test/intro.script | 14 | ||||
| -rw-r--r-- | src/anim/player.zig | 141 | ||||
| -rw-r--r-- | test/scene_compiled.script | 2 | ||||
| -rw-r--r-- | test/scene_live.script | 21 |
4 files changed, 101 insertions, 77 deletions
diff --git a/examples/vecanim/test/intro.script b/examples/vecanim/test/intro.script index 75ce751..9dd61bb 100644 --- a/examples/vecanim/test/intro.script +++ b/examples/vecanim/test/intro.script @@ -2,28 +2,28 @@ # then the same instants on the CPU renderer. frames 60 screenshot .zig-cache/e2e/title.png -console set t 7.5 +console set deck.t 7.5 frames 1 screenshot .zig-cache/e2e/shapes.png -console set t 13 +console set deck.t 13 frames 1 screenshot .zig-cache/e2e/morph.png -console set t 16.5 +console set deck.t 16.5 frames 1 screenshot .zig-cache/e2e/zoom.png -console set t 24 +console set deck.t 24 frames 1 screenshot .zig-cache/e2e/text.png key b expect backend cpu -console set t 13 +console set deck.t 13 frames 1 screenshot .zig-cache/e2e/morph-cpu.png -console set t 24 +console set deck.t 24 frames 1 screenshot .zig-cache/e2e/text-cpu.png key g expect guides true -console set t 16.5 +console set deck.t 16.5 frames 1 screenshot .zig-cache/e2e/guides.png diff --git a/src/anim/player.zig b/src/anim/player.zig index c9f2169..3b0662d 100644 --- a/src/anim/player.zig +++ b/src/anim/player.zig @@ -1,20 +1,26 @@ //! The `anim` module's player: plays the scene file it is built around //! (`sb.addScene`), compiled or live, as a manim-style animation drawn by -//! the vector library on the GPU (or the CPU, with B). Time is state; -//! everything else is a function of it (and, for a live scene, of the -//! input). +//! the vector library on the GPU (or the CPU, with B). Its state is an +//! `anim.Deck`, the playhead and the gates opened, and the viewer's own +//! settings; the picture is a function of them (and, for a live scene, of +//! the input). A scene that pauses is a talk, and steps: //! -//! space pause / play left/right previous / next play -//! click continue after a pause (`s.pause()` in the scene) +//! click, space, page down next step: open the pause the talk waits at, +//! or finish the step under way (in a scene with +//! no pause, space plays or pauses) +//! page up, backspace back a step: the picture before the last click +//! ] / [ next / previous slide (`s.slide()`), at rest +//! P play / pause left/right previous / next play //! ,/. step back / ahead home/end start / end //! up/down speed B GPU or CPU renderer -//! G guides: boxes, origins and axes, in scene units +//! L loop (not a talk) G guides: boxes, origins, axes //! drag, wheel: look around R reset the view //! //! The scene's frame, 1280x720 scene units around its origin, is fitted //! into the canvas, and the canvas is the room the host has: the window, or //! an export's `--size`. Exported (`in.exporting`), the scene plays through -//! once, without pauses or controls, and the player quits. +//! once without controls, each pause held for the scene file's `hold` +//! seconds (1 without one), and the player quits at the end. const std = @import("std"); const sk = @import("sketch"); const vector = @import("vector"); @@ -27,20 +33,20 @@ const typst = @import("typst"); pub const name = if (@hasDecl(scene_file, "name")) scene_file.name else "anim"; /// The page: the scene file's `background` (0xRRGGBB), or manim's. const background: u32 = if (@hasDecl(scene_file, "background")) scene_file.background else anim.background; +/// Exported, how long each pause holds. +const hold: f32 = if (@hasDecl(scene_file, "hold")) scene_file.hold else 1; /// The scene's frame, in scene units: what shows at camera zoom 1. pub const width = anim.width; pub const height = anim.height; pub const State = struct { - t: f32 = 0, - playing: bool = true, - speed: f32 = 1, + deck: anim.Deck = .{ .hold = hold }, looping: bool = true, backend: vector.Backend = .gpu, /// The viewer's own pan and zoom, on top of the scene's camera. view: sk.Camera = .{}, dragging: bool = false, - /// Where the left button went down: a click, not a drag, continues. + /// Where the left button went down: a click, not a drag, steps. press: [2]f32 = .{ 0, 0 }, guides: bool = false, }; @@ -51,32 +57,39 @@ pub const Session = struct { render_ns: f32 = 0, }; +/// A live scene's frame, or a compiled scene as a talk without gates. +const talk = if (@hasDecl(scene, "frame")) scene.frame else struct { + fn frame(s: *anim.Frame) void { + s.cursor = scene.duration; + } +}.frame; + pub fn update(s: *State, in: *const sk.Input) void { - // The timeline as it stands: a live scene builds it as it runs. - var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator); - defer arena.deinit(); - const shot = scene.shot(arena.allocator(), .{ .t = s.t }) catch return; + const d = &s.deck; + // A scene that waits somewhere is a talk: clicks and space step it. + const talking = d.gates > 0; + var ask: anim.Ask = .{}; + var mark: enum { none, previous, next } = .none; for (in.events) |e| switch (e.kind) { - // Exported: nobody clicks, so straight through, once. - .tick => if (in.exporting) { - s.t += e.x; - } else if (s.playing and !held(s.t, shot)) { - const next = s.t + e.x * s.speed; - s.t = stopBetween(s.t, next, shot) orelse next; - if (s.t > shot.duration + 1) s.t = if (s.looping) 0 else shot.duration; - }, .key_down => if (e.flags & sk.Event.repeat == 0 or key(e) == .left or key(e) == .right or key(e) == .comma or key(e) == .period) switch (key(e)) { - .space => if (s.playing and held(s.t, shot)) proceed(s) else { - s.playing = !s.playing; + .space => if (talking) { + ask.next = true; + } else { + d.playing = !d.playing; }, - .home => s.t = 0, - .end => s.t = shot.duration, - .left => s.t = previousMark(s.t, shot), - .right => s.t = nextMark(s.t, shot), - .comma => s.t = @max(s.t - 1.0 / 30.0, 0), - .period => s.t = @min(s.t + 1.0 / 30.0, shot.duration), - .up => s.speed = @min(s.speed * 2, 8), - .down => s.speed = @max(s.speed / 2, 0.125), + .page_down => ask.next = true, + .page_up, .backspace => ask.back = true, + .right_bracket => ask.next_slide = true, + .left_bracket => ask.back_slide = true, + .p => d.playing = !d.playing, + .home => ask.to = 0, + .end => ask.end = true, + .left => mark = .previous, + .right => mark = .next, + .comma => ask.to = @max((ask.to orelse d.t) - 1.0 / 30.0, 0), + .period => ask.to = @min((ask.to orelse d.t) + 1.0 / 30.0, d.duration), + .up => d.speed = @min(d.speed * 2, 8), + .down => d.speed = @max(d.speed / 2, 0.125), .b => s.backend = if (s.backend == .gpu) .cpu else .gpu, .g => s.guides = !s.guides, .l => s.looping = !s.looping, @@ -89,7 +102,7 @@ pub fn update(s: *State, in: *const sk.Input) void { }, .mouse_up => { const click = @abs(e.x - s.press[0]) + @abs(e.y - s.press[1]) < 4; - if (s.dragging and click and held(s.t, shot)) proceed(s); + if (s.dragging and click and talking) ask.next = true; s.dragging = false; }, .cancel => s.dragging = false, @@ -97,28 +110,30 @@ pub fn update(s: *State, in: *const sk.Input) void { .wheel => s.view.zoomAt(e, e.wheel(), .{ .min = 0.2, .max = 20 }), else => {}, }; + if (mark != .none) { + // The plays as the scene lays them now. + var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator); + defer arena.deinit(); + if (shotNow(s, arena.allocator(), in, .{ 0, 0 })) |shot| { + ask.to = if (mark == .previous) previousMark(d.t, shot) else nextMark(d.t, shot); + } else |_| {} + } + // Played past the end: from the start again, or held there (a talk + // holds its last picture). + if (!in.exporting and d.t > d.duration + 1) { + if (s.looping and !talking) ask.to = 0 else d.t = d.duration; + } + d.update(in, ask, talk, .{}); } fn key(e: sk.Event) sk.Key { return @fromBackingInt(@intCast(@as(u8, @truncate(e.code)))); } -/// Waiting at a pause for a click. -fn held(t: f32, shot: anim.Shot) bool { - for (shot.stops) |stop| if (stop == t) return true; - return false; -} - -/// The first pause in `(from, to]`. -fn stopBetween(from: f32, to: f32, shot: anim.Shot) ?f32 { - for (shot.stops) |stop| if (stop > from and stop <= to) return stop; - return null; -} - -/// Past the pause the player waits at. -fn proceed(s: *State) void { - s.t += 1e-3; - s.playing = true; +/// The scene at the deck's `t`: a live one through the deck, with its gates. +fn shotNow(s: *const State, arena: std.mem.Allocator, in: *const sk.Input, mouse: [2]f32) !anim.Shot { + if (@hasDecl(scene, "frame")) return s.deck.shot(arena, in, mouse, scene.frame, .{}); + return scene.shot(arena, .{ .t = s.deck.t, .in = in, .pointer = mouse }); } fn previousMark(t: f32, shot: anim.Shot) f32 { @@ -145,15 +160,11 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { // All the room there is: the window, or the size an export asks for. const canvas: sk.Rect = .{ .w = @max(@floor(f.in.window_width), 1), .h = @max(@floor(f.in.window_height), 1) }; const started = f.host.clock(); - const shot = scene.shot(f.scratch, .{ - .t = s.t, - .in = f.in, - .pointer = pointer(s, f.in, canvas), - }) catch |err| return f.host.print("scene: {t}", .{err}); + const shot = shotNow(s, f.scratch, f.in, pointer(s, f.in, canvas)) catch |err| return f.host.print("scene: {t}", .{err}); ss.painter.paint(f, shot, typst, .{ .view = s.view, .backend = s.backend, .guides = s.guides, .background = background }) catch |err| f.host.print("render: {t}", .{err}); if (f.in.exporting) { // The frame that shows the end is the last. - if (s.t >= shot.duration) f.quit(); + if (s.deck.t >= shot.duration) f.quit(); return; } const elapsed: f32 = @floatFromInt(f.host.clock() -| started); @@ -164,20 +175,28 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { fn hud(s: *const State, ss: *const Session, f: *sk.Frame, shot: anim.Shot, canvas: sk.Rect) void { const w = canvas.w; const h = canvas.h; + const d = &s.deck; var play: usize = 0; - for (shot.marks, 1..) |m, i| if (m <= s.t) { + for (shot.marks, 1..) |m, i| if (m <= d.t) { play = i; }; + var buf: [64]u8 = undefined; + const steps = (if (d.slides > 0) + std.fmt.bufPrint(&buf, " slide {d}/{d} step {d}/{d}", .{ d.slide, d.slides, d.log.count(), d.gates }) + else if (d.gates > 0) + std.fmt.bufPrint(&buf, " step {d}/{d}", .{ d.log.count(), d.gates }) + else + std.fmt.bufPrint(&buf, "", .{})) catch ""; const backend = if (s.backend == .gpu and f.host.gpu()) "GPU" else "CPU"; - const status = if (!s.playing) " paused" else if (held(s.t, shot)) " click to continue" else ""; + const status = if (!d.playing) " paused" else if (d.waiting) " click to continue" else ""; f.fill(sk.Rect{ .y = h - 28, .w = w, .h = 28 }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 }); - f.print(12, h - 19, 1, 0xece6e2, "{d:5.2}s / {d:.2}s play {d}/{d} {s} record {d:.3}ms {d} shapes speed x{d}{s}", .{ - s.t, shot.duration, play, shot.marks.len, backend, ss.render_ns / 1e6, ss.painter.shapes, s.speed, status, + f.print(12, h - 19, 1, 0xece6e2, "{d:5.2}s / {d:.2}s play {d}/{d}{s} {s} record {d:.3}ms {d} shapes speed x{d}{s}", .{ + d.t, shot.duration, play, shot.marks.len, steps, backend, ss.render_ns / 1e6, ss.painter.shapes, d.speed, status, }); if (s.guides) { const p = pointer(s, f.in, canvas); f.print(w - 540, h - 19, 1, anim.yellow, "pointer {d:.0},{d:.0}", .{ p[0], p[1] }); } - f.text(w - 400, h - 19, 1, "space click <- -> , . B G drag wheel", 0x8c8c8c); + f.text(w - 400, h - 19, 1, "click space pgup [ ] P <- -> , . B G drag", 0x8c8c8c); f.cursor(.arrow); } diff --git a/test/scene_compiled.script b/test/scene_compiled.script index 3c9ef44..df2d62f 100644 --- a/test/scene_compiled.script +++ b/test/scene_compiled.script @@ -2,7 +2,7 @@ # 30 fps, 640x360, so half size, played once without the player's controls). frames 30 screenshot .zig-cache/anim/compiled-written.png -console set t 1.5 +console set deck.t 1.5 frames 1 screenshot .zig-cache/anim/compiled-morph.png # Frame 68 is 2.27 s in: the dot over the panel's corner, at half size. diff --git a/test/scene_live.script b/test/scene_live.script index 30b2087..e480d8d 100644 --- a/test/scene_live.script +++ b/test/scene_live.script @@ -1,6 +1,9 @@ -# The live test scene, through the player. It holds at the pause. +# The live test scene, through the player. At the pause it waits for a +# click, time running on and nothing after the pause starting. frames 150 -expect t 2 +expect deck.waiting true +expect deck.t 2.5 ~0.02 +expect deck.log.len 0 screenshot .zig-cache/anim/live.png # The custom panel is over the circle added before it, expect-pixel .zig-cache/anim/live.png 600 330 32 64 96 255 1 @@ -11,23 +14,25 @@ expect-pixel .zig-cache/anim/live.png 725 400 131 193 103 255 1 expect-pixel .zig-cache/anim/live.png 600 390 240 172 95 255 1 expect-pixel .zig-cache/anim/live.png 680 390 32 64 96 255 1 # Half way through its move it is half way. -console set playing false -console set t 1.5 +console set deck.playing false +console set deck.t 1.5 screenshot .zig-cache/anim/live-moving.png expect-pixel .zig-cache/anim/live-moving.png 400 330 32 64 96 255 1 expect-pixel .zig-cache/anim/live-moving.png 600 330 252 98 85 255 1 # The CPU renderer layers the same way. -console set t 2 +console set deck.t 2 key b expect backend cpu screenshot .zig-cache/anim/live-cpu.png expect-pixel .zig-cache/anim/live-cpu.png 600 330 32 64 96 255 1 expect-pixel .zig-cache/anim/live-cpu.png 725 400 131 193 103 255 1 key b -# A click continues: the camera zooms to 2 and the panel fades to half, -# over the background where it now reaches. +# A click opens the pause and plays on: the camera zooms to 2 and the +# panel fades to half, over the background where it now reaches. click 640 360 -expect playing true +expect deck.playing true +expect deck.log.len 1 +expect deck.waiting false frames 70 screenshot .zig-cache/anim/live-zoom.png expect-pixel .zig-cache/anim/live-zoom.png 800 300 30 46 64 255 2 |
