use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::orientation::{Direction, Rotation};
use crate::user_input::InputKind;
use bevy::input::{
gamepad::{GamepadAxisType, GamepadButtonType},
keyboard::KeyCode,
};
use bevy::math::Vec2;
use bevy::reflect::{FromReflect, Reflect};
use bevy::utils::FloatOrd;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct SingleAxis {
pub axis_type: AxisType,
pub positive_low: f32,
pub negative_low: f32,
pub value: Option<f32>,
}
impl SingleAxis {
#[must_use]
pub fn symmetric(axis_type: impl Into<AxisType>, threshold: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
positive_low: threshold,
negative_low: -threshold,
value: None,
}
}
#[must_use]
pub fn from_value(axis_type: impl Into<AxisType>, value: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
positive_low: 0.0,
negative_low: 0.0,
value: Some(value),
}
}
#[must_use]
pub const fn mouse_wheel_x() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseWheel(MouseWheelAxisType::X),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
#[must_use]
pub const fn mouse_wheel_y() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseWheel(MouseWheelAxisType::Y),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
#[must_use]
pub const fn mouse_motion_x() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseMotion(MouseMotionAxisType::X),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
#[must_use]
pub const fn mouse_motion_y() -> SingleAxis {
SingleAxis {
axis_type: AxisType::MouseMotion(MouseMotionAxisType::Y),
positive_low: 0.,
negative_low: 0.,
value: None,
}
}
pub fn negative_only(axis_type: impl Into<AxisType>, threshold: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
negative_low: threshold,
positive_low: f32::MAX,
value: None,
}
}
pub fn positive_only(axis_type: impl Into<AxisType>, threshold: f32) -> SingleAxis {
SingleAxis {
axis_type: axis_type.into(),
negative_low: f32::MIN,
positive_low: threshold,
value: None,
}
}
#[must_use]
pub fn with_deadzone(mut self, deadzone: f32) -> SingleAxis {
self.negative_low = deadzone;
self.positive_low = deadzone;
self
}
}
impl PartialEq for SingleAxis {
fn eq(&self, other: &Self) -> bool {
self.axis_type == other.axis_type
&& FloatOrd(self.positive_low) == FloatOrd(other.positive_low)
&& FloatOrd(self.negative_low) == FloatOrd(other.negative_low)
}
}
impl Eq for SingleAxis {}
impl std::hash::Hash for SingleAxis {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.axis_type.hash(state);
FloatOrd(self.positive_low).hash(state);
FloatOrd(self.negative_low).hash(state);
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct DualAxis {
pub x: SingleAxis,
pub y: SingleAxis,
}
impl DualAxis {
pub const DEFAULT_DEADZONE: f32 = 0.1;
#[must_use]
pub fn symmetric(
x_axis_type: impl Into<AxisType>,
y_axis_type: impl Into<AxisType>,
threshold: f32,
) -> DualAxis {
DualAxis {
x: SingleAxis::symmetric(x_axis_type, threshold),
y: SingleAxis::symmetric(y_axis_type, threshold),
}
}
#[must_use]
pub fn from_value(
x_axis_type: impl Into<AxisType>,
y_axis_type: impl Into<AxisType>,
x_value: f32,
y_value: f32,
) -> DualAxis {
DualAxis {
x: SingleAxis::from_value(x_axis_type, x_value),
y: SingleAxis::from_value(y_axis_type, y_value),
}
}
#[must_use]
pub fn left_stick() -> DualAxis {
DualAxis::symmetric(
GamepadAxisType::LeftStickX,
GamepadAxisType::LeftStickY,
Self::DEFAULT_DEADZONE,
)
}
#[must_use]
pub fn right_stick() -> DualAxis {
DualAxis::symmetric(
GamepadAxisType::RightStickX,
GamepadAxisType::RightStickY,
Self::DEFAULT_DEADZONE,
)
}
pub const fn mouse_wheel() -> DualAxis {
DualAxis {
x: SingleAxis::mouse_wheel_x(),
y: SingleAxis::mouse_wheel_y(),
}
}
pub const fn mouse_motion() -> DualAxis {
DualAxis {
x: SingleAxis::mouse_motion_x(),
y: SingleAxis::mouse_motion_y(),
}
}
#[must_use]
pub fn with_deadzone(mut self, deadzone: f32) -> DualAxis {
self.x = self.x.with_deadzone(deadzone);
self.y = self.y.with_deadzone(deadzone);
self
}
}
#[allow(clippy::doc_markdown)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VirtualDPad {
pub up: InputKind,
pub down: InputKind,
pub left: InputKind,
pub right: InputKind,
}
impl VirtualDPad {
pub fn arrow_keys() -> VirtualDPad {
VirtualDPad {
up: InputKind::Keyboard(KeyCode::Up),
down: InputKind::Keyboard(KeyCode::Down),
left: InputKind::Keyboard(KeyCode::Left),
right: InputKind::Keyboard(KeyCode::Right),
}
}
pub fn wasd() -> VirtualDPad {
VirtualDPad {
up: InputKind::Keyboard(KeyCode::W),
down: InputKind::Keyboard(KeyCode::S),
left: InputKind::Keyboard(KeyCode::A),
right: InputKind::Keyboard(KeyCode::D),
}
}
#[allow(clippy::doc_markdown)] pub fn dpad() -> VirtualDPad {
VirtualDPad {
up: InputKind::GamepadButton(GamepadButtonType::DPadUp),
down: InputKind::GamepadButton(GamepadButtonType::DPadDown),
left: InputKind::GamepadButton(GamepadButtonType::DPadLeft),
right: InputKind::GamepadButton(GamepadButtonType::DPadRight),
}
}
pub fn gamepad_face_buttons() -> VirtualDPad {
VirtualDPad {
up: InputKind::GamepadButton(GamepadButtonType::North),
down: InputKind::GamepadButton(GamepadButtonType::South),
left: InputKind::GamepadButton(GamepadButtonType::West),
right: InputKind::GamepadButton(GamepadButtonType::East),
}
}
pub fn mouse_wheel() -> VirtualDPad {
VirtualDPad {
up: InputKind::MouseWheel(MouseWheelDirection::Up),
down: InputKind::MouseWheel(MouseWheelDirection::Down),
left: InputKind::MouseWheel(MouseWheelDirection::Left),
right: InputKind::MouseWheel(MouseWheelDirection::Right),
}
}
pub fn mouse_motion() -> VirtualDPad {
VirtualDPad {
up: InputKind::MouseMotion(MouseMotionDirection::Up),
down: InputKind::MouseMotion(MouseMotionDirection::Down),
left: InputKind::MouseMotion(MouseMotionDirection::Left),
right: InputKind::MouseMotion(MouseMotionDirection::Right),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct VirtualAxis {
pub negative: InputKind,
pub positive: InputKind,
}
impl VirtualAxis {
pub fn horizontal_arrow_keys() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::Left),
positive: InputKind::Keyboard(KeyCode::Right),
}
}
pub fn vertical_arrow_keys() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::Down),
positive: InputKind::Keyboard(KeyCode::Up),
}
}
pub fn ad() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::A),
positive: InputKind::Keyboard(KeyCode::D),
}
}
pub fn ws() -> VirtualAxis {
VirtualAxis {
negative: InputKind::Keyboard(KeyCode::S),
positive: InputKind::Keyboard(KeyCode::W),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AxisType {
Gamepad(GamepadAxisType),
MouseWheel(MouseWheelAxisType),
MouseMotion(MouseMotionAxisType),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MouseWheelAxisType {
X,
Y,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MouseMotionAxisType {
X,
Y,
}
impl From<GamepadAxisType> for AxisType {
fn from(axis_type: GamepadAxisType) -> Self {
AxisType::Gamepad(axis_type)
}
}
impl From<MouseWheelAxisType> for AxisType {
fn from(axis_type: MouseWheelAxisType) -> Self {
AxisType::MouseWheel(axis_type)
}
}
impl From<MouseMotionAxisType> for AxisType {
fn from(axis_type: MouseMotionAxisType) -> Self {
AxisType::MouseMotion(axis_type)
}
}
impl TryFrom<AxisType> for GamepadAxisType {
type Error = AxisConversionError;
fn try_from(axis_type: AxisType) -> Result<Self, AxisConversionError> {
match axis_type {
AxisType::Gamepad(inner) => Ok(inner),
_ => Err(AxisConversionError),
}
}
}
impl TryFrom<AxisType> for MouseWheelAxisType {
type Error = AxisConversionError;
fn try_from(axis_type: AxisType) -> Result<Self, AxisConversionError> {
match axis_type {
AxisType::MouseWheel(inner) => Ok(inner),
_ => Err(AxisConversionError),
}
}
}
impl TryFrom<AxisType> for MouseMotionAxisType {
type Error = AxisConversionError;
fn try_from(axis_type: AxisType) -> Result<Self, AxisConversionError> {
match axis_type {
AxisType::MouseMotion(inner) => Ok(inner),
_ => Err(AxisConversionError),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct AxisConversionError;
#[derive(Debug, Copy, Clone, PartialEq, Default, Deserialize, Serialize, Reflect, FromReflect)]
pub struct DualAxisData {
xy: Vec2,
}
impl DualAxisData {
pub fn new(x: f32, y: f32) -> DualAxisData {
DualAxisData {
xy: Vec2::new(x, y),
}
}
pub fn from_xy(xy: Vec2) -> DualAxisData {
DualAxisData { xy }
}
pub fn merged_with(&self, other: DualAxisData) -> DualAxisData {
DualAxisData::from_xy(self.xy() + other.xy())
}
}
impl DualAxisData {
#[must_use]
#[inline]
pub fn x(&self) -> f32 {
self.xy.x
}
#[must_use]
#[inline]
pub fn y(&self) -> f32 {
self.xy.y
}
#[must_use]
#[inline]
pub fn xy(&self) -> Vec2 {
self.xy
}
#[must_use]
#[inline]
pub fn direction(&self) -> Option<Direction> {
if self.xy.length() > 0.00001 {
return Some(Direction::new(self.xy));
}
None
}
#[must_use]
#[inline]
pub fn rotation(&self) -> Option<Rotation> {
match Rotation::from_xy(self.xy) {
Ok(rotation) => Some(rotation),
Err(_) => None,
}
}
#[must_use]
#[inline]
pub fn length(&self) -> f32 {
self.xy.length()
}
#[must_use]
#[inline]
pub fn length_squared(&self) -> f32 {
self.xy.length_squared()
}
pub fn clamp_length(&mut self, max: f32) {
self.xy = self.xy.clamp_length_max(max);
}
}
impl From<DualAxisData> for Vec2 {
fn from(data: DualAxisData) -> Vec2 {
data.xy
}
}