camera movement

This commit is contained in:
2019-07-28 16:20:08 +02:00
parent a8a34b90f4
commit 5795d0b635
4 changed files with 94 additions and 37 deletions

View File

@@ -52,7 +52,7 @@ struct InputConfigAxis {
name: String,
positive_button: Option<String>,
negative_button: Option<String>,
mouse_wheel: Option<bool>,
mouse_axis: Option<String>,
ctrl: Option<bool>,
shift: Option<bool>,
alt: Option<bool>,
@@ -63,6 +63,8 @@ struct InputConfigAxis {
pub struct InputState {
pub virtual_buttons: HashMap<String, VirtualButton>,
pub virtual_axes: HashMap<String, VirtualAxis>,
pub mouse_delta_x: f64,
pub mouse_delta_y: f64,
input_events: HashSet<DigitalInputEvent>,
pressed_scan_codes: HashSet<ScanCode>,
pressed_mouse_buttons: HashSet<MouseButton>,
@@ -77,7 +79,7 @@ impl InputState {
let mut state = InputState { virtual_buttons: HashMap::new(), virtual_axes: HashMap::new(),
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
pressed_mouse_buttons: HashSet::new(), analog_wheel_state: 0.0,
config: config.config };
config: config.config, mouse_delta_x: 0.0, mouse_delta_y: 0.0 };
config.button.iter().for_each(|bn| {
let modifiers = ModifiersState {
@@ -109,34 +111,39 @@ impl InputState {
}
});
config.axis.iter().for_each(|a| {
let axis_input = match a {
config.axis.iter().for_each(|axis| {
let axis_input = match axis {
InputConfigAxis { positive_button: Some(pos_button_name), negative_button: Some(neg_button_name), .. } => {
let positive_button = state.virtual_buttons.get(pos_button_name)
.expect(&format!("Button {:?} of axis {:?} not found!", a.positive_button, a.name))
.expect(&format!("Button {:?} of axis {:?} not found!", axis.positive_button, axis.name))
.clone();
let negative_button = state.virtual_buttons.get(neg_button_name)
.expect(&format!("Button {:?} of axis {:?} not found!", a.positive_button, a.name))
.expect(&format!("Button {:?} of axis {:?} not found!", axis.positive_button, axis.name))
.clone();
AxisInput::Digital(positive_button, negative_button)
},
InputConfigAxis { mouse_wheel: Some(true), .. } => {
InputConfigAxis { mouse_axis: Some(axis_name), .. } => {
let modifiers = ModifiersState {
shift: a.shift.is_some(),
ctrl: a.ctrl.is_some(),
alt: a.alt.is_some(),
logo: a.logo.is_some()
shift: axis.shift.is_some(),
ctrl: axis.ctrl.is_some(),
alt: axis.alt.is_some(),
logo: axis.logo.is_some(),
};
AxisInput::Wheel(modifiers)
match axis_name.to_lowercase().as_str() {
"wheel" => AxisInput::Wheel(modifiers),
"x" => AxisInput::MouseMove(MouseMoveDirection::X, modifiers),
"y" => AxisInput::MouseMove(MouseMoveDirection::Y, modifiers),
other => panic!("Axis {:?} has unknown mouse axis name {:?}!", axis_name, other),
}
},
other => panic!("Axis {:?} needs either positive_button and negative_button or mouse_wheel must be set to true!", other.name)
};
if let Some(virtual_axis) = state.virtual_axes.get_mut(&a.name) {
if let Some(virtual_axis) = state.virtual_axes.get_mut(&axis.name) {
virtual_axis.axis_inputs.push(axis_input);
} else {
state.virtual_axes.insert(a.name.clone(), VirtualAxis { axis_inputs: vec![axis_input] });
state.virtual_axes.insert(axis.name.clone(), VirtualAxis { axis_inputs: vec![axis_input] });
}
});
@@ -195,8 +202,22 @@ impl InputState {
if let Some(axis) = self.virtual_axes.get(axis_code) {
axis.axis_inputs.iter().map(|item| {
match item {
AxisInput::Wheel(modifiers) => if self.modifiers_are_pressed(*modifiers) { self.analog_wheel_state } else { 0.0 },
AxisInput::Digital(positive_button, negative_button) => self.virtual_button_to_float(positive_button) - self.virtual_button_to_float(negative_button),
AxisInput::Wheel(modifiers) => {
if self.modifiers_are_pressed(*modifiers) { self.analog_wheel_state } else { 0.0 }
},
AxisInput::MouseMove(direction, modifiers) => {
if self.modifiers_are_pressed(*modifiers) {
match direction {
MouseMoveDirection::X => self.mouse_delta_x as f32,
MouseMoveDirection::Y => self.mouse_delta_y as f32,
}
} else {
0.0
}
},
AxisInput::Digital(positive_button, negative_button) => {
self.virtual_button_to_float(positive_button) - self.virtual_button_to_float(negative_button)
},
}
}).fold(0.0, |fold, it| if f32::abs(it) > f32::abs(fold) { it } else { fold })
} else {
@@ -288,6 +309,8 @@ impl InputState {
pub fn frame_end(self: &mut Self) {
self.analog_wheel_state = 0.0;
self.mouse_delta_x = 0.0;
self.mouse_delta_y = 0.0;
self.input_events.clear();
}
}
@@ -303,6 +326,7 @@ pub fn mods_to_string(modifiers: &ModifiersState) -> String {
#[derive(Debug)]
pub enum AxisInput {
Wheel(ModifiersState),
MouseMove(MouseMoveDirection, ModifiersState),
Digital(VirtualButton, VirtualButton),
}
@@ -337,6 +361,12 @@ pub enum WheelInputDirection {
Down,
}
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum MouseMoveDirection {
X,
Y,
}
#[derive(Debug, PartialEq, Clone)]
pub struct AnalogWheelInput {
value: f32,