Skip to content

Commit

Permalink
Fix up tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice Cecile committed Jan 24, 2024
1 parent 27a1bbf commit 4fb02ec
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
19 changes: 13 additions & 6 deletions examples/default_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ enum PlayerAction {
UseItem,
}

impl PlayerAction {
// The `strum` crate provides a deriveable trait for this!
fn variants() -> &'static [PlayerAction] {
&[Self::Run, Self::Jump, Self::UseItem]
}
}

// Exhaustively match `PlayerAction` and define the default binding to the input
impl PlayerAction {
fn default_keyboard_mouse_input(action: PlayerAction) -> UserInput {
fn default_keyboard_mouse_input(&self) -> UserInput {
// Match against the provided action to get the correct default keyboard-mouse input
match action {
match self {
Self::Run => UserInput::VirtualDPad(VirtualDPad::wasd()),
Self::Jump => UserInput::Single(InputKind::Keyboard(KeyCode::Space)),
Self::UseItem => UserInput::Single(InputKind::Mouse(MouseButton::Left)),
}
}

fn default_gamepad_input(action: PlayerAction) -> UserInput {
fn default_gamepad_input(&self) -> UserInput {
// Match against the provided action to get the correct default gamepad input
match action {
match self {
Self::Run => UserInput::Single(InputKind::DualAxis(DualAxis::left_stick())),
Self::Jump => UserInput::Single(InputKind::GamepadButton(GamepadButtonType::South)),
Self::UseItem => {
Expand All @@ -52,8 +59,8 @@ fn spawn_player(mut commands: Commands) {
// Loop through each action in `PlayerAction` and get the default `UserInput`,
// then insert each default input into input_map
for action in PlayerAction::variants() {
input_map.insert(action, PlayerAction::default_keyboard_mouse_input(action));
input_map.insert(action, PlayerAction::default_gamepad_input(action));
input_map.insert(*action, PlayerAction::default_keyboard_mouse_input(action));
input_map.insert(*action, PlayerAction::default_gamepad_input(action));
}

// Spawn the player with the populated input_map
Expand Down
13 changes: 10 additions & 3 deletions examples/twin_stick_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ pub enum PlayerAction {
Shoot,
}

impl PlayerAction {
// The `strum` crate provides a deriveable trait for this!
fn variants() -> &'static [PlayerAction] {
&[Self::Move, Self::Look, Self::Shoot]
}
}

// Exhaustively match `PlayerAction` and define the default binding to the input
impl PlayerAction {
fn default_gamepad_binding(&self) -> UserInput {
Expand All @@ -51,7 +58,7 @@ impl PlayerAction {
}
}

fn default_mkb_binding(&self) -> UserInput {
fn default_kbm_binding(&self) -> UserInput {
// Match against the provided action to get the correct default gamepad input
match self {
Self::Move => UserInput::VirtualDPad(VirtualDPad::wasd()),
Expand All @@ -64,8 +71,8 @@ impl PlayerAction {
let mut input_map = InputMap::default();

for variant in PlayerAction::variants() {
input_map.insert(variant, variant.default_mkb_binding());
input_map.insert(variant, variant.default_gamepad_binding());
input_map.insert(*variant, variant.default_kbm_binding());
input_map.insert(*variant, variant.default_gamepad_binding());
}
input_map
}
Expand Down
19 changes: 0 additions & 19 deletions tests/actionlike_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,3 @@ enum NamedFieldVariantsAction {
Run { x: usize, y: usize },
Jump,
}

#[test]
fn in_order_iteration() {
let constructed_vec = vec![SimpleAction::Zero, SimpleAction::One, SimpleAction::Two];
let reversed_vec = vec![SimpleAction::Two, SimpleAction::One, SimpleAction::Zero];

let iterated_vec: Vec<SimpleAction> = SimpleAction::variants().collect();

assert_eq!(constructed_vec, iterated_vec);
assert!(iterated_vec != reversed_vec);
}

#[test]
fn get_at() {
assert_eq!(SimpleAction::get_at(0), Some(SimpleAction::Zero));
assert_eq!(SimpleAction::get_at(1), Some(SimpleAction::One));
assert_eq!(SimpleAction::get_at(2), Some(SimpleAction::Two));
assert_eq!(SimpleAction::get_at(3), None);
}
17 changes: 16 additions & 1 deletion tests/clashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ enum Action {
CtrlAltOne,
}

impl Action {
fn variants() -> &'static [Action] {
&[
Self::One,
Self::Two,
Self::OneAndTwo,
Self::TwoAndThree,
Self::OneAndTwoAndThree,
Self::CtrlOne,
Self::AltOne,
Self::CtrlAltOne,
]
}
}

fn spawn_input_map(mut commands: Commands) {
use Action::*;
use KeyCode::*;
Expand Down Expand Up @@ -73,7 +88,7 @@ impl ClashTestExt for App {
let keyboard_input = self.world.resource::<Input<KeyCode>>();

for action in Action::variants() {
if pressed_actions.contains(&action) {
if pressed_actions.contains(action) {
assert!(
input_map.pressed(&action, &InputStreams::from_world(&self.world, None), clash_strategy),
"{action:?} was incorrectly not pressed for {clash_strategy:?} when `Input<KeyCode>` was \n {keyboard_input:?}."
Expand Down
11 changes: 11 additions & 0 deletions tests/mouse_motion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ enum ButtonlikeTestAction {
Right,
}

impl ButtonlikeTestAction {
fn variants() -> &'static [ButtonlikeTestAction] {
&[
ButtonlikeTestAction::Up,
ButtonlikeTestAction::Down,
ButtonlikeTestAction::Left,
ButtonlikeTestAction::Right,
]
}
}

#[derive(Actionlike, Clone, Copy, Debug, Reflect, PartialEq, Eq, Hash)]
enum AxislikeTestAction {
X,
Expand Down
6 changes: 6 additions & 0 deletions tests/mouse_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ enum ButtonlikeTestAction {
Right,
}

impl ButtonlikeTestAction {
fn variants() -> &'static [ButtonlikeTestAction] {
&[Self::Up, Self::Down, Self::Left, Self::Right]
}
}

#[derive(Actionlike, Clone, Copy, Debug, Reflect, PartialEq, Eq, Hash)]
enum AxislikeTestAction {
X,
Expand Down

0 comments on commit 4fb02ec

Please sign in to comment.