use serde::{Deserialize, Serialize};
use crate::Direction;
use super::{EventSource, GamepadControlEvent};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GamepadEvent {
pub source: EventSource,
#[serde(flatten)]
pub control: GamepadControlEvent,
}
impl GamepadEvent {
pub fn direction(&self) -> Option<Direction> {
self.left_direction().or_else(|| self.right_direction())
}
pub fn left_direction(&self) -> Option<Direction> {
match &self.control {
GamepadControlEvent::Button(button) => button.d_pad_direction(),
GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 0 => axis2d.direction(),
_ => None,
}
}
pub fn right_direction(&self) -> Option<Direction> {
match &self.control {
GamepadControlEvent::Axis2D(axis2d) if axis2d.index == 1 => axis2d.direction(),
_ => None,
}
}
}