support drop event

This commit is contained in:
David Allemang
2024-06-27 11:58:18 -04:00
parent 07c96af5d7
commit fbfa8ee8d6
3 changed files with 59 additions and 35 deletions

View File

@@ -4,7 +4,7 @@ const builtin = @import("builtin");
const vk = @import("vk");
const c = @import("c.zig");
const EventBus = @import("au/EventBus.zig");
const Bus = @import("au/Bus.zig");
pub const use_debug_messenger = switch (builtin.mode) {
.Debug, .ReleaseSafe => true,
@@ -63,7 +63,7 @@ var _qp: QueueProxy = undefined;
var _instance: vk.Instance = undefined;
var _window: Window = undefined;
var _events: EventBus = undefined;
var _bus: Bus = undefined;
var _device: vk.Device = undefined;
var _dconfig: CandidateDeviceInfo = undefined;
var _queue: vk.Queue = undefined;
@@ -428,31 +428,31 @@ pub const Window = struct {
}
};
pub fn wait_events() []const EventBus.Event {
_events.clear();
pub fn wait_events() []const Bus.Event {
_bus.clear();
c.glfwWaitEvents();
return _events.events.items;
return _bus.events.items;
}
pub fn poll_events() []const EventBus.Event {
_events.clear();
pub fn poll_events() []const Bus.Event {
_bus.clear();
c.glfwPollEvents();
return _events.events.items;
return _bus.events.items;
}
pub fn wait_events_timeout(seconds: f64) []const EventBus.Event {
_events.clear();
pub fn wait_events_timeout(seconds: f64) []const Bus.Event {
_bus.clear();
c.glfwWaitEventsTimeout(seconds);
return _events.events.items;
return _bus.events.items;
}
fn init_event_bus(alloc: std.mem.Allocator) !void {
_events = EventBus.init(alloc);
errdefer _events.deinit();
try _events.connect(&_window);
_bus = Bus.init(alloc);
errdefer _bus.deinit();
try _bus.connect(&_window);
}
fn deinit_event_bus() void {
try _events.disconnect(&_window);
_events.deinit();
try _bus.disconnect(&_window);
_bus.deinit();
}

View File

@@ -4,17 +4,20 @@ const Window = @import("../au.zig").Window;
const Self = @This();
alloc: std.mem.Allocator,
events: std.ArrayListUnmanaged(Event), // todo bounded array?
events: std.ArrayListUnmanaged(Event) = .{}, // todo bounded array?
drops: std.ArrayListUnmanaged([][]const u8) = .{}, // todo bounded array?
pub fn init(alloc: std.mem.Allocator) Self {
return .{
.alloc = alloc,
.events = .{},
};
}
pub fn deinit(self: *Self) void {
self.clear();
self.events.deinit(self.alloc);
self.drops.deinit(self.alloc);
}
pub fn connect(self: *Self, window: *Window) !void {
@@ -35,7 +38,7 @@ pub fn connect(self: *Self, window: *Window) !void {
_ = c.glfwSetScrollCallback(window.handle, onScroll);
_ = c.glfwSetKeyCallback(window.handle, onKey);
_ = c.glfwSetCharModsCallback(window.handle, onCharMods);
// _ = c.glfwSetDropCallback(window.handle, onDrop);
_ = c.glfwSetDropCallback(window.handle, onDrop);
}
pub fn disconnect(_: *Self, window: *Window) !void {
@@ -56,10 +59,18 @@ pub fn disconnect(_: *Self, window: *Window) !void {
_ = c.glfwSetScrollCallback(window.handle, null);
_ = c.glfwSetKeyCallback(window.handle, null);
_ = c.glfwSetCharModsCallback(window.handle, null);
// _ = c.glfwSetDropCallback(window.handle, null);
_ = c.glfwSetDropCallback(window.handle, null);
}
pub fn clear(self: *Self) void {
for (self.drops.items) |drop| {
for (drop) |path| {
self.alloc.free(path);
}
self.alloc.free(drop);
}
self.drops.clearAndFree(self.alloc);
self.events.clearRetainingCapacity();
}
@@ -99,7 +110,7 @@ pub const Event = union(enum) {
mods: c_int, // todo bitmask
};
const Drop = struct {
paths: []const []const u8, // todo lifetime issues
paths: []const []const u8,
};
windowPos: WindowPos,
@@ -249,9 +260,16 @@ fn onCharMods(handle: ?*c.GLFWwindow, code: c_uint, mods: c_int) callconv(.C) vo
},
}) catch unreachable; // todo circular queue; warn
}
// fn onDrop(handle: ?*c.GLFWwindow, count: c_int, paths: [*c][*c]const u8) callconv(.C) void {
// const bus = getBus(handle);
// bus.events.append(bus.alloc, .{
// .drop = .{},
// }) catch unreachable; // todo circular queue; warn
// }
fn onDrop(handle: ?*c.GLFWwindow, count: c_int, paths: [*c][*c]const u8) callconv(.C) void {
const bus = getBus(handle);
const drops = bus.alloc.alloc([]const u8, @intCast(count)) catch unreachable; // todo warn
for (drops, paths) |*dst, src| {
dst.* = bus.alloc.dupe(u8, std.mem.sliceTo(src, 0)) catch unreachable; // todo warn
}
bus.drops.append(bus.alloc, drops) catch unreachable; // todo warn
bus.events.append(bus.alloc, .{
.drop = .{ .paths = drops },
}) catch unreachable; // todo circular queue; warn
}

View File

@@ -93,18 +93,24 @@ fn render(
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const ally = gpa.allocator();
defer _ = gpa.detectLeaks();
const alloc = gpa.allocator();
try au.init(ally);
try au.init(alloc);
defer au.deinit();
// std.debug.print("Initialized!!\n", .{ });
while (!au.W.should_close()) {
for (au.wait_events()) |event| switch (event) {
.charMods => |e| std.debug.print("{any}\n", .{e}),
.mouseButton => |e| std.debug.print("{any}\n", .{e}),
// todo switch mode depending on if window is focused
const events = au.wait_events_timeout(0.10);
for (events) |u| switch (u) {
.cursorPos,
.windowPos,
.windowSize,
.framebufferSize,
.windowRefresh,
=> {},
else => |e| std.debug.print("{any}\n", .{e}),
};
}