summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/anim/Painter.zig12
-rw-r--r--src/anim/root.zig186
2 files changed, 190 insertions, 8 deletions
diff --git a/src/anim/Painter.zig b/src/anim/Painter.zig
index b5d492f..f317dc1 100644
--- a/src/anim/Painter.zig
+++ b/src/anim/Painter.zig
@@ -56,11 +56,13 @@ pub fn fit(canvas: sk.Rect) f32 {
}
/// Object `o`'s drawing, centered on its origin, and its bounds.
-fn built(p: *Painter, o: anim.Object, svgs: anytype) !Built {
+fn built(p: *Painter, o: anim.Object, svgs: anytype) anim.BuildError!Built {
const id = o.id();
if (p.drawings.get(id)) |b| return b;
const arena = p.arena.?.allocator();
- const d = try anim.build(arena, o, svgs);
+ // A part, or what is left of an object parts were taken from: shapes
+ // of a whole drawing, which is built (parsed) once.
+ const d = if (o.source == .part or o.cut.len > 0) try anim.sift(arena, (try p.built(o.whole(), svgs)).drawing, o) else try anim.build(arena, o, svgs);
const b: Built = .{ .drawing = d, .bounds = d.bounds().rect() };
try p.drawings.put(arena, id, b);
return b;
@@ -122,7 +124,11 @@ fn paintLayers(p: *Painter, layers: anytype, f: *sk.Frame, shot: anim.Shot, svgs
// Morphing: from where this object began the transform to where
// its target will stand.
const morph = shot.morphs[i];
- const target = (try p.built(shot.objects[pose.into], svgs)).drawing;
+ // Into the whole target, parts and all: parts taken from it
+ // once it is made stand where they were and show as it does.
+ var into = shot.objects[pose.into];
+ into.cut = &.{};
+ const target = (try p.built(into, svgs)).drawing;
const between = try vector.morph.between(f.scratch, d, world.mul(model(morph.from)), target, world.mul(model(morph.to)), pose.morph);
try r.draw(between, .identity, pose.opacity);
shapes += between.shapes.len;
diff --git a/src/anim/root.zig b/src/anim/root.zig
index 4acecb3..560d71b 100644
--- a/src/anim/root.zig
+++ b/src/anim/root.zig
@@ -141,6 +141,9 @@ pub const Source = union(enum) {
polygon: []const [2]f32,
/// Drawn by the scene's own code (`custom`).
custom: Custom,
+ /// The shapes of `of` whose bounds' centre lies in `area` (in `of`'s
+ /// units), around the area's centre (`part`).
+ part: struct { of: *const Object, area: vector.geom.Rect },
};
/// An object the scene draws itself with 2D commands (`f.fill`, `sk.draw`,
@@ -225,6 +228,8 @@ pub const Object = struct {
fill: ?u32 = null,
stroke: ?u32 = null,
width: f32 = 4,
+ /// Areas whose shapes parts were taken from it: it draws the rest.
+ cut: []const vector.geom.Rect = &.{},
/// Identifies what the object looks like (not where it is), to cache
/// its drawing by.
@@ -238,11 +243,25 @@ pub const Object = struct {
.circle => |c| h.update(std.mem.asBytes(&c.r)),
.line => |l| h.update(std.mem.asBytes(&[_]f32{ l.dx, l.dy, @floatFromInt(@intFromBool(l.arrow)) })),
.custom => |c| h.update(std.mem.asBytes(&[_]usize{ @intFromPtr(c.draw), @intFromPtr(c.ctx) })),
+ .part => |p| {
+ h.update(std.mem.asBytes(&p.of.id()));
+ h.update(std.mem.asBytes(&p.area));
+ },
}
h.update(std.mem.asBytes(&[_]u64{ o.fill orelse 1 << 32, o.stroke orelse 1 << 32 }));
h.update(std.mem.asBytes(&o.width));
+ h.update(std.mem.sliceAsBytes(o.cut));
return h.final();
}
+
+ /// What a part or a cut object is drawn from (`sift`): the object
+ /// without its cut, or the one a part was taken from.
+ pub fn whole(o: Object) Object {
+ if (o.cut.len == 0) return o.source.part.of.*;
+ var w = o;
+ w.cut = &.{};
+ return w;
+ }
};
/// Pose fields to animate towards; the others stay as they are.
@@ -526,6 +545,25 @@ const adding = struct {
}
};
+/// `part`'s area: `.x`, `.y` its centre (0 when left out), `.w`, `.h`.
+fn areaOf(spec: anytype) vector.geom.Rect {
+ const w: f32 = spec.w;
+ const h: f32 = spec.h;
+ return .{ .x = number(spec, "x", 0) - w / 2, .y = number(spec, "y", 0) - h / 2, .w = w, .h = h };
+}
+
+/// Where a part whose origin is `c` in its object's units stands, when the
+/// object stands at `p`.
+fn partPose(p: Pose, c: vector.geom.Vec) Pose {
+ var q = p;
+ const at = vector.geom.Affine.rotate(p.rotation).apply(c * vector.geom.splat(p.scale));
+ q.x += at[0];
+ q.y += at[1];
+ q.into = Pose.none;
+ q.morph = 0;
+ return q;
+}
+
fn primitive(source: Source, spec: anytype) Object {
var pose: Pose = .{};
inline for (.{ "x", "y", "scale", "rotation", "opacity", "visible" }) |name| {
@@ -553,13 +591,36 @@ pub const Builder = struct {
values: []const f32 = &.{},
plays: []const Play = &.{},
updaters: []const Updater = &.{},
+ /// Parts, each following its object through the plays laid before it
+ /// was taken.
+ follows: []const Follow = &.{},
/// Where the next play starts.
cursor: f32 = 0,
+ const Follow = struct { obj: u16, of: u16, plays: usize, at: vector.geom.Vec };
+
pub fn add(b: *Builder, o: Object) Obj {
b.objects = b.objects ++ .{o};
return @fromBackingInt(@intCast(b.objects.len - 1));
}
+
+ /// The shapes of `of` whose bounds' centre lies in `area` (`.x`, `.y`
+ /// its centre, `.w`, `.h`, in `of`'s units: those of `s.rect` laid
+ /// over `of` at the origin, at scale 1), as an object of their own,
+ /// around the area's centre; `of` draws the rest. It stands where they
+ /// stood and shows as `of` does after the plays so far; plays after it
+ /// move one or the other. A shape goes to the first part that takes it.
+ pub fn part(b: *Builder, of: Obj, area: anytype) Obj {
+ const i = @backingInt(of);
+ const r = areaOf(area);
+ const whole = b.objects[i];
+ var cut = whole;
+ cut.cut = whole.cut ++ .{r};
+ b.objects = b.objects[0..i] ++ .{cut} ++ b.objects[i + 1 ..];
+ const o = b.add(.{ .source = .{ .part = .{ .of = &whole, .area = r } }, .pose = partPose(whole.pose, r.center()) });
+ b.follows = b.follows ++ .{Follow{ .obj = @backingInt(o), .of = i, .plays = b.plays.len, .at = r.center() }};
+ return o;
+ }
pub const svg = adding.svg;
pub const typst = adding.typst;
pub const rect = adding.rect;
@@ -620,13 +681,23 @@ pub fn Scene(comptime construct: fn (*Builder) void) type {
var value_keys: [recorded.values.len][]const Key(f32) = @splat(&.{});
var camera_keys: []const Key(Camera) = &.{};
var marks: []const f32 = &.{};
- for (recorded.plays) |play| {
+ for (recorded.plays, 0..) |play, played| {
var buf: [2 * play.animations.len]Change = undefined;
+ var touched: [n]bool = @splat(false);
for (changes(&buf, &poses, play.animations)) |c| {
const k = play.key(Pose, c.from, c.to, c.ease);
keys[c.obj] = keys[c.obj] ++ .{k};
poses[c.obj] = k.at(k.end);
+ touched[c.obj] = true;
}
+ for (recorded.follows) |f| if (played < f.plays and touched[f.of]) {
+ var k = keys[f.of][keys[f.of].len - 1];
+ k.from = partPose(k.from, f.at);
+ k.to = partPose(k.to, f.at);
+ keys[f.obj] = keys[f.obj] ++ .{k};
+ poses[f.obj] = k.at(k.end);
+ touched[f.obj] = true;
+ };
for (play.animations) |a| if (a == .value) {
const i = @backingInt(a.value.value);
value_keys[i] = value_keys[i] ++ .{play.key(f32, values[i], a.value.to, null)};
@@ -723,9 +794,12 @@ fn latest(comptime P: type, keys: []const Key(P), t: f32) ?Key(P) {
return found;
}
+pub const BuildError = error{ TypstNotCompiled, CustomHasNoDrawing } || vector.svg.Error;
+
/// The drawing of an object, centered on its origin, in pixels at scale 1.
/// `compiled` has `find(source) ?[]const u8` for Typst objects' SVGs.
-pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) !Drawing {
+pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) BuildError!Drawing {
+ if (o.source == .part or o.cut.len > 0) return sift(arena, try build(arena, o.whole(), compiled), o);
const document: ?[]const u8 = switch (o.source) {
.svg => |bytes| bytes,
.typst => |source| compiled.find(source) orelse return error.TypstNotCompiled,
@@ -738,8 +812,14 @@ pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) !Drawing {
// Keep the SVG's pixel size (Typst's points are 4/3 px), centered.
const s = if (d.view.w > 0) d.width / d.view.w else 1;
const c = d.view.center();
+ const centring = vector.geom.Affine.scale(s, s).mul(.translate(-c[0], -c[1]));
const shapes = try arena.dupe(Drawing.Shape, d.shapes);
- for (shapes) |*shape| shape.transform = vector.geom.Affine.scale(s, s).mul(.translate(-c[0], -c[1])).mul(shape.transform);
+ for (shapes) |*shape| {
+ shape.transform = centring.mul(shape.transform);
+ if (shape.clip) |*q| for (q) |*v| {
+ v.* = centring.apply(v.*);
+ };
+ }
var out = d;
out.shapes = shapes;
out.view = .{ .x = -d.view.w * s / 2, .y = -d.view.h * s / 2, .w = d.view.w * s, .h = d.view.h * s };
@@ -779,13 +859,43 @@ pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) !Drawing {
style.stroke = .none;
}
},
- .svg, .typst, .custom => unreachable,
+ .svg, .typst, .custom, .part => unreachable,
}
try b.endShape(.identity, style);
const r = @max(extent(o.source), 1);
return b.finish(.{ .x = -r, .y = -r, .w = 2 * r, .h = 2 * r }, 2 * r, 2 * r);
}
+/// The drawing of a part, or of an object parts were taken from, out of
+/// `whole`, the drawing of `o.whole()`: the shapes whose bounds' centre
+/// lies in the part's area, around the area's centre; or those in none of
+/// the object's `cut`, where they were. Shapes and geometry are shared.
+pub fn sift(arena: std.mem.Allocator, whole: Drawing, o: Object) error{OutOfMemory}!Drawing {
+ const areas: []const vector.geom.Rect = if (o.cut.len > 0) o.cut else &.{o.source.part.area};
+ const keep_inside = o.cut.len == 0;
+ const c: vector.geom.Vec = if (keep_inside) areas[0].center() else .{ 0, 0 };
+ const to = vector.geom.Affine.translate(-c[0], -c[1]);
+ var shapes: std.ArrayList(Drawing.Shape) = .empty;
+ for (whole.shapes) |shape| {
+ const b = whole.shapeBounds(shape);
+ const mid = shape.transform.apply((b.min + b.max) * vector.geom.splat(0.5));
+ const inside = for (areas) |a| {
+ if (mid[0] >= a.x and mid[0] < a.x + a.w and mid[1] >= a.y and mid[1] < a.y + a.h) break true;
+ } else false;
+ if (inside != keep_inside) continue;
+ var kept = shape;
+ kept.transform = to.mul(shape.transform);
+ if (kept.clip) |*q| for (q) |*v| {
+ v.* = to.apply(v.*);
+ };
+ try shapes.append(arena, kept);
+ }
+ var out = whole;
+ out.shapes = shapes.items;
+ if (keep_inside) out.view = .{ .x = -areas[0].w / 2, .y = -areas[0].h / 2, .w = areas[0].w, .h = areas[0].h };
+ return out;
+}
+
fn extent(s: Source) f32 {
return switch (s) {
.rect => |r| @max(r.w, r.h) / 2,
@@ -796,7 +906,7 @@ fn extent(s: Source) f32 {
for (pts) |p| m = @max(m, @max(@abs(p[0]), @abs(p[1])));
break :blk m;
},
- .svg, .typst, .custom => 0,
+ .svg, .typst, .custom, .part => 0,
};
}
@@ -912,6 +1022,19 @@ pub const Frame = struct {
pub const polygon = adding.polygon;
pub const custom = adding.custom;
+ /// The shapes of `of` inside `area`, as an object of their own, showing
+ /// as `of` does now; `of` draws the rest (see `Builder.part`).
+ pub fn part(s: *Frame, of: Obj, area: anytype) Obj {
+ const i = @backingInt(of);
+ const r = areaOf(area);
+ const whole = s.new(s.objects.items[i]);
+ s.objects.items[i].cut = std.mem.concat(s.arena, vector.geom.Rect, &.{ whole.cut, &.{r} }) catch @panic("anim: out of memory");
+ const o = s.add(.{ .source = .{ .part = .{ .of = whole, .area = r } }, .pose = partPose(whole.pose, r.center()) });
+ s.settled.items[@backingInt(o)] = partPose(s.settled.items[i], r.center());
+ s.shown.items[@backingInt(o)] = partPose(s.shown.items[i], r.center());
+ return o;
+ }
+
pub fn value(s: *Frame, initial: f32) Value {
s.push(&s.values, .{ .settled = initial, .shown = initial });
return @fromBackingInt(@intCast(s.values.items.len - 1));
@@ -1800,3 +1923,56 @@ test "beside runs alongside the main line; clear fades out what shows" {
const gone = try live.shot(arena.allocator(), .{ .t = 2 });
try std.testing.expect(!gone.poses[0].visible and !gone.poses[1].visible and gone.duration == 2);
}
+
+/// Three squares, 100 units each, the middle one red; the right one is
+/// taken as a part once the row shows, then each moves its own way.
+fn partScene(s: anytype) void {
+ const row = s.svg(
+ \\<svg viewBox="0 0 300 100" width="300" height="100"><rect width="100" height="100"/>
+ \\<rect x="100" width="100" height="100" fill="red"/><rect x="200" width="100" height="100"/></svg>
+ , .{ .x = 10, .scale = 2 });
+ s.play(.{row.fadeIn()}, .{});
+ const right = s.part(row, .{ .x = 100, .w = 80, .h = 80 });
+ s.play(.{ right.move(.{ .y = 50 }), row.move(.{ .x = 0 }) }, .{});
+}
+
+test "a part takes its shapes from its object, stands where they stood and follows it until taken" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const compiled = Scene(struct {
+ fn construct(s: *Builder) void {
+ partScene(s);
+ }
+ }.construct);
+ const live = Live(struct {
+ fn frame(s: *Frame) void {
+ partScene(s);
+ }
+ }.frame, struct {});
+ for ([_]f32{ 0, 0.5, 1, 1.5, 2, 3 }) |t| {
+ const a = try compiled.shot(arena.allocator(), .{ .t = t });
+ const b = try live.shot(arena.allocator(), .{ .t = t });
+ try std.testing.expectEqualSlices(Pose, a.poses, b.poses);
+ try std.testing.expectEqual(a.objects[0].id(), b.objects[0].id());
+ try std.testing.expectEqual(a.objects[1].id(), b.objects[1].id());
+ }
+ // Fading in with the row, before it was taken; where the right square
+ // stood (10 + 2 * 100) once it was; then on its own.
+ try std.testing.expectEqual(compiled.pose(0, 0.5).opacity, compiled.pose(1, 0.5).opacity);
+ try std.testing.expectEqual(Pose{ .x = 210, .scale = 2, .visible = true }, compiled.pose(1, 1));
+ try std.testing.expectEqual(@as(f32, 50), compiled.pose(1, 2).y);
+ try std.testing.expectEqual(@as(f32, 0), compiled.pose(0, 2).x);
+ try std.testing.expectEqual(@as(f32, 210), compiled.pose(1, 2).x);
+ // The row keeps two squares; the part is the third, around its centre.
+ const no_typst = struct {
+ fn find(_: []const u8) ?[]const u8 {
+ return null;
+ }
+ };
+ const rest = try build(arena.allocator(), compiled.objects[0], no_typst);
+ const taken = try build(arena.allocator(), compiled.objects[1], no_typst);
+ try std.testing.expectEqual(@as(usize, 2), rest.shapes.len);
+ try std.testing.expectEqual(@as(usize, 1), taken.shapes.len);
+ try std.testing.expectEqual(vector.geom.Rect{ .x = -50, .y = -50, .w = 100, .h = 100 }, taken.bounds().rect());
+ try std.testing.expectEqual(vector.geom.Rect{ .x = -150, .y = -50, .w = 200, .h = 100 }, rest.bounds().rect());
+}