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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
//! What every component draws with, in the design's units (1920 by 1080; a
//! component's origin is its own top left corner): rects and rules snapped
//! to pixels, text in the design's faces set on a baseline, and the
//! catalogue's atoms (design/README.md, "1. Atoms"): `address`, `value`,
//! `arrow`, `byte`, the `mark`s, `plaque`, `pin`, `bracket`, and a panel's `tag`.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
pub const theme = @import("theme.zig");
pub const Rect = theme.Rect;
pub const Color = sk.Color;
pub const Face = sk.Face;
pub const Vec = sk.math.Vec;
/// Scene units to a design unit: `anim` frames 1280 by 720.
pub const scale = 1280.0 / @as(comptime_float, theme.canvas.width);
/// While the map's panels draw (`map.zig`): they publish their rows, not
/// their windows and places. `strokes`: the minimap, a twentieth of the
/// map, where text is a stroke a pixel high in its ink at 45% toward the
/// page and the marks are blocks a few pixels across.
/// shortcut: process-wide, for one scene drawn at a time; two players at once want it on the frame.
pub var mapping = false;
pub var strokes = false;
/// How much of a panel's tag and rules shows: less while it dives out of
/// its window on the map (`context.at` sets it for the panel it hands out).
pub var chrome: f32 = 1;
/// The minimap's pixel, in the map's units.
const px = 20;
/// Adds `data` (a component: something with `draw(f, at)`) to the scene,
/// its origin at design `x, y` and its units the design's. Like any
/// object, it shows once something plays it in (`fadeIn`).
pub fn place(s: *anim.Frame, data: anytype, x: f32, y: f32) anim.Obj {
return s.custom(s.new(data), .{ .x = x * scale - 640, .y = y * scale - 360, .scale = scale });
}
/// A camera move that puts the middle of design rect `r` in the middle of
/// the stage (the band between the head and the caption, which stay put),
/// at `zoom` (the design allows 1, 2 and 4).
pub fn lookAt(r: Rect, zoom: f32) anim.Animation {
const to = framing(r, zoom);
return anim.camera(.{ .x = to.x, .y = to.y, .zoom = to.zoom });
}
/// Where the camera is when the middle of design rect `r` is in the middle
/// of the stage, at `zoom`.
pub fn framing(r: Rect, zoom: f32) anim.Camera {
const stage = theme.canvas.stage;
const off = (theme.canvas.height / 2.0 - (stage.y + stage.h / 2)) / zoom;
return .{ .x = (r.x + r.w / 2) * scale - 640, .y = (r.y + r.h / 2 + off) * scale - 360, .zoom = zoom };
}
/// The camera `p` of the way from `a` to `b`. Arcs: it moves in a straight
/// line and zooms by ratio (the logarithm of the scale is what is
/// interpolated), about the one point that stays where it is on screen,
/// or the first half of a move to twice the size looks twice as fast as
/// the second.
pub fn between(a: anim.Camera, b: anim.Camera, p: f32) anim.Camera {
if (@abs(a.zoom - b.zoom) < 1e-4) return .{ .x = std.math.lerp(a.x, b.x, p), .y = std.math.lerp(a.y, b.y, p), .zoom = b.zoom };
const zoom = a.zoom * std.math.pow(f32, b.zoom / a.zoom, p);
const still = [2]f32{ (b.x * b.zoom - a.x * a.zoom) / (b.zoom - a.zoom), (b.y * b.zoom - a.y * a.zoom) / (b.zoom - a.zoom) };
return .{ .x = still[0] - (still[0] - a.x) * a.zoom / zoom, .y = still[1] - (still[1] - a.y) * a.zoom / zoom, .zoom = zoom };
}
/// A theme colour (`0xRRGGBB`) or a `Color` already.
pub fn rgb(c: anytype) Color {
return if (@TypeOf(c) == Color) c else .hex(c);
}
pub fn mix(a: anytype, b: anytype, t: f32) Color {
return sk.tween.lerp(rgb(a), rgb(b), std.math.clamp(t, 0, 1));
}
/// The face a text is set in: every monospace is JetBrains Mono NL, and
/// under 30 its medium weight (light letters on a dark ground lose their
/// thin strokes first in a video stream).
fn set(face: Face, size: f32) Face {
return switch (face) {
.mono => if (size < 30) .code_medium else .code,
.mono_bold => .code_bold,
.mono_italic => .code_italic,
else => face,
};
}
/// Width of one mono character at `size`: the mono faces advance 0.6 em, a
/// whole number of pixels at the design's sizes.
pub fn advance(size: f32) f32 {
return size * 3 / 5;
}
/// How many leading hex digits every address of a column shares (an
/// address's `lead`), leaving at least two: the eye reads the rest.
pub fn shared(addrs: []const u64) usize {
if (addrs.len == 0) return 0;
var bufs: [2][16]u8 = undefined;
const first = std.fmt.bufPrint(&bufs[0], "{x}", .{addrs[0]}) catch unreachable;
var k: usize = first.len -| 2;
for (addrs[1..]) |a| {
const d = std.fmt.bufPrint(&bufs[1], "{x}", .{a}) catch unreachable;
if (d.len != first.len) return 0;
var same: usize = 0;
while (same < k and d[same] == first[same]) same += 1;
k = same;
}
return k;
}
/// The digits of an address, as long as it is.
pub fn digits(addr: u64) usize {
return if (addr == 0) 1 else (64 - @clz(addr) + 3) / 4;
}
pub const Align = enum { left, center, right };
/// What digits are set in: leading zeros in `lead`, the rest in `ink`.
pub const Ink = struct {
ink: Color = rgb(theme.color.text.primary),
lead: Color = rgb(theme.color.text.faint),
face: Face = .mono,
/// One colour for every digit: on a solid fill.
pub fn all(c: anytype, face: Face) Ink {
return .{ .ink = rgb(c), .lead = rgb(c), .face = face };
}
};
pub const Ui = struct {
f: *sk.Frame,
at: anim.Placed,
/// The same, at `opacity` of what it had: a thing arriving or leaving.
pub fn faded(u: Ui, opacity: f32) Ui {
var out = u;
out.at.pose.opacity *= std.math.clamp(opacity, 0, 1);
return out;
}
/// The same, its origin moved to `x, y`: a row or a panel drawn at its place.
pub fn moved(u: Ui, x: f32, y: f32) Ui {
var out = u;
out.at.xf = out.at.xf.mul(.translate(x, y));
return out;
}
fn point(u: Ui, x: f32, y: f32) Vec {
return u.at.xf.apply(.{ x, y });
}
/// A filled rect, its edges on whole pixels.
pub fn fill(u: Ui, r: Rect, c: anytype) void {
const a = u.point(r.x, r.y);
const b = u.point(r.x + r.w, r.y + r.h);
const x = @round(a[0]);
const y = @round(a[1]);
u.f.fill(sk.Rect{ .x = x, .y = y, .w = @round(b[0]) - x, .h = @round(b[1]) - y }, u.at.faded(rgb(c)));
}
/// A stroke `w` wide, wholly inside `r` (on the minimap, a pixel at least).
pub fn frame(u: Ui, r: Rect, c: anytype, w_: f32) void {
const w = if (strokes) @max(w_, px) else w_;
u.fill(.{ .x = r.x, .y = r.y, .w = r.w, .h = w }, c);
u.fill(.{ .x = r.x, .y = r.y + r.h - w, .w = r.w, .h = w }, c);
u.fill(.{ .x = r.x, .y = r.y + w, .w = w, .h = r.h - 2 * w }, c);
u.fill(.{ .x = r.x + r.w - w, .y = r.y + w, .w = w, .h = r.h - 2 * w }, c);
}
/// A dashed frame inside `r`: nobody's bytes.
pub fn dashedFrame(u: Ui, r: Rect, c: anytype, w: f32) void {
const h = w / 2;
const pts = [_]Vec{ .{ r.x + h, r.y + h }, .{ r.x + r.w - h, r.y + h }, .{ r.x + r.w - h, r.y + r.h - h }, .{ r.x + h, r.y + r.h - h }, .{ r.x + h, r.y + h } };
sk.draw.dashed(u.f, &pts, theme.stroke.dash, u.at.pen(.{ .stroke = rgb(c), .width = w, .cap = .butt }));
}
/// Text on a baseline. The host places a line by the top of its box,
/// which is the font's ascent above the baseline.
pub fn text(u: Ui, face: Face, size: f32, x: f32, baseline: f32, bytes: []const u8, c: anytype) void {
if (bytes.len == 0) return;
if (strokes) {
// A run of text: a stroke as long as it is, through its capitals' middle.
if (std.mem.indexOfNone(u8, bytes, " ") == null) return;
return u.fill(.{ .x = x, .y = baseline - 0.36 * size - px / 2, .w = u.width(face, size, bytes), .h = px }, mix(c, theme.color.page, 0.45));
}
// Go's ascent is 0.945 em; JetBrains Mono's, which every monospace is set in, 1.02.
const at = u.point(x, baseline - @as(f32, if (set(face, size) == face) 0.945 else 1.02) * size);
const drawn = size / 13.0 * u.at.scale;
// The camera rests at 1, 2 and 4: there text is shaped for its
// size, sharp. On the way between, it is the text of the nearer
// rest scaled, so letters do not shift against each other.
const one = @min(@floor(u.f.in.window_width) / theme.canvas.width, @floor(u.f.in.window_height) / theme.canvas.height);
const zoom = u.at.scale / one;
const rest: f32 = for (theme.camera.scales) |s| {
if (zoom <= s * 1.002) break s;
} else theme.camera.scales[theme.camera.scales.len - 1];
if (@abs(zoom - rest) <= rest * 0.002) {
u.f.textIn(set(face, size), at[0], at[1], drawn, bytes, u.at.faded(rgb(c)));
} else u.f.textScaled(set(face, size), at[0], at[1], drawn, size / 13.0 * one * rest, bytes, u.at.faded(rgb(c)));
}
pub fn width(u: Ui, face: Face, size: f32, bytes: []const u8) f32 {
return switch (face) {
.mono, .mono_bold, .mono_italic => @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(bytes) catch bytes.len)) * advance(size),
else => u.at.textSizeIn(u.f, face, size / 13.0, bytes)[0],
};
}
/// Text with its capitals centred in `r`'s height, ranged in its width.
pub fn label(u: Ui, face: Face, size: f32, r: Rect, how: Align, bytes: []const u8, c: anytype) void {
const x = switch (how) {
.left => r.x,
.center => r.x + (r.w - u.width(face, size, bytes)) / 2,
.right => r.x + r.w - u.width(face, size, bytes),
};
u.text(face, size, x, theme.baseline(r.y, r.h, size), bytes, c);
}
/// Formats into the frame's scratch memory.
pub fn print(u: Ui, comptime fmt: []const u8, args: anytype) []const u8 {
return u.f.format(fmt, args);
}
/// For tools/motion.py, which plots how much of each panel changes
/// from frame to frame: with CANIM_PANELS in the environment, every
/// frame tells where on the canvas (as fractions of it) `r` is.
pub fn region(u: Ui, name: []const u8, r: Rect) void {
if (mapping or u.f.host.env("CANIM_PANELS") == null) return;
const a = u.point(r.x, r.y);
const b = u.point(r.x + r.w, r.y + r.h);
const w = @floor(u.f.in.window_width);
const h = @floor(u.f.in.window_height);
std.debug.print("canim-panel {d:.4} {d:.4} {d:.4} {d:.4} {s}\n", .{ a[0] / w, a[1] / h, b[0] / w, b[1] / h, name });
}
// ---------------------------------------------------------- atoms
/// `tag`: a panel's title bar, 24 high, raised: a 12-unit box 8 in
/// (solid gold where the narration is, `focus`; a faint outline
/// elsewhere), the title in Go Medium 20, and ranged right a `note`
/// (Go 18, faint) or a `legend` (words in Go Medium 18, each in its
/// ink, 14 apart). With the rule round the body it is a panel, an acme
/// window `w` by `h` at the origin. Returns the body.
pub fn panel(u: Ui, w: f32, h: f32, title: []const u8, options: struct { focus: bool = false, note: []const u8 = "", legend: []const Legend = &.{} }) Rect {
u.region(title, .{ .x = 0, .y = 0, .w = w, .h = h });
if (chrome < 1) return u.faded(chrome).panelChrome(w, h, title, options.focus, options.note, options.legend);
return u.panelChrome(w, h, title, options.focus, options.note, options.legend);
}
fn panelChrome(u: Ui, w: f32, h: f32, title: []const u8, focus: bool, note: []const u8, legend: []const Legend) Rect {
const options = .{ .focus = focus, .note = note, .legend = legend };
const m = theme.metric.context;
const t = theme.color.text;
u.fill(.{ .x = 0, .y = 0, .w = w, .h = m.tag }, theme.color.raised);
u.frame(.{ .x = 0, .y = 0, .w = w, .h = h }, if (options.focus) t.dim else theme.color.rule, theme.stroke.rule);
const box: Rect = .{ .x = 8, .y = 6, .w = 12, .h = 12 };
if (options.focus) u.fill(box, theme.mark(.current).solid) else u.frame(box, t.faint, theme.stroke.rule);
const base = m.tag - 6;
u.text(.sans_medium, theme.size.sans.tag, 28, base, title, if (options.focus) t.primary else t.dim);
const micro = theme.size.sans.micro;
if (options.legend.len > 0) {
var x = w - 10 + 14;
for (options.legend) |entry| x -= u.width(.sans_medium, micro, entry.text) + 14;
for (options.legend) |entry| {
const ew = u.width(.sans_medium, micro, entry.text);
u.text(.sans_medium, micro, x, base, entry.text, entry.ink);
if (entry.under) u.fill(.{ .x = x, .y = base + 3, .w = ew, .h = theme.stroke.rule }, entry.ink);
x += ew + 14;
}
} else u.text(.sans, micro, w - 10 - u.width(.sans, micro, options.note), base, options.note, t.faint);
return .{ .x = 0, .y = m.tag, .w = w, .h = h - m.tag };
}
/// `address`: lower-case hex, unbroken, as long as it is, no `0x`, in
/// the ink of where it points. `lead`: the first n digits at 60% of
/// the ink toward the page (a column's shared digits). `name`: a code
/// address's `name+offset` after it, primary, 10 on. `rwx`: a 2-unit
/// underline. Returns where it ends (after the name).
pub fn address(u: Ui, x: f32, baseline: f32, addr: u64, a: Address) f32 {
const size = theme.size.mono.context;
var buf: [16]u8 = undefined;
const d = std.fmt.bufPrint(&buf, "{x}", .{addr}) catch unreachable;
const lead = @min(a.lead, d.len -| 2);
const adv = advance(size);
if (lead > 0) u.text(a.face, size, x, baseline, d[0..lead], mix(a.ink, theme.color.page, theme.metric.context.lead_alpha));
u.text(a.face, size, x + @as(f32, @floatFromInt(lead)) * adv, baseline, d[lead..], a.ink);
const end = x + @as(f32, @floatFromInt(d.len)) * adv;
if (a.rwx) u.fill(.{ .x = x, .y = baseline + 3, .w = end - x, .h = theme.stroke.rule }, a.ink);
if (a.name.len == 0) return end;
const name = symbol(u, a.name);
u.text(.mono, size, end + 10, baseline, name, theme.color.text.primary);
return end + 10 + @as(f32, @floatFromInt(name.len)) * adv;
}
/// `value`: what is not an address, set by its class (`Value.of`), from
/// `x` at mono 20: a digit; a small number in hex and its decimal 8
/// after it, faint; a large one in hex after `0x`; a small negative.
/// `pointer`: an address, in `ink`. Returns where its digits end (the
/// faint decimal apart: `Value.width` is what a mark goes behind).
pub fn value(u: Ui, x: f32, baseline: f32, v: u64, pointer: bool, ink: Ink) f32 {
const size = theme.size.mono.context;
const shown = Value.of(v, pointer);
u.text(ink.face, size, x, baseline, u.print("{s}{s}", .{ shown.prefix, shown.digits() }), ink.ink);
const end = x + shown.width();
if (shown.decimal) u.text(.mono, size, end + 8, baseline, u.print("{d}", .{v}), theme.color.text.faint);
return end;
}
/// `value` of a class a type or a signature gives, from `x` at mono
/// 20: `fd`, the number and faint what it is open on (`3 "note"`);
/// `boolean`, `true` or `false`; `character`, its code as a small
/// number and faint the character quoted (`0x41 'A'`). Returns where
/// it ends.
pub fn typed(u: Ui, x: f32, baseline: f32, v: u64, class: Class, ink: Ink) f32 {
const size = theme.size.mono.context;
const adv = advance(size);
const t = theme.color.text;
switch (class) {
.fd => |on| {
const number = u.print("{d}", .{@as(i32, @bitCast(@as(u32, @truncate(v))))});
u.text(ink.face, size, x, baseline, number, ink.ink);
const end = x + @as(f32, @floatFromInt(number.len)) * adv;
if (on.len == 0) return end;
u.text(.mono, size, end + 8, baseline, on, t.faint);
return end + 8 + u.width(.mono, size, on);
},
.boolean => {
const word = if (v != 0) "true" else "false";
u.text(ink.face, size, x, baseline, word, ink.ink);
return x + @as(f32, @floatFromInt(word.len)) * adv;
},
.character => {
const code = u.print("0x{x:0>2}", .{v & 0xff});
u.text(ink.face, size, x, baseline, code, ink.ink);
const end = x + @as(f32, @floatFromInt(code.len)) * adv;
const quoted = quote(u, @truncate(v));
u.text(.mono, size, end + 8, baseline, quoted, t.faint);
return end + 8 + u.width(.mono, size, quoted);
},
}
}
/// `arrow`: the sign between two things in a chain, 30 wide from `x`
/// on the row whose middle is `mid`, faint: grammar, not data.
/// Returns where it ends.
pub fn arrow(u: Ui, x: f32, mid: f32, kind: Arrow) f32 {
if (strokes) return x + theme.metric.context.arrow_w + @as(f32, if (kind == .more) 24 else 0);
const c = theme.color.text.faint;
const head = theme.metric.context.arrow_head;
const pen = u.at.pen(.{ .fill = rgb(c) });
switch (kind) {
.points, .more => {
u.fill(.{ .x = x + 6, .y = mid - 1, .w = 11, .h = 2 }, c);
sk.draw.polygon(u.f, &.{ .{ x + 24, mid }, .{ x + 24 - head[0], mid - head[1] / 2 }, .{ x + 24 - head[0], mid + head[1] / 2 } }, pen);
if (kind == .more) for (0..3) |d| u.fill(.{ .x = x + 32 + @as(f32, @floatFromInt(d)) * 8, .y = mid + 2, .w = 3, .h = 3 }, c);
},
.holds => {
sk.draw.polygon(u.f, &.{ .{ x + 6, mid }, .{ x + 6 + head[0], mid - head[1] / 2 }, .{ x + 6 + head[0], mid + head[1] / 2 } }, pen);
u.fill(.{ .x = x + 13, .y = mid - 1, .w = 11, .h = 2 }, c);
},
}
return x + theme.metric.context.arrow_w + @as(f32, if (kind == .more) 24 else 0);
}
// ---------------------------------------------------------- marks
/// `mark` on a value in the context (mono 20), as hot as `h` says:
/// `armed` (a 2-unit vermilion frame round the old value), `struck`
/// (the softened vermilion paper behind it, 5 wider each side, its
/// edges pulsing out 3; the digits bold and bright: the ink this
/// returns), then cooled to nothing, and `held`: a turned corner at
/// the value's top left for three steps, fading in over the cool's
/// last 6 f. A held change never sits under the digits it marks, whose
/// ink is where they point. `r` is the digits: x, width, and the row.
pub fn written(u: Ui, r: Rect, h: Heat) ?Ink {
const w = theme.mark(.write);
const m = theme.metric.context;
if (strokes) {
// On the minimap: a strike a block 3 pixels across at the value's start; held, a pixel.
const mid = r.y + r.h / 2;
if (h.level >= 1 and h.solid * h.in > 0) {
u.faded(h.solid * h.in).fill(.{ .x = r.x, .y = mid - 1.5 * px, .w = 3 * px, .h = 3 * px }, w.solid);
} else if (h.level * h.corner > 0) u.faded(h.level * h.corner).fill(.{ .x = r.x, .y = mid - px / 2, .w = px, .h = px }, w.solid);
return null;
}
if (h.arm > 0) u.faded(h.arm).frame(.{ .x = r.x - 6, .y = r.y, .w = r.w + 12, .h = r.h }, w.solid, m.read_frame);
const solid = if (h.level >= 1) h.solid * h.in else 0;
if (solid > 0) {
const out = @min(h.out, m.pulse_outset);
u.fill(.{ .x = r.x - m.strike_out - out, .y = r.y + 1 - out, .w = r.w + 2 * m.strike_out + 2 * out, .h = r.h - 2 + 2 * out }, mix(theme.color.page, theme.color.struck, solid));
}
u.corner(r.x - 4, r.y + 2, m.corner, h.level * h.corner);
return if (solid >= 0.5) Ink.all(theme.color.text.bright, .mono_bold) else null;
}
/// `held`: a turned corner, a vermilion right triangle `side` on its
/// sides, its right angle at `x, y` (a value's or a cell's top left),
/// `shows` of it.
pub fn corner(u: Ui, x: f32, y: f32, side: f32, shows: f32) void {
if (shows <= 0) return;
sk.draw.polygon(u.f, &.{ .{ x, y }, .{ x + side, y }, .{ x, y + side } }, u.faded(shows).at.pen(.{ .fill = rgb(theme.mark(.write).solid) }));
}
/// `read` in the context: a 2-unit frame in read's colour, 6 out at
/// the sides, no fill; `shows` of it (0 to 1: it fades in and out).
pub fn read(u: Ui, r: Rect, shows: f32) void {
if (strokes) {
if (shows > 0) u.faded(shows).frame(.{ .x = r.x, .y = r.y + r.h / 2 - 1.5 * px, .w = 3 * px, .h = 3 * px }, theme.mark(.read).solid, px);
return;
}
if (shows > 0) u.faded(shows).frame(.{ .x = r.x - 6, .y = r.y, .w = r.w + 12, .h = r.h }, theme.mark(.read).solid, theme.metric.context.read_frame);
}
/// `mark` on a lens's cell: armed (a 3-unit frame), struck (the
/// softened vermilion coming in, its edges out 4 and back, the digits
/// bold and bright), cooled to its ground (`ground`; null: nothing),
/// and `held`: a turned corner 12 on its sides at its top left. Returns
/// what to set its digits in.
pub fn cellWritten(u: Ui, r: Rect, h: Heat, ground: ?u24) Ink {
const under: Color = rgb(ground orelse theme.color.page);
// No mark under coloured digits but a strike's: it cools to its ground, and the corner stays.
var body = if (h.level < 1) under else mix(under, theme.color.struck, h.solid);
// Timing: a strike arrives over a tenth of a second, not in a frame.
if (h.in < 1) body = mix(under, body, h.in);
if (h.level > 0 or ground != null) u.fill(.{ .x = r.x - h.out, .y = r.y - h.out, .w = r.w + 2 * h.out, .h = r.h + 2 * h.out }, body);
u.corner(r.x, r.y, theme.metric.corner_cell, h.level * h.corner);
if (h.arm > 0) u.faded(h.arm).frame(r, theme.mark(.write).solid, theme.stroke.line);
// Bold and bright on the paper. The face changes at the middle of
// the cool, where the colour is moving fastest and hides it.
const heat = h.solid * h.in;
if (h.level < 1 or heat <= 0) return .{};
const cooled: Ink = .{};
const t = theme.color.text;
return .{ .ink = mix(cooled.ink, t.bright, heat), .lead = mix(cooled.lead, t.bright, heat), .face = if (heat >= 0.5) .mono_bold else .mono };
}
/// `read` on a lens's cell: a 2-unit frame 2 out, no fill.
pub fn cellRead(u: Ui, r: Rect, shows: f32) void {
if (shows > 0) u.faded(shows).frame(.{ .x = r.x - 2, .y = r.y - 2, .w = r.w + 4, .h = r.h + 4 }, theme.mark(.read).solid, theme.stroke.rule);
}
/// `current` on a row: the white wash across `r`, and a gold plaque
/// `w` wide at its left, its edges out `out` units (the fire).
pub fn current(u: Ui, r: Rect, w: f32, out: f32) void {
const gold = theme.mark(.current);
// On the minimap: a gold stroke across the column, 2 pixels high.
if (strokes) return u.fill(.{ .x = r.x, .y = r.y + r.h / 2 - px, .w = r.w, .h = 2 * px }, gold.solid);
u.fill(r, gold.ember);
u.fill(.{ .x = r.x - out, .y = r.y - out, .w = w + 2 * out, .h = r.h + 2 * out }, gold.solid);
}
/// `trail`: a 4-unit bar at a row's left edge, `k` steps old (0 newest).
pub fn trail(u: Ui, x: f32, y: f32, k: usize) void {
if (k < theme.color.trail.len) u.fill(.{ .x = x, .y = y + 1, .w = 4, .h = theme.metric.context.row - 2 }, theme.color.trail[k]);
}
/// `unknown`: never observed: two dots where the digits would be, centred in `r`.
pub fn unknown(u: Ui, r: Rect) void {
const d = 4;
const y = r.y + (r.h - d) / 2;
u.fill(.{ .x = r.x + r.w / 2 - d - 3, .y = y, .w = d, .h = d }, theme.color.text.faint);
u.fill(.{ .x = r.x + r.w / 2 + 3, .y = y, .w = d, .h = d }, theme.color.text.faint);
}
/// `plaque` `chip`: raised, `r`, behind a set flag.
pub fn chip(u: Ui, r: Rect) void {
u.fill(r, theme.color.raised);
}
/// `pin` `row`: a register's names at the row it points at, bold,
/// primary, then 5 on a head 9 by 12 in the target's region ink,
/// pointing right at the row's address. Three names, then a count.
pub fn pinRow(u: Ui, x: f32, y: f32, names: []const []const u8, ink: anytype) void {
const size = theme.size.mono.context;
const p = theme.metric.pin;
const shown = joined(u, names);
u.text(.mono_bold, size, x, theme.baseline(y, theme.metric.context.row, size), shown, theme.color.text.primary);
const hx = x + u.width(.mono, size, shown) + p.row_gap;
const mid = y + theme.metric.context.row / 2;
sk.draw.polygon(u.f, &.{ .{ hx + p.row_head[0], mid }, .{ hx, mid - p.row_head[1] / 2 }, .{ hx, mid + p.row_head[1] / 2 } }, u.at.pen(.{ .fill = rgb(ink) }));
}
/// `pin` `under`: a stub 3 wide rising `size` + 10 to a node's bottom
/// edge at (`cx`, `y`), a head 10 by 10 on it, the first name left of
/// the stub and the others right, bold, primary.
pub fn pinUnder(u: Ui, cx: f32, y: f32, names: []const []const u8, ink: anytype, size: f32) void {
u.pinUnderInked(cx, y, names, &.{}, ink, size);
}
/// The same, each name in its own ink (`pinNames`).
pub fn pinUnderInked(u: Ui, cx: f32, y: f32, names: []const []const u8, inks: []const u24, ink: anytype, size: f32) void {
const p = theme.metric.pin;
const rise = size + 10;
// A keyline of page round the stub and its head: an arrow between
// nodes that runs under it is seen to pass behind, not to join it.
const key = theme.stroke.rule;
u.fill(.{ .x = cx - p.stub / 2 - key, .y = y + p.under_head[0] - 1, .w = p.stub + 2 * key, .h = rise - p.under_head[0] + 1 }, theme.color.page);
sk.draw.polygon(u.f, &.{ .{ cx, y }, .{ cx - p.under_head[1] / 2 - key - 1, y + 1 + p.under_head[0] + key }, .{ cx + p.under_head[1] / 2 + key + 1, y + 1 + p.under_head[0] + key } }, u.at.pen(.{ .fill = rgb(theme.color.page) }));
u.fill(.{ .x = cx - p.stub / 2, .y = y + p.under_head[0] - 1, .w = p.stub, .h = rise - p.under_head[0] + 1 }, ink);
sk.draw.polygon(u.f, &.{ .{ cx, y + 1 }, .{ cx - p.under_head[1] / 2, y + 1 + p.under_head[0] }, .{ cx + p.under_head[1] / 2, y + 1 + p.under_head[0] } }, u.at.pen(.{ .fill = rgb(ink) }));
if (names.len == 0) return;
u.pinNames(cx, y, names, inks, size);
}
/// The names of a `pin` `under`: the first left of the stub, the
/// second right of it ("" for a side kept free), the rest a count;
/// each bold in `inks` (a variable's: where it lives) or primary.
pub fn pinNames(u: Ui, cx: f32, y: f32, names: []const []const u8, inks: []const u24, size: f32) void {
const p = theme.metric.pin;
const by = y + 8 + size * 0.86;
const ink = struct {
fn of(all: []const u24, i: usize) u24 {
return if (i < all.len) all[i] else theme.color.text.primary;
}
}.of;
u.text(.mono_bold, size, cx - p.under_names - u.width(.mono, size, names[0]), by, names[0], ink(inks, 0));
// One name a side: a node's neighbour has its own. The rest are a count.
if (names.len > 1) {
u.text(.mono_bold, size, cx + p.under_names, by, names[1], ink(inks, 1));
if (names.len > 2) u.text(.mono_bold, size, cx + p.under_names + u.width(.mono, size, names[1]) + advance(size) / 2, by, u.print("+{d}", .{names.len - 2}), theme.color.text.primary);
}
}
/// `bracket`: a span marked at its edge, a stroke down `y0 .. y1` at
/// `x` and a serif at each end toward the span (right of the stroke,
/// or left with `side` -1). `open`: no bottom serif (a frame that
/// runs on past the panel).
pub fn bracket(u: Ui, x: f32, y0: f32, y1: f32, ink: anytype, kind: [2]f32, side: f32, open: bool) void {
const sw = kind[0];
const serif = kind[1];
u.fill(.{ .x = x - sw / 2, .y = y0, .w = sw, .h = y1 - y0 }, ink);
const sx = if (side > 0) x - sw / 2 else x + sw / 2 - serif - sw;
u.fill(.{ .x = sx, .y = y0, .w = serif + sw, .h = sw }, ink);
if (!open) u.fill(.{ .x = sx, .y = y1 - sw, .w = serif + sw, .h = sw }, ink);
}
/// The veil over what cannot be read at the pace, or is not the
/// subject: `amount` of it, 0 to 1, at `alpha` (`color.veil_alpha`).
pub fn veil(u: Ui, r: Rect, amount: f32) void {
if (amount > 0) u.fill(r, rgb(theme.color.veil).alphaFloat(theme.color.veil_alpha * std.math.clamp(amount, 0, 1)));
}
/// Draws only inside `r` until `unclip`: text scrolling in its panel.
pub fn clip(u: Ui, r: Rect) void {
const a = u.point(r.x, r.y);
const b = u.point(r.x + r.w, r.y + r.h);
u.f.clip(sk.Rect{ .x = @round(a[0]), .y = @round(a[1]), .w = @round(b[0]) - @round(a[0]), .h = @round(b[1]) - @round(a[1]) });
}
pub fn unclip(u: Ui) void {
u.f.clip(null);
}
/// A path through `points` with corners of radius `radius`, `progress`
/// of its length drawn from its start (a tie drawing head first, a node's
/// arrow growing to its target), dashed or whole, with a filled head at
/// its end or none. `from`: the share of its length left undrawn at its
/// start (a tie drawing back into its pin).
pub fn route(u: Ui, points: []const Vec, c: anytype, options: struct { width: f32 = theme.stroke.arrow, head: ?[2]f32 = theme.stroke.head, dashed: bool = false, progress: f32 = 1, from: f32 = 0, radius: f32 = theme.stroke.elbow_radius }) void {
if (points.len < 2 or options.progress <= 0) return;
var path: std.ArrayList(Vec) = .empty;
const gpa = u.f.scratch;
path.append(gpa, points[0]) catch return;
for (1..points.len - 1) |i| {
// The corner as a quadratic through its fillet's ends.
const a = points[i - 1];
const b = points[i];
const z = points[i + 1];
const r = @min(options.radius, @min(sk.math.distance(a, b), sk.math.distance(b, z)) / 2);
const from = b + sk.math.withLength(a - b, r);
const to = b + sk.math.withLength(z - b, r);
for (0..7) |k| {
const t = @as(f32, @floatFromInt(k)) / 6;
path.append(gpa, sk.math.mix(sk.math.mix(from, b, t), sk.math.mix(b, to, t), t)) catch return;
}
}
path.append(gpa, points[points.len - 1]) catch return;
// Cut to `progress` of its length, short of the head, and from `from` of it.
var total: f32 = 0;
for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| total += sk.math.distance(a, b);
const head_len: f32 = if (options.head) |h| h[0] else 0;
const tip_at = total * std.math.clamp(options.progress, 0, 1);
const keep = @max(tip_at - @max(head_len - 2, 0), 0);
const start_at = total * std.math.clamp(options.from, 0, 1);
var tip = path.items[0];
var dir: Vec = sk.math.normalize(path.items[1] - path.items[0]);
var body: std.ArrayList(Vec) = .empty;
var walked: f32 = 0;
for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| {
const len = sk.math.distance(a, b);
if (len == 0) continue;
// The part of this segment between `start_at` and `keep`.
const lo = std.math.clamp((start_at - walked) / len, 0, 1);
const hi = std.math.clamp((keep - walked) / len, 0, 1);
if (hi > lo) {
if (body.items.len == 0) body.append(gpa, sk.math.mix(a, b, lo)) catch return;
body.append(gpa, sk.math.mix(a, b, hi)) catch return;
}
if (walked <= tip_at) {
dir = sk.math.normalize(b - a);
tip = if (tip_at - walked >= len) b else sk.math.mix(a, b, (tip_at - walked) / len);
}
walked += len;
}
const pen = u.at.pen(.{ .stroke = rgb(c), .fill = rgb(c), .width = options.width, .join = .round, .cap = .butt });
var stroke = pen;
stroke.fill = null;
if (body.items.len > 1) {
if (options.dashed) sk.draw.dashed(u.f, body.items, theme.stroke.dash, stroke) else sk.draw.polyline(u.f, body.items, stroke);
}
if (options.head) |h| if (tip_at > start_at) {
const back = tip - sk.math.scale(dir, h[0]);
const side = sk.math.scale(sk.math.perp(dir), h[1] / 2);
var solid = pen;
solid.stroke = null;
sk.draw.polygon(u.f, &.{ tip, back + side, back - side }, solid);
};
}
/// A straight line between two points (a tree's edge).
pub fn line(u: Ui, a: Vec, b: Vec, c: anytype, w: f32) void {
sk.draw.line(u.f, a, b, u.at.pen(.{ .stroke = rgb(c), .width = w, .cap = .butt }));
}
pub fn disc(u: Ui, center: Vec, radius: f32, fill_color: ?Color, stroke: ?Color, w: f32) void {
sk.draw.circle(u.f, center, radius, u.at.pen(.{ .fill = fill_color, .stroke = stroke, .width = w }));
}
};
/// How a value is set, by its class.
pub const Value = struct {
buf: [20]u8 = undefined,
len: usize = 0,
prefix: []const u8 = "",
/// Its decimal follows, faint: a small number that may be either.
decimal: bool = false,
/// The class decides: an address (`pointer`) is hex as long as it is;
/// a digit is itself; a small number is hex with `0x` and its decimal;
/// a large one, hex with `0x`; within 4096 of 2^64, a small negative.
pub fn of(v: u64, pointer: bool) Value {
var shown: Value = .{};
const negative = !pointer and v > std.math.maxInt(u64) - 4096;
if (negative) {
shown.len = (std.fmt.bufPrint(&shown.buf, "-{d}", .{0 -% v}) catch unreachable).len;
} else if (!pointer and v < 10) {
shown.len = (std.fmt.bufPrint(&shown.buf, "{d}", .{v}) catch unreachable).len;
} else {
shown.len = (std.fmt.bufPrint(&shown.buf, "{x}", .{v}) catch unreachable).len;
shown.decimal = !pointer and v <= 0xffff;
if (!pointer) shown.prefix = "0x";
}
return shown;
}
pub fn digits(shown: *const Value) []const u8 {
return shown.buf[0..shown.len];
}
/// Units its digits take at mono 20 (the faint decimal apart).
pub fn width(shown: *const Value) f32 {
return @as(f32, @floatFromInt(shown.prefix.len + shown.len)) * advance(theme.size.mono.context);
}
};
/// The classes of `value` a type or a signature gives (`Ui.typed`); `fd`
/// carries what the descriptor is open on.
pub const Class = union(enum) { fd: []const u8, boolean, character };
/// A character as C writes it, quoted: `'A'`, `'\n'`, `'\x00'`.
pub fn quote(u: Ui, c: u8) []const u8 {
return switch (c) {
'\n' => "'\\n'",
'\t' => "'\\t'",
'\r' => "'\\r'",
0 => "'\\0'",
'\'' => "'\\''",
'\\' => "'\\\\'",
else => if (std.ascii.isPrint(c)) u.print("'{c}'", .{c}) else u.print("'\\x{x:0>2}'", .{c}),
};
}
/// `address`'s options.
pub const Address = struct {
ink: Color = rgb(theme.color.region.code_),
lead: usize = 0,
name: []const u8 = "",
rwx: bool = false,
face: Face = .mono,
};
/// The kinds of `arrow`: "which points to", "which holds", "and goes on".
pub const Arrow = enum { points, holds, more };
/// A word of a `tag`'s legend, in its ink.
pub const Legend = struct { text: []const u8, ink: Color, under: bool = false };
/// A code address's name as the catalogue writes it: `name+offset`, and
/// for an address with no symbol its module, `libc+27781` (the recording
/// says `libc.so.6!27781`).
pub fn symbol(u: Ui, name: []const u8) []const u8 {
const bang = std.mem.indexOfScalar(u8, name, '!') orelse return name;
const after = name[bang + 1 ..];
// A symbol of the module's: its own name is enough.
for (after) |ch| if (!std.ascii.isHex(ch)) return after;
const module = name[0..bang];
return u.print("{s}+{s}", .{ module[0 .. std.mem.indexOfScalar(u8, module, '.') orelse module.len], after });
}
/// Names joined by a space: three, then a count.
fn joined(u: Ui, names: []const []const u8) []const u8 {
var out: []const u8 = "";
for (names[0..@min(names.len, 3)], 0..) |n, i| out = if (i == 0) n else u.print("{s} {s}", .{ out, n });
if (names.len > 3) out = u.print("{s} +{d}", .{ out, names.len - 3 });
return out;
}
/// How hot a written thing is, from `cursor.Now.heat`.
pub const Heat = struct {
/// 0 no mark, up to 1 the mark in full (less while it leaves).
level: f32 = 0,
/// 1 solid, 0 cooled to the ember.
solid: f32 = 0,
/// 0 to 1 as the strike's fill comes in over what was there (`from`).
in: f32 = 1,
/// What it strikes over: the mark the steps before left.
from: enum { nothing, ember } = .nothing,
/// 0 to 1: armed, a frame round the cell before it is struck.
arm: f32 = 0,
/// The strike's pulse: how far out its edges are, in units.
out: f32 = 0,
/// 0 to 1: how much of `held`'s turned corner shows (of `level`): none
/// while struck, in over the cool's last 6 f; the old one's going as a
/// strike comes in over it.
corner: f32 = 1,
pub const none: Heat = .{};
pub const ember: Heat = .{ .level = 1 };
/// Struck, at rest: the paper solid, no corner yet.
pub const struck: Heat = .{ .level = 1, .solid = 1, .corner = 0 };
};
test "the camera zooms by ratio and ends where it was sent" {
const a: anim.Camera = .{};
const b = framing(.{ .x = 64, .y = 528, .w = 728, .h = 360 }, 2);
try std.testing.expectApproxEqAbs(@sqrt(2.0), between(a, b, 0.5).zoom, 1e-4);
try std.testing.expectApproxEqAbs(a.x, between(a, b, 0).x, 1e-3);
try std.testing.expectApproxEqAbs(b.x, between(a, b, 1).x, 1e-3);
try std.testing.expectApproxEqAbs(b.y, between(a, b, 1).y, 1e-3);
}
test "a value is set by its class" {
const expectEqualStrings = std.testing.expectEqualStrings;
try expectEqualStrings("4", Value.of(4, false).digits());
try std.testing.expect(Value.of(0x21, false).decimal and std.mem.eql(u8, Value.of(0x21, false).prefix, "0x"));
try expectEqualStrings("-1", Value.of(std.math.maxInt(u64), false).digits());
// An address: as long as it is, no `0x`, unbroken.
const address = Value.of(0x7ffe970f17b0, true);
try expectEqualStrings("7ffe970f17b0", address.digits());
try std.testing.expectEqual(@as(f32, 12 * 12), address.width());
try std.testing.expect(address.prefix.len == 0 and !address.decimal);
}
test "a column's addresses share their leading digits, leaving two" {
try std.testing.expectEqual(@as(usize, 5), shared(&.{ 0x1001910, 0x1001937 }));
try std.testing.expectEqual(@as(usize, 6), shared(&.{0x28b41070}));
try std.testing.expectEqual(@as(usize, 0), shared(&.{ 0x1001910, 0x7ffe0000 }));
try std.testing.expectEqual(@as(usize, 12), digits(0x7ffe970f17b0));
}
|