diff options
Diffstat (limited to 'anim/src/aside.zig')
| -rw-r--r-- | anim/src/aside.zig | 73 |
1 files changed, 43 insertions, 30 deletions
diff --git a/anim/src/aside.zig b/anim/src/aside.zig index 7cf6867..5400da5 100644 --- a/anim/src/aside.zig +++ b/anim/src/aside.zig @@ -46,7 +46,8 @@ pub const Aside = struct { /// A scene's texts beside their parts, said one after another: each waits /// until the last has been read, arrives (18 f, `enter`), and is followed /// by its lead (60 f); the one before it dims (18 f), or leaves (12 f, -/// `exit`) where the new one would stand on it. `leave`: all go together. +/// `exit`, then the caption's gap) where the new one would stand on it. +/// `leave`: all go together. pub const Asides = struct { shown: [8]?anim.Obj = @splat(null), dims: [8]anim.Value = undefined, @@ -61,15 +62,18 @@ pub const Asides = struct { const obj = s.custom(made, .{ .x = -640, .y = -360, .scale = ui.scale }); const span = [2]f32{ level - first, level + first + @as(f32, @floatFromInt(lines(text) - 1)) * line_pitch }; const at = s.cursor; + var in_the_way = false; for (a.shown[0..a.n], a.dims[0..a.n], a.spans[0..a.n]) |*o, d, other| { s.cursor = at; if (o.* == null) continue; if (other[0] < span[1] + 12 and span[0] < other[1] + 12) { s.play(.{o.*.?.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit }); o.* = null; + in_the_way = true; } else s.play(.{d.to(1)}, .{ .seconds = 18.0 / 60.0, .ease = easeGlide }); } - s.cursor = at; + // One it would stand on is gone first, then a gap: never two texts over each other. + s.cursor = if (in_the_way) at + time.caption_out + time.caption_gap else at; s.play(.{obj.fadeIn()}, .{ .seconds = time.caption_in, .ease = easeEnter }); a.shown[a.n] = obj; a.dims[a.n] = dim; @@ -100,19 +104,23 @@ fn easeGlide(x: f32) f32 { return theme.ease(.glide, x); } -/// How many lines `wrap` makes of `text`. +/// How many lines `wrap` makes of `text` (a newline breaks one where the author says). fn lines(text: []const u8) usize { - var it = std.mem.splitScalar(u8, text, ' '); - var n: usize = 0; - var count: usize = 1; - while (it.next()) |word| { - const seen = (std.unicode.utf8CountCodepoints(word) catch word.len) - std.mem.count(u8, word, "`"); - if (n > 0 and n + 1 + seen > chars) { - count += 1; - n = 0; + var count: usize = 0; + var paragraphs = std.mem.splitScalar(u8, text, '\n'); + while (paragraphs.next()) |paragraph| { + var it = std.mem.splitScalar(u8, paragraph, ' '); + var n: usize = 0; + count += 1; + while (it.next()) |word| { + const seen = (std.unicode.utf8CountCodepoints(word) catch word.len) - std.mem.count(u8, word, "`"); + if (n > 0 and n + 1 + seen > chars) { + count += 1; + n = 0; + } + if (n > 0) n += 1; + n += seen; } - if (n > 0) n += 1; - n += seen; } return count; } @@ -129,25 +137,28 @@ fn width(u: ui.Ui, line: []const u8) f32 { /// run keeps its backticks on each line it spans. fn wrap(u: ui.Ui, text: []const u8) []const []const u8 { var out: std.ArrayList([]const u8) = .empty; - var it = std.mem.splitScalar(u8, text, ' '); - var line: std.ArrayList(u8) = .empty; - var n: usize = 0; - while (it.next()) |word| { - const seen = std.unicode.utf8CountCodepoints(word) catch word.len; - const ticks = std.mem.count(u8, word, "`"); - if (n > 0 and n + 1 + seen - ticks > chars) { - out.append(u.f.scratch, line.items) catch break; - line = .empty; - n = 0; + var paragraphs = std.mem.splitScalar(u8, text, '\n'); + while (paragraphs.next()) |paragraph| { + var it = std.mem.splitScalar(u8, paragraph, ' '); + var line: std.ArrayList(u8) = .empty; + var n: usize = 0; + while (it.next()) |word| { + const seen = std.unicode.utf8CountCodepoints(word) catch word.len; + const ticks = std.mem.count(u8, word, "`"); + if (n > 0 and n + 1 + seen - ticks > chars) { + out.append(u.f.scratch, line.items) catch break; + line = .empty; + n = 0; + } + if (n > 0) { + line.append(u.f.scratch, ' ') catch break; + n += 1; + } + line.appendSlice(u.f.scratch, word) catch break; + n += seen - ticks; } - if (n > 0) { - line.append(u.f.scratch, ' ') catch break; - n += 1; - } - line.appendSlice(u.f.scratch, word) catch break; - n += seen - ticks; + out.append(u.f.scratch, line.items) catch {}; } - if (line.items.len > 0) out.append(u.f.scratch, line.items) catch {}; if (out.items.len == 0) out.append(u.f.scratch, "") catch {}; return out.items; } @@ -157,4 +168,6 @@ test "lines of at most forty characters, backticks not counted" { try std.testing.expectEqual(@as(usize, 2), lines("Its address space, 128 TiB, to scale: mapped only at each end.")); // Four code runs of nine: 39 shown, 47 with their backticks. try std.testing.expectEqual(@as(usize, 1), lines("`012345678` `012345678` `012345678` `012345678`")); + // A newline breaks where the author says. + try std.testing.expectEqual(@as(usize, 2), lines("And all of it at once:\nthe map.")); } |
