rust_webvr_api/
vr_gamepad.rs

1use std::sync::Arc;
2use std::cell::RefCell;
3use VRPose;
4
5pub type VRGamepadPtr = Arc<RefCell<dyn VRGamepad>>;
6
7pub trait VRGamepad {
8    fn id(&self) -> u32;
9    fn data(&self) -> VRGamepadData;
10    fn state(&self) -> VRGamepadState;
11}
12
13#[derive(Debug, Clone)]
14#[cfg_attr(feature = "serde-serialization", derive(Deserialize, Serialize))]
15pub struct VRGamepadState {
16    pub gamepad_id: u32,
17    pub connected: bool,
18    pub timestamp: f64,
19    pub axes: Vec<f64>,
20    pub buttons: Vec<VRGamepadButton>,
21    pub pose: VRPose
22}
23
24impl Default for VRGamepadState {
25     fn default() -> VRGamepadState {
26         VRGamepadState {
27            gamepad_id: 0,
28            connected: false,
29            timestamp: 0.0,
30            axes: Vec::new(),
31            buttons: Vec::new(),
32            pose: VRPose::default()
33         }
34     }
35}
36
37#[derive(Debug, Clone)]
38#[cfg_attr(feature = "serde-serialization", derive(Deserialize, Serialize))]
39pub enum VRGamepadHand {
40    Unknown,
41    Left,
42    Right
43}
44
45#[derive(Debug, Clone)]
46#[cfg_attr(feature = "serde-serialization", derive(Deserialize, Serialize))]
47pub struct VRGamepadData {
48    pub display_id: u32,
49    pub name: String,
50    pub hand: VRGamepadHand
51}
52
53impl Default for VRGamepadData {
54     fn default() -> VRGamepadData {
55         Self {
56            display_id: 0,
57            name: String::new(),
58            hand: VRGamepadHand::Unknown
59         }
60     }
61}
62
63#[derive(Debug, Clone)]
64#[cfg_attr(feature = "serde-serialization", derive(Deserialize, Serialize))]
65pub struct VRGamepadButton {
66    pub pressed: bool,
67    pub touched: bool
68}
69
70impl VRGamepadButton {
71    pub fn new(pressed: bool) -> Self {
72        Self {
73            pressed: pressed,
74            touched: pressed,
75        }
76    }
77}