Mouse button handling
This commit is contained in:
12
src/Game.zig
12
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;
|
||||
|
||||
32
src/main.zig
32
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,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();
|
||||
ctx.cursor_last_xpos = cursor_last_xpos;
|
||||
ctx.cursor_last_ypos = cursor_last_ypos;
|
||||
ctx.focused = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (maybe_ctx) |ctx| {
|
||||
if (ctx.focused) {
|
||||
switch (action) {
|
||||
.press => ctx.game.onKeyDown(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 {
|
||||
const maybe_ctx = window.getUserPointer(CallbackContext);
|
||||
if (maybe_ctx) |ctx| {
|
||||
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);
|
||||
@@ -126,3 +133,28 @@ fn cursorPosCallback(window: *glfw.Window, cursor_xpos: f64, cursor_ypos: f64) c
|
||||
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 => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user