rich_sdl2_rust/event/joystick/
axis.rs

1//! Axes for a physical joystick.
2
3use std::os::raw::c_int;
4
5use crate::bind;
6
7use super::{InputIndex, Joystick};
8
9/// An axis for a physical joystick.
10#[derive(Debug)]
11pub struct Axis<'joystick> {
12    index: c_int,
13    joystick: &'joystick Joystick,
14}
15
16impl<'joystick> Axis<'joystick> {
17    pub(super) fn new(index: InputIndex, joystick: &'joystick Joystick) -> Self {
18        Self {
19            index: index.0,
20            joystick,
21        }
22    }
23
24    /// Returns the state of axis.
25    #[must_use]
26    pub fn state(&self) -> i16 {
27        unsafe { bind::SDL_JoystickGetAxis(self.joystick.ptr.as_ptr(), self.index) }
28    }
29}
30
31/// A set of `Axis` for a physical joystick.
32#[derive(Debug)]
33pub struct Axes<'joystick>(pub Vec<Axis<'joystick>>);
34
35impl<'joystick> Axes<'joystick> {
36    pub(super) fn new(joystick: &'joystick Joystick) -> Self {
37        let num_axes = unsafe { bind::SDL_JoystickNumAxes(joystick.ptr.as_ptr()) };
38        let axes = (0..num_axes)
39            .map(|index| Axis { index, joystick })
40            .collect();
41        Self(axes)
42    }
43}