summaryrefslogtreecommitdiff
path: root/examples/live/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/live/src')
-rw-r--r--examples/live/src/main.zig62
1 files changed, 43 insertions, 19 deletions
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig
index e62522f..ca4d0be 100644
--- a/examples/live/src/main.zig
+++ b/examples/live/src/main.zig
@@ -274,36 +274,56 @@ const scale = 1.25;
const margin = 16;
const ink = 0xe8e8e8;
const red = 0xff5a50;
+const selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 170 };
+const also_selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 90 };
-/// The text over the picture on a dark band, the caret at its end, and the
-/// problem: a red mark under its byte and its words under the text.
-// ponytail: one face at one size, so a column is a fixed advance; part 2
-// draws the editor's text with sketchbook's new text instead.
+/// The editor's text over the picture on a dark band: its lines, every
+/// range (the primary stronger), a caret at each range's head that says
+/// the mode (a block in normal and select, a bar in insert), the mode's
+/// name in the corner; the problem as a red mark under its byte and its
+/// words under the text.
+// ponytail: one face at one size, a column a fixed advance and a byte of
+// this ASCII language a column; sketchbook's paragraph call (spans, any
+// byte's place) takes the place of the measuring and the textIn here.
fn code(s: *const State, f: *sk.Frame) void {
const text = s.ed.bytes(f.scratch) catch return;
+ const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return;
+ const got = s.ed.ranges(f.scratch, ranges) catch return;
const cell = f.host.textSizeIn(.code, scale, "M");
- const lines = std.mem.count(u8, text, "\n") + 1;
- var widest: f32 = 0;
- var it = std.mem.splitScalar(u8, text, '\n');
- while (it.next()) |line| widest = @max(widest, f.host.textSizeIn(.code, scale, line)[0]);
const problem = s.problem.len > 0;
- if (problem) widest = @max(widest, f.host.textSizeIn(.code, scale, s.problem.text())[0]);
- const band_h = @as(f32, @floatFromInt(lines + @intFromBool(problem))) * cell[1] + 2 * margin;
- f.fill(.{ .x = 0, .y = 0, .w = @max(widest, 24 * cell[0]) + 2 * margin + cell[0], .h = band_h }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 });
+ var lines: usize = 1;
+ var widest: usize = 24;
+ var it = std.mem.splitScalar(u8, text, '\n');
+ while (it.next()) |line| : (lines += 1) widest = @max(widest, line.len + 1);
+ if (problem) widest = @max(widest, s.problem.len);
+ f.fill(.{ .x = 0, .y = 0, .w = @as(f32, @floatFromInt(widest)) * cell[0] + 2 * margin, .h = @as(f32, @floatFromInt(lines - 1 + @intFromBool(problem))) * cell[1] + 2 * margin }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 });
var y: f32 = margin;
var start: usize = 0;
it = std.mem.splitScalar(u8, text, '\n');
while (it.next()) |line| : (y += cell[1]) {
+ // a line's bytes and its newline, start..end
+ const end = start + line.len + 1;
+ for (ranges[0..got.n], 0..) |r, i| {
+ const from = @max(@min(r.anchor, r.head), start);
+ const to = @min(@max(r.anchor, r.head), end);
+ if (from < to) f.fill(.{ .x = margin + @as(f32, @floatFromInt(from - start)) * cell[0], .y = y, .w = @as(f32, @floatFromInt(to - from)) * cell[0], .h = cell[1] }, if (i == got.primary) selected else also_selected);
+ }
f.textIn(.code, margin, y, scale, line, ink);
- const end = start + line.len;
- if (problem and s.problem.at >= start and s.problem.at <= end) {
- const x = margin + f.host.textSizeIn(.code, scale, text[start..s.problem.at])[0];
- f.fill(.{ .x = x, .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red);
+ for (ranges[0..got.n], 0..) |r, i| {
+ // the head's character: the one before it in a range that
+ // grows forward
+ const at = if (r.head > r.anchor) r.head - 1 else r.head;
+ if (at < start or at >= end) continue;
+ const x = margin + @as(f32, @floatFromInt(at - start)) * cell[0];
+ const caret: sk.Color = .{ .r = 0xe8, .g = 0xe8, .b = 0xe8, .a = if (i == got.primary) 200 else 110 };
+ if (s.ed.mode() == .insert) {
+ f.fill(.{ .x = x - 1, .y = y, .w = 2, .h = cell[1] }, caret);
+ } else f.fill(.{ .x = x, .y = y, .w = cell[0], .h = cell[1] }, caret.alpha(caret.a / 2));
}
- // The caret, after the last byte.
- if (end == text.len) f.fill(.{ .x = margin + f.host.textSizeIn(.code, scale, line)[0], .y = y, .w = 2, .h = cell[1] }, ink);
- start = end + 1;
+ if (problem and s.problem.at >= start and s.problem.at < end)
+ f.fill(.{ .x = margin + @as(f32, @floatFromInt(s.problem.at - start)) * cell[0], .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red);
+ start = end;
}
if (problem) f.textIn(.code, margin, y, scale, s.problem.text(), red);
@@ -314,5 +334,9 @@ fn code(s: *const State, f: *sk.Frame) void {
.paused => "paused",
};
const lead = @as(f32, @floatFromInt(f.in.lead)) / @as(f32, @floatFromInt(f.in.sample_rate)) * 1000;
- f.print(width - 260, 12, 1, ink, "{s} {d:.1} s lead {d:.1} ms", .{ mode, s.time, lead });
+ f.print(width - 300, 12, 1, ink, "{s} {s} {d:.1} s lead {d:.1} ms", .{ switch (s.ed.mode()) {
+ .normal => "NOR",
+ .insert => "INS",
+ .select => "SEL",
+ }, mode, s.time, lead });
}