glfw: enums (#41)

* move key values in an enum

* bitmask for key modifiers

* export Key type and move key fns

instead of exporting the key file, export Key enum.
functions related to the Key type are moved into the enum as well so that they get exported with the type
This commit is contained in:
Aksel Hjerpbakk 2021-10-24 15:23:20 +02:00 committed by GitHub
parent 388f3aa659
commit 1a3f391891
Failed to generate hash of commit
4 changed files with 380 additions and 225 deletions

View file

@ -4,20 +4,165 @@
const c = @import("c.zig").c;
/// If this bit is set one or more Shift keys were held down.
pub const shift = c.GLFW_MOD_SHIFT;
// must be in sync with https://www.glfw.org/docs/3.3/group__mods.html
/// A bitmask of all key modifiers
pub const Mods = packed struct {
shift: bool align(@alignOf(c_int)) = false,
control: bool = false,
alt: bool = false,
super: bool = false,
caps_lock: bool = false,
num_lock: bool = false,
/// If this bit is set one or more Control keys were held down.
pub const control = c.GLFW_MOD_CONTROL;
inline fn verifyIntType(comptime IntType: type) void {
comptime {
switch (@typeInfo(IntType)) {
.Int => {},
else => @compileError("Int was not of int type"),
}
}
}
/// If this bit is set one or more Alt keys were held down.
pub const alt = c.GLFW_MOD_ALT;
pub inline fn toInt(comptime IntType: type, self: Mods) IntType {
verifyIntType(IntType);
return @bitCast(IntType, self);
}
/// If this bit is set one or more Super keys were held down.
pub const super = c.GLFW_MOD_SUPER;
pub inline fn fromInt(flags: anytype) Mods {
verifyIntType(@TypeOf(flags));
return @bitCast(Mods, flags);
}
};
/// If this bit is set the Caps Lock key is enabled and the glfw.lock_key_mods input mode is set.
pub const caps_lock = c.GLFW_MOD_CAPS_LOCK;
/// Hold all glfw values as is
pub const RawMods = struct {
/// If this bit is set one or more Shift keys were held down.
pub const shift = c.GLFW_MOD_SHIFT;
/// If this bit is set the Num Lock key is enabled and the glfw.lock_key_mods input mode is set.
pub const num_lock = c.GLFW_MOD_NUM_LOCK;
/// If this bit is set one or more Control keys were held down.
pub const control = c.GLFW_MOD_CONTROL;
/// If this bit is set one or more Alt keys were held down.
pub const alt = c.GLFW_MOD_ALT;
/// If this bit is set one or more Super keys were held down.
pub const super = c.GLFW_MOD_SUPER;
/// If this bit is set the Caps Lock key is enabled and the glfw.lock_key_mods input mode is set.
pub const caps_lock = c.GLFW_MOD_CAPS_LOCK;
/// If this bit is set the Num Lock key is enabled and the glfw.lock_key_mods input mode is set.
pub const num_lock = c.GLFW_MOD_NUM_LOCK;
};
test "shift int to bitmask" {
const std = @import("std");
const int_mod = RawMods.shift;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == true);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == false);
std.testing.expect(mod.super == false);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == false);
}
test "shift int and alt to bitmask" {
const std = @import("std");
const int_mod = RawMods.shift | RawMods.alt;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == true);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == true);
std.testing.expect(mod.super == false);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == false);
}
test "super int to bitmask" {
const std = @import("std");
const int_mod = RawMods.super;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == false);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == false);
std.testing.expect(mod.super == true);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == false);
}
test "num lock int to bitmask" {
const std = @import("std");
const int_mod = RawMods.num_lock;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == false);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == false);
std.testing.expect(mod.super == false);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == true);
}
test "all int to bitmask" {
const std = @import("std");
const int_mod = RawMods.shift | RawMods.control |
RawMods.alt | RawMods.super |
RawMods.caps_lock | RawMods.num_lock;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == true);
std.testing.expect(mod.control == true);
std.testing.expect(mod.alt == true);
std.testing.expect(mod.super == true);
std.testing.expect(mod.caps_lock == true);
std.testing.expect(mod.num_lock == true);
}
test "shift bitmask to int" {
const std = @import("std");
const mod = Mods{ .shift = true };
const int_mod = mod.toInt(c_int);
std.testing.expectEqual(int_mod, RawMods.shift);
}
test "shift and alt bitmask to int" {
const std = @import("std");
const mod = Mods{ .shift = true, .alt = true };
const int_mod = mod.toInt(c_int);
std.testing.expectEqual(int_mod, RawMods.shift | RawMods.alt);
}
test "all bitmask to int" {
const std = @import("std");
const mod = Mods{
.shift = true,
.control = true,
.alt = true,
.super = true,
.caps_lock = true,
.num_lock = true,
};
const int_mod = mod.toInt(c_int);
const expected = RawMods.shift | RawMods.control |
RawMods.alt | RawMods.super |
RawMods.caps_lock | RawMods.num_lock;
std.testing.expectEqual(int_mod, expected);
}