Make it easy to clear/reset mappings
This commit is contained in:
parent
ce7ba71ea0
commit
0c35a1e234
66
src/input.rs
66
src/input.rs
|
@ -22,19 +22,23 @@ pub struct GamepadInfo {
|
||||||
pub trait Mappings {
|
pub trait Mappings {
|
||||||
fn mapping_names(&self) -> HashMap<VBKey, Vec<String>>;
|
fn mapping_names(&self) -> HashMap<VBKey, Vec<String>>;
|
||||||
fn clear_mappings(&mut self, key: VBKey);
|
fn clear_mappings(&mut self, key: VBKey);
|
||||||
|
fn clear_all_mappings(&mut self);
|
||||||
|
fn use_default_mappings(&mut self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GamepadMapping {
|
pub struct GamepadMapping {
|
||||||
buttons: HashMap<Code, VBKey>,
|
buttons: HashMap<Code, VBKey>,
|
||||||
axes: HashMap<Code, (VBKey, VBKey)>,
|
axes: HashMap<Code, (VBKey, VBKey)>,
|
||||||
|
default_buttons: HashMap<Code, VBKey>,
|
||||||
|
default_axes: HashMap<Code, (VBKey, VBKey)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GamepadMapping {
|
impl GamepadMapping {
|
||||||
fn for_gamepad(gamepad: &Gamepad) -> Self {
|
fn for_gamepad(gamepad: &Gamepad) -> Self {
|
||||||
let mut buttons = HashMap::new();
|
let mut default_buttons = HashMap::new();
|
||||||
let mut default_button = |btn: Button, key: VBKey| {
|
let mut default_button = |btn: Button, key: VBKey| {
|
||||||
if let Some(code) = gamepad.button_code(btn) {
|
if let Some(code) = gamepad.button_code(btn) {
|
||||||
buttons.insert(code, key);
|
default_buttons.insert(code, key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
default_button(Button::South, VBKey::A);
|
default_button(Button::South, VBKey::A);
|
||||||
|
@ -44,10 +48,10 @@ impl GamepadMapping {
|
||||||
default_button(Button::Start, VBKey::STA);
|
default_button(Button::Start, VBKey::STA);
|
||||||
default_button(Button::Select, VBKey::SEL);
|
default_button(Button::Select, VBKey::SEL);
|
||||||
|
|
||||||
let mut axes = HashMap::new();
|
let mut default_axes = HashMap::new();
|
||||||
let mut default_axis = |axis: Axis, neg: VBKey, pos: VBKey| {
|
let mut default_axis = |axis: Axis, neg: VBKey, pos: VBKey| {
|
||||||
if let Some(code) = gamepad.axis_code(axis) {
|
if let Some(code) = gamepad.axis_code(axis) {
|
||||||
axes.insert(code, (neg, pos));
|
default_axes.insert(code, (neg, pos));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
default_axis(Axis::LeftStickX, VBKey::LL, VBKey::LR);
|
default_axis(Axis::LeftStickX, VBKey::LL, VBKey::LR);
|
||||||
|
@ -57,7 +61,12 @@ impl GamepadMapping {
|
||||||
default_axis(Axis::DPadX, VBKey::LL, VBKey::LR);
|
default_axis(Axis::DPadX, VBKey::LL, VBKey::LR);
|
||||||
default_axis(Axis::DPadY, VBKey::LD, VBKey::LU);
|
default_axis(Axis::DPadY, VBKey::LD, VBKey::LU);
|
||||||
|
|
||||||
Self { buttons, axes }
|
Self {
|
||||||
|
buttons: default_buttons.clone(),
|
||||||
|
axes: default_axes.clone(),
|
||||||
|
default_buttons,
|
||||||
|
default_axes,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_button_mapping(&mut self, key: VBKey, code: Code) {
|
pub fn add_button_mapping(&mut self, key: VBKey, code: Code) {
|
||||||
|
@ -112,6 +121,16 @@ impl Mappings for GamepadMapping {
|
||||||
!keys.is_empty()
|
!keys.is_empty()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clear_all_mappings(&mut self) {
|
||||||
|
self.axes.clear();
|
||||||
|
self.buttons.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn use_default_mappings(&mut self) {
|
||||||
|
self.axes = self.default_axes.clone();
|
||||||
|
self.buttons = self.default_buttons.clone();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -162,23 +181,15 @@ impl Mappings for InputMapping {
|
||||||
!keys.is_empty()
|
!keys.is_empty()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
fn clear_all_mappings(&mut self) {
|
||||||
pub struct MappingProvider {
|
self.keys.clear();
|
||||||
device_mappings: Arc<RwLock<HashMap<DeviceId, Arc<RwLock<GamepadMapping>>>>>,
|
}
|
||||||
sim_mappings: HashMap<SimId, Arc<RwLock<InputMapping>>>,
|
|
||||||
gamepad_info: Arc<RwLock<HashMap<GamepadId, GamepadInfo>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MappingProvider {
|
fn use_default_mappings(&mut self) {
|
||||||
pub fn new() -> Self {
|
self.keys.clear();
|
||||||
let mut mappings = HashMap::new();
|
|
||||||
|
|
||||||
let mut p1_mappings = InputMapping::default();
|
|
||||||
let p2_mappings = InputMapping::default();
|
|
||||||
let mut default_key = |code, key| {
|
let mut default_key = |code, key| {
|
||||||
p1_mappings.add_keyboard_mapping(key, PhysicalKey::Code(code));
|
self.keys.insert(PhysicalKey::Code(code), key);
|
||||||
};
|
};
|
||||||
default_key(KeyCode::KeyA, VBKey::SEL);
|
default_key(KeyCode::KeyA, VBKey::SEL);
|
||||||
default_key(KeyCode::KeyS, VBKey::STA);
|
default_key(KeyCode::KeyS, VBKey::STA);
|
||||||
|
@ -194,6 +205,23 @@ impl MappingProvider {
|
||||||
default_key(KeyCode::ArrowLeft, VBKey::LL);
|
default_key(KeyCode::ArrowLeft, VBKey::LL);
|
||||||
default_key(KeyCode::ArrowDown, VBKey::LD);
|
default_key(KeyCode::ArrowDown, VBKey::LD);
|
||||||
default_key(KeyCode::ArrowRight, VBKey::LR);
|
default_key(KeyCode::ArrowRight, VBKey::LR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct MappingProvider {
|
||||||
|
device_mappings: Arc<RwLock<HashMap<DeviceId, Arc<RwLock<GamepadMapping>>>>>,
|
||||||
|
sim_mappings: HashMap<SimId, Arc<RwLock<InputMapping>>>,
|
||||||
|
gamepad_info: Arc<RwLock<HashMap<GamepadId, GamepadInfo>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MappingProvider {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let mut mappings = HashMap::new();
|
||||||
|
|
||||||
|
let mut p1_mappings = InputMapping::default();
|
||||||
|
p1_mappings.use_default_mappings();
|
||||||
|
let p2_mappings = InputMapping::default();
|
||||||
|
|
||||||
mappings.insert(SimId::Player1, Arc::new(RwLock::new(p1_mappings)));
|
mappings.insert(SimId::Player1, Arc::new(RwLock::new(p1_mappings)));
|
||||||
mappings.insert(SimId::Player2, Arc::new(RwLock::new(p2_mappings)));
|
mappings.insert(SimId::Player2, Arc::new(RwLock::new(p2_mappings)));
|
||||||
|
|
|
@ -50,6 +50,17 @@ impl InputWindow {
|
||||||
mappings: &RwLock<T>,
|
mappings: &RwLock<T>,
|
||||||
bind_message: &str,
|
bind_message: &str,
|
||||||
) {
|
) {
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
if ui.button("Use defaults").clicked() {
|
||||||
|
mappings.write().unwrap().use_default_mappings();
|
||||||
|
self.now_binding = None;
|
||||||
|
}
|
||||||
|
if ui.button("Clear all").clicked() {
|
||||||
|
mappings.write().unwrap().clear_all_mappings();
|
||||||
|
self.now_binding = None;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ui.separator();
|
||||||
let mut names = {
|
let mut names = {
|
||||||
let mapping = mappings.read().unwrap();
|
let mapping = mappings.read().unwrap();
|
||||||
mapping.mapping_names()
|
mapping.mapping_names()
|
||||||
|
|
Loading…
Reference in New Issue