Added setMouseScrollCallback

This commit is contained in:
Brandon Dyck 2022-01-10 12:20:52 -07:00
parent 2c465861e1
commit a816791914
2 changed files with 61 additions and 6 deletions

View File

@ -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)); 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 { 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); return c.mfb_set_viewport(self.cwin, offset_x, offset_y, width, height);
} }

View File

@ -17,12 +17,30 @@ const Coords = struct {
y: i32, 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 { const State = struct {
period: f64, period: f64,
timer: *mfb.Timer, timer: *mfb.Timer,
active: bool = true, active: bool = true,
alloc: std.mem.Allocator, 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 { pub fn init(alloc: std.mem.Allocator, period: f64) !State {
var timer = try alloc.create(mfb.Timer); 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 }; 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 = 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(
buf.drawRectangle(.{ .x = 0, .y = 0 }, .{ .x = 10, .y = 10 }, mfb.Rgb{ .r = 0, .g = 0, .b = 255 }); (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 // Draw cursor
buf.drawRectangle( buf.drawRectangle(
.{ .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.x + CursorWidth / 2, .y = self.cursor.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 }, 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 { 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 { pub fn main() !void {
@ -163,6 +212,7 @@ pub fn main() !void {
win.setResizeCallback(handleResize); win.setResizeCallback(handleResize);
win.setMouseButtonCallback(handleMouseButton); win.setMouseButtonCallback(handleMouseButton);
win.setMouseMoveCallback(handleMouseMove); win.setMouseMoveCallback(handleMouseMove);
win.setMouseScrollCallback(handleMouseScroll);
var buf = try Buffer.init(alloc, Width, Height); var buf = try Buffer.init(alloc, Width, Height);
defer buf.deinit(); defer buf.deinit();