diff options
-rw-r--r-- | src/Window.zig | 22 | ||||
-rw-r--r-- | src/gfz.zig | 2 | ||||
-rw-r--r-- | src/input.zig | 38 |
3 files changed, 50 insertions, 12 deletions
diff --git a/src/Window.zig b/src/Window.zig index 82ae8c3..5ff2de9 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -19,8 +19,10 @@ usingnamespace @import("cimport.zig"); const Error = gfz.Error; const Key = input.Key; +const KeyAction = input.KeyAction; const Mods = input.Mods; const Monitor = @import("Monitor.zig"); +const MouseButton = input.MouseButton; const checkError = gfz.checkError; const getError = gfz.getError; const gfz = @import("gfz.zig"); @@ -29,9 +31,9 @@ const input = @import("input.zig"); const Self = @This(); const CursorPosFun = fn (window: Self, xpos: f64, ypos: f64) void; const KeyFun = fn (window: Self, key: Key, scancode: c_int, - action: c_int, mods: Mods) void; -const MouseButtonFun = fn (window: Self, button: c_int, - action: c_int, mods: Mods) void; + action: KeyAction, mods: Mods) void; +const MouseButtonFun = fn (window: Self, button: MouseButton, + action: MouseButton.Action, mods: Mods) void; const SizeFun = fn (window: Self, width: c_int, height: c_int) void; const Impl = GLFWwindow; pimpl: *Impl, @@ -49,14 +51,16 @@ fn fnCast(comptime DestType: type, comptime fun: anytype) DestType { pub fn callback(window: ?*Impl, key: c_int, scancode: c_int, action: c_int, mods: c_int) callconv(.C) void { fun(@fieldParentPtr(Self, "pimpl", &window.?).*, - @intToEnum(Key, key), scancode, action, Mods.fromInt(mods)); + @intToEnum(Key, key), scancode, + @intToEnum(KeyAction, action), Mods.fromInt(mods)); } }, GLFWmousebuttonfun => struct { pub fn callback(window: ?*Impl, button: c_int, action: c_int, mods: c_int) callconv(.C) void { fun(@fieldParentPtr(Self, "pimpl", &window.?).*, - button, action, Mods.fromInt(mods)); + @intToEnum(MouseButton, button), + @intToEnum(MouseButton.Action, action), Mods.fromInt(mods)); } }, GLFWwindowsizefun => struct { @@ -243,7 +247,7 @@ pub fn swapBuffers(self: Self) Error!void { try checkError(); } -pub const InputMode = enum(c_int) { +const InputMode = enum(c_int) { sticky_keys = GLFW_STICKY_KEYS, sticky_mouse_buttons = GLFW_STICKY_MOUSE_BUTTONS, lock_key_mods = GLFW_LOCK_KEY_MODS, @@ -264,7 +268,7 @@ pub fn setInputMode(self: Self, mode: InputMode, flag: bool) Error!void { try checkError(); } -pub const CursorMode = enum(c_int) { +const CursorMode = enum(c_int) { normal = GLFW_CURSOR_NORMAL, hidden = GLFW_CURSOR_HIDDEN, disabled = GLFW_CURSOR_DISABLED, @@ -314,8 +318,8 @@ const State = enum(c_int) { }; /// Return the last reported state of a mouse button. -pub fn getMouseButton(self: Self, button: c_int) Error!State { - const state = glfwGetMouseButton(self.pimpl, button); +pub fn getMouseButton(self: Self, button: MouseButton) Error!State { + const state = glfwGetMouseButton(self.pimpl, @enumToInt(button)); try checkError(); return @intToEnum(State, state); } diff --git a/src/gfz.zig b/src/gfz.zig index 31051dd..83c89aa 100644 --- a/src/gfz.zig +++ b/src/gfz.zig @@ -132,7 +132,9 @@ pub fn rawMouseMotionSupported() Error!bool { pub const Window = @import("Window.zig"); pub const Key = input.Key; +pub const KeyAction = input.KeyAction; pub const Mods = input.Mods; +pub const MouseButton = input.MouseButton; test { try init(); diff --git a/src/input.zig b/src/input.zig index ef99038..97ad9b3 100644 --- a/src/input.zig +++ b/src/input.zig @@ -17,8 +17,7 @@ // along with gfz. If not, see <https://www.gnu.org/licenses/>. const Int = std.meta.Int; -const EnumField = TypeInfo.EnumField; -const TypeInfo = std.builtin.TypeInfo; +const EnumField = std.builtin.TypeInfo.EnumField; const endian = @import("builtin").target.cpu.arch.endian(); const glfw = @import("cimport.zig"); const std = @import("std"); @@ -46,7 +45,7 @@ const keys = [_][]const u8{ }; /// Keyboard key enumeration: https://www.glfw.org/docs/latest/group__keys.html -pub const Key = @Type(TypeInfo{ +pub const Key = @Type(.{ .Enum = .{ .layout = .Auto, .tag_type = c_int, @@ -104,3 +103,36 @@ pub const Mods = packed struct { }); } }; + +usingnamespace glfw; + +/// Key action: https://www.glfw.org/docs/latest/input_guide.html#input_key +// TODO: move inside Key: https://github.com/ziglang/zig/issues/6709 +pub const KeyAction = enum(c_int) { + press = GLFW_PRESS, + release = GLFW_RELEASE, + repeat = GLFW_REPEAT, +}; + +/// Mouse buttons: https://www.glfw.org/docs/latest/group__buttons.html +pub const MouseButton = enum(c_int) { + @"1" = GLFW_MOUSE_BUTTON_1, + @"2" = GLFW_MOUSE_BUTTON_2, + @"3" = GLFW_MOUSE_BUTTON_3, + @"4" = GLFW_MOUSE_BUTTON_4, + @"5" = GLFW_MOUSE_BUTTON_5, + @"6" = GLFW_MOUSE_BUTTON_6, + @"7" = GLFW_MOUSE_BUTTON_7, + @"8" = GLFW_MOUSE_BUTTON_8, + + pub const left = @intToEnum(MouseButton, GLFW_MOUSE_BUTTON_LEFT); + pub const right = @intToEnum(MouseButton, GLFW_MOUSE_BUTTON_RIGHT); + pub const middle = @intToEnum(MouseButton, GLFW_MOUSE_BUTTON_MIDDLE); + + /// Mouse button input action: + /// https://www.glfw.org/docs/latest/input_guide.html#input_mouse_button + pub const Action = enum(c_int) { + press = GLFW_PRESS, + release = GLFW_RELEASE, + }; +}; |