diff --git a/examples/advanced-gen-texture-light/main.zig b/examples/advanced-gen-texture-light/main.zig index a43a5076..97789ded 100755 --- a/examples/advanced-gen-texture-light/main.zig +++ b/examples/advanced-gen-texture-light/main.zig @@ -88,6 +88,7 @@ pub fn update(app: *App, engine: *mach.Engine) !bool { }, else => {}, }, + else => {}, } } diff --git a/src/enums.zig b/src/enums.zig index 29221085..0029d2f6 100644 --- a/src/enums.zig +++ b/src/enums.zig @@ -20,6 +20,17 @@ pub const VSyncMode = enum { triple, }; +pub const MouseButton = enum { + left, + right, + middle, + four, + five, + six, + seven, + eight, +}; + pub const Key = enum { a, b, diff --git a/src/platform/native.zig b/src/platform/native.zig index ca478edc..e6fd5a33 100644 --- a/src/platform/native.zig +++ b/src/platform/native.zig @@ -216,6 +216,55 @@ pub const Platform = struct { }.callback; platform.window.setKeyCallback(callback); + const mouse_motion_callback = struct { + fn callback(window: glfw.Window, xpos: f64, ypos: f64) void { + const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform; + pf.pushEvent(.{ + .mouse_motion = .{ + .x = xpos, + .y = ypos, + }, + }); + } + }.callback; + platform.window.setCursorPosCallback(mouse_motion_callback); + + const mouse_button_callback = struct { + fn callback(window: glfw.Window, button: glfw.mouse_button.MouseButton, action: glfw.Action, mods: glfw.Mods) void { + const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform; + + switch (action) { + .press => pf.pushEvent(.{ + .mouse_press = .{ + .button = toMachButton(button), + }, + }), + .release => pf.pushEvent(.{ + .mouse_release = .{ + .button = toMachButton(button), + }, + }), + else => {}, + } + + _ = mods; + } + }.callback; + platform.window.setMouseButtonCallback(mouse_button_callback); + + const scroll_callback = struct { + fn callback(window: glfw.Window, xoffset: f64, yoffset: f64) void { + const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform; + pf.pushEvent(.{ + .scroll = .{ + .xoffset = xoffset, + .yoffset = yoffset, + }, + }); + } + }.callback; + platform.window.setScrollCallback(scroll_callback); + const size_callback = struct { fn callback(window: glfw.Window, width: i32, height: i32) void { const pf = (window.getUserPointer(UserPtr) orelse unreachable).platform; @@ -264,6 +313,19 @@ pub const Platform = struct { return null; } + fn toMachButton(button: glfw.mouse_button.MouseButton) enums.MouseButton { + return switch (button) { + .left => .left, + .right => .right, + .middle => .middle, + .four => .four, + .five => .five, + .six => .six, + .seven => .seven, + .eight => .eight, + }; + } + fn toMachKey(key: glfw.Key) enums.Key { return switch (key) { .a => .a, diff --git a/src/structs.zig b/src/structs.zig index 85593aae..b364f302 100644 --- a/src/structs.zig +++ b/src/structs.zig @@ -51,4 +51,19 @@ pub const Event = union(enum) { key_release: struct { key: enums.Key, }, + mouse_motion: struct { + // These are in window coordinates (not framebuffer coords) + x: f64, + y: f64, + }, + mouse_press: struct { + button: enums.MouseButton, + }, + mouse_release: struct { + button: enums.MouseButton, + }, + scroll: struct { + xoffset: f64, + yoffset: f64, + }, };