From 5d731022a8949b965bb0b2c13524ee7c5bfaf2e6 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= <tomas.wenstrom@gmail.com>
Date: Sun, 17 Jan 2021 18:40:03 +0100
Subject: [PATCH] Placeholder controller mapping

---
 src/core/controller.rs | 121 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 100 insertions(+), 21 deletions(-)

diff --git a/src/core/controller.rs b/src/core/controller.rs
index 15b3a70..2eec33a 100644
--- a/src/core/controller.rs
+++ b/src/core/controller.rs
@@ -12,6 +12,7 @@ use time::{Duration, prelude::*};
 
 #[derive(Debug, Default)]
 pub struct Button {
+    id: u8,
     pub time_pressed: Duration,
     pub time_released: Duration,
     pub is_pressed: bool,
@@ -20,9 +21,9 @@ pub struct Button {
 }
 
 impl Button {
-    fn update(&mut self, device: &Joystick, dt: Duration, 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.seconds();
@@ -38,28 +39,31 @@ impl Button {
 		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: Duration, 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,
@@ -67,14 +71,14 @@ pub struct Stick {
 }
 
 impl Stick {
-    fn update(&mut self, device: &Joystick, _dt: Duration, 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 = {
@@ -116,6 +120,41 @@ 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,
+}
+
+#[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(Debug)]
 pub struct Controller {
     pub device: Joystick,
@@ -130,7 +169,7 @@ pub struct Controller {
 
 impl Controller {
     pub fn new(device: Joystick, haptic: Option<Rc<RefCell<Haptic>>>) -> Self {
-	Controller {
+	let mut ctrl = Controller {
 	    device,
 	    haptic,
 	    mov: Default::default(),
@@ -138,15 +177,55 @@ impl Controller {
 	    jump: Default::default(),
 	    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
+    }
+
+    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;
     }
 
     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
-	self.shoot.update(&self.device, dt, 5); // right shoulder
-	self.start.update(&self.device, dt, 9);	 // start
+	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]
@@ -190,9 +269,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) }
 	    _ => {}
 	}
     }
-- 
2.11.0