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);
|
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;
|
||||||
|
|||||||
50
src/main.zig
50
src/main.zig
@@ -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,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();
|
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| {
|
||||||
switch (action) {
|
if (ctx.focused) {
|
||||||
.press => ctx.game.onKeyDown(key_code, mods),
|
switch (action) {
|
||||||
.release => ctx.game.onKeyUp(key_code, mods),
|
.press => ctx.game.onKeyDown(key_code, mods),
|
||||||
.repeat => {},
|
.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 {
|
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| {
|
||||||
const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos);
|
if (ctx.focused) {
|
||||||
const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos);
|
const dx: f32 = @floatCast(cursor_xpos - ctx.cursor_last_xpos);
|
||||||
ctx.game.onMouseMove(dx, dy);
|
const dy: f32 = @floatCast(cursor_ypos - ctx.cursor_last_ypos);
|
||||||
ctx.cursor_last_xpos = cursor_xpos;
|
ctx.game.onMouseMove(dx, dy);
|
||||||
ctx.cursor_last_ypos = cursor_ypos;
|
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 => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user