Skip to content

Commit

Permalink
Rework how gui callbacks check whether the player is in the game or not.
Browse files Browse the repository at this point in the history
It now behaves correctly on key collisions where one of them changes the grab state.

fixes #1001
  • Loading branch information
IntegratedQuantum committed Feb 2, 2025
1 parent 44d0504 commit 3a8d4d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
2 changes: 0 additions & 2 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ pub const Player = struct { // MARK: Player
}

pub fn placeBlock() void {
if(!main.Window.grabbed) return;
if(main.renderer.MeshSelection.selectedBlockPos) |blockPos| {
const block = main.renderer.mesh_storage.getBlock(blockPos[0], blockPos[1], blockPos[2]) orelse main.blocks.Block{.typ = 0, .data = 0};
const gui = block.gui();
Expand All @@ -527,7 +526,6 @@ pub const Player = struct { // MARK: Player
}

pub fn breakBlock(deltaTime: f64) void {
if(!main.Window.grabbed) return;
inventory.breakBlock(selectedSlot, deltaTime);
}

Expand Down
76 changes: 38 additions & 38 deletions src/graphics/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub const Gamepad = struct {
}
}
}
const isGrabbed = grabbed;
for(&main.KeyBoard.keys) |*key| {
if(key.gamepadAxis == null) {
if(key.gamepadButton >= 0) {
Expand All @@ -81,15 +82,7 @@ pub const Gamepad = struct {
if(oldPressed != newPressed) {
key.pressed = newPressed;
key.value = if(newPressed) 1.0 else 0.0;
if(key.pressed) {
if(key.pressAction) |pressAction| {
pressAction();
}
} else {
if(key.releaseAction) |releaseAction| {
releaseAction();
}
}
key.action(if(key.pressed) .press else .release, isGrabbed, .{});
}
}
} else {
Expand All @@ -107,15 +100,7 @@ pub const Gamepad = struct {
const newPressed = newAxis > 0.5;
if (oldPressed != newPressed) {
key.pressed = newPressed;
if (newPressed) {
if (key.pressAction) |pressAction| {
pressAction();
}
} else {
if (key.releaseAction) |releaseAction| {
releaseAction();
}
}
key.action(if(key.pressed) .press else .release, isGrabbed, .{});
}
if (newAxis != oldAxis) {
key.value = newAxis;
Expand Down Expand Up @@ -293,6 +278,8 @@ pub const Key = struct { // MARK: Key
releaseAction: ?*const fn() void = null,
pressAction: ?*const fn() void = null,
repeatAction: ?*const fn(Modifiers) void = null,
notifyRequirement: Requirement = .always,
grabbedOnPress: bool = false,

pub const Modifiers = packed struct(u6) {
shift: bool = false,
Expand All @@ -302,6 +289,19 @@ pub const Key = struct { // MARK: Key
capsLock: bool = false,
numLock: bool = false,
};
const Requirement = enum {
always,
inGame,
inMenu,

fn met(self: Requirement, isGrabbed: bool) bool {
switch(self) {
.always => return true,
.inGame => return isGrabbed,
.inMenu => return !isGrabbed,
}
}
};
pub fn getGamepadName(self: Key) []const u8 {
if(self.gamepadAxis != null) {
const positive = self.gamepadAxis.?.positive;
Expand Down Expand Up @@ -410,6 +410,16 @@ pub const Key = struct { // MARK: Key
};
}
}

fn action(self: *Key, typ: enum{press, release, repeat}, isGrabbed: bool, mods: Modifiers) void {
if(typ == .press) self.grabbedOnPress = isGrabbed;
if(!self.notifyRequirement.met(self.grabbedOnPress)) return;
switch(typ) {
.press => if(self.pressAction) |a| a(),
.release => if(self.releaseAction) |a| a(),
.repeat => if(self.repeatAction) |a| a(mods),
}
}
};

pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
Expand All @@ -419,18 +429,15 @@ pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
fn keyCallback(_: ?*c.GLFWwindow, glfw_key: c_int, scancode: c_int, action: c_int, _mods: c_int) callconv(.C) void {
const mods: Key.Modifiers = @bitCast(@as(u6, @intCast(_mods)));
if(!mods.control and main.gui.selectedTextInput != null and c.glfwGetKeyName(glfw_key, scancode) != null) return; // Don't send events for keys that are used in writing letters.
const isGrabbed = grabbed;
if(action == c.GLFW_PRESS) {
for(&main.KeyBoard.keys) |*key| {
if(glfw_key == key.key) {
if(glfw_key != c.GLFW_KEY_UNKNOWN or scancode == key.scancode) {
key.pressed = true;
key.value = 1.0;
if(key.pressAction) |pressAction| {
pressAction();
}
if(key.repeatAction) |repeatAction| {
repeatAction(mods);
}
key.action(.press, isGrabbed, mods);
key.action(.repeat, isGrabbed, mods);
}
}
}
Expand All @@ -444,19 +451,15 @@ pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
if(glfw_key != c.GLFW_KEY_UNKNOWN or scancode == key.scancode) {
key.pressed = false;
key.value = 0.0;
if(key.releaseAction) |releaseAction| {
releaseAction();
}
key.action(.release, isGrabbed, mods);
}
}
}
} else if(action == c.GLFW_REPEAT) {
for(&main.KeyBoard.keys) |*key| {
if(glfw_key == key.key) {
if(glfw_key != c.GLFW_KEY_UNKNOWN or scancode == key.scancode) {
if(key.repeatAction) |repeatAction| {
repeatAction(mods);
}
key.action(.repeat, isGrabbed, mods);
}
}
}
Expand Down Expand Up @@ -502,16 +505,15 @@ pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
currentPos = newPos;
lastUsedMouse = true;
}
fn mouseButton(_: ?*c.GLFWwindow, button: c_int, action: c_int, mods: c_int) callconv(.C) void {
_ = mods;
fn mouseButton(_: ?*c.GLFWwindow, button: c_int, action: c_int, _mods: c_int) callconv(.C) void {
const mods: Key.Modifiers = @bitCast(@as(u6, @intCast(_mods)));
const isGrabbed = grabbed;
if(action == c.GLFW_PRESS) {
for(&main.KeyBoard.keys) |*key| {
if(button == key.mouseButton) {
key.pressed = true;
key.value = 1.0;
if(key.pressAction) |pressAction| {
pressAction();
}
key.action(.press, isGrabbed, mods);
}
}
if(nextKeypressListener) |listener| {
Expand All @@ -523,9 +525,7 @@ pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
if(button == key.mouseButton) {
key.pressed = false;
key.value = 0.0;
if(key.releaseAction) |releaseAction| {
releaseAction();
}
key.action(.release, isGrabbed, mods);
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/gui/gui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ pub const textCallbacks = struct {
};

pub fn mainButtonPressed() void {
if(main.Window.grabbed) return;
inventory.update();
selectedWindow = null;
selectedTextInput = null;
Expand All @@ -478,7 +477,6 @@ pub fn mainButtonPressed() void {
}

pub fn mainButtonReleased() void {
if(main.Window.grabbed) return;
inventory.applyChanges(true);
const oldWindow = selectedWindow;
selectedWindow = null;
Expand All @@ -499,12 +497,10 @@ pub fn mainButtonReleased() void {
}

pub fn secondaryButtonPressed() void {
if(main.Window.grabbed) return;
inventory.update();
}

pub fn secondaryButtonReleased() void {
if(main.Window.grabbed) return;
inventory.applyChanges(false);
}

Expand Down
12 changes: 6 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,21 @@ pub const KeyBoard = struct { // MARK: KeyBoard
.{.name = "hyperSpeed", .key = c.GLFW_KEY_H, .pressAction = &game.hyperSpeedToggle},
.{.name = "fall", .key = c.GLFW_KEY_LEFT_SHIFT, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB},
.{.name = "shift", .key = c.GLFW_KEY_LEFT_SHIFT, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_RIGHT_THUMB},
.{.name = "fullscreen", .key = c.GLFW_KEY_F11, .pressAction = &Window.toggleFullscreen},
.{.name = "placeBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT, .gamepadAxis = .{.axis = c.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER}, .pressAction = &game.pressPlace, .releaseAction = &game.releasePlace},
.{.name = "breakBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .gamepadAxis = .{.axis = c.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER}, .pressAction = &game.pressBreak, .releaseAction = &game.releaseBreak},
.{.name = "acquireSelectedBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_MIDDLE, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_DPAD_LEFT, .pressAction = &game.pressAcquireSelectedBlock},
.{.name = "placeBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT, .gamepadAxis = .{.axis = c.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER}, .pressAction = &game.pressPlace, .releaseAction = &game.releasePlace, .notifyRequirement = .inGame},
.{.name = "breakBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .gamepadAxis = .{.axis = c.GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER}, .pressAction = &game.pressBreak, .releaseAction = &game.releaseBreak, .notifyRequirement = .inGame},
.{.name = "acquireSelectedBlock", .mouseButton = c.GLFW_MOUSE_BUTTON_MIDDLE, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_DPAD_LEFT, .pressAction = &game.pressAcquireSelectedBlock, .notifyRequirement = .inGame},

.{.name = "takeBackgroundImage", .key = c.GLFW_KEY_PRINT_SCREEN, .pressAction = &takeBackgroundImageFn},
.{.name = "fullscreen", .key = c.GLFW_KEY_F11, .pressAction = &Window.toggleFullscreen},

// Gui:
.{.name = "escape", .key = c.GLFW_KEY_ESCAPE, .pressAction = &escape, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_B},
.{.name = "openInventory", .key = c.GLFW_KEY_E, .pressAction = &openInventory, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_X},
.{.name = "openCreativeInventory(aka cheat inventory)", .key = c.GLFW_KEY_C, .pressAction = &openCreativeInventory, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_Y},
.{.name = "openChat", .key = c.GLFW_KEY_T, .releaseAction = &openChat},
.{.name = "openCommand", .key = c.GLFW_KEY_SLASH, .releaseAction = &openCommand},
.{.name = "mainGuiButton", .mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .pressAction = &gui.mainButtonPressed, .releaseAction = &gui.mainButtonReleased, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_A},
.{.name = "secondaryGuiButton", .mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT, .pressAction = &gui.secondaryButtonPressed, .releaseAction = &gui.secondaryButtonReleased, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_Y},
.{.name = "mainGuiButton", .mouseButton = c.GLFW_MOUSE_BUTTON_LEFT, .pressAction = &gui.mainButtonPressed, .releaseAction = &gui.mainButtonReleased, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_A, .notifyRequirement = .inMenu},
.{.name = "secondaryGuiButton", .mouseButton = c.GLFW_MOUSE_BUTTON_RIGHT, .pressAction = &gui.secondaryButtonPressed, .releaseAction = &gui.secondaryButtonReleased, .gamepadButton = c.GLFW_GAMEPAD_BUTTON_Y, .notifyRequirement = .inMenu},
// gamepad gui.
.{.name = "scrollUp", .gamepadAxis = .{.axis = c.GLFW_GAMEPAD_AXIS_RIGHT_Y, .positive = false}},
.{.name = "scrollDown", .gamepadAxis = .{.axis = c.GLFW_GAMEPAD_AXIS_RIGHT_Y, .positive = true}},
Expand Down

0 comments on commit 3a8d4d3

Please sign in to comment.