glfw: make Window.setKeyCallback use Mods packed struct directly

Helps hexops/mach#37

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-30 12:50:39 -07:00 committed by Stephen Gutekanst
parent ab01edee1c
commit 60eeae4904

View file

@ -12,6 +12,7 @@ const Monitor = @import("Monitor.zig");
const Cursor = @import("Cursor.zig"); const Cursor = @import("Cursor.zig");
const Key = @import("key.zig").Key; const Key = @import("key.zig").Key;
const Action = @import("action.zig").Action; const Action = @import("action.zig").Action;
const Mods = @import("mod.zig").Mods;
const Window = @This(); const Window = @This();
@ -48,7 +49,7 @@ pub const InternalUserPointer = struct {
setMaximizeCallback: ?fn (window: Window, maximized: bool) void, setMaximizeCallback: ?fn (window: Window, maximized: bool) void,
setFramebufferSizeCallback: ?fn (window: Window, width: isize, height: isize) void, setFramebufferSizeCallback: ?fn (window: Window, width: isize, height: isize) void,
setContentScaleCallback: ?fn (window: Window, xscale: f32, yscale: f32) void, setContentScaleCallback: ?fn (window: Window, xscale: f32, yscale: f32) void,
setKeyCallback: ?fn (window: Window, key: isize, scancode: isize, action: Action, mods: isize) void, setKeyCallback: ?fn (window: Window, key: isize, scancode: isize, action: Action, mods: Mods) void,
setCharCallback: ?fn (window: Window, codepoint: u21) void, setCharCallback: ?fn (window: Window, codepoint: u21) void,
setMouseButtonCallback: ?fn (window: Window, button: isize, action: Action, mods: isize) void, setMouseButtonCallback: ?fn (window: Window, button: isize, action: Action, mods: isize) void,
setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void, setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void,
@ -1524,7 +1525,7 @@ pub inline fn setCursor(self: Window, cursor: Cursor) Error!void {
fn setKeyCallbackWrapper(handle: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void { fn setKeyCallbackWrapper(handle: ?*c.GLFWwindow, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void {
const window = from(handle.?) catch unreachable; const window = from(handle.?) catch unreachable;
const internal = window.getInternal(); const internal = window.getInternal();
internal.setKeyCallback.?(window, @intCast(isize, key), @intCast(isize, scancode), @intToEnum(Action, action), @intCast(isize, mods)); internal.setKeyCallback.?(window, @intCast(isize, key), @intCast(isize, scancode), @intToEnum(Action, action), Mods.fromInt(mods));
} }
/// Sets the key callback. /// Sets the key callback.
@ -1561,7 +1562,7 @@ fn setKeyCallbackWrapper(handle: ?*c.GLFWwindow, key: c_int, scancode: c_int, ac
/// @thread_safety This function must only be called from the main thread. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: input_key /// see also: input_key
pub inline fn setKeyCallback(self: Window, callback: ?fn (window: Window, key: isize, scancode: isize, action: Action, mods: isize) void) void { pub inline fn setKeyCallback(self: Window, callback: ?fn (window: Window, key: isize, scancode: isize, action: Action, mods: Mods) void) void {
var internal = self.getInternal(); var internal = self.getInternal();
internal.setKeyCallback = callback; internal.setKeyCallback = callback;
_ = c.glfwSetKeyCallback(self.handle, if (callback != null) setKeyCallbackWrapper else null); _ = c.glfwSetKeyCallback(self.handle, if (callback != null) setKeyCallbackWrapper else null);
@ -2696,7 +2697,7 @@ test "setKeyCallback" {
defer window.destroy(); defer window.destroy();
window.setKeyCallback((struct { window.setKeyCallback((struct {
fn callback(_window: Window, key: isize, scancode: isize, action: Action, mods: isize) void { fn callback(_window: Window, key: isize, scancode: isize, action: Action, mods: Mods) void {
_ = _window; _ = _window;
_ = key; _ = key;
_ = scancode; _ = scancode;