glfw: add glfw.Window.setMouseButtonCallback

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-23 12:01:13 -07:00 committed by Stephen Gutekanst
parent 2bdc71518b
commit 0b84cfd3c4

View file

@ -49,6 +49,7 @@ pub const InternalUserPointer = struct {
setScrollCallback: ?fn (window: Window, xoffset: f64, yoffset: f64) void, setScrollCallback: ?fn (window: Window, xoffset: f64, yoffset: f64) void,
setCursorEnterCallback: ?fn (window: Window, entered: bool) void, setCursorEnterCallback: ?fn (window: Window, entered: bool) void,
setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void, setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void,
setMouseButtonCallback: ?fn (window: Window, button: isize, action: isize, mods: isize) void,
}; };
/// Resets all window hints to their default values. /// Resets all window hints to their default values.
@ -1299,30 +1300,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
getError() catch {}; getError() catch {};
} }
// TODO(input):
// /// The function pointer type for mouse button callbacks.
// ///
// /// This is the function pointer type for mouse button callback functions.
// /// A mouse button callback function has the following signature:
// /// @code
// /// void function_name(GLFWwindow* window, int button, int action, int mods)
// /// @endcode
// ///
// /// @param[in] window The window that received the event.
// /// @param[in] button The mouse button that was pressed or
// /// released.
// /// @param[in] action One of `glfw.press` or `glfw.release`. Future releases
// /// may add more actions.
// /// @param[in] mods Bit field describing which [modifier keys](@ref mods) were
// /// held down.
// ///
// /// see also: input_mouse_button, glfwSetMouseButtonCallback
// ///
// /// @glfw3 Added window handle and modifier mask parameters.
// ///
// /// @ingroup input
// typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int,int);
// /// The function pointer type for keyboard key callbacks. // /// The function pointer type for keyboard key callbacks.
// /// // ///
// /// This is the function pointer type for keyboard key callbacks. A keyboard // /// This is the function pointer type for keyboard key callbacks. A keyboard
@ -1700,41 +1677,46 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// @ingroup input // /// @ingroup input
// GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* window, GLFWcharfun callback); // GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* window, GLFWcharfun callback);
// TODO(mouse button) fn setMouseButtonCallbackWrapper(handle: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void {
// /// Sets the mouse button callback. const window = from(handle.?) catch unreachable;
// /// const internal = window.getInternal();
// /// This function sets the mouse button callback of the specified window, which internal.setMouseButtonCallback.?(window, @intCast(isize, button), @intCast(isize, action), @intCast(isize, mods));
// /// is called when a mouse button is pressed or released. }
// ///
// /// When a window loses input focus, it will generate synthetic mouse button /// Sets the mouse button callback.
// /// release events for all pressed mouse buttons. You can tell these events ///
// /// from user-generated events by the fact that the synthetic ones are generated /// This function sets the mouse button callback of the specified window, which is called when a
// /// after the focus loss event has been processed, i.e. after the /// mouse button is pressed or released.
// /// [window focus callback](@ref glfwSetWindowFocusCallback) has been called. ///
// /// /// When a window loses input focus, it will generate synthetic mouse button release events for all
// /// @param[in] window The window whose callback to set. /// pressed mouse buttons. You can tell these events from user-generated events by the fact that the
// /// @param[in] callback The new callback, or null to remove the currently set /// synthetic ones are generated after the focus loss event has been processed, i.e. after the
// /// callback. /// window focus callback (see glfw.Window.setFocusCallback) has been called.
// /// @return The previously set callback, or null if no callback was set or the ///
// /// library had not been [initialized](@ref intro_init). /// @param[in] window The window whose callback to set.
// /// /// @param[in] callback The new callback, or null to remove the currently set callback.
// /// @callback_signature ///
// /// @code /// @callback_param[in] window The window that received the event.
// /// void function_name(GLFWwindow* window, int button, int action, int mods) /// @callback_param[in] button The mouse button that was pressed or released.
// /// @endcode /// @callback_param[in] action One of `glfw.press` or `glfw.release`. Future releases may add more
// /// For more information about the callback parameters, see the /// actions.
// /// [function pointer type](@ref GLFWmousebuttonfun). /// @callback_param[in] mods Bit field describing which modifier keys (see mods) were held down.
// /// ///
// /// Possible errors include glfw.Error.NotInitialized. /// Possible errors include glfw.Error.NotInitialized.
// /// ///
// /// @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_mouse_button /// see also: input_mouse_button
// /// pub inline fn setMouseButtonCallback(self: Window, callback: ?fn (window: Window, button: isize, action: isize, mods: isize) void) void {
// /// @glfw3 Added window handle parameter and return value. var internal = self.getInternal();
// /// internal.setMouseButtonCallback = callback;
// /// @ingroup input _ = c.glfwSetMouseButtonCallback(self.handle, if (callback != null) setMouseButtonCallbackWrapper else null);
// GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun callback);
// The only error this could return would be glfw.Error.NotInitialized, which should
// definitely have occurred before calls to this. Returning an error here makes the API
// awkward to use, so we discard it instead.
getError() catch {};
}
fn setCursorPosCallbackWrapper(handle: ?*c.GLFWwindow, xpos: f64, ypos: f64) callconv(.C) void { fn setCursorPosCallbackWrapper(handle: ?*c.GLFWwindow, xpos: f64, ypos: f64) callconv(.C) void {
const window = from(handle.?) catch unreachable; const window = from(handle.?) catch unreachable;
@ -2708,3 +2690,26 @@ test "setCursorPosCallback" {
} }
}).callback); }).callback);
} }
test "setMouseButtonCallback" {
const glfw = @import("main.zig");
try glfw.init();
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});
return;
};
defer window.destroy();
window.setMouseButtonCallback((struct {
fn callback(_window: Window, button: isize, action: isize, mods: isize) void {
_ = _window;
_ = button;
_ = action;
_ = mods;
}
}).callback);
}