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

Make virtual axial controls more flexible, accepting any kind of Buttonlike #623

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release Notes

## Version 0.16.0

### Usability (0.16)

- made virtual axial controls more flexible, accepting any kind of `Buttonlike`
- removed `KeyboardVirtualAxis` and `GamepadVirtualAxis` in favor of `VirtualAxis`
- removed `KeyboardVirtualDPad` and `GamepadVirtualDPad` in favor of `VirtualDPad`
- removed `KeyboardVirtualDPad3D` in favor of `VirtualDPad3D`

## Version 0.15.1

### Enhancements (0.15.1)
Expand Down
2 changes: 1 addition & 1 deletion examples/action_state_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum PlayerAction {
impl PlayerAction {
fn mkb_input_map() -> InputMap<Self> {
InputMap::new([(Self::Jump, KeyCode::Space)])
.with_dual_axis(Self::Move, KeyboardVirtualDPad::WASD)
.with_dual_axis(Self::Move, VirtualDPad::wasd())
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/default_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl PlayerAction {
input_map.insert(Self::UseItem, GamepadButtonType::RightTrigger2);

// Default kbm input bindings
input_map.insert_dual_axis(Self::Run, KeyboardVirtualDPad::WASD);
input_map.insert_dual_axis(Self::Run, VirtualDPad::wasd());
input_map.insert(Self::Jump, KeyCode::Space);
input_map.insert(Self::UseItem, MouseButton::Left);

Expand Down
2 changes: 1 addition & 1 deletion examples/input_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn spawn_player(mut commands: Commands) {
let input_map = InputMap::default()
.with_dual_axis(
Action::Move,
KeyboardVirtualDPad::WASD
VirtualDPad::wasd()
// You can configure a processing pipeline to handle axis-like user inputs.
//
// This step adds a circular deadzone that normalizes input values
Expand Down
4 changes: 2 additions & 2 deletions examples/twin_stick_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl PlayerAction {
input_map.insert(Self::Shoot, GamepadButtonType::RightTrigger);

// Default kbm input bindings
input_map.insert_dual_axis(Self::Move, KeyboardVirtualDPad::WASD);
input_map.insert_dual_axis(Self::Look, KeyboardVirtualDPad::ARROW_KEYS);
input_map.insert_dual_axis(Self::Move, VirtualDPad::wasd());
input_map.insert_dual_axis(Self::Look, VirtualDPad::arrow_keys());
input_map.insert(Self::Shoot, MouseButton::Left);

input_map
Expand Down
10 changes: 7 additions & 3 deletions examples/virtual_dpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ fn spawn_player(mut commands: Commands) {
// Stores "which actions are currently activated"
let input_map = InputMap::default().with_dual_axis(
Action::Move,
// Define a virtual D-pad using four arbitrary keys.
// You can also use GamepadVirtualDPad to create similar ones using gamepad buttons.
KeyboardVirtualDPad::new(KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD),
// Define a virtual D-pad using four arbitrary buttons.
VirtualDPad::new(
KeyCode::KeyW,
KeyCode::KeyS,
GamepadButtonType::DPadLeft,
GamepadButtonType::DPadRight,
),
);
commands
.spawn(InputManagerBundle::with_map(input_map))
Expand Down
17 changes: 8 additions & 9 deletions src/clashing_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ mod tests {
use bevy::prelude::Reflect;

use super::*;
use crate::prelude::{KeyboardVirtualDPad, UserInput};
use crate::prelude::{UserInput, VirtualDPad};
use crate::user_input::ButtonlikeChord;

use crate as leafwing_input_manager;
Expand Down Expand Up @@ -456,7 +456,7 @@ mod tests {
CtrlAltOne,
ButtonlikeChord::new([ControlLeft, AltLeft, Digit1]),
);
input_map.insert_dual_axis(MoveDPad, KeyboardVirtualDPad::ARROW_KEYS);
input_map.insert_dual_axis(MoveDPad, VirtualDPad::arrow_keys());
input_map.insert(CtrlUp, ButtonlikeChord::new([ControlLeft, ArrowUp]));

input_map
Expand All @@ -473,16 +473,15 @@ mod tests {
}

mod basic_functionality {
use super::*;
use crate::{
input_map::UpdatedValue,
plugin::{AccumulatorPlugin, CentralInputStorePlugin},
prelude::{AccumulatedMouseMovement, AccumulatedMouseScroll, ModifierKey},
prelude::{AccumulatedMouseMovement, AccumulatedMouseScroll, ModifierKey, VirtualDPad},
};
use bevy::{input::InputPlugin, prelude::*};
use Action::*;

use super::*;

#[test]
#[ignore = "Figuring out how to handle the length of chords with group inputs is out of scope."]
fn input_types_have_right_length() {
Expand All @@ -501,7 +500,7 @@ mod tests {
let modified_chord = ButtonlikeChord::modified(ModifierKey::Control, KeyA).decompose();
assert_eq!(modified_chord.len(), 2);

let group = KeyboardVirtualDPad::WASD.decompose();
let group = VirtualDPad::wasd().decompose();
assert_eq!(group.len(), 1);
}

Expand All @@ -513,11 +512,11 @@ mod tests {
let ab = ButtonlikeChord::new([KeyA, KeyB]);
let bc = ButtonlikeChord::new([KeyB, KeyC]);
let abc = ButtonlikeChord::new([KeyA, KeyB, KeyC]);
let axyz_dpad = KeyboardVirtualDPad::new(KeyA, KeyX, KeyY, KeyZ);
let abcd_dpad = KeyboardVirtualDPad::WASD;
let axyz_dpad = VirtualDPad::new(KeyA, KeyX, KeyY, KeyZ);
let abcd_dpad = VirtualDPad::wasd();

let ctrl_up = ButtonlikeChord::new([ArrowUp, ControlLeft]);
let directions_dpad = KeyboardVirtualDPad::ARROW_KEYS;
let directions_dpad = VirtualDPad::arrow_keys();

assert!(!inputs_clash(a, b));
assert!(inputs_clash(a, ab.clone()));
Expand Down
4 changes: 2 additions & 2 deletions src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn find_gamepad(_gamepads: &Gamepads) -> Gamepad {
/// (Action::Jump, KeyCode::Space),
/// ])
/// // Associate actions with other input types.
/// .with_dual_axis(Action::Move, KeyboardVirtualDPad::WASD)
/// .with_dual_axis(Action::Move, VirtualDPad::wasd())
/// .with_dual_axis(Action::Move, GamepadStick::LEFT)
/// // Associate an action with multiple inputs at once.
/// .with_one_to_many(Action::Jump, [KeyCode::KeyJ, KeyCode::KeyU]);
Expand Down Expand Up @@ -966,7 +966,7 @@ mod tests {
Axis,
#[actionlike(DualAxis)]
DualAxis,
#[actionlike(Axis)]
#[actionlike(TripleAxis)]
TripleAxis,
}

Expand Down
14 changes: 7 additions & 7 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,18 @@ impl<A: Actionlike + TypePath + bevy::reflect::GetTypeRegistration> Plugin

#[cfg(feature = "keyboard")]
app.register_buttonlike_input::<KeyCode>()
.register_buttonlike_input::<ModifierKey>()
.register_axislike_input::<KeyboardVirtualAxis>()
.register_dual_axislike_input::<KeyboardVirtualDPad>()
.register_triple_axislike_input::<KeyboardVirtualDPad3D>();
.register_buttonlike_input::<ModifierKey>();

#[cfg(feature = "gamepad")]
app.register_buttonlike_input::<GamepadControlDirection>()
.register_axislike_input::<GamepadControlAxis>()
.register_dual_axislike_input::<GamepadStick>()
.register_buttonlike_input::<GamepadButtonType>()
.register_axislike_input::<GamepadVirtualAxis>()
.register_dual_axislike_input::<GamepadVirtualDPad>();
.register_buttonlike_input::<GamepadButtonType>();

// Virtual Axes
app.register_axislike_input::<VirtualAxis>()
.register_dual_axislike_input::<VirtualDPad>()
.register_triple_axislike_input::<VirtualDPad3D>();

// Chords
app.register_buttonlike_input::<ButtonlikeChord>()
Expand Down
Loading