summary refs log tree commit diff
path: root/src/input.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/input.zig')
-rw-r--r--src/input.zig75
1 files changed, 75 insertions, 0 deletions
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);
+};