pybevy_input/
gamepad_input.rs1use bevy::input::gamepad::GamepadInput;
2use pyo3::prelude::*;
3
4use crate::{gamepad_axis::PyGamepadAxis, gamepad_button::PyGamepadButton};
5
6#[pyclass(name = "GamepadInput", eq, frozen)]
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum PyGamepadInput {
9 Axis(PyGamepadAxis),
10 Button(PyGamepadButton),
11}
12
13impl From<GamepadInput> for PyGamepadInput {
14 fn from(input: GamepadInput) -> Self {
15 match input {
16 GamepadInput::Axis(axis) => PyGamepadInput::Axis(axis.into()),
17 GamepadInput::Button(button) => PyGamepadInput::Button(button.into()),
18 }
19 }
20}
21
22impl From<PyGamepadInput> for GamepadInput {
23 fn from(input: PyGamepadInput) -> Self {
24 match input {
25 PyGamepadInput::Axis(axis) => GamepadInput::Axis(axis.into()),
26 PyGamepadInput::Button(button) => GamepadInput::Button(button.into()),
27 }
28 }
29}
30
31#[pymethods]
32impl PyGamepadInput {
33 #[staticmethod]
34 pub fn from_axis(axis: PyGamepadAxis) -> Self {
35 PyGamepadInput::Axis(axis)
36 }
37
38 #[staticmethod]
39 pub fn from_button(button: PyGamepadButton) -> Self {
40 PyGamepadInput::Button(button)
41 }
42
43 pub fn axis(&self) -> Option<PyGamepadAxis> {
44 match self {
45 PyGamepadInput::Axis(a) => Some(*a),
46 PyGamepadInput::Button(_) => None,
47 }
48 }
49
50 pub fn button(&self) -> Option<PyGamepadButton> {
51 match self {
52 PyGamepadInput::Axis(_) => None,
53 PyGamepadInput::Button(b) => Some(*b),
54 }
55 }
56
57 pub fn is_axis(&self) -> bool {
58 matches!(self, PyGamepadInput::Axis(_))
59 }
60
61 pub fn is_button(&self) -> bool {
62 matches!(self, PyGamepadInput::Button(_))
63 }
64
65 fn __repr__(&self) -> String {
66 match self {
67 PyGamepadInput::Axis(a) => format!("GamepadInput.Axis({:?})", a),
68 PyGamepadInput::Button(b) => format!("GamepadInput.Button({:?})", b),
69 }
70 }
71}