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,
id: js.CanvasId,
last_cursor_position: Position,
last_key_mods: KeyMods,
pub const EventIterator = struct {
core: *Core,
key_mods: KeyMods,
last_cursor_position: Position,
pub inline fn next(self: *EventIterator) ?Event {
while (true) {
const event_int = js.machEventShift();
if (event_int == -1) return null;
@ -37,48 +36,52 @@ pub const EventIterator = struct {
.key_press, .key_repeat => blk: {
const key = @intToEnum(Key, js.machEventShift());
switch (key) {
.left_shift, .right_shift => self.last_key_mods.shift = true,
.left_control, .right_control => self.last_key_mods.control = true,
.left_alt, .right_alt => self.last_key_mods.alt = true,
.left_super, .right_super => self.last_key_mods.super = true,
.caps_lock => self.last_key_mods.caps_lock = true,
.num_lock => self.last_key_mods.num_lock = true,
else => {},
}
.left_shift, .right_shift => self.key_mods.shift = true,
.left_control, .right_control => self.key_mods.control = true,
.left_alt, .right_alt => self.key_mods.alt = true,
.left_super, .right_super => self.key_mods.super = true,
.caps_lock => self.key_mods.caps_lock = true,
.num_lock => self.key_mods.num_lock = true,
else => {
break :blk switch (event_type) {
.key_press => Event{
.key_press = .{
.key = key,
.mods = self.last_key_mods,
.mods = self.key_mods,
},
},
.key_repeat => Event{
.key_repeat = .{
.key = key,
.mods = self.last_key_mods,
.mods = self.key_mods,
},
},
else => unreachable,
};
},
}
continue;
},
.key_release => blk: {
const key = @intToEnum(Key, js.machEventShift());
switch (key) {
.left_shift, .right_shift => self.last_key_mods.shift = false,
.left_control, .right_control => self.last_key_mods.control = false,
.left_alt, .right_alt => self.last_key_mods.alt = false,
.left_super, .right_super => self.last_key_mods.super = false,
.caps_lock => self.last_key_mods.caps_lock = false,
.num_lock => self.last_key_mods.num_lock = false,
else => {},
}
.left_shift, .right_shift => self.key_mods.shift = false,
.left_control, .right_control => self.key_mods.control = false,
.left_alt, .right_alt => self.key_mods.alt = false,
.left_super, .right_super => self.key_mods.super = false,
.caps_lock => self.key_mods.caps_lock = false,
.num_lock => self.key_mods.num_lock = false,
else => {
break :blk Event{
.key_release = .{
.key = key,
.mods = self.last_key_mods,
.mods = self.key_mods,
},
};
},
}
continue;
},
.mouse_motion => blk: {
const x = @intToFloat(f64, js.machEventShift());
const y = @intToFloat(f64, js.machEventShift());
@ -99,14 +102,14 @@ pub const EventIterator = struct {
.mouse_press = .{
.button = toMachButton(js.machEventShift()),
.pos = self.last_cursor_position,
.mods = self.last_key_mods,
.mods = self.key_mods,
},
},
.mouse_release => Event{
.mouse_release = .{
.button = toMachButton(js.machEventShift()),
.pos = self.last_cursor_position,
.mods = self.last_key_mods,
.mods = self.key_mods,
},
},
.mouse_scroll => Event{
@ -131,6 +134,7 @@ pub const EventIterator = struct {
else => null,
};
}
}
};
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{
.allocator = allocator,
.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 {
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 {

View file

@ -346,6 +346,21 @@ function convertKeyCode(code) {
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 = {
KeyA: 0,
KeyB: 1,
@ -373,6 +388,7 @@ const Key = {
KeyX: 23,
KeyY: 24,
KeyZ: 25,
Digit0: 26,
Digit1: 27,
Digit2: 28,
@ -383,6 +399,7 @@ const Key = {
Digit7: 33,
Digit8: 34,
Digit9: 35,
F1: 36,
F2: 37,
F3: 38,
@ -408,6 +425,7 @@ const Key = {
F23: 58,
F24: 59,
F25: 60,
NumpadDivide: 61,
NumpadMultiply: 62,
NumpadSubtract: 63,
@ -425,6 +443,7 @@ const Key = {
NumpadDecimal: 75,
NumpadEqual: 76,
NumpadEnter: 77,
Enter: 78,
Escape: 79,
Tab: 80,
@ -469,21 +488,6 @@ const Key = {
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 = {
windowed: 0,
fullscreen: 1,