diff options
author | alaric <alaric@netmythos.org> | 2024-04-13 16:29:31 -0700 |
---|---|---|
committer | alaric <alaric@netmythos.org> | 2024-04-13 16:29:31 -0700 |
commit | 4c4b5a6ae33c363b0f0a21ee3de136aae99919d9 (patch) | |
tree | 95344f6709d6e42c5ea01cb9aaa6338cb97359c4 | |
parent | 8d0b946c1bfcc437458dc0971ad7024110f37b25 (diff) | |
download | engine-4c4b5a6ae33c363b0f0a21ee3de136aae99919d9.tar.gz engine-4c4b5a6ae33c363b0f0a21ee3de136aae99919d9.zip |
feat:Add a new way to do key input
-rw-r--r-- | build.zig | 1 | ||||
-rw-r--r-- | src/input.zig | 7 | ||||
-rw-r--r-- | src/shell/shell.html | 41 | ||||
-rw-r--r-- | src/wasm.zig | 8 |
4 files changed, 38 insertions, 19 deletions
@@ -17,6 +17,7 @@ pub fn build(b: *std.Build) void { "init", "update", "mouseEvent", + "keyEvent", }; const engine_unit_tests = b.addTest(.{ diff --git a/src/input.zig b/src/input.zig index 870bca5..a05389e 100644 --- a/src/input.zig +++ b/src/input.zig @@ -2,6 +2,7 @@ const root = @import("root"); pub const InputEvent = union(enum) { mouse: MouseInfo, + key: KeyEvent, }; pub const MouseInfo = struct { @@ -10,6 +11,12 @@ pub const MouseInfo = struct { buttons: MouseButtons, }; +pub const KeyEvent = struct { + is_down: bool, + is_repeat: bool, + codepoint: u32, +}; + pub const MouseButtons = packed struct { left: bool = false, right: bool = false, diff --git a/src/shell/shell.html b/src/shell/shell.html index 9c93f99..d0faf3a 100644 --- a/src/shell/shell.html +++ b/src/shell/shell.html @@ -82,25 +82,12 @@ }); }; - const keyRegistry = new Map(); - canvas.addEventListener("keydown", (e) => { - const p = keyRegistry.get(e.code); - if (p) { - p[0] = 1; - } - }); - - canvas.addEventListener("keyup", (e) => { - const p = keyRegistry.get(e.code); - if (p) { - p[0] = 0; - } - }); + canvas.onbeforeunload = function (e) { + // Cancel the event + e.preventDefault(); - const registerKeyInput = (ptr, len, out_ptr) => { - const code = readString(ptr, len); - const list = new Uint8Array(memory.buffer, out_ptr, 1); - keyRegistry.set(code, list); + // Chrome requires returnValue to be set + e.returnValue = 'Really want to quit the game?'; }; const cursors = { @@ -121,7 +108,6 @@ consoleLog, rand, loadTexture, - registerKeyInput, setCursor, }, }; @@ -131,10 +117,27 @@ obj.instance.exports.mouseEvent(ev.x, ev.y, ev.movementX, ev.movementY, ev.buttons); }; + const keyHandler = (is_pressed) => { + return (ev) => { + //TODO: Do something for special keycodes, which don't have normal codepoints + var codepoint = 0xFFFD; + if (ev.key.length <= 4) { + const chars = new TextEncoder().encode(ev.key); + const inter = new Uint8Array(4); + inter.set(chars); + const num = new Uint32Array(inter); + codepoint = num[0]; + } + obj.instance.exports.keyEvent(codepoint, is_pressed, ev.repeat); + }; + }; + canvas.oncontextmenu = (ev) => { return false; }; canvas.addEventListener("mousedown", mouseHandler); canvas.addEventListener("mouseup", mouseHandler); canvas.addEventListener("pointermove", mouseHandler); + canvas.addEventListener("keydown", keyHandler(true)); + canvas.addEventListener("keyup", keyHandler(false)); }; WebAssembly.instantiateStreaming(fetch("bin/game.wasm"), importObject).then( diff --git a/src/wasm.zig b/src/wasm.zig index 04767b7..e1ddaab 100644 --- a/src/wasm.zig +++ b/src/wasm.zig @@ -35,3 +35,11 @@ export fn mouseEvent( .buttons = buttons, } }); } + +export fn keyEvent(codepoint: u32, is_press: bool, is_repeat: bool) void { + handleInput(.{ .key = .{ + .codepoint = codepoint, + .is_down = is_press, + .is_repeat = is_repeat, + } }); +} |