From a81679191497b565238db29077ee7318be171f89 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 10 Jan 2022 12:20:52 -0700 Subject: [PATCH] Added setMouseScrollCallback --- lib/minifb/src/minifb.zig | 5 ++++ src/main.zig | 62 +++++++++++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index 744e422..7ad04b1 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -148,6 +148,11 @@ pub fn Window(comptime TUserData: type) type { c.mfb_set_mouse_move_callback(self.cwin, @ptrCast(c.mfb_mouse_move_func, callback)); } + pub const MouseScrollFunc = fn (win: Window(TUserData), key_mod: KeyMod, delta_x: f32, delty_y: f32) callconv(.C) void; + pub fn setMouseScrollCallback(self: Window(TUserData), callback: MouseScrollFunc) void { + c.mfb_set_mouse_scroll_callback(self.cwin, @ptrCast(c.mfb_mouse_scroll_func, callback)); + } + pub fn setViewport(self: Window(TUserData), offset_x: u32, offset_y: u32, width: u32, height: u32) bool { return c.mfb_set_viewport(self.cwin, offset_x, offset_y, width, height); } diff --git a/src/main.zig b/src/main.zig index 6c9bfe6..75d8bf7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -17,12 +17,30 @@ const Coords = struct { y: i32, }; +const FCoords = struct { + x: f32, + y: f32, + + pub fn toCoords(self: FCoords) Coords { + return Coords{ + .x = @floatToInt(i32, self.x), + .y = @floatToInt(i32, self.y), + }; + } +}; + const State = struct { period: f64, timer: *mfb.Timer, active: bool = true, alloc: std.mem.Allocator, - cursor: Coords = undefined, + cursor_pos: Coords = undefined, + rectangle_pos: FCoords = FCoords{ + .x = @intToFloat(f32, Width) / 2.0, + .y = @intToFloat(f32, Height) / 2.0, + }, + rectangle_width: f32 = @intToFloat(f32, Width) / 3.0, + rectangle_height: f32 = @intToFloat(f32, Height) / 3.0, pub fn init(alloc: std.mem.Allocator, period: f64) !State { var timer = try alloc.create(mfb.Timer); @@ -48,13 +66,22 @@ const State = struct { const rectColor = if (self.active) mfb.Rgb{ .r = 0, .g = 255, .b = 0 } else mfb.Rgb{ .r = 255, .g = 0, .b = 0 }; buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = Width, .y = Height }, grey(@floatToInt(u8, bgValue))); - buf.drawRectangle(.{ .x = Width / 3, .y = Height / 3 }, .{ .x = 2 * Width / 3, .y = 2 * Height / 3 }, rectColor); - buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = 10, .y = 10 }, mfb.Rgb{ .r = 0, .g = 0, .b = 255 }); + buf.drawRectangle( + (FCoords{ + .x = self.rectangle_pos.x - self.rectangle_width / 2, + .y = self.rectangle_pos.y - self.rectangle_height / 2, + }).toCoords(), + (FCoords{ + .x = self.rectangle_pos.x + self.rectangle_width / 2, + .y = self.rectangle_pos.y + self.rectangle_height / 2, + }).toCoords(), + rectColor, + ); // Draw cursor buf.drawRectangle( - .{ .x = self.cursor.x - CursorWidth / 2, .y = self.cursor.y - CursorWidth / 2 }, - .{ .x = self.cursor.x + CursorWidth / 2, .y = self.cursor.y + CursorWidth / 2 }, + .{ .x = self.cursor_pos.x - CursorWidth / 2, .y = self.cursor_pos.y - CursorWidth / 2 }, + .{ .x = self.cursor_pos.x + CursorWidth / 2, .y = self.cursor_pos.y + CursorWidth / 2 }, mfb.Rgb{ .r = 61, .g = 97, .b = 170 }, ); } @@ -143,7 +170,29 @@ fn handleMouseButton(_: mfb.Window(State), mouse_button: mfb.MouseButton, key_mo } fn handleMouseMove(win: mfb.Window(State), x: i32, y: i32) callconv(.C) void { - win.getUserData().?.*.cursor = .{ .x = x, .y = y }; + win.getUserData().?.*.cursor_pos = .{ .x = x, .y = y }; +} + +fn handleMouseScroll(win: mfb.Window(State), key_mod: mfb.KeyMod, delta_x: f32, delta_y: f32) callconv(.C) void { + _ = delta_x; + + const scroll_speed = 3.5; + + var state = win.getUserData().?; + std.log.info("scroll dx={d} dy={d}", .{ delta_x, delta_y }); + if (key_mod.control) { + if (key_mod.shift) { + state.rectangle_width += delta_y * scroll_speed; + } else { + state.rectangle_height += delta_y * scroll_speed; + } + } else { + if (key_mod.shift) { + state.rectangle_pos.x += delta_y * scroll_speed; + } else { + state.rectangle_pos.y -= delta_y * scroll_speed; + } + } } pub fn main() !void { @@ -163,6 +212,7 @@ pub fn main() !void { win.setResizeCallback(handleResize); win.setMouseButtonCallback(handleMouseButton); win.setMouseMoveCallback(handleMouseMove); + win.setMouseScrollCallback(handleMouseScroll); var buf = try Buffer.init(alloc, Width, Height); defer buf.deinit();