From 5d42eba1124851e61be30fde5e26dab1a3e086a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 7 Feb 2021 17:20:00 +0100 Subject: [PATCH] Use enum values directly --- src/core/controller.rs | 76 ++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/src/core/controller.rs b/src/core/controller.rs index 030d962..54dae94 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -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, 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(); + 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 { 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 { 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) } -- 2.11.0