From 210e12a4371f06027931f3a4da7fb611ad1d9ad5 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 30 Oct 2021 11:35:31 -0700 Subject: [PATCH] glfw: ziggify gamepad button enumerations Helps hexops/mach#37 Signed-off-by: Stephen Gutekanst --- glfw/src/Joystick.zig | 16 ++++++++-- glfw/src/gamepad_axis.zig | 4 ++- glfw/src/gamepad_button.zig | 58 ++++++++++++++++++++++--------------- glfw/src/main.zig | 2 +- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/glfw/src/Joystick.zig b/glfw/src/Joystick.zig index cfdd34f9..66fe6b80 100644 --- a/glfw/src/Joystick.zig +++ b/glfw/src/Joystick.zig @@ -9,7 +9,9 @@ const c = @import("c.zig").c; const Window = @import("Window.zig"); const Error = @import("errors.zig").Error; const getError = @import("errors.zig").getError; +const Action = @import("action.zig").Action; const GamepadAxis = @import("gamepad_axis.zig").GamepadAxis; +const GamepadButton = @import("gamepad_button.zig").GamepadButton; const Joystick = @This(); @@ -44,6 +46,8 @@ pub const last = c.GLFW_JOYSTICK_LAST; /// see also: gamepad, glfwGetGamepadState const GamepadState = extern struct { /// The states of each gamepad button (see gamepad_buttons), `glfw.Action.press` or `glfw.Action.release`. + /// + /// Use the enumeration helper e.g. `.getButton(.dpad_up)` to access these indices. buttons: [15]u8, /// The states of each gamepad axis (see gamepad_axes), in the range -1.0 to 1.0 inclusive. @@ -51,9 +55,16 @@ const GamepadState = extern struct { /// Use the enumeration helper e.g. `.getAxis(.left_x)` to access these indices. axes: [6]f32, + /// Returns the state of the specified gamepad button. + pub fn getButton(self: @This(), which: GamepadButton) Action { + _ = self; + return @intToEnum(Action, self.buttons[@intCast(usize, @enumToInt(which))]); + } + /// Returns the status of the specified gamepad axis, in the range -1.0 to 1.0 inclusive. pub fn getAxis(self: @This(), which: GamepadAxis) f32 { - return self.axis[which]; + _ = self; + return self.axes[@intCast(usize, @enumToInt(which))]; } }; @@ -548,5 +559,6 @@ test "getGamepadState" { const joystick = glfw.Joystick{ .jid = glfw.Joystick.one }; _ = joystick.getGamepadState() catch |err| std.debug.print("failed to get gamepad state, joysticks not supported? error={}\n", .{err}); - _ = (GamepadState{}).getAxis(.left_x); + _ = (std.mem.zeroes(GamepadState)).getAxis(.left_x); + _ = (std.mem.zeroes(GamepadState)).getButton(.dpad_up); } diff --git a/glfw/src/gamepad_axis.zig b/glfw/src/gamepad_axis.zig index 24a524d8..97fdf374 100644 --- a/glfw/src/gamepad_axis.zig +++ b/glfw/src/gamepad_axis.zig @@ -10,5 +10,7 @@ pub const GamepadAxis = enum(c_int) { right_y = c.GLFW_GAMEPAD_AXIS_RIGHT_Y, left_trigger = c.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, right_trigger = c.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, - last = .right_trigger, }; + +/// Not in the GamepadAxis enumeration as it is a duplicate value which is forbidden. +pub const last = GamepadAxis.right_trigger; diff --git a/glfw/src/gamepad_button.zig b/glfw/src/gamepad_button.zig index 7ff26c42..ac47ebea 100644 --- a/glfw/src/gamepad_button.zig +++ b/glfw/src/gamepad_button.zig @@ -1,27 +1,37 @@ -//! Gamepad buttons. -//! -//! See glfw.getGamepadState for how these are used. - const c = @import("c.zig").c; -pub const a = c.GLFW_GAMEPAD_BUTTON_A; -pub const b = c.GLFW_GAMEPAD_BUTTON_B; -pub const x = c.GLFW_GAMEPAD_BUTTON_X; -pub const y = c.GLFW_GAMEPAD_BUTTON_Y; -pub const left_bumper = c.GLFW_GAMEPAD_BUTTON_LEFT_BUMPER; -pub const right_bumper = c.GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER; -pub const back = c.GLFW_GAMEPAD_BUTTON_BACK; -pub const start = c.GLFW_GAMEPAD_BUTTON_START; -pub const guide = c.GLFW_GAMEPAD_BUTTON_GUIDE; -pub const left_thumb = c.GLFW_GAMEPAD_BUTTON_LEFT_THUMB; -pub const right_thumb = c.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB; -pub const dpad_up = c.GLFW_GAMEPAD_BUTTON_DPAD_UP; -pub const dpad_right = c.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT; -pub const dpad_down = c.GLFW_GAMEPAD_BUTTON_DPAD_DOWN; -pub const dpad_left = c.GLFW_GAMEPAD_BUTTON_DPAD_LEFT; -pub const last = dpad_left; +/// Gamepad buttons. +/// +/// See glfw.getGamepadState for how these are used. +pub const GamepadButton = enum(c_int) { + a = c.GLFW_GAMEPAD_BUTTON_A, + b = c.GLFW_GAMEPAD_BUTTON_B, + x = c.GLFW_GAMEPAD_BUTTON_X, + y = c.GLFW_GAMEPAD_BUTTON_Y, + left_bumper = c.GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, + right_bumper = c.GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, + back = c.GLFW_GAMEPAD_BUTTON_BACK, + start = c.GLFW_GAMEPAD_BUTTON_START, + guide = c.GLFW_GAMEPAD_BUTTON_GUIDE, + left_thumb = c.GLFW_GAMEPAD_BUTTON_LEFT_THUMB, + right_thumb = c.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, + dpad_up = c.GLFW_GAMEPAD_BUTTON_DPAD_UP, + dpad_right = c.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, + dpad_down = c.GLFW_GAMEPAD_BUTTON_DPAD_DOWN, + dpad_left = c.GLFW_GAMEPAD_BUTTON_DPAD_LEFT, +}; -pub const cross = a; -pub const circle = b; -pub const square = x; -pub const triangle = y; +/// Not in the GamepadAxis enumeration as it is a duplicate value which is forbidden. +pub const last = GamepadButton.dpad_left; + +/// Not in the GamepadAxis enumeration as it is a duplicate value which is forbidden. +pub const cross = GamepadButton.a; + +/// Not in the GamepadAxis enumeration as it is a duplicate value which is forbidden. +pub const circle = GamepadButton.b; + +/// Not in the GamepadAxis enumeration as it is a duplicate value which is forbidden. +pub const square = GamepadButton.x; + +/// Not in the GamepadAxis enumeration as it is a duplicate value which is forbidden. +pub const triangle = GamepadButton.y; diff --git a/glfw/src/main.zig b/glfw/src/main.zig index feeeb63c..c1b953d3 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -11,7 +11,7 @@ const getError = @import("errors.zig").getError; pub const Action = @import("action.zig").Action; pub const GamepadAxis = @import("gamepad_axis.zig").GamepadAxis; -pub const gamepad_button = @import("gamepad_button.zig"); +pub const GamepadButton = @import("gamepad_button.zig").GamepadButton; pub const GammaRamp = @import("GammaRamp.zig"); pub const hat = @import("hat.zig"); pub const Image = @import("Image.zig");