X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Fcontroller.rs;h=de91928e0fdf2a1a19ad2a1963789198780d3d74;hb=bb3eb700e040846ca793832da4adc133218f2954;hp=2eec33ade4cfcf255647aad6e7f008f1a3e045cc;hpb=5d731022a8949b965bb0b2c13524ee7c5bfaf2e6;p=kaka%2Frust-sdl-test.git diff --git a/src/core/controller.rs b/src/core/controller.rs index 2eec33a..de91928 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -1,5 +1,5 @@ -use common::Point2D; -use common::Radians; +use geometry::{Angle, Point}; +use {hashmap, point}; use sdl2::HapticSubsystem; use sdl2::JoystickSubsystem; use sdl2::event::Event; @@ -66,8 +66,7 @@ pub struct Stick { idy: u8, pub x: f32, pub y: f32, - pub a: Radians, - pub len: f32, + pub a: Angle, } impl Stick { @@ -80,32 +79,29 @@ impl Stick { Ok(val) => val as f32 / 32768.0, Err(_) => panic!("invalid y axis {}", self.idy), }; - self.a = Radians(self.y.atan2(self.x) as f64); - self.len = { - let x = (self.x / self.y).abs().min(1.0); - let y = (self.y / self.x).abs().min(1.0); - (self.x.powi(2) + self.y.powi(2)).sqrt() / (x.powi(2) + y.powi(2)).sqrt() - } + self.a = point!(self.x as f64, self.y as f64).to_angle(); } - #[inline(always)] #[allow(dead_code)] fn up(&self) -> bool { self.y > 0.99 } - #[inline(always)] #[allow(dead_code)] fn down(&self) -> bool { self.y < -0.99 } - #[inline(always)] #[allow(dead_code)] fn left(&self) -> bool { self.x < -0.99 } - #[inline(always)] #[allow(dead_code)] fn right(&self) -> bool { self.x > 0.99 } + #[inline(always)] #[allow(dead_code)] pub fn up(&self) -> bool { self.y < -0.99 } + #[inline(always)] #[allow(dead_code)] pub fn down(&self) -> bool { self.y > 0.99 } + #[inline(always)] #[allow(dead_code)] pub fn left(&self) -> bool { self.x < -0.99 } + #[inline(always)] #[allow(dead_code)] pub fn right(&self) -> bool { self.x > 0.99 } - pub fn to_point(&self) -> Point2D { - Point2D { - x: self.x as f64, - y: self.y as f64, - } + pub fn to_axis_point(&self) -> Point { + point!(self.x as f64, self.y as f64) } - pub fn to_adjusted_point(&self) -> Point2D { - Point2D::from(self.a) * self.len as f64 + pub fn to_point(&self) -> Point { + let p = point!(self.x as f64, self.y as f64); + if p.length() > 1.0 { + p.normalized() + } else { + p + } } } -impl From<&Stick> for Point2D { +impl From<&Stick> for Point { fn from(item: &Stick) -> Self { Self { x: item.x as f64, @@ -120,40 +116,45 @@ 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, } +use self::DeviceControls::*; -#[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, } +use self::ActionControls::*; //#[derive(Debug)] pub struct Controller { @@ -169,6 +170,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 +181,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(&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) { @@ -236,6 +211,49 @@ impl Controller { } } +fn get_action_mapping() -> HashMap { + hashmap!( + 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!( + 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) + } +} + //#[derive(Debug)] pub struct ControllerManager { pub joystick: JoystickSubsystem,