-use sdl2::JoystickSubsystem;
-use common::Nanoseconds;
-use common::Radians;
use common::Point2D;
+use common::Radians;
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,
+ 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, btn: u8) {
self.was_pressed = self.is_pressed;
self.is_pressed = match device.button(btn 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;
}
Ok(false) => {
if self.was_pressed {
- self.time_released = 0;
+ self.time_released = 0.seconds();
}
self.time_released += dt;
false
impl Axis {
#[allow(dead_code)]
- fn update(&mut self, device: &Joystick, _dt: Nanoseconds, axis: u8) {
+ fn update(&mut self, device: &Joystick, _dt: Duration, axis: u8) {
self.val = match device.axis(axis as u32) {
Ok(val) => val as f32 / 32768.0,
Err(_) => panic!("invalid axis {}", axis),
}
impl Stick {
- fn update(&mut self, device: &Joystick, _dt: Nanoseconds, x_axis: u8, y_axis: u8) {
+ fn update(&mut self, device: &Joystick, _dt: Duration, x_axis: u8, y_axis: u8) {
self.x = match device.axis(x_axis as u32) {
Ok(val) => val as f32 / 32768.0,
Err(_) => panic!("invalid x axis {}", x_axis),
}
}
- pub fn update(&mut self, dt: Nanoseconds) {
+ pub fn update(&mut self, dt: Duration) {
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
}
/// 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);
}
}
}
}
}
- 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));
}