diff options
Diffstat (limited to 'anim/src/ui.zig')
| -rw-r--r-- | anim/src/ui.zig | 117 |
1 files changed, 85 insertions, 32 deletions
diff --git a/anim/src/ui.zig b/anim/src/ui.zig index 6120bbb..cf12914 100644 --- a/anim/src/ui.zig +++ b/anim/src/ui.zig @@ -16,6 +16,19 @@ 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`). @@ -145,8 +158,9 @@ pub const Ui = struct { 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`. - pub fn frame(u: Ui, r: Rect, c: anytype, w: f32) void { + /// 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); @@ -164,6 +178,11 @@ pub const Ui = struct { /// 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; @@ -206,7 +225,7 @@ pub const Ui = struct { /// 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 (u.f.host.env("CANIM_PANELS") == null) return; + 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); @@ -224,6 +243,12 @@ pub const Ui = struct { /// 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); @@ -319,6 +344,7 @@ pub const Ui = struct { /// 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) }); @@ -340,53 +366,72 @@ pub const Ui = struct { /// `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` - /// (solid vermilion behind it, 5 wider each side, its edges pulsing out - /// 3; the digits bold, in ink: the ink this returns), then cooled to - /// nothing, and `held`: a tick 4 wide at the value's left for three - /// steps. 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; - /// the tick is `tick` units left of them (5 after a chain's arrow). - pub fn written(u: Ui, r: Rect, h: Heat, tick: f32) ?Ink { + /// (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; - // The tick is what a strike cools to: not under it while it is solid. - const held = h.level * (1 - (if (h.level >= 1) h.solid * h.in else 0)); - if (held > 0) u.fill(.{ .x = r.x - tick, .y = r.y + 3, .w = m.tick, .h = r.h - 6 }, mix(theme.color.page, w.solid, held)); + 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) return null; - 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, w.solid, solid)); - return if (solid >= 0.5) Ink.all(w.on_solid, .mono_bold) else null; + 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 (a solid - /// fill coming in, its edges out 4 and back), cooled to its ember and - /// a vermilion bar 4 high along its bottom edge (`held`), `ground` - /// (null: nothing) once that has gone. Returns what to set its digits in. + /// `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 w = theme.mark(.write); 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 bar stays. - var body = if (h.level < 1) under else mix(under, w.solid, h.solid); + // 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); - const bar = theme.metric.ember_bar; - if (h.level > 0 and (h.in >= 1 or h.from == .ember)) u.fill(.{ .x = r.x, .y = r.y + r.h - bar, .w = r.w, .h = bar }, mix(under, w.solid, @min(h.level, 1))); - if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, theme.stroke.line); - // Bold ink on the solid. The face changes at the middle of the - // cool, where the colour is moving fastest and hides it. + 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 = .{}; - return .{ .ink = mix(cooled.ink, w.on_solid, heat), .lead = mix(cooled.lead, w.on_solid, heat), .face = if (heat >= 0.5) .mono_bold else .mono }; + 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. @@ -394,10 +439,12 @@ pub const Ui = struct { 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: gold ember across `r`, and a gold plaque `w` - /// wide at its left, its edges out `out` units (the fire). + /// `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); } @@ -683,9 +730,15 @@ pub const Heat = struct { 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" { |
