diff options
Diffstat (limited to 'engine/src/windowing.rs')
| -rw-r--r-- | engine/src/windowing.rs | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 808e85e..805004a 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -32,7 +32,7 @@ use crate::ecs::sole::Single; use crate::ecs::system::observer::Observe; use crate::ecs::uid::Uid; use crate::ecs::{declare_entity, pair, Query, Sole}; -use crate::util::MapVec; +use crate::util::{BitArray, MapVec}; use crate::vector::Vec2; use crate::windowing::dpi::{PhysicalPosition, PhysicalSize, Position}; use crate::windowing::keyboard::{Key, KeyState, Keyboard, UnknownKeyCodeError}; @@ -207,8 +207,21 @@ fn update_stuff( mouse_button_input.flags.clear(); } + for (key_index, key_state_bits) in input.keys.iter_occupied() { + let key = Key::KEYS[key_index]; + + let key_state = match key_state_bits { + KEY_PRESSED_BITS => KeyState::Pressed, + KEY_RELEASED_BITS => KeyState::Released, + _ => unreachable!(), + }; + + keyboard.set_key_state(key, key_state); + } + input.relative_mouse_pos_delta = Vec2 { x: 0.0, y: 0.0 }; input.mouse_scroll_delta = MouseScrollDelta { vert_lines: 0.0, hor_lines: 0.0 }; + input.keys.clear(); }; keyboard.set_text_keys(iter_array_queue(&context.shared_state.text_keys)); @@ -307,9 +320,6 @@ fn update_stuff( window.set_changed(); } - MessageFromApp::KeyboardKeyStateChanged(key, key_state) => { - keyboard.set_key_state(key, key_state); - } } } @@ -650,7 +660,9 @@ fn create_event_loop() -> Result<EventLoop<()>, EventLoopError> true, ); } - _ => { compile_error!("Unsupported platform") } + _ => { + compile_error!("Unsupported platform") + } } event_loop_builder.build() @@ -673,7 +685,6 @@ enum MessageFromApp WindowResized(WindowId, PhysicalSize<u32>), WindowCloseRequested(WindowId), WindowScaleFactorChanged(WindowId, f64), - KeyboardKeyStateChanged(Key, KeyState), } #[derive(Debug)] @@ -724,6 +735,7 @@ struct Input absolute_mouse_pos: PhysicalPosition<f64>, mouse_scroll_delta: MouseScrollDelta, mouse_buttons: MapVec<MouseButton, MouseButtonInput>, + keys: BitArray<{ (Key::KEYS.len() * BITS_PER_KEY).div_ceil(8) }, BITS_PER_KEY>, } impl Default for Input @@ -741,6 +753,7 @@ impl Default for Input (MouseButton::Back, MouseButtonInput::default()), (MouseButton::Forward, MouseButtonInput::default()), ]), + keys: BitArray::new(), } } } @@ -761,6 +774,11 @@ struct MouseButtonFlags: u8 } } +const BITS_PER_KEY: usize = 2; + +const KEY_PRESSED_BITS: u8 = 0b11; +const KEY_RELEASED_BITS: u8 = 0b01; + #[derive(Debug)] struct InitData { @@ -977,10 +995,17 @@ impl ApplicationHandler for App } }; - self.send_message(MessageFromApp::KeyboardKeyStateChanged( - key, - keyboard_event.state.into(), - )); + let Some(mut input) = self.lock_input() else { + tracing::error!("Locking input mutex timed out after 100ms"); + return; + }; + + let key_state_bits = match keyboard_event.state.into() { + KeyState::Pressed => KEY_PRESSED_BITS, + KeyState::Released => KEY_RELEASED_BITS, + }; + + input.keys.set(key as usize, key_state_bits); } WindowEvent::CursorMoved { device_id: _, position } => { { @@ -1148,21 +1173,23 @@ fn get_monitor_native_id( target_os = "linux" => { use winit::platform::wayland::{ ActiveEventLoopExtWayland, - MonitorHandleExtWayland + MonitorHandleExtWayland, }; use winit::platform::x11::MonitorHandleExtX11; if event_loop.is_wayland() { NativeMonitorId::Wayland( - <WinitMonitorHandle as MonitorHandleExtWayland>::native_id(monitor) + <WinitMonitorHandle as MonitorHandleExtWayland>::native_id(monitor), ) } else { NativeMonitorId::X11( - <WinitMonitorHandle as MonitorHandleExtX11>::native_id(monitor) + <WinitMonitorHandle as MonitorHandleExtX11>::native_id(monitor), ) } } - _ => { compile_error!("Unsupported target platform") } + _ => { + compile_error!("Unsupported target platform") + } } } |
