core: fix event iterator

This commit is contained in:
Ali Chraghi 2023-03-28 15:41:11 +03:30 committed by Stephen Gutekanst
parent 266b651b34
commit 354716b927
2 changed files with 136 additions and 128 deletions

View file

@ -22,13 +22,12 @@ pub const Core = @This();
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
id: js.CanvasId, id: js.CanvasId,
last_cursor_position: Position,
last_key_mods: KeyMods,
pub const EventIterator = struct { pub const EventIterator = struct {
core: *Core, key_mods: KeyMods,
last_cursor_position: Position,
pub inline fn next(self: *EventIterator) ?Event { pub inline fn next(self: *EventIterator) ?Event {
while (true) {
const event_int = js.machEventShift(); const event_int = js.machEventShift();
if (event_int == -1) return null; if (event_int == -1) return null;
@ -37,48 +36,52 @@ pub const EventIterator = struct {
.key_press, .key_repeat => blk: { .key_press, .key_repeat => blk: {
const key = @intToEnum(Key, js.machEventShift()); const key = @intToEnum(Key, js.machEventShift());
switch (key) { switch (key) {
.left_shift, .right_shift => self.last_key_mods.shift = true, .left_shift, .right_shift => self.key_mods.shift = true,
.left_control, .right_control => self.last_key_mods.control = true, .left_control, .right_control => self.key_mods.control = true,
.left_alt, .right_alt => self.last_key_mods.alt = true, .left_alt, .right_alt => self.key_mods.alt = true,
.left_super, .right_super => self.last_key_mods.super = true, .left_super, .right_super => self.key_mods.super = true,
.caps_lock => self.last_key_mods.caps_lock = true, .caps_lock => self.key_mods.caps_lock = true,
.num_lock => self.last_key_mods.num_lock = true, .num_lock => self.key_mods.num_lock = true,
else => {}, else => {
}
break :blk switch (event_type) { break :blk switch (event_type) {
.key_press => Event{ .key_press => Event{
.key_press = .{ .key_press = .{
.key = key, .key = key,
.mods = self.last_key_mods, .mods = self.key_mods,
}, },
}, },
.key_repeat => Event{ .key_repeat => Event{
.key_repeat = .{ .key_repeat = .{
.key = key, .key = key,
.mods = self.last_key_mods, .mods = self.key_mods,
}, },
}, },
else => unreachable, else => unreachable,
}; };
}, },
}
continue;
},
.key_release => blk: { .key_release => blk: {
const key = @intToEnum(Key, js.machEventShift()); const key = @intToEnum(Key, js.machEventShift());
switch (key) { switch (key) {
.left_shift, .right_shift => self.last_key_mods.shift = false, .left_shift, .right_shift => self.key_mods.shift = false,
.left_control, .right_control => self.last_key_mods.control = false, .left_control, .right_control => self.key_mods.control = false,
.left_alt, .right_alt => self.last_key_mods.alt = false, .left_alt, .right_alt => self.key_mods.alt = false,
.left_super, .right_super => self.last_key_mods.super = false, .left_super, .right_super => self.key_mods.super = false,
.caps_lock => self.last_key_mods.caps_lock = false, .caps_lock => self.key_mods.caps_lock = false,
.num_lock => self.last_key_mods.num_lock = false, .num_lock => self.key_mods.num_lock = false,
else => {}, else => {
}
break :blk Event{ break :blk Event{
.key_release = .{ .key_release = .{
.key = key, .key = key,
.mods = self.last_key_mods, .mods = self.key_mods,
}, },
}; };
}, },
}
continue;
},
.mouse_motion => blk: { .mouse_motion => blk: {
const x = @intToFloat(f64, js.machEventShift()); const x = @intToFloat(f64, js.machEventShift());
const y = @intToFloat(f64, js.machEventShift()); const y = @intToFloat(f64, js.machEventShift());
@ -99,14 +102,14 @@ pub const EventIterator = struct {
.mouse_press = .{ .mouse_press = .{
.button = toMachButton(js.machEventShift()), .button = toMachButton(js.machEventShift()),
.pos = self.last_cursor_position, .pos = self.last_cursor_position,
.mods = self.last_key_mods, .mods = self.key_mods,
}, },
}, },
.mouse_release => Event{ .mouse_release => Event{
.mouse_release = .{ .mouse_release = .{
.button = toMachButton(js.machEventShift()), .button = toMachButton(js.machEventShift()),
.pos = self.last_cursor_position, .pos = self.last_cursor_position,
.mods = self.last_key_mods, .mods = self.key_mods,
}, },
}, },
.mouse_scroll => Event{ .mouse_scroll => Event{
@ -131,6 +134,7 @@ pub const EventIterator = struct {
else => null, else => null,
}; };
} }
}
}; };
pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void { pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void {
@ -141,20 +145,6 @@ pub fn init(core: *Core, allocator: std.mem.Allocator, options: Options) !void {
core.* = Core{ core.* = Core{
.allocator = allocator, .allocator = allocator,
.id = id, .id = id,
// TODO initialize these properly
.last_cursor_position = .{
.x = 0,
.y = 0,
},
.last_key_mods = .{
.shift = false,
.control = false,
.alt = false,
.super = false,
.caps_lock = false,
.num_lock = false,
},
}; };
} }
@ -163,7 +153,21 @@ pub fn deinit(self: *Core) void {
} }
pub inline fn pollEvents(self: *Core) EventIterator { pub inline fn pollEvents(self: *Core) EventIterator {
return EventIterator{ .core = self }; _ = self;
return EventIterator{
.key_mods = .{
.shift = false,
.control = false,
.alt = false,
.super = false,
.caps_lock = false,
.num_lock = false,
},
.last_cursor_position = .{
.x = 0,
.y = 0,
},
};
} }
pub fn framebufferSize(self: *Core) Size { pub fn framebufferSize(self: *Core) Size {

View file

@ -346,6 +346,21 @@ function convertKeyCode(code) {
return 118; // Unknown return 118; // Unknown
} }
const EventCode = {
key_press: 0,
key_repeat: 1,
key_release: 2,
char_input: 3,
mouse_motion: 4,
mouse_press: 5,
mouse_release: 6,
mouse_scroll: 7,
framebuffer_resize: 8,
focus_gained: 9,
focus_lost: 10,
close: 11,
};
const Key = { const Key = {
KeyA: 0, KeyA: 0,
KeyB: 1, KeyB: 1,
@ -373,6 +388,7 @@ const Key = {
KeyX: 23, KeyX: 23,
KeyY: 24, KeyY: 24,
KeyZ: 25, KeyZ: 25,
Digit0: 26, Digit0: 26,
Digit1: 27, Digit1: 27,
Digit2: 28, Digit2: 28,
@ -383,6 +399,7 @@ const Key = {
Digit7: 33, Digit7: 33,
Digit8: 34, Digit8: 34,
Digit9: 35, Digit9: 35,
F1: 36, F1: 36,
F2: 37, F2: 37,
F3: 38, F3: 38,
@ -408,6 +425,7 @@ const Key = {
F23: 58, F23: 58,
F24: 59, F24: 59,
F25: 60, F25: 60,
NumpadDivide: 61, NumpadDivide: 61,
NumpadMultiply: 62, NumpadMultiply: 62,
NumpadSubtract: 63, NumpadSubtract: 63,
@ -425,6 +443,7 @@ const Key = {
NumpadDecimal: 75, NumpadDecimal: 75,
NumpadEqual: 76, NumpadEqual: 76,
NumpadEnter: 77, NumpadEnter: 77,
Enter: 78, Enter: 78,
Escape: 79, Escape: 79,
Tab: 80, Tab: 80,
@ -469,21 +488,6 @@ const Key = {
Backquote: 117, Backquote: 117,
}; };
const EventCode = {
key_press: 0,
key_repeat: 1,
key_release: 2,
char_input: 3,
mouse_motion: 4,
mouse_press: 5,
mouse_release: 6,
mouse_scroll: 7,
framebuffer_resize: 8,
focus_gained: 9,
focus_lost: 10,
close: 11,
};
const DisplayMode = { const DisplayMode = {
windowed: 0, windowed: 0,
fullscreen: 1, fullscreen: 1,