-use common::Point2D;
-use hashmap;
-use common::Radians;
+use common::{Angle, Point};
+use {hashmap, point};
use sdl2::HapticSubsystem;
use sdl2::JoystickSubsystem;
use sdl2::event::Event;
idy: u8,
pub x: f32,
pub y: f32,
- pub a: Radians,
- pub len: f32,
+ pub a: Angle,
}
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<f64> {
- Point2D {
- x: self.x as f64,
- y: self.y as f64,
- }
+ pub fn to_axis_point(&self) -> Point<f64> {
+ point!(self.x as f64, self.y as f64)
}
- pub fn to_adjusted_point(&self) -> Point2D<f64> {
- Point2D::from(self.a) * self.len as f64
+ pub fn to_point(&self) -> Point<f64> {
+ 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<f64> {
+impl From<&Stick> for Point<f64> {
fn from(item: &Stick) -> Self {
Self {
x: item.x as f64,
ButtonLeft,
ButtonRight,
}
+use self::DeviceControls::*;
#[derive(Eq, PartialEq, Hash)]
enum ActionControls {
Shoot,
Start,
}
+use self::ActionControls::*;
//#[derive(Debug)]
pub struct 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) {
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::ButtonL1,
- 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)
}