Use durations everywhere!
[kaka/rust-sdl-test.git] / src / core / controller.rs
index 127c309..15b3a70 100644 (file)
@@ -1,31 +1,31 @@
-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;
@@ -33,7 +33,7 @@ impl Button {
            }
            Ok(false) => {
                if self.was_pressed {
-                   self.time_released = 0;
+                   self.time_released = 0.seconds();
                }
                self.time_released += dt;
                false
@@ -50,7 +50,7 @@ pub struct Axis {
 
 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),
@@ -67,7 +67,7 @@ pub struct Stick {
 }
 
 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),
@@ -141,7 +141,7 @@ impl Controller {
        }
     }
 
-    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
@@ -150,9 +150,9 @@ impl Controller {
     }
 
     /// 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);
        }
     }
 }
@@ -182,7 +182,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));
     }