mach: add mouse motion and mouse button events (#325)

* add mouse motion and mouse button events
* add scroll events
* switch from button/action to mouse_press and mouse_release events
This commit is contained in:
David Vanderson 2022-06-04 09:29:23 -04:00 committed by GitHub
parent 7b395759b7
commit a2a6c2a288
Failed to generate hash of commit
4 changed files with 89 additions and 0 deletions

View file

@ -20,6 +20,17 @@ pub const VSyncMode = enum {
triple,
};
pub const MouseButton = enum {
left,
right,
middle,
four,
five,
six,
seven,
eight,
};
pub const Key = enum {
a,
b,

View file

@ -216,6 +216,55 @@ pub const Platform = struct {
}.callback;
platform.window.setKeyCallback(callback);
const mouse_motion_callback = struct {
fn callback(window: glfw.Window, xpos: f64, ypos: f64) void {
const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform;
pf.pushEvent(.{
.mouse_motion = .{
.x = xpos,
.y = ypos,
},
});
}
}.callback;
platform.window.setCursorPosCallback(mouse_motion_callback);
const mouse_button_callback = struct {
fn callback(window: glfw.Window, button: glfw.mouse_button.MouseButton, action: glfw.Action, mods: glfw.Mods) void {
const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform;
switch (action) {
.press => pf.pushEvent(.{
.mouse_press = .{
.button = toMachButton(button),
},
}),
.release => pf.pushEvent(.{
.mouse_release = .{
.button = toMachButton(button),
},
}),
else => {},
}
_ = mods;
}
}.callback;
platform.window.setMouseButtonCallback(mouse_button_callback);
const scroll_callback = struct {
fn callback(window: glfw.Window, xoffset: f64, yoffset: f64) void {
const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform;
pf.pushEvent(.{
.scroll = .{
.xoffset = xoffset,
.yoffset = yoffset,
},
});
}
}.callback;
platform.window.setScrollCallback(scroll_callback);
const size_callback = struct {
fn callback(window: glfw.Window, width: i32, height: i32) void {
const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform;
@ -264,6 +313,19 @@ pub const Platform = struct {
return null;
}
fn toMachButton(button: glfw.mouse_button.MouseButton) enums.MouseButton {
return switch (button) {
.left => .left,
.right => .right,
.middle => .middle,
.four => .four,
.five => .five,
.six => .six,
.seven => .seven,
.eight => .eight,
};
}
fn toMachKey(key: glfw.Key) enums.Key {
return switch (key) {
.a => .a,

View file

@ -51,4 +51,19 @@ pub const Event = union(enum) {
key_release: struct {
key: enums.Key,
},
mouse_motion: struct {
// These are in window coordinates (not framebuffer coords)
x: f64,
y: f64,
},
mouse_press: struct {
button: enums.MouseButton,
},
mouse_release: struct {
button: enums.MouseButton,
},
scroll: struct {
xoffset: f64,
yoffset: f64,
},
};