glfw: make mouse buttons a proper enum
Helps hexops/mach#37 Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
4d1b92666b
commit
7806709a90
2 changed files with 28 additions and 25 deletions
|
|
@ -13,6 +13,7 @@ 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 Mods = @import("mod.zig").Mods;
|
||||||
|
const MouseButton = @import("mouse_button.zig").MouseButton;
|
||||||
|
|
||||||
const Window = @This();
|
const Window = @This();
|
||||||
|
|
||||||
|
|
@ -51,8 +52,7 @@ pub const InternalUserPointer = struct {
|
||||||
setContentScaleCallback: ?fn (window: Window, xscale: f32, yscale: f32) void,
|
setContentScaleCallback: ?fn (window: Window, xscale: f32, yscale: f32) void,
|
||||||
setKeyCallback: ?fn (window: Window, key: Key, scancode: isize, action: Action, mods: Mods) void,
|
setKeyCallback: ?fn (window: Window, key: Key, scancode: isize, action: Action, mods: Mods) void,
|
||||||
setCharCallback: ?fn (window: Window, codepoint: u21) void,
|
setCharCallback: ?fn (window: Window, codepoint: u21) void,
|
||||||
// TODO(enumify): button
|
setMouseButtonCallback: ?fn (window: Window, button: MouseButton, action: Action, mods: Mods) void,
|
||||||
setMouseButtonCallback: ?fn (window: Window, button: isize, action: Action, mods: Mods) void,
|
|
||||||
setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void,
|
setCursorPosCallback: ?fn (window: Window, xpos: f64, ypos: f64) void,
|
||||||
setCursorEnterCallback: ?fn (window: Window, entered: bool) void,
|
setCursorEnterCallback: ?fn (window: Window, entered: bool) void,
|
||||||
setScrollCallback: ?fn (window: Window, xoffset: f64, yoffset: f64) void,
|
setScrollCallback: ?fn (window: Window, xoffset: f64, yoffset: f64) void,
|
||||||
|
|
@ -1632,8 +1632,8 @@ pub inline fn getKey(self: Window, key: Key) Error!Action {
|
||||||
/// @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 getMouseButton(self: Window, button: isize) Error!Action {
|
pub inline fn getMouseButton(self: Window, button: MouseButton) Error!Action {
|
||||||
const state = c.glfwGetMouseButton(self.handle, @intCast(c_int, button));
|
const state = c.glfwGetMouseButton(self.handle, @enumToInt(button));
|
||||||
try getError();
|
try getError();
|
||||||
return @intToEnum(Action, state);
|
return @intToEnum(Action, state);
|
||||||
}
|
}
|
||||||
|
|
@ -1820,7 +1820,7 @@ pub inline fn setCharCallback(self: Window, callback: ?fn (window: Window, codep
|
||||||
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;
|
||||||
const internal = window.getInternal();
|
const internal = window.getInternal();
|
||||||
internal.setMouseButtonCallback.?(window, @intCast(isize, button), @intToEnum(Action, action), Mods.fromInt(mods));
|
internal.setMouseButtonCallback.?(window, @intToEnum(MouseButton, button), @intToEnum(Action, action), Mods.fromInt(mods));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the mouse button callback.
|
/// Sets the mouse button callback.
|
||||||
|
|
@ -1845,7 +1845,7 @@ fn setMouseButtonCallbackWrapper(handle: ?*c.GLFWwindow, button: c_int, action:
|
||||||
/// @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: Action, mods: Mods) void) void {
|
pub inline fn setMouseButtonCallback(self: Window, callback: ?fn (window: Window, button: MouseButton, action: Action, mods: Mods) void) void {
|
||||||
var internal = self.getInternal();
|
var internal = self.getInternal();
|
||||||
internal.setMouseButtonCallback = callback;
|
internal.setMouseButtonCallback = callback;
|
||||||
_ = c.glfwSetMouseButtonCallback(self.handle, if (callback != null) setMouseButtonCallbackWrapper else null);
|
_ = c.glfwSetMouseButtonCallback(self.handle, if (callback != null) setMouseButtonCallbackWrapper else null);
|
||||||
|
|
@ -2829,7 +2829,7 @@ test "getMouseButton" {
|
||||||
};
|
};
|
||||||
defer window.destroy();
|
defer window.destroy();
|
||||||
|
|
||||||
_ = try window.getMouseButton(glfw.mouse_button.left);
|
_ = try window.getMouseButton(.left);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "getCursorPos" {
|
test "getCursorPos" {
|
||||||
|
|
@ -2945,7 +2945,7 @@ test "setMouseButtonCallback" {
|
||||||
defer window.destroy();
|
defer window.destroy();
|
||||||
|
|
||||||
window.setMouseButtonCallback((struct {
|
window.setMouseButtonCallback((struct {
|
||||||
fn callback(_window: Window, button: isize, action: Action, mods: Mods) void {
|
fn callback(_window: Window, button: MouseButton, action: Action, mods: Mods) void {
|
||||||
_ = _window;
|
_ = _window;
|
||||||
_ = button;
|
_ = button;
|
||||||
_ = action;
|
_ = action;
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,23 @@
|
||||||
//! Mouse button IDs.
|
|
||||||
//!
|
|
||||||
//! See glfw.setMouseButtonCallback for how these are used.
|
|
||||||
|
|
||||||
const c = @import("c.zig").c;
|
const c = @import("c.zig").c;
|
||||||
|
|
||||||
// TODO(enumify)
|
/// Mouse button IDs.
|
||||||
pub const one = c.GLFW_MOUSE_BUTTON_1;
|
///
|
||||||
pub const two = c.GLFW_MOUSE_BUTTON_2;
|
/// See glfw.setMouseButtonCallback for how these are used.
|
||||||
pub const three = c.GLFW_MOUSE_BUTTON_3;
|
pub const MouseButton = enum(c_int) {
|
||||||
pub const four = c.GLFW_MOUSE_BUTTON_4;
|
// We use left/right/middle aliases here because those are more common and we cannot have
|
||||||
pub const five = c.GLFW_MOUSE_BUTTON_5;
|
// duplicate values in a Zig enum.
|
||||||
pub const six = c.GLFW_MOUSE_BUTTON_6;
|
left = c.GLFW_MOUSE_BUTTON_1,
|
||||||
pub const seven = c.GLFW_MOUSE_BUTTON_7;
|
right = c.GLFW_MOUSE_BUTTON_2,
|
||||||
pub const eight = c.GLFW_MOUSE_BUTTON_8;
|
middle = c.GLFW_MOUSE_BUTTON_3,
|
||||||
|
four = c.GLFW_MOUSE_BUTTON_4,
|
||||||
|
five = c.GLFW_MOUSE_BUTTON_5,
|
||||||
|
six = c.GLFW_MOUSE_BUTTON_6,
|
||||||
|
seven = c.GLFW_MOUSE_BUTTON_7,
|
||||||
|
eight = c.GLFW_MOUSE_BUTTON_8,
|
||||||
|
};
|
||||||
|
|
||||||
pub const last = eight;
|
/// Not in the MouseButton enumeration as it is a duplicate value which is forbidden.
|
||||||
pub const left = one;
|
pub const last = MouseButton.eight;
|
||||||
pub const right = two;
|
pub const one = MouseButton.left;
|
||||||
pub const middle = three;
|
pub const two = MouseButton.right;
|
||||||
|
pub const three = MouseButton.middle;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue