Skip to content
33 changes: 20 additions & 13 deletions src/graphics/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub const Gamepad = struct {
if ((newState.buttons[btn] == 0) and (oldState.buttons[btn] != 0)) {
nextGamepadListener.?(null, @intCast(btn));
nextGamepadListener = null;
break;
return;
}
}
}
Expand All @@ -91,7 +91,7 @@ pub const Gamepad = struct {
if (newAxis != 0 and oldAxis == 0) {
nextGamepadListener.?(.{.axis = @intCast(axis), .positive = newState.axes[axis] > 0}, -1);
nextGamepadListener = null;
break;
return;
}
}
}
Expand Down Expand Up @@ -510,19 +510,20 @@ pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
const textKeyPressedInTextField = main.gui.selectedTextInput != null and c.glfwGetKeyName(glfw_key, scancode) != null;
const isGrabbed = grabbed;
if (action == c.GLFW_PRESS or action == c.GLFW_RELEASE) {
if (action == c.GLFW_PRESS) {
if (nextKeypressListener) |listener| {
listener(glfw_key, -1, scancode);
nextKeypressListener = null;
return;
}
}
for (&main.KeyBoard.keys) |*key| {
if (glfw_key == key.key) {
if (glfw_key != c.GLFW_KEY_UNKNOWN or scancode == key.scancode) {
key.setPressed(action == c.GLFW_PRESS, isGrabbed, mods, textKeyPressedInTextField);
}
}
}
if (action == c.GLFW_PRESS) {
if (nextKeypressListener) |listener| {
listener(glfw_key, -1, scancode);
nextKeypressListener = null;
}
}
} else if (action == c.GLFW_REPEAT) {
for (&main.KeyBoard.keys) |*key| {
if (glfw_key == key.key) {
Expand Down Expand Up @@ -583,15 +584,16 @@ pub const GLFWCallbacks = struct { // MARK: GLFWCallbacks
const mods: Key.Modifiers = @bitCast(@as(u6, @intCast(_mods)));
const isGrabbed = grabbed;
if (action == c.GLFW_PRESS or action == c.GLFW_RELEASE) {
for (&main.KeyBoard.keys) |*key| {
if (button == key.mouseButton) {
key.setPressed(action == c.GLFW_PRESS, isGrabbed, mods, false);
}
}
if (action == c.GLFW_PRESS) {
if (nextKeypressListener) |listener| {
listener(c.GLFW_KEY_UNKNOWN, button, 0);
nextKeypressListener = null;
return;
}
}
for (&main.KeyBoard.keys) |*key| {
if (button == key.mouseButton) {
key.setPressed(action == c.GLFW_PRESS, isGrabbed, mods, false);
}
}
}
Expand Down Expand Up @@ -647,6 +649,11 @@ pub fn setNextGamepadListener(listener: ?*const fn (?GamepadAxis, c_int) void) !
nextGamepadListener = listener;
}

pub fn resetNextInputListenters() void {
nextGamepadListener = null;
nextKeypressListener = null;
}

fn updateCursor() void {
if (grabbed) {
c.glfwSetInputMode(window, c.GLFW_CURSOR, c.GLFW_CURSOR_DISABLED);
Expand Down
33 changes: 27 additions & 6 deletions src/gui/windows/controls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ fn sensitivityFormatter(allocator: main.heap.NeverFailingAllocator, value: f32)
return std.fmt.allocPrint(allocator.allocator, "{s} Sensitivity: {d:.0}%", .{if (editingKeyboard) "Mouse" else "Controller", value*100}) catch unreachable;
}

fn abortBindingProcess() void {
selectedKey = null;
main.Window.resetNextInputListenters();
needsUpdate = true;
}

fn toggleKeyboard() void {
editingKeyboard = !editingKeyboard;
needsUpdate = true;
abortBindingProcess();
}

fn unbindKey(keyPtr: usize) void {
var key: ?*main.Window.Key = @ptrFromInt(keyPtr);
if (editingKeyboard) {
Expand All @@ -95,7 +102,7 @@ fn unbindKey(keyPtr: usize) void {
needsUpdate = true;
}

pub fn onOpen() void {
fn initWindow() void {
const controlsListWidth: u32 = 256;
const keybindButtonWidth: u32 = 160;
const unbindButtonWidth: u32 = 64;
Expand All @@ -111,7 +118,11 @@ pub fn onOpen() void {
}
for (&main.KeyBoard.keys) |*key| {
const label = Label.init(.{0, 0}, keybindButtonWidth, key.name, .left);
const button = if (key == selectedKey) (Button.initText(.{16, 0}, keybindButtonWidth, "...", .{})) else (Button.initText(.{16, 0}, keybindButtonWidth, if (editingKeyboard) key.getName() else key.getGamepadName(), if (editingKeyboard) .initWithPtr(keyFunction, key) else .initWithPtr(gamepadFunction, key)));
const button = if (key == selectedKey)
Button.initText(.{16, 0}, keybindButtonWidth, "...", .{})
else
Button.initText(.{16, 0}, keybindButtonWidth, if (editingKeyboard) key.getName() else key.getGamepadName(), if (editingKeyboard) .initWithPtr(keyFunction, key) else .initWithPtr(gamepadFunction, key));

const unbindBtn = Button.initText(.{16, 0}, unbindButtonWidth, "Unbind", .initWithPtr(unbindKey, key));
const row = HorizontalList.init();
row.add(label);
Expand All @@ -126,18 +137,28 @@ pub fn onOpen() void {
gui.updateWindowPositions();
}

pub fn onClose() void {
fn deinitWindow() void {
if (window.rootComponent) |*comp| {
comp.deinit();
}
}

pub fn onOpen() void {
abortBindingProcess();
initWindow();
}

pub fn onClose() void {
abortBindingProcess();
deinitWindow();
}

pub fn render() void {
if (needsUpdate) {
needsUpdate = false;
const oldScroll = window.rootComponent.?.verticalList.scrollBar.currentState;
onClose();
onOpen();
deinitWindow();
initWindow();
window.rootComponent.?.verticalList.scrollBar.currentState = oldScroll;
}
}
Loading