diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Window.zig | 9 | ||||
-rw-r--r-- | src/input.zig | 75 |
2 files changed, 80 insertions, 4 deletions
diff --git a/src/Window.zig b/src/Window.zig index d15dad3..74433e8 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -18,6 +18,7 @@ usingnamespace @import("cimport.zig"); const Error = gfz.Error; +const Key = @import("input.zig").Key; const Monitor = @import("Monitor.zig"); const checkError = gfz.checkError; const getError = gfz.getError; @@ -25,7 +26,7 @@ const gfz = @import("gfz.zig"); const Self = @This(); const CursorPosFun = fn (window: Self, xpos: f64, ypos: f64) void; -const KeyFun = fn (window: Self, key: c_int, scancode: c_int, +const KeyFun = fn (window: Self, key: Key, scancode: c_int, action: c_int, mods: c_int) void; const MouseButtonFun = fn (window: Self, button: c_int, action: c_int, mods: c_int) void; @@ -46,7 +47,7 @@ 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.?).*, - key, scancode, action, mods); + @intToEnum(Key, key), scancode, action, mods); } }, GLFWmousebuttonfun => struct { @@ -325,8 +326,8 @@ pub fn setMouseButtonCallback(self: Self, } /// Return the last reported state of a keyboard key. -pub fn getKey(self: Self, key: c_int) Error!State { - const state = glfwGetKey(self.pimpl, key); +pub fn getKey(self: Self, key: Key) Error!State { + const state = glfwGetKey(self.pimpl, @enumToInt(key)); try checkError(); return @intToEnum(State, state); } diff --git a/src/input.zig b/src/input.zig new file mode 100644 index 0000000..8900252 --- /dev/null +++ b/src/input.zig @@ -0,0 +1,75 @@ +// Input constants +// Copyright (C) 2021 Nguyễn Gia Phong +// +// This file is part of gfz. +// +// gfz is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// gfz is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with gfz. If not, see <https://www.gnu.org/licenses/>. + +const ComptimeStringMap = std.ComptimeStringMap; +const EnumField = TypeInfo.EnumField; +const TypeInfo = std.builtin.TypeInfo; +const glfw = @import("cimport.zig"); +const std = @import("std"); + +const keys = [_][]const u8{ + // Printable keys + "SPACE", "APOSTROPHE", "COMMA", "MINUS", "PERIOD", "SLASH", + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "SEMICOLON", "EQUAL", + "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", + "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", + "LEFT_BRACKET", "BACKSLASH", "RIGHT_BRACKET", "GRAVE_ACCENT", + "WORLD_1", "WORLD_2", // keys lacking a clear US mapping + // Function keys + "ESCAPE", "ENTER", "TAB", "BACKSPACE", "INSERT", "DELETE", + "RIGHT", "LEFT", "DOWN", "UP", "PAGE_UP", "PAGE_DOWN", "HOME", "END", + "CAPS_LOCK", "SCROLL_LOCK", "NUM_LOCK", "PRINT_SCREEN", "PAUSE", + "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", + "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", + "F18", "F19", "F20", "F21", "F22", "F23", "F24", "F25", + "KP_0", "KP_1", "KP_2", "KP_3", "KP_4", "KP_5", "KP_6", + "KP_7", "KP_8", "KP_9", "KP_DECIMAL", "KP_DIVIDE", "KP_MULTIPLY", + "KP_SUBTRACT", "KP_ADD", "KP_ENTER", "KP_EQUAL", + "LEFT_SHIFT", "LEFT_CONTROL", "LEFT_ALT", "LEFT_SUPER", + "RIGHT_SHIFT", "RIGHT_CONTROL", "RIGHT_ALT", "RIGHT_SUPER", "MENU", +}; + +/// Keyboard key enumeration +pub const Key = @Type(TypeInfo{ + .Enum = .{ + .layout = .Auto, + .tag_type = c_int, + .fields = blk: { + var fields: [keys.len]EnumField = undefined; + for (keys) |name, i| + fields[i] = .{ + .name = name, + .value = @field(glfw, "GLFW_KEY_" ++ name), + }; + break :blk fields[0..]; + }, + .decls = &.{}, + .is_exhaustive = true, + } +}); + +/// Mapping of name to keyboard key +pub const KeyMap = blk: { + var map: [keys.len]struct { + @"0": []const u8, + @"1": Key, + } = undefined; + for (keys) |name, i| + map[i] = .{ name, @field(Key, name) }; + break :blk ComptimeStringMap(Key, map); +}; |