glfw: ziggify Joystick hat bitmasks

Helps hexops/mach#37

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-30 12:04:41 -07:00 committed by Stephen Gutekanst
parent 210e12a437
commit 32fa90eca9
3 changed files with 66 additions and 31 deletions

View file

@ -1,16 +1,46 @@
//! Joystick hat states
//!
//! See glfw.getJoystickHats for how these are used.
const c = @import("c.zig").c;
pub const centered = c.GLFW_HAT_CENTERED;
pub const up = c.GLFW_HAT_UP;
pub const right = c.GLFW_HAT_RIGHT;
pub const down = c.GLFW_HAT_DOWN;
pub const left = c.GLFW_HAT_LEFT;
// must be in sync with GLFW C constants in hat state group, search for "@defgroup hat_state Joystick hat states"
/// A bitmask of all Joystick hat states
///
/// See glfw.Joystick.getHats for how these are used.
pub const Hat = packed struct {
centered: bool align(@alignOf(u8)) = false,
up: bool = false,
right: bool = false,
down: bool = false,
left: bool = false,
pub const right_up = right | up;
pub const right_down = right | down;
pub const left_up = left | up;
pub const left_down = left | down;
inline fn verifyIntType(comptime IntType: type) void {
comptime {
switch (@typeInfo(IntType)) {
.Int => {},
else => @compileError("Int was not of int type"),
}
}
}
pub inline fn toInt(comptime IntType: type, self: Hat) IntType {
verifyIntType(IntType);
return @bitCast(IntType, self);
}
pub inline fn fromInt(flags: anytype) Hat {
verifyIntType(@TypeOf(flags));
return @bitCast(Hat, flags);
}
};
/// Holds all GLFW hat values in their raw form.
pub const RawHats = struct {
pub const centered = c.GLFW_HAT_CENTERED;
pub const up = c.GLFW_HAT_UP;
pub const right = c.GLFW_HAT_RIGHT;
pub const down = c.GLFW_HAT_DOWN;
pub const left = c.GLFW_HAT_LEFT;
pub const right_up = right | up;
pub const right_down = right | down;
pub const left_up = left | up;
pub const left_down = left | down;
};