diff options
Diffstat (limited to 'examples/live/src')
| -rw-r--r-- | examples/live/src/main.zig | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index cc11dbe..d7a514f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -48,6 +48,8 @@ pub const State = struct { /// expression the primary range is on, heard and seen (grey) instead of /// out and color until the next key; empty otherwise. probe: Lang.Tape = .{}, + /// A number the pointer was pressed on and drags. + drag: Drag = .{}, /// What is wrong with the text, if it does not parse (`len` 0: nothing). problem: Lang.Problem = .{}, /// Seconds, from recorded ticks: a replay draws the same frames. @@ -57,6 +59,21 @@ pub const State = struct { crossfade: bool = true, }; +/// Drag a number: its literal's bytes, at..at + len, rewritten as the +/// pointer moves, a step for every 4 pixels to the right (left, down) from +/// where it was pressed: its own precision, `220` by ones, `0.25` by +/// hundredths. The drag is one step to undo. +pub const Drag = struct { + active: bool = false, + /// Rewritten once: the next frames join its step to undo. + moved: bool = false, + at: u32 = 0, + len: u32 = 0, + x: f32 = 0, + value: f64 = 0, + decimals: u8 = 0, +}; + pub const Session = struct { picture: sk.Image = .none, size: [2]u32 = .{ 0, 0 }, @@ -107,12 +124,59 @@ pub fn update(s: *State, in: *const sk.Input) void { // a chord's key_down may come with its letter as text too: not typed var chord = false; for (in.events) |e| { + var fba: std.heap.FixedBufferAllocator = .init(&scratch); var utf8: [4]u8 = undefined; const k: pe.Key = switch (e.kind) { .tick => { s.time += e.x; continue; }, + .mouse_down => { + // the number token under the pointer, by the grammar's parse + if (e.code != 1 or e.x < margin or e.y < margin) continue; + var text: [Editor.capacity.text]u8 = undefined; + const t = s.ed.read(&text); + const row: usize = @intFromFloat((e.y - margin) / cell[1]); + const col: usize = @intFromFloat((e.x - margin) / cell[0]); + var start: usize = 0; + for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len) + 1; + if (start > t.len) continue; + const end = std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len; + if (start + col >= end) continue; + const tree = syn.parse(fba.allocator(), &s.ed, language()) orelse continue; + const n = tree.rootNode().descendantForByteRange(@intCast(start + col), @intCast(start + col + 1)) orelse continue; + if (!std.mem.eql(u8, n.kind(), "number")) continue; + const literal = t[n.startByte()..n.endByte()]; + // ponytail: a number with an exponent is not dragged + if (std.mem.indexOfScalar(u8, literal, 'e') != null) continue; + const point = std.mem.indexOfScalar(u8, literal, '.'); + s.drag = .{ + .active = true, + .at = n.startByte(), + .len = @intCast(literal.len), + .x = e.x, + .value = std.fmt.parseFloat(f64, literal) catch continue, + .decimals = @intCast(if (point) |p| literal.len - p - 1 else 0), + }; + continue; + }, + .mouse_move => { + if (!s.drag.active) continue; + const d = &s.drag; + const steps = @round((e.x - d.x) / 4); + var buf: [32]u8 = undefined; + const num = std.fmt.bufPrint(&buf, "{[0]d:.[1]}", .{ d.value + steps * std.math.pow(f64, 10, -@as(f64, @floatFromInt(d.decimals))), d.decimals }) catch continue; + var text: [Editor.capacity.text]u8 = undefined; + if (std.mem.eql(u8, s.ed.read(&text)[d.at..][0..d.len], num)) continue; + if (s.ed.replace(fba.allocator(), d.at, d.at + d.len, num, d.moved) != .done) continue; + d.len = @intCast(num.len); + d.moved = true; + continue; + }, + .mouse_up, .cancel => { + s.drag.active = false; + continue; + }, .text => text: { const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; if (chord) { @@ -128,7 +192,6 @@ pub fn update(s: *State, in: *const sk.Input) void { }, 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. const outcome = s.ed.key(fba.allocator(), &syn, k); @@ -306,6 +369,11 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { } const scale = 1.25; +/// A character of the code face at `scale`, wide and high, in pixels: +/// measured (`f.host.textSizeIn(.code, 1.25, "M")` natively, and ten of +/// them are 100), so `update` finds the byte under the pointer as `code` +/// lays it out. +const cell: [2]f32 = .{ 10, 22 }; const margin = 16; const ink = 0xe8e8e8; const red = 0xff5a50; @@ -329,7 +397,6 @@ fn code(s: *const State, f: *sk.Frame) void { const got = s.ed.ranges(f.scratch, ranges) catch return; // a class a byte, from the grammar's highlights (none if it fails) const colors = pe.Syntax.colors(f.scratch, &s.ed, language(), 0, text.len) catch &.{}; - const cell = f.host.textSizeIn(.code, scale, "M"); const problem = s.problem.len > 0; var lines: usize = 1; var widest: usize = 24; |
