diff --git a/src/Game.zig b/src/Game.zig index c2db7a0..32ff80b 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -864,6 +864,18 @@ pub fn onMouseMove(self: *Game, dx: f32, dy: f32) void { self.camera_yaw = @mod(self.camera_yaw, 2 * std.math.pi); } +pub fn onMouseDown(self: *Game, button: glfw.MouseButton, mods: glfw.Mods) void { + _ = self; + _ = button; + _ = mods; +} + +pub fn onMouseUp(self: *Game, button: glfw.MouseButton, mods: glfw.Mods) void { + _ = self; + _ = button; + _ = mods; +} + fn render(self: *Game) !void { const engine = self.engine; const extent = self.swapchain.extent; diff --git a/src/main.zig b/src/main.zig index b720337..da85d67 100644 --- a/src/main.zig +++ b/src/main.zig @@ -49,6 +49,7 @@ pub fn main() !void { _ = window.setKeyCallback(keyCallback); _ = window.setCursorPosCallback(cursorPosCallback); + _ = window.setMouseButtonCallback(mouseButtonCallback); var engine = try Engine.init(allocator, window); defer engine.deinit(); @@ -63,6 +64,7 @@ pub fn main() !void { .game = &game, .cursor_last_xpos = cursor_last_xpos, .cursor_last_ypos = cursor_last_ypos, + .focused = (window.getInputMode(.cursor) catch .normal) == .disabled, }; }; window.setUserPointer(&callback_context); @@ -90,6 +92,7 @@ pub fn main() !void { const CallbackContext = struct { game: *Game, + focused: bool, cursor_last_xpos: f64, cursor_last_ypos: f64, }; @@ -103,15 +106,18 @@ fn keyCallback(window: *glfw.Window, key_code: glfw.Key, _: c_int, action: glfw. const cursor_last_xpos, const cursor_last_ypos = window.getCursorPos(); ctx.cursor_last_xpos = cursor_last_xpos; ctx.cursor_last_ypos = cursor_last_ypos; + ctx.focused = false; } return; } if (maybe_ctx) |ctx| { - switch (action) { - .press => ctx.game.onKeyDown(key_code, mods), - .release => ctx.game.onKeyUp(key_code, mods), - .repeat => {}, + if (ctx.focused) { + switch (action) { + .press => ctx.game.onKeyDown(key_code, mods), + .release => ctx.game.onKeyUp(key_code, mods), + .repeat => {}, + } } } } @@ -119,10 +125,36 @@ fn keyCallback(window: *glfw.Window, key_code: glfw.Key, _: c_int, action: glfw. fn cursorPosCallback(window: *glfw.Window, cursor_xpos: f64, cursor_ypos: f64) callconv(.c) void { const maybe_ctx = window.getUserPointer(CallbackContext); if (maybe_ctx) |ctx| { - const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos); - const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos); - ctx.game.onMouseMove(dx, dy); - ctx.cursor_last_xpos = cursor_xpos; - ctx.cursor_last_ypos = cursor_ypos; + if (ctx.focused) { + const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos); + const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos); + ctx.game.onMouseMove(dx, dy); + ctx.cursor_last_xpos = cursor_xpos; + ctx.cursor_last_ypos = cursor_ypos; + } + } +} + +fn mouseButtonCallback(window: *glfw.Window, button: glfw.MouseButton, action: glfw.Action, mods: glfw.Mods) callconv(.c) void { + const maybe_ctx = window.getUserPointer(CallbackContext); + + if (button == .left and action == .press and (window.getInputMode(.cursor) catch .normal) == .normal) { + window.setInputMode(.cursor, .disabled) catch {}; + if (maybe_ctx) |ctx| { + const cursor_last_xpos, const cursor_last_ypos = window.getCursorPos(); + ctx.cursor_last_xpos = cursor_last_xpos; + ctx.cursor_last_ypos = cursor_last_ypos; + ctx.focused = true; + } + } + + if (maybe_ctx) |ctx| { + if (ctx.focused) { + switch (action) { + .press => ctx.game.onMouseDown(button, mods), + .release => ctx.game.onMouseUp(button, mods), + .repeat => {}, + } + } } }