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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
//! 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 (twice as many
//! for a text `above` a subject across both columns: `Asides.wrap`), 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,
/// Units it has glided down from `level` to make room for a newer one.
drop: ?anim.Value = null,
/// Characters a line, at most.
wrap: usize = chars,
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 + if (a.drop) |d| placed.value(d) else 0);
const wrapped = wrap(u, a.text, a.wrap);
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.
/// With a `floor`, one in the way glides down under it instead (24 f,
/// `glide`), the others under that with it, as far as the floor (past it,
/// it leaves), and three stand at most (the column's way).
/// `leave`: all go together.
pub const Asides = struct {
shown: [8]?anim.Obj = @splat(null),
dims: [8]anim.Value = undefined,
drops: [8]anim.Value = undefined,
spans: [8][2]f32 = undefined,
/// Each text's first top, before it glided.
tops: [8]f32 = undefined,
n: usize = 0,
read_by: f32 = 0,
/// Characters a line, at most, of the texts it says.
wrap: usize = chars,
/// Where texts in the way glide down to, at most (null: they leave).
floor: ?f32 = null,
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 drop = s.value(0);
const made = s.new(Aside{ .text = text, .x = x, .level = level, .dim = dim, .wrap = a.wrap, .drop = drop });
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, a.wrap) - 1)) * line_pitch };
const at = s.cursor;
var in_the_way = false;
var glided = false;
// With a floor: the two newest stay, top down, each in the way gliding under what is over it.
var keep: [8]bool = @splat(false);
if (a.floor) |floor| {
var kept: usize = 0;
var k = a.n;
while (k > 0) {
k -= 1;
if (a.shown[k] == null or kept == 2) continue;
keep[k] = true;
kept += 1;
}
var bottom = span[1] + 12;
while (true) {
// The highest kept one not yet placed.
var next: ?usize = null;
for (0..a.n) |j| if (keep[j] and a.shown[j] != null and (next == null or a.spans[j][0] < a.spans[next.?][0])) {
next = j;
};
const j = next orelse break;
keep[j] = false;
const sp = a.spans[j];
s.cursor = at;
s.play(.{a.dims[j].to(1)}, .{ .seconds = 18.0 / 60.0, .ease = easeGlide });
s.cursor = at;
if (sp[1] + 12 <= span[0]) continue;
const shift = @max(bottom - sp[0], 0);
if (sp[1] + shift > floor) {
s.play(.{a.shown[j].?.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit });
a.shown[j] = null;
in_the_way = true;
continue;
}
if (shift > 0) {
s.play(.{a.drops[j].to(sp[0] + shift - a.tops[j])}, .{ .seconds = 24.0 / 60.0, .ease = easeGlide });
a.spans[j] = .{ sp[0] + shift, sp[1] + shift };
glided = true;
}
bottom = a.spans[j][1] + 12;
}
}
for (a.shown[0..a.n], a.dims[0..a.n], a.spans[0..a.n], 0..) |*o, d, other, j| {
s.cursor = at;
if (o.* == null) continue;
if (a.floor != null) {
// Past the two newest: it leaves.
var newer: usize = 0;
for (a.shown[j + 1 .. a.n]) |later| newer += @intFromBool(later != null);
if (newer >= 2) {
s.play(.{o.*.?.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit });
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, or has glided from under it: never two texts over each other.
s.cursor = at + @max(@as(f32, if (in_the_way) time.caption_out + time.caption_gap else 0), @as(f32, if (glided) 24.0 / 60.0 else 0));
s.play(.{obj.fadeIn()}, .{ .seconds = time.caption_in, .ease = easeEnter });
a.shown[a.n] = obj;
a.dims[a.n] = dim;
a.drops[a.n] = drop;
a.spans[a.n] = span;
a.tops[a.n] = span[0];
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, most: usize) 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 > most) {
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, most: usize) []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 > most) {
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.", chars));
try std.testing.expectEqual(@as(usize, 2), lines("Its address space, 128 TiB, to scale: mapped only at each end.", chars));
// Four code runs of nine: 39 shown, 47 with their backticks.
try std.testing.expectEqual(@as(usize, 1), lines("`012345678` `012345678` `012345678` `012345678`", chars));
// A newline breaks where the author says.
try std.testing.expectEqual(@as(usize, 2), lines("And all of it at once:\nthe map.", chars));
}
|