Skip to content

Commit

Permalink
fix: fb inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
kotborealis authored and kotborealis committed Feb 22, 2023
1 parent 365ec5b commit f03fee1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/wasi-experimental-io-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 16 additions & 11 deletions lib/wasi-experimental-io-devices/src/link-ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) struct FrameBufferState {

pub last_mouse_pos: (u32, u32),
pub inputs: VecDeque<InputEvent>,
pub keys_pressed: BTreeSet<minifb::Key>,
pub keys: Vec<minifb::Key>,
}

impl FrameBufferState {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -117,22 +117,27 @@ impl FrameBufferState {
}

pub fn fill_input_buffer(&mut self) -> Option<()> {
let keys_pressed = self.keys_pressed.iter().cloned().collect::<Vec<Key>>();
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::<Vec<Key>>();
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
Expand Down

0 comments on commit f03fee1

Please sign in to comment.