From 82e8cfdbe1a7f2956cd9c579ed23d63fa49ebcef Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 22 Sep 2023 14:03:33 +0200 Subject: [PATCH] Implement input handler --- src/input.zig | 32 ++++++++++++++++++++++++++++++++ src/raylib.zig | 22 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/input.zig diff --git a/src/input.zig b/src/input.zig new file mode 100644 index 0000000..7e3e58f --- /dev/null +++ b/src/input.zig @@ -0,0 +1,32 @@ +const rl = @import("raylib.zig"); + +const Input = struct { + name: []const u8, + code: i32, + default: i32, + + pub fn isDown(self: Input) bool { + return rl.isKeyDown(self.code); + } + pub fn isJustPressed(self: Input) bool { + return rl.isKeyPressed(self.code); + } + pub fn isJustReleased(self: Input) bool { + return rl.isKeyReleased(self.code); + } +}; + +fn init(comptime name: []const u8, comptime key_code: []const u8) Input { + return Input{ + .name = name, + .code = rl.getKeyCode(key_code), + .default = rl.getKeyCode(key_code), + }; +} + +pub const menu_left = init("Menu Left", "LEFT"); +pub const menu_right = init("Menu Right", "RIGHT"); +pub const menu_down = init("Menu Down", "DOWN"); +pub const menu_up = init("Menu Up", "UP"); +pub const menu_accept = init("Menu Accept", "ENTER"); +pub const menu_cancel = init("Menu Cancel", "BACKSPACE"); diff --git a/src/raylib.zig b/src/raylib.zig index 427ecf3..f20b774 100644 --- a/src/raylib.zig +++ b/src/raylib.zig @@ -27,6 +27,28 @@ pub fn endDrawing() void { c.EndDrawing(); } +pub fn getKeyCode(comptime name: []const u8) i32 { + const full_name = "KEY_" ++ name; + + if (!@hasDecl(c, full_name)) { + @compileError(full_name ++ " keycode does not exist."); + } + + return @field(c, full_name); +} + +pub fn isKeyPressed(key: i32) bool { + return c.IsKeyPressed(key); +} + +pub fn isKeyDown(key: i32) bool { + return c.IsKeyDown(key); +} + +pub fn isKeyReleased(key: i32) bool { + return c.IsKeyReleased(key); +} + pub fn drawText(text: [*c]const u8, x: i32, y: i32, fontSize: i32, color: [4]u8) void { c.DrawText(text, x, y, fontSize, .{ .r = color[0], .g = color[1], .b = color[2], .a = color[3] }); }