X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Fcontroller.rs;h=030d962108caaa5ef46f701c9456d37cfa5c22b9;hb=e570927ad1703298a2c85599c7e25475c60b33d4;hp=127c3096d470b4f852831d94d9e6e4f07299cc00;hpb=bf7b5671bb386ccd3d325ae3dea33046342d129c;p=kaka%2Frust-sdl-test.git diff --git a/src/core/controller.rs b/src/core/controller.rs index 127c309..030d962 100644 --- a/src/core/controller.rs +++ b/src/core/controller.rs @@ -1,31 +1,33 @@ -use sdl2::JoystickSubsystem; -use common::Nanoseconds; +use common::Point; +use {hashmap, point}; use common::Radians; -use common::Point2D; use sdl2::HapticSubsystem; +use sdl2::JoystickSubsystem; use sdl2::event::Event; use sdl2::haptic::Haptic; use sdl2::joystick::Joystick; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; +use time::{Duration, prelude::*}; #[derive(Debug, Default)] pub struct Button { - pub time_pressed: Nanoseconds, - pub time_released: Nanoseconds, + id: u8, + pub time_pressed: Duration, + pub time_released: Duration, pub is_pressed: bool, pub was_pressed: bool, pub toggle: bool, } impl Button { - fn update(&mut self, device: &Joystick, dt: Nanoseconds, btn: u8) { + fn update(&mut self, device: &Joystick, dt: Duration) { self.was_pressed = self.is_pressed; - self.is_pressed = match device.button(btn as u32) { + self.is_pressed = match device.button(self.id as u32) { Ok(true) => { if !self.was_pressed { - self.time_pressed = 0; + self.time_pressed = 0.seconds(); self.toggle = !self.toggle; } self.time_pressed += dt; @@ -33,75 +35,74 @@ impl Button { } Ok(false) => { if self.was_pressed { - self.time_released = 0; + self.time_released = 0.seconds(); } self.time_released += dt; false } - Err(_) => { panic!("invalid button {}", btn) } + Err(_) => { panic!("invalid button {}", self.id) } } } } #[derive(Debug, Default)] pub struct Axis { + id: u8, pub val: f32, } impl Axis { #[allow(dead_code)] - fn update(&mut self, device: &Joystick, _dt: Nanoseconds, axis: u8) { - self.val = match device.axis(axis as u32) { + fn update(&mut self, device: &Joystick, _dt: Duration) { + self.val = match device.axis(self.id as u32) { Ok(val) => val as f32 / 32768.0, - Err(_) => panic!("invalid axis {}", axis), + Err(_) => panic!("invalid axis {}", self.id), } } } #[derive(Debug, Default)] pub struct Stick { + idx: u8, + idy: u8, pub x: f32, pub y: f32, pub a: Radians, - pub len: f32, } impl Stick { - fn update(&mut self, device: &Joystick, _dt: Nanoseconds, x_axis: u8, y_axis: u8) { - self.x = match device.axis(x_axis as u32) { + fn update(&mut self, device: &Joystick, _dt: Duration) { + self.x = match device.axis(self.idx as u32) { Ok(val) => val as f32 / 32768.0, - Err(_) => panic!("invalid x axis {}", x_axis), + Err(_) => panic!("invalid x axis {}", self.idx), }; - self.y = match device.axis(y_axis as u32) { + self.y = match device.axis(self.idy as u32) { Ok(val) => val as f32 / 32768.0, - Err(_) => panic!("invalid y axis {}", y_axis), + 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() - } } - #[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, @@ -116,6 +117,44 @@ impl From<&Stick> for (f64, f64) { } } +#[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, +} + +#[derive(Eq, PartialEq, Hash)] +enum ActionControls { + MovementX, + MovementY, + AimX, + AimY, + Jump, + Shoot, + Start, +} + //#[derive(Debug)] pub struct Controller { pub device: Joystick, @@ -130,7 +169,9 @@ pub struct Controller { impl Controller { pub fn new(device: Joystick, haptic: Option>>) -> Self { - Controller { + let action_map = get_action_mapping(); + let device_map = get_device_mapping(&device.name()); + let mut ctrl = Controller { device, haptic, mov: Default::default(), @@ -138,25 +179,80 @@ impl Controller { jump: Default::default(), start: Default::default(), shoot: Default::default(), - } + }; + ctrl.set_mapping(&action_map, &device_map); + ctrl } - pub fn update(&mut self, dt: Nanoseconds) { - self.mov.update(&self.device, dt, 0, 1); // left stick - self.aim.update(&self.device, dt, 3, 4); // right stick - self.jump.update(&self.device, dt, 4); // left shoulder - self.shoot.update(&self.device, dt, 5); // right shoulder - self.start.update(&self.device, dt, 9); // 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) { + self.mov.update(&self.device, dt); + self.aim.update(&self.device, dt); + self.jump.update(&self.device, dt); + self.shoot.update(&self.device, dt); + self.start.update(&self.device, dt); } /// strength [0 - 1] - pub fn rumble(&self, strength: f32, duration_ms: u32) { + pub fn rumble(&self, strength: f32, duration: Duration) { if let Some(h) = &self.haptic { - h.borrow_mut().rumble_play(strength, duration_ms); + h.borrow_mut().rumble_play(strength, duration.whole_milliseconds() as u32); } } } +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 + ) +} + +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, @@ -182,7 +278,7 @@ impl ControllerManager { } } - pub fn update(&mut self, dt: Nanoseconds) { + pub fn update(&mut self, dt: Duration) { self.controllers.iter().for_each(|(_, v)| v.borrow_mut().update(dt)); } @@ -190,9 +286,9 @@ impl ControllerManager { match event { Event::JoyDeviceAdded { which, .. } => { self.add_device(*which) } Event::JoyDeviceRemoved { which, .. } => { self.remove_device(*which) } - Event::JoyButtonDown { which, button_idx, .. } => { println!("device {} button {} down!", which, button_idx) } - Event::JoyButtonUp { which, button_idx, .. } => { println!("device {} button {} up!", which, button_idx) } - Event::JoyAxisMotion { which, axis_idx, .. } => { println!("device {} axis motion {}!", which, axis_idx) } + // Event::JoyButtonDown { which, button_idx, .. } => { println!("device {} button {} down!", which, button_idx) } + // Event::JoyButtonUp { which, button_idx, .. } => { println!("device {} button {} up!", which, button_idx) } + // Event::JoyAxisMotion { which, axis_idx, .. } => { println!("device {} axis motion {}!", which, axis_idx) } _ => {} } }