glfw: add Window.setCursor

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-23 12:34:59 -07:00 committed by Stephen Gutekanst
parent bad1b9f246
commit d50d84f935

View file

@ -9,6 +9,7 @@ const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError; const getError = @import("errors.zig").getError;
const Image = @import("Image.zig"); const Image = @import("Image.zig");
const Monitor = @import("Monitor.zig"); const Monitor = @import("Monitor.zig");
const Cursor = @import("Cursor.zig");
const Window = @This(); const Window = @This();
@ -1502,27 +1503,25 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// see also: cursor_pos, glfw.getCursorPos // /// see also: cursor_pos, glfw.getCursorPos
// GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos); // GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos);
// TODO(cursor icon) /// Sets the cursor for the window.
// /// Sets the cursor for the window. ///
// /// /// This function sets the cursor image to be used when the cursor is over the content area of the
// /// This function sets the cursor image to be used when the cursor is over the /// specified window. The set cursor will only be visible when the cursor mode (see cursor_mode) of
// /// content area of the specified window. The set cursor will only be visible /// the window is `glfw.Cursor.normal`.
// /// when the [cursor mode](@ref cursor_mode) of the window is ///
// /// `GLFW_CURSOR_NORMAL`. /// On some platforms, the set cursor may not be visible unless the window also has input focus.
// /// ///
// /// On some platforms, the set cursor may not be visible unless the window also /// @param[in] cursor The cursor to set, or null to switch back to the default arrow cursor.
// /// has input focus. ///
// /// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError.
// /// @param[in] window The window to set the cursor for. ///
// /// @param[in] cursor The cursor to set, or null to switch back to the default /// @thread_safety This function must only be called from the main thread.
// /// arrow cursor. ///
// /// /// see also: cursor_object
// /// Possible errors include glfw.Error.NotInitialized and glfw.Error.PlatformError. pub inline fn setCursor(self: Window, cursor: Cursor) Error!void {
// /// c.glfwSetCursor(self.handle, cursor.ptr);
// /// @thread_safety This function must only be called from the main thread. try getError();
// /// }
// /// see also: cursor_object
// GLFWAPI void glfwSetCursor(GLFWwindow* window, GLFWcursor* cursor);
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;
@ -2563,6 +2562,28 @@ test "setDropCallback" {
}).callback) catch |err| std.debug.print("can't set window drop callback, not supported by OS maybe? error={}\n", .{err}); }).callback) catch |err| std.debug.print("can't set window drop callback, not supported by OS maybe? error={}\n", .{err});
} }
test "setKeyCallback" {
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();
const cursor = glfw.Cursor.createStandard(.ibeam) catch |err| {
std.debug.print("failed to create cursor, custom cursors not supported? error={}\n", .{err});
return;
};
defer cursor.destroy();
window.setCursor(cursor) catch |err| std.debug.print("failed to set cursor, custom cursors not supported? error={}\n", .{err});
}
test "setKeyCallback" { test "setKeyCallback" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
try glfw.init(); try glfw.init();