Use enum values directly
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 7 Feb 2021 16:20:00 +0000 (17:20 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 7 Feb 2021 16:20:00 +0000 (17:20 +0100)
src/core/controller.rs

index 030d962..54dae94 100644 (file)
@@ -143,6 +143,7 @@ enum DeviceControls {
     ButtonLeft,
     ButtonRight,
 }
+use self::DeviceControls::*;
 
 #[derive(Eq, PartialEq, Hash)]
 enum ActionControls {
@@ -154,6 +155,7 @@ enum ActionControls {
     Shoot,
     Start,
 }
+use self::ActionControls::*;
 
 //#[derive(Debug)]
 pub struct Controller {
@@ -185,13 +187,13 @@ impl Controller {
     }
 
     fn set_mapping(&mut self, action: &HashMap<ActionControls, DeviceControls>, device: &HashMap<DeviceControls, u8>) {
-       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();
+       self.mov.idx = *action.get(&MovementX).map(|i| device.get(i)).flatten().unwrap();
+       self.mov.idy = *action.get(&MovementY).map(|i| device.get(i)).flatten().unwrap();
+       self.aim.idx = *action.get(&AimX).map(|i| device.get(i)).flatten().unwrap();
+       self.aim.idy = *action.get(&AimY).map(|i| device.get(i)).flatten().unwrap();
+       self.jump.id = *action.get(&Jump).map(|i| device.get(i)).flatten().unwrap();
+       self.shoot.id = *action.get(&Shoot).map(|i| device.get(i)).flatten().unwrap();
+       self.start.id = *action.get(&Start).map(|i| device.get(i)).flatten().unwrap();
     }
 
     pub fn update(&mut self, dt: Duration) {
@@ -212,42 +214,42 @@ impl Controller {
 
 fn get_action_mapping() -> HashMap<ActionControls, DeviceControls> {
     hashmap!(
-       ActionControls::MovementX => DeviceControls::AxisLX,
-       ActionControls::MovementY => DeviceControls::AxisLY,
-       ActionControls::AimX => DeviceControls::AxisRX,
-       ActionControls::AimY => DeviceControls::AxisRY,
-       ActionControls::Jump => DeviceControls::ButtonA,
-       ActionControls::Shoot => DeviceControls::ButtonR1,
-       ActionControls::Start => DeviceControls::ButtonStart
+       MovementX => AxisLX,
+       MovementY => AxisLY,
+       AimX => AxisRX,
+       AimY => AxisRY,
+       Jump => ButtonA,
+       Shoot => ButtonR1,
+       Start => ButtonStart
     )
 }
 
 fn get_device_mapping(device_name: &str) -> HashMap<DeviceControls, u8> {
     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
+           AxisLX => 0,
+           AxisLY => 1,
+           AxisRX => 3,
+           AxisRY => 4,
+           AxisL2 => 2,
+           AxisR2 => 5,
+           ButtonA => 0,
+           ButtonB => 1,
+           ButtonY => 3,
+           ButtonX => 2,
+           ButtonSelect => 8,
+           ButtonStart => 9,
+           ButtonHome => 10,
+           ButtonL3 => 11,
+           ButtonR3 => 12,
+           ButtonL1 => 4,
+           ButtonR1 => 5,
+           ButtonL2 => 6,
+           ButtonR2 => 7,
+           ButtonUp => 13,
+           ButtonDown => 14,
+           ButtonLeft => 15,
+           ButtonRight => 16
        ),
        _ => panic!("No controller mapping for device '{}'", device_name)
     }