1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
//! 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`) 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;
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;
} else s.play(.{d.to(1)}, .{ .seconds = 18.0 / 60.0, .ease = easeGlide });
}
s.cursor = 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`.
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;
}
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 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;
}
if (n > 0) {
line.append(u.f.scratch, ' ') catch break;
n += 1;
}
line.appendSlice(u.f.scratch, word) catch break;
n += seen - ticks;
}
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;
}
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`"));
}
|