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