glfw: add glfw.Window.setCursorPosCallback

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-23 11:46:58 -07:00 committed by Stephen Gutekanst
parent dd6242ee24
commit 2bdc71518b

View file

@ -48,6 +48,7 @@ pub const InternalUserPointer = struct {
setDropCallback: ?fn (window: Window, paths: [][*c]const u8) void, setDropCallback: ?fn (window: Window, paths: [][*c]const u8) void,
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,
}; };
/// Resets all window hints to their default values. /// Resets all window hints to their default values.
@ -1322,26 +1323,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// @ingroup input // /// @ingroup input
// typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int,int); // typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int,int);
// /// The function pointer type for cursor position callbacks.
// ///
// /// This is the function pointer type for cursor position callbacks. A cursor
// /// position callback function has the following signature:
// /// @code
// /// void function_name(GLFWwindow* window, double xpos, double ypos);
// /// @endcode
// ///
// /// @param[in] window The window that received the event.
// /// @param[in] xpos The new cursor x-coordinate, relative to the left edge of
// /// the content area.
// /// @param[in] ypos The new cursor y-coordinate, relative to the top edge of the
// /// content area.
// ///
// /// see also: cursor_pos, glfw.setCursorPosCallback
// /// Replaces `GLFWmouseposfun`.
// ///
// /// @ingroup input
// typedef void (* GLFWcursorposfun)(GLFWwindow*,double,double);
// /// 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
@ -1755,36 +1736,39 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// @ingroup input // /// @ingroup input
// GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun callback); // GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun callback);
// TODO(cursor) fn setCursorPosCallbackWrapper(handle: ?*c.GLFWwindow, xpos: f64, ypos: f64) callconv(.C) void {
// /// Sets the cursor position callback. const window = from(handle.?) catch unreachable;
// /// const internal = window.getInternal();
// /// This function sets the cursor position callback of the specified window, internal.setCursorPosCallback.?(window, xpos, ypos);
// /// which is called when the cursor is moved. The callback is provided with the }
// /// position, in screen coordinates, relative to the upper-left corner of the
// /// content area of the window. /// Sets the cursor position callback.
// /// ///
// /// @param[in] window The window whose callback to set. /// This function sets the cursor position callback of the specified window, which is called when
// /// @param[in] callback The new callback, or null to remove the currently set /// the cursor is moved. The callback is provided with the position, in screen coordinates, relative
// /// callback. /// to the upper-left corner of the content area of the window.
// /// @return The previously set callback, or null if no callback was set or the ///
// /// library had not been [initialized](@ref intro_init). /// @param[in] callback The new callback, or null to remove the currently set callback.
// /// ///
// /// @callback_signature /// @callback_param[in] window The window that received the event.
// /// @code /// @callback_param[in] xpos The new cursor x-coordinate, relative to the left edge of the content
// /// void function_name(GLFWwindow* window, double xpos, double ypos); /// area.
// /// @endcode /// callback_@param[in] ypos The new cursor y-coordinate, relative to the top edge of the content
// /// For more information about the callback parameters, see the /// area.
// /// [function pointer type](@ref GLFWcursorposfun). ///
// /// /// @thread_safety This function must only be called from the main thread.
// /// Possible errors include glfw.Error.NotInitialized. ///
// /// /// see also: cursor_pos
// /// @thread_safety This function must only be called from the main thread. pub inline fn setCursorPosCallback(self: Window, callback: ?fn (window: Window, xpos: f64, ypos: f64) void) void {
// /// var internal = self.getInternal();
// /// see also: cursor_pos internal.setCursorPosCallback = callback;
// /// Replaces `glfwSetMousePosCallback`. _ = c.glfwSetCursorPosCallback(self.handle, if (callback != null) setCursorPosCallbackWrapper else null);
// ///
// /// @ingroup input // The only error this could return would be glfw.Error.NotInitialized, which should
// GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* window, GLFWcursorposfun callback); // 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 setCursorEnterCallbackWrapper(handle: ?*c.GLFWwindow, entered: c_int) callconv(.C) void { fn setCursorEnterCallbackWrapper(handle: ?*c.GLFWwindow, entered: c_int) callconv(.C) void {
const window = from(handle.?) catch unreachable; const window = from(handle.?) catch unreachable;
@ -1803,8 +1787,6 @@ fn setCursorEnterCallbackWrapper(handle: ?*c.GLFWwindow, entered: c_int) callcon
/// @callback_param[in] entered `true` if the cursor entered the window's content area, or `false` /// @callback_param[in] entered `true` if the cursor entered the window's content area, or `false`
/// if it left it. /// if it left it.
/// ///
/// 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: cursor_enter /// see also: cursor_enter
@ -1840,8 +1822,6 @@ fn setScrollCallbackWrapper(handle: ?*c.GLFWwindow, xoffset: f64, yoffset: f64)
/// @callback_param[in] xoffset The scroll offset along the x-axis. /// @callback_param[in] xoffset The scroll offset along the x-axis.
/// @callback_param[in] yoffset The scroll offset along the y-axis. /// @callback_param[in] yoffset The scroll offset along the y-axis.
/// ///
/// 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: scrolling /// see also: scrolling
@ -2706,3 +2686,25 @@ test "setCursorEnterCallback" {
} }
}).callback); }).callback);
} }
test "setCursorPosCallback" {
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.setCursorPosCallback((struct {
fn callback(_window: Window, xpos: f64, ypos: f64) void {
_ = _window;
_ = xpos;
_ = ypos;
}
}).callback);
}