axis made out of buttons
This commit is contained in:
@@ -3,9 +3,12 @@ name = "QUIT"
|
||||
scan_code = 1
|
||||
|
||||
[[button]]
|
||||
name = "QUIT"
|
||||
name = "FORWARD"
|
||||
mouse = "Left"
|
||||
shift = true
|
||||
|
||||
[[button]]
|
||||
name = "BACKWARD"
|
||||
mouse = "Right"
|
||||
|
||||
[[button]]
|
||||
name = "RELOAD_SHADERS"
|
||||
@@ -13,6 +16,6 @@ scan_code = 19
|
||||
ctrl = true
|
||||
|
||||
[[axis]]
|
||||
name = "FORWARD"
|
||||
scan_code_positive = 17
|
||||
scan_code_negative = 31
|
||||
name = "FORWARD_AXIS"
|
||||
positive_button = "FORWARD"
|
||||
negative_button = "BACKWARD"
|
||||
@@ -1 +1 @@
|
||||
input = true
|
||||
input = false
|
||||
55
src/input.rs
55
src/input.rs
@@ -8,26 +8,29 @@ use toml;
|
||||
use serde_derive::{Serialize, Deserialize};
|
||||
use std::iter::FromIterator;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct VirtualButton {
|
||||
pub digital_inputs: Vec<DigitalInput>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VirtualAxis {
|
||||
pub axis_inputs: Vec<AxisInput>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InputState {
|
||||
pub virtual_buttons: HashMap<String, VirtualButton>,
|
||||
pub virtual_axes: HashMap<String, VirtualAxis>,
|
||||
input_events: HashSet<DigitalInputEvent>,
|
||||
pressed_scan_codes: HashSet<ScanCode>,
|
||||
pressed_mouse_buttons: HashSet<MouseButton>
|
||||
pressed_mouse_buttons: HashSet<MouseButton>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum DigitalInputEvent {
|
||||
Pressed(DigitalInput),
|
||||
Released(DigitalInput)
|
||||
Released(DigitalInput),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@@ -50,10 +53,8 @@ struct InputConfigButton {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct InputConfigAxis {
|
||||
name: String,
|
||||
scan_code_positive: u32,
|
||||
ctrl_positive: Option<bool>,
|
||||
scan_code_negative: u32,
|
||||
ctrl_negative: Option<bool>
|
||||
positive_button: String,
|
||||
negative_button: String,
|
||||
}
|
||||
|
||||
impl InputState {
|
||||
@@ -92,26 +93,15 @@ impl InputState {
|
||||
});
|
||||
|
||||
config.axis.iter().for_each(|a| {
|
||||
let modifiers_positive = ModifiersState {
|
||||
shift: false,
|
||||
ctrl: a.ctrl_positive.is_some(),
|
||||
alt: false,
|
||||
logo: false
|
||||
};
|
||||
let modifiers_negative = ModifiersState {
|
||||
shift: false,
|
||||
ctrl: a.ctrl_negative.is_some(),
|
||||
alt: false,
|
||||
logo: false
|
||||
};
|
||||
let axis = AxisInput::Digital(
|
||||
DigitalInput::Keyboard(KeyboardInput { scan_code: a.scan_code_positive, modifiers: modifiers_positive }),
|
||||
DigitalInput::Keyboard(KeyboardInput { scan_code: a.scan_code_negative, modifiers: modifiers_negative })
|
||||
);
|
||||
let positive_button = state.virtual_buttons.get(&a.positive_button).expect(&format!("Button {:?} of axis {:?} not found!", a.positive_button, a.name)).clone();
|
||||
let negative_button = state.virtual_buttons.get(&a.negative_button).expect(&format!("Button {:?} of axis {:?} not found!", a.positive_button, a.name)).clone();
|
||||
|
||||
let axis_input = AxisInput::Digital(positive_button, negative_button);
|
||||
|
||||
if let Some(virtual_axis) = state.virtual_axes.get_mut(&a.name) {
|
||||
virtual_axis.axis_inputs.push(axis);
|
||||
virtual_axis.axis_inputs.push(axis_input);
|
||||
} else {
|
||||
state.virtual_axes.insert(a.name.clone(), VirtualAxis { axis_inputs: vec![axis] });
|
||||
state.virtual_axes.insert(a.name.clone(), VirtualAxis { axis_inputs: vec![axis_input] });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -171,7 +161,7 @@ impl InputState {
|
||||
axis.axis_inputs.iter().fold(0.0, |fold, item| {
|
||||
let val = match item {
|
||||
AxisInput::Analog(_) => 0.0, //TODO
|
||||
AxisInput::Digital(positive_button, negative_button) => self.digital_input_to_float(positive_button) - self.digital_input_to_float(negative_button),
|
||||
AxisInput::Digital(positive_button, negative_button) => self.virtual_button_to_float(positive_button) - self.virtual_button_to_float(negative_button),
|
||||
};
|
||||
if f32::abs(val) > fold {
|
||||
val
|
||||
@@ -205,8 +195,8 @@ impl InputState {
|
||||
&& (!modifiers.logo || self.pressed_scan_codes.contains(&91) || self.pressed_scan_codes.contains(&92))
|
||||
}
|
||||
|
||||
fn digital_input_to_float(self: &Self, input: &DigitalInput) -> f32 {
|
||||
if self.digital_input_pressed(input) { 1.0 } else { 0.0 }
|
||||
fn virtual_button_to_float(self: &Self, input: &VirtualButton) -> f32 {
|
||||
if input.digital_inputs.iter().any(|di| self.digital_input_pressed(di)) { 1.0 } else { 0.0 }
|
||||
}
|
||||
|
||||
pub fn on_keyboard_event(self: &mut Self, state: ElementState, scan_code: ScanCode, modifiers: ModifiersState) {
|
||||
@@ -250,30 +240,31 @@ pub fn mods_to_string(modifiers: &ModifiersState) -> String {
|
||||
.map(|(&name, _state)| name))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AxisInput {
|
||||
Analog(AnalogInput),
|
||||
Digital(DigitalInput, DigitalInput),
|
||||
Digital(VirtualButton, VirtualButton),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub enum DigitalInput {
|
||||
Keyboard(KeyboardInput),
|
||||
Mouse(MouseInput)
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct KeyboardInput {
|
||||
scan_code: ScanCode,
|
||||
modifiers: ModifiersState
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Hash)]
|
||||
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
|
||||
pub struct MouseInput {
|
||||
button: MouseButton,
|
||||
modifiers: ModifiersState
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct AnalogInput {
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ impl Game for TestGame {
|
||||
game_data.recreate_pipeline = true;
|
||||
}
|
||||
|
||||
self.cam_pos.x += self.input.get_axis("FORWARD");
|
||||
self.cam_pos.x += self.input.get_axis("FORWARD_AXIS");
|
||||
|
||||
// Move game objects
|
||||
game_data.push_constants.time = game_data.start_time.elapsed().unwrap().as_millis() as f32 / 1000.0;
|
||||
|
||||
Reference in New Issue
Block a user