//! A text beside what it speaks of, for a scene that builds its parts in //! place rather than in the column's panes (the introduction): the //! column's `bubble` (design/README.md, "column"), standing alone. Go 40 //! on 54, code in mono 35, at most 40 characters a line, 16 of padding; //! the newest on `raised` paper, primary; an older one loses its paper and //! dims to `dim` (`dim`, 18 f). It arrives rising 8 (its object's fade, //! 18 f, `enter`) and leaves with its fade (12 f, `exit`). No stem: its //! level is the tie. //! shortcut: a second drawing of column.zig's bubble (wrap, paper); fold into one when the column takes custom panes. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const ui = @import("ui.zig"); const words = @import("words.zig"); const theme = ui.theme; const t = theme.color.text; const time = theme.time; const pad = 16; const chars = 40; const first = 40; const line_pitch = theme.size.sans.caption_leading; pub const Aside = struct { text: []const u8, /// The paper's left, and the middle of its first line. x: f32, level: f32, /// 0 newest, 1 the account (no paper, `dim`). dim: anim.Value, pub fn draw(a: *const Aside, f: *sk.Frame, placed: anim.Placed) void { const u = (ui.Ui{ .f = f, .at = placed }).moved(0, (1 - placed.pose.opacity) * theme.time.caption_rise); const wrapped = wrap(u, a.text); const top = a.level - first; const h = first * 2 + @as(f32, @floatFromInt(wrapped.len - 1)) * line_pitch; var w: f32 = 0; for (wrapped) |line| w = @max(w, width(u, line)); const dim = std.math.clamp(placed.value(a.dim), 0, 1); if (dim < 1) u.faded(1 - dim).fill(.{ .x = a.x, .y = top, .w = w + 2 * pad, .h = h }, theme.color.raised); const ink = ui.mix(t.primary, t.dim, dim); for (wrapped, 0..) |line, n| _ = words.runs(u, a.x + pad, top + first + @as(f32, @floatFromInt(n)) * line_pitch + 14, line, .sans, theme.size.sans.caption, theme.size.mono.value, ink); } }; /// 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`, 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, spans: [8][2]f32 = undefined, n: usize = 0, read_by: f32 = 0, pub fn say(a: *Asides, s: *anim.Frame, text: []const u8, x: f32, level: f32) void { s.wait(@max(a.read_by - s.cursor, 0)); const dim = s.value(0); const made = s.new(Aside{ .text = text, .x = x, .level = level, .dim = dim }); 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 }); } // 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; a.spans[a.n] = span; a.n += 1; a.read_by = s.cursor + time.caption(@intCast(std.unicode.utf8CountCodepoints(text) catch text.len)); s.wait(time.caption_lead); } /// All leave together (12 f), once the last is read. pub fn leave(a: *Asides, s: *anim.Frame) void { s.wait(@max(a.read_by - s.cursor, 0)); const at = s.cursor; for (a.shown[0..a.n]) |o| if (o) |obj| { s.cursor = at; s.play(.{obj.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit }); }; } }; fn easeExit(x: f32) f32 { return theme.ease(.exit, x); } fn easeEnter(x: f32) f32 { return theme.ease(.enter, x); } fn easeGlide(x: f32) f32 { return theme.ease(.glide, x); } /// How many lines `wrap` makes of `text` (a newline breaks one where the author says). fn lines(text: []const u8) usize { 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; } } return count; } fn width(u: ui.Ui, line: []const u8) f32 { var w: f32 = 0; var parts = std.mem.splitScalar(u8, line, '`'); var code = false; while (parts.next()) |part| : (code = !code) w += if (code) u.width(.mono, theme.size.mono.value, part) else u.width(.sans, theme.size.sans.caption, part); return w; } /// Lines of at most `chars` visible characters, broken at spaces; a code /// 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 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; } out.append(u.f.scratch, line.items) catch {}; } if (out.items.len == 0) out.append(u.f.scratch, "") catch {}; return out.items; } test "lines of at most forty characters, backticks not counted" { try std.testing.expectEqual(@as(usize, 1), lines("Its address space, 128 TiB, to scale.")); 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.")); }