mirror of
https://github.com/fluencelabs/wasmer
synced 2025-07-30 23:02:06 +00:00
Add KeyRelease, prevent key repeat, update minifb
This commit is contained in:
@@ -11,7 +11,7 @@ description = "An experimental non-standard WASI extension for graphics"
|
||||
maintenance = { status = "experimental" }
|
||||
|
||||
[dependencies]
|
||||
minifb = "0.11"
|
||||
minifb = "0.13"
|
||||
wasmer-wasi = { version = "0.11.0", path = "../wasi" }
|
||||
wasmer-runtime-core = { version = "0.11.0", path = "../runtime-core" }
|
||||
ref_thread_local = "0.0"
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::VecDeque;
|
||||
use std::collections::{VecDeque, BTreeSet};
|
||||
use std::convert::TryInto;
|
||||
use std::io::{Read, Seek, SeekFrom, Write};
|
||||
use wasmer_wasi::{
|
||||
@@ -47,6 +47,7 @@ pub(crate) struct FrameBufferState {
|
||||
|
||||
pub last_mouse_pos: (u32, u32),
|
||||
pub inputs: VecDeque<InputEvent>,
|
||||
pub keys_pressed: BTreeSet<minifb::Key>,
|
||||
}
|
||||
|
||||
impl FrameBufferState {
|
||||
@@ -70,6 +71,7 @@ impl FrameBufferState {
|
||||
window,
|
||||
last_mouse_pos: (0, 0),
|
||||
inputs: VecDeque::with_capacity(Self::MAX_INPUTS),
|
||||
keys_pressed: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,8 +114,16 @@ impl FrameBufferState {
|
||||
}
|
||||
|
||||
pub fn fill_input_buffer(&mut self) -> Option<()> {
|
||||
let keys = self.window.get_keys_pressed(KeyRepeat::Yes)?;
|
||||
let keys_pressed = self.keys_pressed.iter().cloned().collect::<Vec<Key>>();
|
||||
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.window.get_keys_pressed(KeyRepeat::No)?;
|
||||
for key in keys {
|
||||
self.keys_pressed.insert(key.clone());
|
||||
self.push_input_event(InputEvent::KeyPress(key))?;
|
||||
}
|
||||
|
||||
@@ -157,7 +167,8 @@ impl FrameBufferState {
|
||||
&self.data_1[..]
|
||||
} else {
|
||||
&self.data_2[..]
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn get_buffer(&self) -> &[u32] {
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
pub const KEY_PRESS: u8 = 1;
|
||||
pub const MOUSE_MOVE: u8 = 2;
|
||||
pub const KEY_RELEASE: u8 = 3;
|
||||
pub const MOUSE_PRESS_LEFT: u8 = 4;
|
||||
pub const MOUSE_PRESS_RIGHT: u8 = 5;
|
||||
pub const MOUSE_PRESS_MIDDLE: u8 = 7;
|
||||
@@ -11,6 +12,7 @@ use minifb::{Key, MouseButton};
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum InputEvent {
|
||||
KeyPress(Key),
|
||||
KeyRelease(Key),
|
||||
MouseEvent(u32, u32, MouseButton),
|
||||
MouseMoved(u32, u32),
|
||||
}
|
||||
@@ -25,6 +27,10 @@ pub fn bytes_for_input_event(input_event: InputEvent) -> (u8, [u8; 8], usize) {
|
||||
data[0] = map_key_to_bytes(k);
|
||||
(KEY_PRESS, data, 1)
|
||||
}
|
||||
InputEvent::KeyRelease(k) => {
|
||||
data[0] = map_key_to_bytes(k);
|
||||
(KEY_RELEASE, data, 1)
|
||||
}
|
||||
InputEvent::MouseEvent(x, y, btn) => {
|
||||
let tag = match btn {
|
||||
MouseButton::Left => MOUSE_PRESS_LEFT,
|
||||
|
Reference in New Issue
Block a user