diff options
Diffstat (limited to 'src/host/script.zig')
| -rw-r--r-- | src/host/script.zig | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/host/script.zig b/src/host/script.zig index 38954f3..cf551cd 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -4,7 +4,7 @@ //! # comment //! click 710 320 move there, press and release the left button //! move|down|up X Y one pointer event, then a frame -//! wheel X Y DELTA +//! wheel|wheel-x X Y DELTA //! cancel what losing focus sends //! key NAME press and release a key (`sketch.Key` names) //! type TEXT text input, as typed into a focused field @@ -56,23 +56,26 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons const eq = std.mem.eql; if (eq(u8, command, "click")) { const x, const y = try point(words); - try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y }); - try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1 }); - try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1 }); + try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y, .flags = held(host) }); + try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); + try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); } else if (eq(u8, command, "move") or eq(u8, command, "down") or eq(u8, command, "up")) { const x, const y = try point(words); const kind: abi.Event.Kind = if (eq(u8, command, "move")) .mouse_move else if (eq(u8, command, "down")) .mouse_down else .mouse_up; - try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1 }); - } else if (eq(u8, command, "wheel")) { + try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); + } else if (eq(u8, command, "wheel") or eq(u8, command, "wheel-x")) { const x, const y = try point(words); const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); - try host.route(.{ .kind = .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta) }); + try host.route(.{ .kind = if (eq(u8, command, "wheel-x")) .wheel_horizontal else .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); } else if (eq(u8, command, "cancel")) { try host.route(.{ .kind = .cancel, .time = now.* }); } else if (eq(u8, command, "key")) { const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = .key_down, .time = now.*, .code = @intFromEnum(key) }); - try host.route(.{ .kind = .key_up, .time = now.*, .code = @intFromEnum(key) }); + try host.route(.{ .kind = .key_down, .time = now.*, .code = @backingInt(key) }); + try host.route(.{ .kind = .key_up, .time = now.*, .code = @backingInt(key) }); + } else if (eq(u8, command, "hold") or eq(u8, command, "release")) { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + try host.route(.{ .kind = if (eq(u8, command, "hold")) .key_down else .key_up, .time = now.*, .code = @backingInt(key) }); } else if (eq(u8, command, "type")) { for (rest) |byte| try host.route(.{ .kind = .text, .time = now.*, .value = byte }); } else if (eq(u8, command, "console")) { @@ -109,7 +112,7 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons return; } else if (eq(u8, command, "screenshot")) { if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); - host.screenshot = try arena.dupeZ(u8, rest); + host.screenshot = try arena.dupeSentinel(u8, rest, 0); } else if (eq(u8, command, "load")) { try host.load(rest); } else if (eq(u8, command, "reload")) { @@ -131,6 +134,16 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons try advance(host, now); } +/// The modifiers `hold` has down, for a pointer event's `flags`. +fn held(host: *const Host) u8 { + var mods: u32 = 0; + if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) mods |= abi.Key.Mod.shift; + if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) mods |= abi.Key.Mod.ctrl; + if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) mods |= abi.Key.Mod.alt; + if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) mods |= abi.Key.Mod.gui; + return abi.Event.withModifiers(mods); +} + fn advance(host: *Host, now: *u64) !void { try host.frame(now.*); now.* += frame_ns; |
