From 354716b927c27c59e4136ba90eee0ff7f37f128c Mon Sep 17 00:00:00 2001 From: Ali Chraghi Date: Tue, 28 Mar 2023 15:41:11 +0330 Subject: [PATCH] core: fix event iterator --- libs/core/src/platform/wasm/Core.zig | 230 ++++++++++++++------------- libs/core/src/platform/wasm/mach.js | 34 ++-- 2 files changed, 136 insertions(+), 128 deletions(-) diff --git a/libs/core/src/platform/wasm/Core.zig b/libs/core/src/platform/wasm/Core.zig index 6fecb985..9b09c902 100644 --- a/libs/core/src/platform/wasm/Core.zig +++ b/libs/core/src/platform/wasm/Core.zig @@ -22,114 +22,118 @@ 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 { - const event_int = js.machEventShift(); - if (event_int == -1) return null; + while (true) { + const event_int = js.machEventShift(); + if (event_int == -1) return null; - const event_type = @intToEnum(std.meta.Tag(Event), event_int); - return switch (event_type) { - .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 => {}, - } - break :blk switch (event_type) { - .key_press => Event{ - .key_press = .{ - .key = key, - .mods = self.last_key_mods, + const event_type = @intToEnum(std.meta.Tag(Event), event_int); + return switch (event_type) { + .key_press, .key_repeat => blk: { + const key = @intToEnum(Key, js.machEventShift()); + switch (key) { + .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.key_mods, + }, + }, + .key_repeat => Event{ + .key_repeat = .{ + .key = key, + .mods = self.key_mods, + }, + }, + else => unreachable, + }; }, - }, - .key_repeat => Event{ - .key_repeat = .{ - .key = key, - .mods = self.last_key_mods, + } + continue; + }, + .key_release => blk: { + 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, - }; - }, - .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 => {}, - } - 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, + } + continue; + }, + .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 = .{ - .button = toMachButton(js.machEventShift()), - .pos = self.last_cursor_position, - .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_release => Event{ + .mouse_release = .{ + .button = toMachButton(js.machEventShift()), + .pos = self.last_cursor_position, + .mods = self.key_mods, }, - }; - }, - .focus_gained => Event.focus_gained, - .focus_lost => Event.focus_lost, - else => null, - }; + }, + .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, + }, + }; + }, + .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{ .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 { diff --git a/libs/core/src/platform/wasm/mach.js b/libs/core/src/platform/wasm/mach.js index 87e8db45..ca87dcd5 100644 --- a/libs/core/src/platform/wasm/mach.js +++ b/libs/core/src/platform/wasm/mach.js @@ -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,