feat: enable pixel mouse coordinates in libvaxis

This commit is contained in:
CJ van den Berg 2024-05-21 23:45:39 +02:00
parent 453fb12055
commit 36f167221e

View file

@ -76,7 +76,7 @@ pub fn run(self: *Self) !void {
const ws = try vaxis.Tty.getWinsize(self.input_fd_blocking()); const ws = try vaxis.Tty.getWinsize(self.input_fd_blocking());
try self.vx.resize(self.a, ws); try self.vx.resize(self.a, ws);
self.vx.queueRefresh(); self.vx.queueRefresh();
try self.vx.setMouseMode(true); try self.vx.setMouseMode(.pixels);
} }
pub fn render(self: *Self) !void { pub fn render(self: *Self) !void {
@ -158,44 +158,52 @@ pub fn process_input(self: *Self, input_: []const u8) !void {
if (self.dispatch_input) |f| f(self.handler_ctx, cbor_msg); if (self.dispatch_input) |f| f(self.handler_ctx, cbor_msg);
}, },
.mouse => |mouse| { .mouse => |mouse| {
const ypos = mouse.row - 1;
const xpos = mouse.col - 1;
const ycell = self.vx.screen.height_pix / self.vx.screen.height;
const xcell = self.vx.screen.width_pix / self.vx.screen.width;
const y = ypos / ycell;
const x = xpos / xcell;
const ypx = ypos % ycell;
const xpx = xpos % xcell;
if (self.dispatch_mouse) |f| switch (mouse.type) { if (self.dispatch_mouse) |f| switch (mouse.type) {
.motion => f(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), try self.fmtmsg(.{ .motion => f(self.handler_ctx, @intCast(y), @intCast(x), try self.fmtmsg(.{
"M", "M",
mouse.col, x,
mouse.row, y,
0, xpx,
0, ypx,
})), })),
.press => f(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), try self.fmtmsg(.{ .press => f(self.handler_ctx, @intCast(y), @intCast(x), try self.fmtmsg(.{
"B", "B",
event_type.PRESS, event_type.PRESS,
@intFromEnum(mouse.button), @intFromEnum(mouse.button),
input.utils.button_id_string(@intFromEnum(mouse.button)), input.utils.button_id_string(@intFromEnum(mouse.button)),
mouse.col, x,
mouse.row, y,
0, xpx,
0, ypx,
})), })),
.release => f(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), try self.fmtmsg(.{ .release => f(self.handler_ctx, @intCast(y), @intCast(x), try self.fmtmsg(.{
"B", "B",
event_type.RELEASE, event_type.RELEASE,
@intFromEnum(mouse.button), @intFromEnum(mouse.button),
input.utils.button_id_string(@intFromEnum(mouse.button)), input.utils.button_id_string(@intFromEnum(mouse.button)),
mouse.col, x,
mouse.row, y,
0, xpx,
0, ypx,
})), })),
.drag => if (self.dispatch_mouse_drag) |f_| .drag => if (self.dispatch_mouse_drag) |f_|
f_(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), true, try self.fmtmsg(.{ f_(self.handler_ctx, @intCast(y), @intCast(x), true, try self.fmtmsg(.{
"D", "D",
event_type.PRESS, event_type.PRESS,
@intFromEnum(mouse.button), @intFromEnum(mouse.button),
input.utils.button_id_string(@intFromEnum(mouse.button)), input.utils.button_id_string(@intFromEnum(mouse.button)),
mouse.col, x,
mouse.row, y,
0, xpx,
0, ypx,
})), })),
}; };
}, },