diff --git a/examples/default_controls.rs b/examples/default_controls.rs index ba878a0a..77fccd56 100644 --- a/examples/default_controls.rs +++ b/examples/default_controls.rs @@ -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 => { @@ -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 diff --git a/examples/twin_stick_controller.rs b/examples/twin_stick_controller.rs index b692c01d..18c1c6bd 100644 --- a/examples/twin_stick_controller.rs +++ b/examples/twin_stick_controller.rs @@ -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 { @@ -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()), @@ -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 } diff --git a/tests/actionlike_derive.rs b/tests/actionlike_derive.rs index 27e42481..0eefa1d9 100644 --- a/tests/actionlike_derive.rs +++ b/tests/actionlike_derive.rs @@ -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::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); -} diff --git a/tests/clashes.rs b/tests/clashes.rs index dc667f6f..d456da13 100644 --- a/tests/clashes.rs +++ b/tests/clashes.rs @@ -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::*; @@ -73,7 +88,7 @@ impl ClashTestExt for App { let keyboard_input = self.world.resource::>(); 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` was \n {keyboard_input:?}." diff --git a/tests/mouse_motion.rs b/tests/mouse_motion.rs index bc88680f..72741ecb 100644 --- a/tests/mouse_motion.rs +++ b/tests/mouse_motion.rs @@ -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, diff --git a/tests/mouse_wheel.rs b/tests/mouse_wheel.rs index 58460408..80fe96c2 100644 --- a/tests/mouse_wheel.rs +++ b/tests/mouse_wheel.rs @@ -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,