From 05ba09764d251b4dbf3d6572e1fe54bcbc154dda Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Tue, 19 Jan 2021 19:01:41 +0100 Subject: [PATCH] Replaced mapping struct with hashmap --- src/core/controller.rs | 157 ++++++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 68 deletions(-) diff --git a/src/core/controller.rs b/src/core/controller.rs index 2eec33a..832480d 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -1,4 +1,5 @@ use common::Point2D; +use hashmap; use common::Radians; use sdl2::HapticSubsystem; use sdl2::JoystickSubsystem; @@ -120,39 +121,42 @@ impl From<&Stick> for (f64, f64) { } } -#[allow(dead_code)] -struct Axes { - left_x: u8, - left_y: u8, - right_x: u8, - right_y: u8, - trigger_left: u8, - trigger_right: u8, +#[derive(Eq, PartialEq, Hash)] +enum DeviceControls { + AxisLX, + AxisLY, + AxisRX, + AxisRY, + AxisL2, + AxisR2, + ButtonA, + ButtonB, + ButtonY, + ButtonX, + ButtonSelect, + ButtonStart, + ButtonHome, + ButtonL3, + ButtonR3, + ButtonL1, + ButtonR1, + ButtonL2, + ButtonR2, + ButtonUp, + ButtonDown, + ButtonLeft, + ButtonRight, } -#[allow(dead_code)] -struct Buttons { - a: u8, - b: u8, - x: u8, - y: u8, - select: u8, - start: u8, - left_stick: u8, - right_stick: u8, - left_shoulder: u8, - right_shoulder: u8, - left_trigger: u8, - right_trigger: u8, - d_pad_up: u8, - d_pad_down: u8, - d_pad_left: u8, - d_pad_right: u8, -} - -struct Mapping { - axis: Axes, - btn: Buttons, +#[derive(Eq, PartialEq, Hash)] +enum ActionControls { + MovementX, + MovementY, + AimX, + AimY, + Jump, + Shoot, + Start, } //#[derive(Debug)] @@ -169,6 +173,8 @@ pub struct Controller { impl Controller { pub fn new(device: Joystick, haptic: Option>>) -> Self { + let action_map = get_action_mapping(); + let device_map = get_device_mapping(&device.name()); let mut ctrl = Controller { device, haptic, @@ -178,46 +184,18 @@ impl Controller { start: Default::default(), shoot: Default::default(), }; - let dualshock3 = Mapping { - axis: Axes { - left_x: 0, - left_y: 1, - right_x: 3, - right_y: 4, - trigger_left: 2, - trigger_right: 5, - }, - btn: Buttons { - a: 0, - b: 1, - x: 3, - y: 2, - select: 8, - start: 9, - left_stick: 11, - right_stick: 12, - left_shoulder: 4, - right_shoulder: 5, - left_trigger: 6, - right_trigger: 7, - d_pad_up: 13, - d_pad_down: 14, - d_pad_left: 15, - d_pad_right: 16, - }, - }; - ctrl.set_mapping(&dualshock3); + ctrl.set_mapping(&action_map, &device_map); ctrl } - fn set_mapping(&mut self, map: &Mapping) { - self.mov.idx = map.axis.left_x; - self.mov.idy = map.axis.left_y; - self.aim.idx = map.axis.right_x; - self.aim.idy = map.axis.right_y; - self.jump.id = map.btn.left_shoulder; - self.shoot.id = map.btn.right_shoulder; - self.start.id = map.btn.start; + fn set_mapping(&mut self, action: &HashMap, device: &HashMap) { + self.mov.idx = *action.get(&ActionControls::MovementX).map(|i| device.get(i)).flatten().unwrap(); + self.mov.idy = *action.get(&ActionControls::MovementY).map(|i| device.get(i)).flatten().unwrap(); + self.aim.idx = *action.get(&ActionControls::AimX).map(|i| device.get(i)).flatten().unwrap(); + self.aim.idy = *action.get(&ActionControls::AimY).map(|i| device.get(i)).flatten().unwrap(); + self.jump.id = *action.get(&ActionControls::Jump).map(|i| device.get(i)).flatten().unwrap(); + self.shoot.id = *action.get(&ActionControls::Shoot).map(|i| device.get(i)).flatten().unwrap(); + self.start.id = *action.get(&ActionControls::Start).map(|i| device.get(i)).flatten().unwrap(); } pub fn update(&mut self, dt: Duration) { @@ -236,6 +214,49 @@ impl Controller { } } +fn get_action_mapping() -> HashMap { + hashmap!( + ActionControls::MovementX => DeviceControls::AxisLX, + ActionControls::MovementY => DeviceControls::AxisLY, + ActionControls::AimX => DeviceControls::AxisRX, + ActionControls::AimY => DeviceControls::AxisRY, + ActionControls::Jump => DeviceControls::ButtonL1, + ActionControls::Shoot => DeviceControls::ButtonR1, + ActionControls::Start => DeviceControls::ButtonStart + ) +} + +fn get_device_mapping(device_name: &str) -> HashMap { + match device_name { + "Sony PLAYSTATION(R)3 Controller" => hashmap!( + DeviceControls::AxisLX => 0, + DeviceControls::AxisLY => 1, + DeviceControls::AxisRX => 3, + DeviceControls::AxisRY => 4, + DeviceControls::AxisL2 => 2, + DeviceControls::AxisR2 => 5, + DeviceControls::ButtonA => 0, + DeviceControls::ButtonB => 1, + DeviceControls::ButtonY => 3, + DeviceControls::ButtonX => 2, + DeviceControls::ButtonSelect => 8, + DeviceControls::ButtonStart => 9, + DeviceControls::ButtonHome => 10, + DeviceControls::ButtonL3 => 11, + DeviceControls::ButtonR3 => 12, + DeviceControls::ButtonL1 => 4, + DeviceControls::ButtonR1 => 5, + DeviceControls::ButtonL2 => 6, + DeviceControls::ButtonR2 => 7, + DeviceControls::ButtonUp => 13, + DeviceControls::ButtonDown => 14, + DeviceControls::ButtonLeft => 15, + DeviceControls::ButtonRight => 16 + ), + _ => panic!("No controller mapping for device '{}'", device_name) + } +} + //#[derive(Debug)] pub struct ControllerManager { pub joystick: JoystickSubsystem, -- 2.11.0