summaryrefslogtreecommitdiff
path: root/examples/live
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 07:58:27 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit4350e6840940da0b7af800faf5a1455a1de1908e (patch)
tree29f0109de63fbee2a8146c620c0f5e83065ea8d1 /examples/live
parent610ffe373f0f5dd5ccb59bf575f5479ad1ecdbaf (diff)
downloadsketchbook-4350e6840940da0b7af800faf5a1455a1de1908e.tar.gz
sketchbook-4350e6840940da0b7af800faf5a1455a1de1908e.zip
examples/live: drag a number, the program following as it moves
The left button pressed on a number token (the grammar's `number` node at the byte under the pointer, found with the module's parse) starts a drag kept in State: the literal's bytes, the value, its precision. Each pointer move rewrites the literal through the editor's `replace`: a step for every 4 pixels right of where it was pressed (left, down), the step from the literal's own precision (`220` by ones, `0.25` by hundredths), printed with as many decimals. The first rewrite is a step to undo and the rest join it, so one `u` takes the whole drag back. The text changes, so the program is compiled and heard as it moves. A literal with an exponent is not dragged (marked ponytail). `update` finds the byte under the pointer as `code` lays the text out: a character is 10 by 22 pixels, measured once (the code face at 1.25, natively: ten M are 100) and now a constant both use, where `code` asked the host each frame. Test: test/live.script presses 220, drags 40 pixels right (230), back to 12 left of the press (217), lets go and undoes the drag in one step (220). zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/live')
-rw-r--r--examples/live/src/main.zig71
-rw-r--r--examples/live/test/live.script13
2 files changed, 82 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;
diff --git a/examples/live/test/live.script b/examples/live/test/live.script
index a06ed97..bedcf83 100644
--- a/examples/live/test/live.script
+++ b/examples/live/test/live.script
@@ -81,3 +81,16 @@ expect probe.len 3
expect probe.code[2].op mul
key escape
expect probe.len 0
+# Drag a number: 220 (line 1, columns 16..18; a column 10 pixels from 16,
+# a line 22 from 16) pressed and dragged 40 pixels right is 230, then 12
+# left of where it was pressed is 217; the program follows; the drag is one
+# step to undo.
+down 181 27
+move 201 27
+move 221 27
+expect out.code[1].a 1130758144
+move 169 27
+expect out.code[1].a 1129906176
+up 169 27
+type u
+expect out.code[1].a 1130102784