glfw: ziggify gamepad button enumerations

Helps hexops/mach#37

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-30 11:35:31 -07:00 committed by Stephen Gutekanst
parent 2154ee5aea
commit 210e12a437
4 changed files with 52 additions and 28 deletions

View file

@ -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);
}

View file

@ -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;

View file

@ -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;

View file

@ -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");