From 93f399957c9f1534e3d06469a35e74331643a7b5 Mon Sep 17 00:00:00 2001 From: kotborealis Date: Sat, 18 Feb 2023 17:29:43 +0300 Subject: [PATCH] fix: fb inputs --- Cargo.lock | 4 +-- lib/wasi-experimental-io-devices/Cargo.toml | 2 +- .../src/link-ext.rs | 27 +++++++++++-------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44bf7dea91a..dc9ee994cdd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2144,9 +2144,9 @@ dependencies = [ [[package]] name = "minifb" -version = "0.23.0" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9e21c5f89bb820c7878c300c5b944e65de0f1b2a75e0be92ce670b95943740e" +checksum = "c66a1fdd7e946fe33fe9725012e25836bba3655769bee9ee347cce7de3f396df" dependencies = [ "cc", "dlib", diff --git a/lib/wasi-experimental-io-devices/Cargo.toml b/lib/wasi-experimental-io-devices/Cargo.toml index c2ea81bfb84..38e6c05399f 100644 --- a/lib/wasi-experimental-io-devices/Cargo.toml +++ b/lib/wasi-experimental-io-devices/Cargo.toml @@ -17,7 +17,7 @@ maintenance = { status = "experimental" } wasmer-wasi = { version = "=3.2.0-alpha.1", path = "../wasi", default-features=false } wasmer-wasi-types = { path = "../wasi-types", version = "=3.2.0-alpha.1" } tracing = "0.1" -minifb = { version = "0.23", optional = true } +minifb = { version = "0.24.0", optional = true } nix = "0.25.0" ref_thread_local = "0.1" serde = "1" diff --git a/lib/wasi-experimental-io-devices/src/link-ext.rs b/lib/wasi-experimental-io-devices/src/link-ext.rs index da93b0bce51..eb0a4ac78cb 100644 --- a/lib/wasi-experimental-io-devices/src/link-ext.rs +++ b/lib/wasi-experimental-io-devices/src/link-ext.rs @@ -50,7 +50,7 @@ pub(crate) struct FrameBufferState { pub last_mouse_pos: (u32, u32), pub inputs: VecDeque, - pub keys_pressed: BTreeSet, + pub keys: Vec, } impl FrameBufferState { @@ -74,7 +74,7 @@ impl FrameBufferState { window, last_mouse_pos: (0, 0), inputs: VecDeque::with_capacity(Self::MAX_INPUTS), - keys_pressed: BTreeSet::new(), + keys: Vec::new(), } } @@ -117,22 +117,27 @@ impl FrameBufferState { } pub fn fill_input_buffer(&mut self) -> Option<()> { - let keys_pressed = self.keys_pressed.iter().cloned().collect::>(); if !self.window.is_open() { self.push_input_event(InputEvent::WindowClosed)?; } - for key in keys_pressed { - if self.window.is_key_released(key) { - self.keys_pressed.remove(&key); - self.push_input_event(InputEvent::KeyRelease(key))?; + + let keys = self.keys.iter().cloned().collect::>(); + let new_keys = self.window.get_keys(); + + for key in &keys { + if !new_keys.contains(&key) { + self.push_input_event(InputEvent::KeyRelease(*key))?; } } - let keys = self.window.get_keys_pressed(KeyRepeat::No); - for key in keys { - self.keys_pressed.insert(key); - self.push_input_event(InputEvent::KeyPress(key))?; + + for key in &new_keys { + if !keys.contains(&key) { + self.push_input_event(InputEvent::KeyPress(*key))?; + } } + self.keys = new_keys; + let mouse_position = self.window.get_mouse_pos(minifb::MouseMode::Clamp)?; if mouse_position.0 as u32 != self.last_mouse_pos.0 || mouse_position.1 as u32 != self.last_mouse_pos.1