summaryrefslogtreecommitdiff
path: root/examples/live/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/live/src')
-rw-r--r--examples/live/src/main.zig122
1 files changed, 91 insertions, 31 deletions
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig
index 965152a..e62522f 100644
--- a/examples/live/src/main.zig
+++ b/examples/live/src/main.zig
@@ -18,6 +18,7 @@ const gpu = sk.abi.gpu;
const Lang = @import("Lang");
const shaders = @import("shaders");
const scope = @import("scope");
+const pe = @import("pardes-editor");
pub const name = "live";
pub const width = 1280;
@@ -31,11 +32,15 @@ const stream = 1;
/// The most samples synthesised in one frame.
const max_block = 8192;
+/// The text: pardes's editor, helix's keys over a value of plain data
+/// (src/lib_editor.zig in pardes), its capacities sized to the language's
+/// 4 KiB of text. The undo ring keeps whole texts, the oldest dropped: four
+/// at the most text, hundreds of a short program. 39,264 bytes in all, of a
+/// State of 43,480.
+pub const Editor = pe.Editor(.{ .text = 4096, .undo = 16 * 1024, .registers = 1024, .selections = 64 });
+
pub const State = struct {
- // ponytail: a plain buffer, typed characters appended and backspace
- // deleting the last; part 2 puts pardes's editor here.
- text: [4096]u8 = @splat(0),
- len: u32 = 0,
+ ed: Editor = .{},
/// The last program that parsed.
out: Lang.Tape = .{},
color: Lang.Tape = .{},
@@ -72,35 +77,45 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool {
return true;
}
+/// What the editor thaws its value into for each call, reset after it:
+/// `update` may not allocate from the host, and a fixed buffer keeps a
+/// call deterministic and bounded. A key at the most text took 29 KiB of it.
+var scratch: [128 * 1024]u8 = undefined;
+
pub fn update(s: *State, in: *const sk.Input) void {
- var edited = false;
- for (in.events) |e| switch (e.kind) {
- .tick => s.time += e.x,
- .text => {
- var utf8: [4]u8 = undefined;
- const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue;
- if (s.len + n > s.text.len) continue;
- @memcpy(s.text[s.len..][0..n], utf8[0..n]);
- s.len += n;
- edited = true;
- },
- .key_down => switch (e.key()) {
- .backspace => if (s.len > 0) {
- s.len -= 1;
- while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1;
- edited = true;
+ const revision = s.ed.revision;
+ // a chord's key_down may come with its letter as text too: not typed
+ var chord = false;
+ for (in.events) |e| {
+ var utf8: [4]u8 = undefined;
+ const k: pe.Key = switch (e.kind) {
+ .tick => {
+ s.time += e.x;
+ continue;
},
- .@"return", .keypad_enter => if (s.len < s.text.len) {
- s.text[s.len] = '\n';
- s.len += 1;
- edited = true;
+ .text => text: {
+ const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue;
+ if (chord) {
+ chord = false;
+ continue;
+ }
+ break :text .{ .cp = @intCast(@min(e.value, 0x10ffff)), .text = utf8[0..n] };
},
- else => {},
- },
- else => {},
- };
- if (!edited) return;
- switch (Lang.compile(s.text[0..s.len])) {
+ .key_down => chord: {
+ const k = key(e) orelse continue;
+ chord = (k.ctrl or k.alt) and k.cp >= ' ' and k.cp < 0x7f;
+ break :chord k;
+ },
+ else => continue,
+ };
+ var fba: std.heap.FixedBufferAllocator = .init(&scratch);
+ // ponytail: a key the text has no room for is dropped, as part 1's
+ // buffer dropped it; the editor leaves the value as it was.
+ _ = s.ed.key(fba.allocator(), null, k);
+ }
+ if (s.ed.revision == revision) return;
+ var text: [Editor.capacity.text]u8 = undefined;
+ switch (Lang.compile(s.ed.read(&text))) {
.program => |p| {
s.out = p.out;
s.color = p.color;
@@ -110,6 +125,51 @@ pub fn update(s: *State, in: *const sk.Input) void {
}
}
+/// A key_down as the editor takes it: the specials, and chords (ctrl or alt
+/// with a letter, digit or sign), helix's `A-I` an upper-case letter with
+/// alt. A printable without a chord comes as `text`, so it is not one here.
+fn key(e: sk.Event) ?pe.Key {
+ const mods = e.value;
+ const ctrl = mods & sk.Key.Mod.ctrl != 0;
+ const alt = mods & sk.Key.Mod.alt != 0;
+ const shift = mods & sk.Key.Mod.shift != 0;
+ const special: u21 = switch (e.key()) {
+ .escape => pe.Key.escape,
+ .@"return", .keypad_enter, .return2 => pe.Key.enter,
+ .backspace => pe.Key.backspace,
+ .tab => pe.Key.tab,
+ .delete => pe.Key.delete,
+ .left => pe.Key.left,
+ .right => pe.Key.right,
+ .up => pe.Key.up,
+ .down => pe.Key.down,
+ .home => pe.Key.home,
+ .end => pe.Key.end,
+ .page_up => pe.Key.page_up,
+ .page_down => pe.Key.page_down,
+ else => 0,
+ };
+ if (special != 0) return .{ .cp = special, .ctrl = ctrl, .alt = alt, .shift = shift };
+ if (!ctrl and !alt) return null;
+ const scan = @backingInt(e.key());
+ const cp: u21 = switch (e.key()) {
+ .a, .b, .c, .d, .e, .f, .g, .h, .i, .j, .k, .l, .m, .n, .o, .p, .q, .r, .s, .t, .u, .v, .w, .x, .y, .z => @as(u21, if (shift) 'A' else 'a') + scan - @backingInt(sk.Key.a),
+ .@"1", .@"2", .@"3", .@"4", .@"5", .@"6", .@"7", .@"8", .@"9" => '1' + @as(u21, scan - @backingInt(sk.Key.@"1")),
+ .@"0" => '0',
+ .space => ' ',
+ .minus => '-',
+ .equals => '=',
+ .left_bracket => '[',
+ .right_bracket => ']',
+ .semicolon => ';',
+ .comma => ',',
+ .period => '.',
+ .slash => '/',
+ else => return null,
+ };
+ return .{ .cp = cp, .ctrl = ctrl, .alt = alt };
+}
+
const synth: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 };
const picture_pipeline: gpu.Pipeline = .{
.vertex = .{ .code = shaders.fullscreen_vert },
@@ -220,7 +280,7 @@ const red = 0xff5a50;
// 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.
fn code(s: *const State, f: *sk.Frame) void {
- const text = s.text[0..s.len];
+ const text = s.ed.bytes(f.scratch) catch return;
const cell = f.host.textSizeIn(.code, scale, "M");
const lines = std.mem.count(u8, text, "\n") + 1;
var widest: f32 = 0;