Added setMouseMoveCallback

This commit is contained in:
Brandon Dyck 2022-01-10 10:21:37 -07:00
parent ea1c1281ef
commit 3445df5315
2 changed files with 21 additions and 0 deletions

View File

@ -143,6 +143,11 @@ pub fn Window(comptime TUserData: type) type {
c.mfb_set_mouse_button_callback(self.cwin, @ptrCast(c.mfb_mouse_button_func, callback));
}
pub const MouseMoveFunc = fn (win: Window(TUserData), x: i32, y: i32) callconv(.C) void;
pub fn setMouseMoveCallback(self: Window(TUserData), callback: MouseMoveFunc) void {
c.mfb_set_mouse_move_callback(self.cwin, @ptrCast(c.mfb_mouse_move_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);
}

View File

@ -6,6 +6,7 @@ const max = std.math.max;
const Width = 800;
const Height = 600;
const AspectRatio = @intToFloat(comptime_float, Width) / @intToFloat(comptime_float, Height);
const CursorWidth = 25;
fn grey(value: u8) mfb.Rgb {
return mfb.Rgb{ .r = value, .g = value, .b = value };
@ -21,6 +22,7 @@ const State = struct {
timer: *mfb.Timer,
active: bool = true,
alloc: std.mem.Allocator,
cursor: Coords = undefined,
pub fn init(alloc: std.mem.Allocator, period: f64) !State {
var timer = try alloc.create(mfb.Timer);
@ -48,6 +50,13 @@ const State = struct {
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 });
// 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 },
mfb.Rgb{ .r = 61, .g = 97, .b = 170 },
);
}
};
@ -137,6 +146,12 @@ fn handleMouseButton(_: mfb.Window(State), mouse_button: mfb.MouseButton, key_mo
std.log.info("{any} mouse{d} {s}", .{ key_mod, @enumToInt(mouse_button), up_down });
}
fn handleMouseMove(win: mfb.Window(State), x: i32, y: i32) callconv(.C) void {
var state = win.getUserData().?;
std.log.debug("cursor={any}", .{state.*.cursor});
state.*.cursor = .{ .x = x, .y = y };
}
pub fn main() !void {
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gp_allocator.allocator();
@ -153,6 +168,7 @@ pub fn main() !void {
win.setActiveCallback(handleActive);
win.setResizeCallback(handleResize);
win.setMouseButtonCallback(handleMouseButton);
win.setMouseMoveCallback(handleMouseMove);
var buf = try Buffer.init(alloc, Width, Height);
defer buf.deinit();