Permalink
Browse files

Pass all keys from js to rust

This is brittle but it works. We'll probably want to generate both
maps (js and rust) in the build script at some point.
  • Loading branch information...
tomassedovic committed Dec 20, 2017
1 parent 07d9ca7 commit 18fd5aab1836520284a4c2c124a158f48f238666
Showing with 176 additions and 11 deletions.
  1. +97 −4 dose-response.js
  2. +79 −7 src/main.rs
@@ -16,13 +16,103 @@ var gamestate_ptr;
var pressed_keys = [];

const keymap = {
ArrowUp: 0,
ArrowDown: 1,
ArrowLeft: 2,
ArrowRight: 3
"1": 0,
"2": 1,
"3": 2,
"4": 3,
"5": 4,
"6": 5,
"7": 6,
"8": 7,
"9": 8,
"0": 9,

"a": 10,
"b": 11,
"c": 12,
"d": 13,
"e": 14,
"f": 15,
"g": 16,
"h": 17,
"i": 18,
"j": 19,
"k": 20,
"l": 21,
"m": 22,
"n": 23,
"o": 24,
"p": 25,
"q": 26,
"r": 27,
"s": 28,
"t": 29,
"u": 30,
"v": 31,
"w": 32,
"x": 33,
"y": 34,
"z": 35,

// `event.key` is uppercase when shift is pressed. But our Rust code
// expects it to be the same as lowerase so we need to handle it
// specially:

"A": 10,
"B": 11,
"C": 12,
"D": 13,
"E": 14,
"F": 15,
"G": 16,
"H": 17,
"I": 18,
"J": 19,
"K": 20,
"L": 21,
"M": 22,
"N": 23,
"O": 24,
"P": 25,
"Q": 26,
"R": 27,
"S": 28,
"T": 29,
"U": 30,
"V": 31,
"W": 32,
"X": 33,
"Y": 34,
"Z": 35,

// Codes 36 - 45 are for the numpad

// Codes 46 - 57 are for the F keys

"ArrowLeft": 58,
"ArrowRight": 59,
"ArrowUp": 60,
"ArrowDown": 61,
"Enter": 62,
" ": 63,
"Escape": 64
};

const numpad_keymap = {
"Numpad0": 36,
"Numpad1": 37,
"Numpad2": 38,
"Numpad3": 39,
"Numpad4": 40,
"Numpad5": 41,
"Numpad6": 42,
"Numpad7": 43,
"Numpad8": 44,
"Numpad9": 45
};



fetch('target/wasm32-unknown-unknown/release/dose-response.wasm')
.then(response => response.arrayBuffer())

@@ -73,6 +163,9 @@ fetch('target/wasm32-unknown-unknown/release/dose-response.wasm')
if(key.key in keymap) {
key_code = keymap[key.key];
}
if(key.code.startsWith("Numpad")) {
key_code = numpad_keymap[key.code];
}
wasm_instance.exports.key_pressed(
gamestate_ptr,
key_code,
@@ -442,20 +442,92 @@ pub fn key_pressed(
#[allow(unsafe_code)]
let mut state: Box<State> = unsafe { Box::from_raw(state_ptr) };

let code = match external_code {
0 => Some(keys::KeyCode::Up),
1 => Some(keys::KeyCode::Down),
2 => Some(keys::KeyCode::Left),
3 => Some(keys::KeyCode::Right),
_ => None,
};
let code = from_js_keycode(external_code);
if let Some(code) = code {
state.keys.push(keys::Key { code, alt, ctrl, shift});
}

std::mem::forget(state);
}

fn from_js_keycode(js_keycode: i32) -> Option<keys::KeyCode> {
use keys::KeyCode::*;
let map = [
D1,
D2,
D3,
D4,
D5,
D6,
D7,
D8,
D9,
D0,
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
NumPad0,
NumPad1,
NumPad2,
NumPad3,
NumPad4,
NumPad5,
NumPad6,
NumPad7,
NumPad8,
NumPad9,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
Left,
Right,
Up,
Down,
Enter,
Space,
Esc,
];

if js_keycode >= 0 && (js_keycode as usize) < map.len() {
Some(map[js_keycode as usize])
} else {
None
}
}



fn main() {
// NOTE: at our current font, the height of 43 is the maximum

0 comments on commit 18fd5aa

Please sign in to comment.