virtual_input: Use RwLock instead of Mutex
This commit is contained in:
parent
45ccf030f6
commit
9d3800c718
1 changed files with 22 additions and 22 deletions
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use egui_winit_vulkano::egui::mutex::Mutex;
|
||||
use egui_winit_vulkano::egui::mutex::RwLock;
|
||||
use winit::{
|
||||
event::{AxisId, ElementState, MouseButton},
|
||||
keyboard::PhysicalKey,
|
||||
|
@ -14,14 +14,14 @@ use super::{
|
|||
#[derive(Default)]
|
||||
pub struct VirtualInput {
|
||||
// Global states
|
||||
states: HashMap<String, Arc<Mutex<VirtualInputState>>>,
|
||||
states: HashMap<String, Arc<RwLock<VirtualInputState>>>,
|
||||
|
||||
// Per kind of input states to keep complexity low during state updates
|
||||
states_by_key: HashMap<PhysicalKey, Vec<Arc<Mutex<VirtualInputState>>>>,
|
||||
mouse_move_states: Vec<Arc<Mutex<VirtualInputState>>>,
|
||||
mouse_wheel_states: Vec<Arc<Mutex<VirtualInputState>>>,
|
||||
mouse_button_states: HashMap<MouseButton, Vec<Arc<Mutex<VirtualInputState>>>>,
|
||||
axis_states: HashMap<AxisId, Vec<Arc<Mutex<VirtualInputState>>>>,
|
||||
states_by_key: HashMap<PhysicalKey, Vec<Arc<RwLock<VirtualInputState>>>>,
|
||||
mouse_move_states: Vec<Arc<RwLock<VirtualInputState>>>,
|
||||
mouse_wheel_states: Vec<Arc<RwLock<VirtualInputState>>>,
|
||||
mouse_button_states: HashMap<MouseButton, Vec<Arc<RwLock<VirtualInputState>>>>,
|
||||
axis_states: HashMap<AxisId, Vec<Arc<RwLock<VirtualInputState>>>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for VirtualInput {
|
||||
|
@ -29,7 +29,7 @@ impl std::fmt::Debug for VirtualInput {
|
|||
let mut debug = f.debug_struct("VirtualInput");
|
||||
|
||||
for (name, state) in &self.states {
|
||||
debug.field(name, &state.lock().value);
|
||||
debug.field(name, &state.read().value);
|
||||
}
|
||||
|
||||
debug.finish()
|
||||
|
@ -40,18 +40,18 @@ impl VirtualInput {
|
|||
pub fn get_state(&self, value_name: &str) -> f32 {
|
||||
self.states
|
||||
.get(value_name)
|
||||
.map(|state| state.lock().value)
|
||||
.map(|state| state.read().value)
|
||||
.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
pub fn add_bindings(&mut self, value_name: String, new_bindings: Vec<VirtualBinding>) {
|
||||
let state =
|
||||
self.states
|
||||
.entry(value_name)
|
||||
.or_insert(Arc::new(Mutex::new(VirtualInputState {
|
||||
value: 0.0,
|
||||
bindings: Vec::new(),
|
||||
})));
|
||||
let state = self
|
||||
.states
|
||||
.entry(value_name)
|
||||
.or_insert(Arc::new(RwLock::new(VirtualInputState {
|
||||
value: 0.0,
|
||||
bindings: Vec::new(),
|
||||
})));
|
||||
|
||||
for binding in &new_bindings {
|
||||
match binding {
|
||||
|
@ -83,7 +83,7 @@ impl VirtualInput {
|
|||
}
|
||||
|
||||
state
|
||||
.lock()
|
||||
.write()
|
||||
.bindings
|
||||
.extend(new_bindings.iter().map(|b| VirtualBindingState {
|
||||
value: 0.0,
|
||||
|
@ -96,7 +96,7 @@ impl VirtualInput {
|
|||
|
||||
if let Some(states) = states {
|
||||
for state in states {
|
||||
let mut state = state.lock();
|
||||
let mut state = state.write();
|
||||
state.update_from_key(key, key_state);
|
||||
}
|
||||
}
|
||||
|
@ -104,14 +104,14 @@ impl VirtualInput {
|
|||
|
||||
pub(super) fn update_mouse_move_binding(&mut self, delta: &glam::Vec2) {
|
||||
for state in &mut self.mouse_move_states {
|
||||
let mut state = state.lock();
|
||||
let mut state = state.write();
|
||||
state.update_from_mouse(delta);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn update_mouse_wheel_binding(&mut self, delta: &glam::Vec2) {
|
||||
for state in &mut self.mouse_wheel_states {
|
||||
let mut state = state.lock();
|
||||
let mut state = state.write();
|
||||
state.update_from_mouse_wheel(delta);
|
||||
}
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ impl VirtualInput {
|
|||
|
||||
if let Some(states) = states {
|
||||
for state in states {
|
||||
let mut state = state.lock();
|
||||
let mut state = state.write();
|
||||
state.update_from_mouse_button(button, button_state);
|
||||
}
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ impl VirtualInput {
|
|||
|
||||
if let Some(states) = states {
|
||||
for state in states {
|
||||
let mut state = state.lock();
|
||||
let mut state = state.write();
|
||||
state.update_from_axis(axis, axis_state);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue