Mouse button handling

This commit is contained in:
2025-11-28 15:03:11 +01:00
parent 652e5e82bd
commit 94c43a170f
2 changed files with 53 additions and 9 deletions

View File

@@ -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); 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 { fn render(self: *Game) !void {
const engine = self.engine; const engine = self.engine;
const extent = self.swapchain.extent; const extent = self.swapchain.extent;

View File

@@ -49,6 +49,7 @@ pub fn main() !void {
_ = window.setKeyCallback(keyCallback); _ = window.setKeyCallback(keyCallback);
_ = window.setCursorPosCallback(cursorPosCallback); _ = window.setCursorPosCallback(cursorPosCallback);
_ = window.setMouseButtonCallback(mouseButtonCallback);
var engine = try Engine.init(allocator, window); var engine = try Engine.init(allocator, window);
defer engine.deinit(); defer engine.deinit();
@@ -63,6 +64,7 @@ pub fn main() !void {
.game = &game, .game = &game,
.cursor_last_xpos = cursor_last_xpos, .cursor_last_xpos = cursor_last_xpos,
.cursor_last_ypos = cursor_last_ypos, .cursor_last_ypos = cursor_last_ypos,
.focused = (window.getInputMode(.cursor) catch .normal) == .disabled,
}; };
}; };
window.setUserPointer(&callback_context); window.setUserPointer(&callback_context);
@@ -90,6 +92,7 @@ pub fn main() !void {
const CallbackContext = struct { const CallbackContext = struct {
game: *Game, game: *Game,
focused: bool,
cursor_last_xpos: f64, cursor_last_xpos: f64,
cursor_last_ypos: f64, cursor_last_ypos: f64,
}; };
@@ -103,11 +106,13 @@ fn keyCallback(window: *glfw.Window, key_code: glfw.Key, _: c_int, action: glfw.
const cursor_last_xpos, const cursor_last_ypos = window.getCursorPos(); const cursor_last_xpos, const cursor_last_ypos = window.getCursorPos();
ctx.cursor_last_xpos = cursor_last_xpos; ctx.cursor_last_xpos = cursor_last_xpos;
ctx.cursor_last_ypos = cursor_last_ypos; ctx.cursor_last_ypos = cursor_last_ypos;
ctx.focused = false;
} }
return; return;
} }
if (maybe_ctx) |ctx| { if (maybe_ctx) |ctx| {
if (ctx.focused) {
switch (action) { switch (action) {
.press => ctx.game.onKeyDown(key_code, mods), .press => ctx.game.onKeyDown(key_code, mods),
.release => ctx.game.onKeyUp(key_code, mods), .release => ctx.game.onKeyUp(key_code, mods),
@@ -115,10 +120,12 @@ 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 { fn cursorPosCallback(window: *glfw.Window, cursor_xpos: f64, cursor_ypos: f64) callconv(.c) void {
const maybe_ctx = window.getUserPointer(CallbackContext); const maybe_ctx = window.getUserPointer(CallbackContext);
if (maybe_ctx) |ctx| { if (maybe_ctx) |ctx| {
if (ctx.focused) {
const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos); const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos);
const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos); const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos);
ctx.game.onMouseMove(dx, dy); ctx.game.onMouseMove(dx, dy);
@@ -126,3 +133,28 @@ fn cursorPosCallback(window: *glfw.Window, cursor_xpos: f64, cursor_ypos: f64) c
ctx.cursor_last_ypos = cursor_ypos; 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 => {},
}
}
}
}