Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Reflect for InputMap #386

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ bevy_egui = { version = "0.22", optional = true }

petitset = { version = "0.2.1", features = ["serde_compat"] }
derive_more = { version = "0.99", default-features = false, features = [
"as_ref",
"as_mut",
"from",
"display",
"error",
] }
Expand Down
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

### Enhancements

- Implement `Reflect` for `InputMap` and most other types.
- Added `DeadZoneShape` for `DualAxis` which allows for different deadzones shapes: cross, rectangle, and ellipse.
- Added sensitivity for `SingleAxis` and `DualAxis`, allowing you to scale mouse, keypad and gamepad inputs differently for each action.
- Added a helper `from_keys` to `VirtualAxis` to simplify creating one from two keys
Expand Down
16 changes: 8 additions & 8 deletions src/axislike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize};
/// # Warning
///
/// `positive_low` must be greater than or equal to `negative_low` for this type to be validly constructed.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Reflect)]
pub struct SingleAxis {
/// The axis that is being checked.
pub axis_type: AxisType,
Expand Down Expand Up @@ -203,7 +203,7 @@ impl std::hash::Hash for SingleAxis {
/// # Warning
///
/// `positive_low` must be greater than or equal to `negative_low` for both `x` and `y` for this type to be validly constructed.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, Reflect)]
pub struct DualAxis {
/// The axis representing horizontal movement.
pub x: SingleAxis,
Expand Down Expand Up @@ -342,7 +342,7 @@ impl DualAxis {
/// even though it can be stored as an [`InputKind`].
///
/// Instead, use it directly as [`InputKind::DualAxis`]!
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub struct VirtualDPad {
/// The input that represents the up direction in this virtual DPad
pub up: InputKind,
Expand Down Expand Up @@ -449,7 +449,7 @@ impl VirtualDPad {
/// even though it can be stored as an [`InputKind`].
///
/// Instead, use it directly as [`InputKind::SingleAxis`]!
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub struct VirtualAxis {
/// The input that represents the negative direction of this virtual axis
pub negative: InputKind,
Expand Down Expand Up @@ -516,7 +516,7 @@ impl VirtualAxis {
/// The type of axis used by a [`UserInput`](crate::user_input::UserInput).
///
/// This is stored in either a [`SingleAxis`] or [`DualAxis`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub enum AxisType {
/// Input associated with a gamepad, such as the triggers or one axis of an analog stick.
Gamepad(GamepadAxisType),
Expand All @@ -529,7 +529,7 @@ pub enum AxisType {
/// The direction of motion of the mouse wheel.
///
/// Stored in the [`AxisType`] enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub enum MouseWheelAxisType {
/// Horizontal movement.
///
Expand All @@ -544,7 +544,7 @@ pub enum MouseWheelAxisType {
/// The direction of motion of the mouse.
///
/// Stored in the [`AxisType`] enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub enum MouseMotionAxisType {
/// Horizontal movement.
X,
Expand Down Expand Up @@ -736,7 +736,7 @@ impl From<DualAxisData> for Vec2 {
/// If a volume of a shape is 0, then all input values are read.
///
/// Deadzone values should be in the range `0.0..=1.0`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Reflect)]
pub enum DeadZoneShape {
/// Deadzone with the shape of a cross.
///
Expand Down
4 changes: 2 additions & 2 deletions src/buttonlike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl ButtonState {
/// A buttonlike-input triggered by [`MouseWheel`](bevy::input::mouse::MouseWheel) events
///
/// These will be considered pressed if non-zero net movement in the correct direction is detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub enum MouseWheelDirection {
/// Corresponds to `+y`
Up,
Expand All @@ -102,7 +102,7 @@ pub enum MouseWheelDirection {
/// A buttonlike-input triggered by [`MouseMotion`](bevy::input::mouse::MouseMotion) events
///
/// These will be considered pressed if non-zero net movement in the correct direction is detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect)]
pub enum MouseMotionDirection {
/// Corresponds to `+y`
Up,
Expand Down
14 changes: 8 additions & 6 deletions src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use crate::action_state::ActionData;
use crate::buttonlike::ButtonState;
use crate::clashing_inputs::ClashStrategy;
use crate::input_streams::InputStreams;
use crate::reflect::ReflectPetitSet;
use crate::user_input::{InputKind, Modifier, UserInput};
use crate::Actionlike;

use bevy::ecs::component::Component;
use bevy::ecs::system::Resource;
use bevy::input::gamepad::Gamepad;
use bevy::reflect::TypeUuid;
use bevy::reflect::{Reflect, TypeUuid};

use core::fmt::Debug;
use petitset::PetitSet;
Expand Down Expand Up @@ -114,20 +115,21 @@ fn change_left_stick_values(mut query: Query<&mut InputMap<Action>>){
}
```
**/
#[derive(Resource, Component, Debug, Clone, PartialEq, Eq, TypeUuid)]
#[derive(Resource, Component, Debug, Clone, PartialEq, Eq, TypeUuid, Reflect)]
#[uuid = "D7DECC78-8573-42FF-851A-F0344C7D05C9"]
pub struct InputMap<A: Actionlike> {
/// The raw vector of [PetitSet]s used to store the input mapping,
/// indexed by the `Actionlike::id` of `A`
map: Vec<PetitSet<UserInput, 16>>,
map: Vec<ReflectPetitSet<UserInput, 16>>,
associated_gamepad: Option<Gamepad>,
#[reflect(ignore)]
marker: PhantomData<A>,
}

impl<A: Actionlike> Default for InputMap<A> {
fn default() -> Self {
InputMap {
map: A::variants().map(|_| PetitSet::default()).collect(),
map: A::variants().map(|_| ReflectPetitSet::default()).collect(),
associated_gamepad: None,
marker: PhantomData,
}
Expand Down Expand Up @@ -452,12 +454,12 @@ impl<A: Actionlike> InputMap<A> {
self.map
.iter()
.enumerate()
.map(|(action_index, inputs)| (inputs, A::get_at(action_index).unwrap()))
.map(|(action_index, inputs)| (inputs.as_ref(), A::get_at(action_index).unwrap()))
}

/// Returns an iterator over all mapped inputs
pub fn iter_inputs(&self) -> impl Iterator<Item = &PetitSet<UserInput, 16>> {
self.map.iter()
self.map.iter().map(|inputs| inputs.as_ref())
}

/// Returns the `action` mappings
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod input_streams;
pub mod orientation;
pub mod plugin;
pub mod press_scheduler;
mod reflect;
pub mod scan_codes;
pub mod systems;
pub mod user_input;
Expand Down
17 changes: 16 additions & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! Contains main plugin exported by this crate.

use crate::action_state::ActionState;
use crate::axislike::{DualAxis, SingleAxis, VirtualAxis, VirtualDPad};
use crate::buttonlike::{MouseMotionDirection, MouseWheelDirection};
use crate::clashing_inputs::ClashStrategy;
use crate::prelude::ActionState;
use crate::input_map::InputMap;
use crate::user_input::{InputKind, Modifier, UserInput};
use crate::Actionlike;
use core::hash::Hash;
use core::marker::PhantomData;
Expand Down Expand Up @@ -153,6 +157,17 @@ impl<A: Actionlike> Plugin for InputManagerPlugin<A> {
};

app.register_type::<ActionState<A>>()
.register_type::<InputMap<A>>()
.register_type::<UserInput>()
.register_type::<UserInput>()
.register_type::<InputKind>()
.register_type::<VirtualDPad>()
.register_type::<VirtualAxis>()
.register_type::<SingleAxis>()
.register_type::<DualAxis>()
.register_type::<Modifier>()
.register_type::<MouseWheelDirection>()
.register_type::<MouseMotionDirection>()
// Resources
.init_resource::<ToggleActions<A>>()
.init_resource::<ClashStrategy>();
Expand Down
Loading