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:
parent
7b395759b7
commit
a2a6c2a288
4 changed files with 89 additions and 0 deletions
|
|
@ -88,6 +88,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool {
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
},
|
},
|
||||||
|
else => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,17 @@ pub const VSyncMode = enum {
|
||||||
triple,
|
triple,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const MouseButton = enum {
|
||||||
|
left,
|
||||||
|
right,
|
||||||
|
middle,
|
||||||
|
four,
|
||||||
|
five,
|
||||||
|
six,
|
||||||
|
seven,
|
||||||
|
eight,
|
||||||
|
};
|
||||||
|
|
||||||
pub const Key = enum {
|
pub const Key = enum {
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
|
|
|
||||||
|
|
@ -216,6 +216,55 @@ pub const Platform = struct {
|
||||||
}.callback;
|
}.callback;
|
||||||
platform.window.setKeyCallback(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 {
|
const size_callback = struct {
|
||||||
fn callback(window: glfw.Window, width: i32, height: i32) void {
|
fn callback(window: glfw.Window, width: i32, height: i32) void {
|
||||||
const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform;
|
const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform;
|
||||||
|
|
@ -264,6 +313,19 @@ pub const Platform = struct {
|
||||||
return null;
|
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 {
|
fn toMachKey(key: glfw.Key) enums.Key {
|
||||||
return switch (key) {
|
return switch (key) {
|
||||||
.a => .a,
|
.a => .a,
|
||||||
|
|
|
||||||
|
|
@ -51,4 +51,19 @@ pub const Event = union(enum) {
|
||||||
key_release: struct {
|
key_release: struct {
|
||||||
key: enums.Key,
|
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,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue