glfw: add glfw.Window.setCharCallback

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

View file

@ -50,6 +50,7 @@ pub const InternalUserPointer = struct {
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, setMouseButtonCallback: ?fn (window: Window, button: isize, action: isize, mods: isize) void,
setCharCallback: ?fn (window: Window, codepoint: u21) void,
}; };
/// Resets all window hints to their default values. /// Resets all window hints to their default values.
@ -1323,24 +1324,6 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// @ingroup input // /// @ingroup input
// typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int); // typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int);
// /// The function pointer type for Unicode character callbacks.
// ///
// /// This is the function pointer type for Unicode character callbacks.
// /// A Unicode character callback function has the following signature:
// /// @code
// /// void function_name(GLFWwindow* window, unsigned int codepoint)
// /// @endcode
// ///
// /// @param[in] window The window that received the event.
// /// @param[in] codepoint The Unicode code point of the character.
// ///
// /// see also: input_char, glfwSetCharCallback
// ///
// /// @glfw3 Added window handle parameter.
// ///
// /// @ingroup input
// typedef void (* GLFWcharfun)(GLFWwindow*,unsigned int);
// TODO(mouinput options) // TODO(mouinput options)
// /// Returns the value of an input option for the specified window. // /// Returns the value of an input option for the specified window.
// /// // ///
@ -1635,47 +1618,46 @@ pub inline fn setContentScaleCallback(self: Window, callback: ?fn (window: Windo
// /// @ingroup input // /// @ingroup input
// GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun callback); // GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun callback);
// TODO(unicode character) fn setCharCallbackWrapper(handle: ?*c.GLFWwindow, codepoint: c_uint) callconv(.C) void {
// /// Sets the Unicode character callback. const window = from(handle.?) catch unreachable;
// /// const internal = window.getInternal();
// /// This function sets the character callback of the specified window, which is internal.setCharCallback.?(window, @intCast(u21, codepoint));
// /// called when a Unicode character is input. }
// ///
// /// The character callback is intended for Unicode text input. As it deals with /// Sets the Unicode character callback.
// /// characters, it is keyboard layout dependent, whereas the ///
// /// [key callback](@ref glfwSetKeyCallback) is not. Characters do not map 1:1 /// This function sets the character callback of the specified window, which is called when a
// /// to physical keys, as a key may produce zero, one or more characters. If you /// Unicode character is input.
// /// want to know whether a specific physical key was pressed or released, see ///
// /// the key callback instead. /// The character callback is intended for Unicode text input. As it deals with characters, it is
// /// /// keyboard layout dependent, whereas the key callback (see glfw.Window.setKeyCallback) is not.
// /// The character callback behaves as system text input normally does and will /// Characters do not map 1:1 to physical keys, as a key may produce zero, one or more characters.
// /// not be called if modifier keys are held down that would prevent normal text /// If you want to know whether a specific physical key was pressed or released, see the key
// /// input on that platform, for example a Super (Command) key on macOS or Alt key /// callback instead.
// /// on Windows. ///
// /// /// The character callback behaves as system text input normally does and will not be called if
// /// @param[in] window The window whose callback to set. /// modifier keys are held down that would prevent normal text input on that platform, for example a
// /// @param[in] callback The new callback, or null to remove the currently set /// Super (Command) key on macOS or Alt key on Windows.
// /// callback. ///
// /// @return The previously set callback, or null if no callback was set or the /// @param[in] window The window whose callback to set.
// /// 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] codepoint The Unicode code point of the character.
// /// void function_name(GLFWwindow* window, unsigned int codepoint) ///
// /// @endcode /// @thread_safety This function must only be called from the main thread.
// /// For more information about the callback parameters, see the ///
// /// [function pointer type](@ref GLFWcharfun). /// see also: input_char
// /// pub inline fn setCharCallback(self: Window, callback: ?fn (window: Window, codepoint: u21) void) void {
// /// Possible errors include glfw.Error.NotInitialized. var internal = self.getInternal();
// /// internal.setCharCallback = callback;
// /// @thread_safety This function must only be called from the main thread. _ = c.glfwSetCharCallback(self.handle, if (callback != null) setCharCallbackWrapper else null);
// ///
// /// see also: input_char // 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
// /// @glfw3 Added window handle parameter and return value. // awkward to use, so we discard it instead.
// /// getError() catch {};
// /// @ingroup input }
// GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* window, GLFWcharfun callback);
fn setMouseButtonCallbackWrapper(handle: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void { fn setMouseButtonCallbackWrapper(handle: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void {
const window = from(handle.?) catch unreachable; const window = from(handle.?) catch unreachable;
@ -1702,8 +1684,6 @@ fn setMouseButtonCallbackWrapper(handle: ?*c.GLFWwindow, button: c_int, action:
/// actions. /// actions.
/// @callback_param[in] mods Bit field describing which modifier keys (see mods) were held down. /// @callback_param[in] mods Bit field describing which modifier keys (see mods) were held down.
/// ///
/// 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
@ -2713,3 +2693,24 @@ test "setMouseButtonCallback" {
} }
}).callback); }).callback);
} }
test "setCharCallback" {
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.setCharCallback((struct {
fn callback(_window: Window, codepoint: u21) void {
_ = _window;
_ = codepoint;
}
}).callback);
}