win32 gui: mouse drag

initial implementation for left mouse drag
This commit is contained in:
Jonathan Marler 2025-01-05 22:50:37 -07:00 committed by CJ van den Berg
parent 22ddaef78f
commit 21a7106fe3
2 changed files with 68 additions and 13 deletions

View file

@ -274,6 +274,39 @@ pub fn process_renderer_event(self: *Self, msg: []const u8) !void {
return;
}
}
{
var args: struct {
pos: MousePos,
button_id: u8,
} = undefined;
if (try cbor.match(msg, .{
cbor.any,
"D",
cbor.extract(&args.button_id),
cbor.extract(&args.pos.col),
cbor.extract(&args.pos.row),
cbor.extract(&args.pos.xoffset),
cbor.extract(&args.pos.yoffset),
})) {
var buf: [200]u8 = undefined;
if (self.dispatch_mouse_drag) |f| f(
self.handler_ctx,
@intCast(args.pos.row),
@intCast(args.pos.col),
fmtmsg(&buf, .{
"D",
input.event.press,
args.button_id,
input.utils.button_id_string(@enumFromInt(args.button_id)),
args.pos.col,
args.pos.row,
args.pos.xoffset,
args.pos.yoffset,
}),
);
return;
}
}
return error.UnexpectedRendererEvent;
}

View file

@ -588,6 +588,16 @@ pub fn fmtmsg(buf: []u8, value: anytype) []const u8 {
return buf[0..fbs.pos];
}
const MouseFlags = packed struct(u8) {
left_down: bool,
right_down: bool,
shift_down: bool,
control_down: bool,
middle_down: bool,
xbutton1_down: bool,
xbutton2_down: bool,
_: bool,
};
fn sendMouse(
hwnd: win32.HWND,
kind: enum {
@ -597,6 +607,7 @@ fn sendMouse(
right_down,
right_up,
},
wparam: win32.WPARAM,
lparam: win32.LPARAM,
) void {
const point = ddui.pointFromLparam(lparam);
@ -607,14 +618,25 @@ fn sendMouse(
};
const cell = CellPos.init(cell_size, point.x, point.y);
switch (kind) {
.move => state.pid.send(.{
"RDR",
"M",
cell.cell.x,
cell.cell.y,
cell.offset.x,
cell.offset.y,
}) catch |e| onexit(e),
.move => {
const flags: MouseFlags = @bitCast(@as(u8, @intCast(0xff & wparam)));
if (flags.left_down) state.pid.send(.{
"RDR",
"D",
@intFromEnum(input.mouse.BUTTON1),
cell.cell.x,
cell.cell.y,
cell.offset.x,
cell.offset.y,
}) catch |e| onexit(e) else state.pid.send(.{
"RDR",
"M",
cell.cell.x,
cell.cell.y,
cell.offset.x,
cell.offset.y,
}) catch |e| onexit(e);
},
else => |b| state.pid.send(.{
"RDR",
"B",
@ -953,23 +975,23 @@ fn WndProc(
switch (msg) {
win32.WM_MOUSEMOVE => {
sendMouse(hwnd, .move, lparam);
sendMouse(hwnd, .move, wparam, lparam);
return 0;
},
win32.WM_LBUTTONDOWN => {
sendMouse(hwnd, .left_down, lparam);
sendMouse(hwnd, .left_down, wparam, lparam);
return 0;
},
win32.WM_LBUTTONUP => {
sendMouse(hwnd, .left_up, lparam);
sendMouse(hwnd, .left_up, wparam, lparam);
return 0;
},
win32.WM_RBUTTONDOWN => {
sendMouse(hwnd, .right_down, lparam);
sendMouse(hwnd, .right_down, wparam, lparam);
return 0;
},
win32.WM_RBUTTONUP => {
sendMouse(hwnd, .right_up, lparam);
sendMouse(hwnd, .right_up, wparam, lparam);
return 0;
},
win32.WM_MOUSEWHEEL => {