Added setMouseButtonCallback

This commit is contained in:
Brandon Dyck 2022-01-09 23:44:57 -07:00
parent e53d1dd0aa
commit ea1c1281ef
2 changed files with 55 additions and 1 deletions

View File

@ -21,6 +21,49 @@ pub const KeyMod = packed struct {
capsLock: bool = false, capsLock: bool = false,
numLock: bool = false, numLock: bool = false,
_reserved: u26 = 0, _reserved: u26 = 0,
fn putName(present: bool, name: []const u8, names: *[6][]const u8, index: *usize) void {
if (present) {
names.*[index.*] = name;
index.* += 1;
}
}
pub fn format(self: KeyMod, comptime fmt: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
var names: [6][]const u8 = undefined;
var i: usize = 0;
if (fmt.len > 0) {
@compileError("Unknown format character: '" ++ fmt ++ "'");
}
putName(self.shift, "Shift", &names, &i);
putName(self.control, "Control", &names, &i);
putName(self.alt, "Alt", &names, &i);
putName(self.super, "Super", &names, &i);
putName(self.capsLock, "Caps_Lock", &names, &i);
putName(self.numLock, "Num_Lock", &names, &i);
var first = true;
for (names[0..i]) |name| {
if (!first) {
_ = try writer.write("+");
}
_ = try writer.write(name);
first = false;
}
}
};
pub const MouseButton = enum(c_int) {
@"0" = c.MOUSE_BTN_0,
@"1" = c.MOUSE_BTN_1,
@"2" = c.MOUSE_BTN_2,
@"3" = c.MOUSE_BTN_3,
@"4" = c.MOUSE_BTN_4,
@"5" = c.MOUSE_BTN_5,
@"6" = c.MOUSE_BTN_6,
@"7" = c.MOUSE_BTN_7,
_,
}; };
pub fn Window(comptime TUserData: type) type { pub fn Window(comptime TUserData: type) type {
@ -95,6 +138,11 @@ pub fn Window(comptime TUserData: type) type {
c.mfb_set_resize_callback(self.cwin, @ptrCast(c.mfb_resize_func, callback)); c.mfb_set_resize_callback(self.cwin, @ptrCast(c.mfb_resize_func, callback));
} }
pub const MouseButtonFunc = fn (win: Window(TUserData), mouse_button: MouseButton, key_mod: KeyMod, is_pressed: bool) callconv(.C) void;
pub fn setMouseButtonCallback(self: Window(TUserData), callback: MouseButtonFunc) void {
c.mfb_set_mouse_button_callback(self.cwin, @ptrCast(c.mfb_mouse_button_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

@ -132,6 +132,11 @@ fn handleResize(win: mfb.Window(State), width: i32, height: i32) callconv(.C) vo
} }
} }
fn handleMouseButton(_: mfb.Window(State), mouse_button: mfb.MouseButton, key_mod: mfb.KeyMod, is_pressed: bool) callconv(.C) void {
const up_down: []const u8 = if (is_pressed) "down" else "up";
std.log.info("{any} mouse{d} {s}", .{ key_mod, @enumToInt(mouse_button), up_down });
}
pub fn main() !void { pub fn main() !void {
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){}; var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gp_allocator.allocator(); const alloc = gp_allocator.allocator();
@ -141,12 +146,13 @@ pub fn main() !void {
var win = mfb.Window(State).open("Hello minifb-zig", Width, Height, .{ .resizable = true, .alwaysOnTop = true }) catch unreachable; var win = mfb.Window(State).open("Hello minifb-zig", Width, Height, .{ .resizable = true, .alwaysOnTop = true }) catch unreachable;
const scale = win.getMonitorScale(); const scale = win.getMonitorScale();
std.log.info("Monitor scale: {d} * {d}", .{scale.x, scale.y}); std.log.info("Monitor scale: {d} * {d}", .{ scale.x, scale.y });
mfb.setTargetFPS(30); mfb.setTargetFPS(30);
win.setUserData(&state); win.setUserData(&state);
win.setActiveCallback(handleActive); win.setActiveCallback(handleActive);
win.setResizeCallback(handleResize); win.setResizeCallback(handleResize);
win.setMouseButtonCallback(handleMouseButton);
var buf = try Buffer.init(alloc, Width, Height); var buf = try Buffer.init(alloc, Width, Height);
defer buf.deinit(); defer buf.deinit();