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,114 +22,118 @@ 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 {
const event_int = js.machEventShift(); while (true) {
if (event_int == -1) return null; const event_int = js.machEventShift();
if (event_int == -1) return null;
const event_type = @intToEnum(std.meta.Tag(Event), event_int); const event_type = @intToEnum(std.meta.Tag(Event), event_int);
return switch (event_type) { return switch (event_type) {
.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.key_mods,
.mods = self.last_key_mods, },
},
.key_repeat => Event{
.key_repeat = .{
.key = key,
.mods = self.key_mods,
},
},
else => unreachable,
};
}, },
}, }
.key_repeat => Event{ continue;
.key_repeat = .{ },
.key = key, .key_release => blk: {
.mods = self.last_key_mods, const key = @intToEnum(Key, js.machEventShift());
switch (key) {
.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.key_mods,
},
};
}, },
}, }
else => unreachable, continue;
}; },
}, .mouse_motion => blk: {
.key_release => blk: { const x = @intToFloat(f64, js.machEventShift());
const key = @intToEnum(Key, js.machEventShift()); const y = @intToFloat(f64, js.machEventShift());
switch (key) { self.last_cursor_position = .{
.left_shift, .right_shift => self.last_key_mods.shift = false, .x = x,
.left_control, .right_control => self.last_key_mods.control = false, .y = y,
.left_alt, .right_alt => self.last_key_mods.alt = false, };
.left_super, .right_super => self.last_key_mods.super = false, break :blk Event{
.caps_lock => self.last_key_mods.caps_lock = false, .mouse_motion = .{
.num_lock => self.last_key_mods.num_lock = false, .pos = .{
else => {}, .x = x,
} .y = y,
break :blk Event{ },
.key_release = .{
.key = key,
.mods = self.last_key_mods,
},
};
},
.mouse_motion => blk: {
const x = @intToFloat(f64, js.machEventShift());
const y = @intToFloat(f64, js.machEventShift());
self.last_cursor_position = .{
.x = x,
.y = y,
};
break :blk Event{
.mouse_motion = .{
.pos = .{
.x = x,
.y = y,
}, },
};
},
.mouse_press => Event{
.mouse_press = .{
.button = toMachButton(js.machEventShift()),
.pos = self.last_cursor_position,
.mods = self.key_mods,
}, },
};
},
.mouse_press => Event{
.mouse_press = .{
.button = toMachButton(js.machEventShift()),
.pos = self.last_cursor_position,
.mods = self.last_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.key_mods,
.mods = self.last_key_mods,
},
},
.mouse_scroll => Event{
.mouse_scroll = .{
.xoffset = @floatCast(f32, std.math.sign(js.machEventShiftFloat())),
.yoffset = @floatCast(f32, std.math.sign(js.machEventShiftFloat())),
},
},
.framebuffer_resize => blk: {
const width = @intCast(u32, js.machEventShift());
const height = @intCast(u32, js.machEventShift());
const pixel_ratio = @intCast(u32, js.machEventShift());
break :blk Event{
.framebuffer_resize = .{
.width = width * pixel_ratio,
.height = height * pixel_ratio,
}, },
}; },
}, .mouse_scroll => Event{
.focus_gained => Event.focus_gained, .mouse_scroll = .{
.focus_lost => Event.focus_lost, .xoffset = @floatCast(f32, std.math.sign(js.machEventShiftFloat())),
else => null, .yoffset = @floatCast(f32, std.math.sign(js.machEventShiftFloat())),
}; },
},
.framebuffer_resize => blk: {
const width = @intCast(u32, js.machEventShift());
const height = @intCast(u32, js.machEventShift());
const pixel_ratio = @intCast(u32, js.machEventShift());
break :blk Event{
.framebuffer_resize = .{
.width = width * pixel_ratio,
.height = height * pixel_ratio,
},
};
},
.focus_gained => Event.focus_gained,
.focus_lost => Event.focus_lost,
else => null,
};
}
} }
}; };
@ -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,