rich_sdl2_rust/haptic/
direction.rs

1//! Defining directions and coordinate systems of the haptic movements.
2
3use crate::bind;
4
5/// A direction and coordinate system of the haptic movements.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Direction {
9    /// A polar coordinate system.
10    Polar {
11        /// The direction in degrees times 100. The north is 0, the east is 9000, and so on.
12        degree_100: i32,
13    },
14    /// A cartesian coordinate system.
15    Cartesian {
16        /// The east is positive direction.
17        x: i32,
18        /// The south is positive direction.
19        y: i32,
20        /// Z means the level of power if supported it.
21        z: i32,
22    },
23    /// A spherical coordinate system (3-axis haptic device).
24    Spherical {
25        /// The direction in degrees times 100, rotating from (1, 0, 0) to (0, 1, 0).
26        z_degree_100: i32,
27        /// The degree times 100, rotating to (0, 0, 1) after above.
28        elevation_degree_100: i32,
29    },
30}
31
32impl Direction {
33    pub(super) fn into_raw(self) -> bind::SDL_HapticDirection {
34        match self {
35            Direction::Polar { degree_100 } => bind::SDL_HapticDirection {
36                type_: bind::SDL_HAPTIC_POLAR as u8,
37                dir: [degree_100, 0, 0],
38            },
39            Direction::Cartesian { x, y, z } => bind::SDL_HapticDirection {
40                type_: bind::SDL_HAPTIC_CARTESIAN as u8,
41                dir: [x, y, z],
42            },
43            Direction::Spherical {
44                z_degree_100,
45                elevation_degree_100,
46            } => bind::SDL_HapticDirection {
47                type_: bind::SDL_HAPTIC_SPHERICAL as u8,
48                dir: [z_degree_100, elevation_degree_100, 0],
49            },
50        }
51    }
52}
53
54impl From<bind::SDL_HapticDirection> for Direction {
55    fn from(raw: bind::SDL_HapticDirection) -> Self {
56        match raw.type_ as u32 {
57            bind::SDL_HAPTIC_POLAR => Self::Polar {
58                degree_100: raw.dir[0],
59            },
60            bind::SDL_HAPTIC_CARTESIAN => Self::Cartesian {
61                x: raw.dir[0],
62                y: raw.dir[0],
63                z: raw.dir[0],
64            },
65            bind::SDL_HAPTIC_SPHERICAL => Self::Spherical {
66                z_degree_100: raw.dir[0],
67                elevation_degree_100: raw.dir[1],
68            },
69            _ => unreachable!(),
70        }
71    }
72}