diff --git a/tests/action_diffs.rs b/tests/action_diffs.rs index c65f4565..379c44ce 100644 --- a/tests/action_diffs.rs +++ b/tests/action_diffs.rs @@ -3,22 +3,21 @@ use leafwing_input_manager::{ action_state::ActionDiff, axislike::DualAxisData, prelude::*, - systems::{generate_action_diffs, process_action_diffs}, + systems::{generate_action_diffs}, }; +use leafwing_input_manager::action_state::ActionDiffEvent; #[derive(Actionlike, Clone, Copy, Debug, Reflect, PartialEq, Eq, Hash)] enum Action { PayTheBills, } -#[derive(Clone, Copy, Component, Debug, Reflect, PartialEq, Eq, Hash)] -struct BankAccountId(u32); #[derive(Default)] struct Counter(pub u8); -fn spawn_da_bills(mut commands: Commands) { - commands.spawn((BankAccountId(1337), ActionState::::default())); +fn spawn_da_bills(mut commands: Commands, ) { + commands.spawn(ActionState::::default()); } fn pay_da_bills( @@ -37,6 +36,18 @@ fn pay_da_bills( } } +fn process_action_diffs( + mut action_state_query: Query<&mut ActionState>, + mut action_diff_events: EventReader>, +) { + for action_diff_event in action_diff_events.read() { + if action_diff_event.owner.is_some() { + let mut action_state = action_state_query.get_single_mut().unwrap(); + action_state.apply_diff(&action_diff_event.action_diff); + } + } +} + fn create_app() -> App { let mut app = App::new(); app.add_plugins(( @@ -45,7 +56,8 @@ fn create_app() -> App { InputManagerPlugin::::default(), )) .add_systems(Startup, spawn_da_bills) - .add_event::>(); + .add_event::>(); + app.update(); app } @@ -56,13 +68,13 @@ fn get_events_mut(app: &mut App) -> Mut> { app.world.resource_mut() } -fn send_action_diff(app: &mut App, action_diff: ActionDiff) { - let mut action_diff_events = get_events_mut::>(app); +fn send_action_diff(app: &mut App, action_diff: ActionDiffEvent) { + let mut action_diff_events = get_events_mut::>(app); action_diff_events.send(action_diff); } fn assert_has_no_action_diffs(app: &mut App) { - let action_diff_events = get_events::>(app); + let action_diff_events = get_events::>(app); let action_diff_event_reader = &mut action_diff_events.get_reader(); match action_diff_event_reader.read(action_diff_events).next() { Some(action_diff) => panic!( @@ -75,9 +87,9 @@ fn assert_has_no_action_diffs(app: &mut App) { fn assert_action_diff_created( app: &mut App, - predicate: impl Fn(&ActionDiff), + predicate: impl Fn(&ActionDiffEvent), ) { - let mut action_diff_events = get_events_mut::>(app); + let mut action_diff_events = get_events_mut::>(app); let action_diff_event_reader = &mut action_diff_events.get_reader(); assert!(action_diff_event_reader.len(action_diff_events.as_ref()) < 2); match action_diff_event_reader @@ -90,21 +102,20 @@ fn assert_action_diff_created( action_diff_events.clear(); } -fn assert_action_diff_received(app: &mut App, action_diff: ActionDiff) { +fn assert_action_diff_received(app: &mut App, action_diff: ActionDiffEvent) { let mut action_state_query = app.world.query::<&ActionState>(); let action_state = action_state_query.get_single(&app.world).unwrap(); - match action_diff { - ActionDiff::Pressed { id: _, action } => { + match action_diff.action_diff { + ActionDiff::Pressed { action } => { assert!(action_state.pressed(action)); assert!(action_state.value(action) == 1.); } - ActionDiff::Released { id: _, action } => { + ActionDiff::Released { action } => { assert!(action_state.released(action)); assert!(action_state.value(action) == 0.); assert!(action_state.axis_pair(action).is_none()); } ActionDiff::ValueChanged { - id: _, action, value, } => { @@ -112,7 +123,6 @@ fn assert_action_diff_received(app: &mut App, action_diff: ActionDiff { @@ -131,33 +141,28 @@ fn assert_action_diff_received(app: &mut App, action_diff: ActionDiff>>().single(&app.world); app.add_systems( Update, pay_da_bills(|mut action_state| { action_state.action_data_mut(Action::PayTheBills).value = 1.; }), ) - .add_systems(PostUpdate, generate_action_diffs::); + .add_systems(PostUpdate, generate_action_diffs::); app.update(); - assert_action_diff_created(&mut app, |action_diff| match action_diff { - ActionDiff::Pressed { id, action } => { - assert!(*id == BankAccountId(1337)); - assert!(*action == Action::PayTheBills); - } - ActionDiff::Released { action: _, id: _ } => { - panic!("Expected a `Pressed` variant got a `Released` variant") + assert_action_diff_created(&mut app, |action_diff| { + assert_eq!(action_diff.owner, Some(entity)); + match action_diff.action_diff { + ActionDiff::Pressed { action } => { + assert_eq!(action, Action::PayTheBills); + } + ActionDiff::Released { .. } => { + panic!("Expected a `Pressed` variant got a `Released` variant") + } + ActionDiff::ValueChanged { .. } => panic!("Expected a `Pressed` variant got a `ValueChanged` variant"), + ActionDiff::AxisPairChanged { .. } => panic!("Expected a `Pressed` variant got a `AxisPairChanged` variant"), } - ActionDiff::ValueChanged { - action: _, - id: _, - value: _, - } => panic!("Expected a `Pressed` variant got a `ValueChanged` variant"), - ActionDiff::AxisPairChanged { - action: _, - id: _, - axis_pair: _, - } => panic!("Expected a `Pressed` variant got a `AxisPairChanged` variant"), }); app.update(); @@ -166,24 +171,18 @@ fn generate_binary_action_diffs() { app.update(); - assert_action_diff_created(&mut app, |action_diff| match action_diff { - ActionDiff::Released { id, action } => { - assert!(*id == BankAccountId(1337)); - assert!(*action == Action::PayTheBills); - } - ActionDiff::Pressed { action: _, id: _ } => { - panic!("Expected a `Released` variant got a `Pressed` variant") + assert_action_diff_created(&mut app, |action_diff| { + assert_eq!(action_diff.owner, Some(entity)); + match action_diff.action_diff { + ActionDiff::Released { action } => { + assert_eq!(action, Action::PayTheBills); + } + ActionDiff::Pressed { .. } => { + panic!("Expected a `Released` variant got a `Pressed` variant") + } + ActionDiff::ValueChanged { .. } => panic!("Expected a `Released` variant got a `ValueChanged` variant"), + ActionDiff::AxisPairChanged { .. } => panic!("Expected a `Released` variant got a `AxisPairChanged` variant"), } - ActionDiff::ValueChanged { - action: _, - id: _, - value: _, - } => panic!("Expected a `Released` variant got a `ValueChanged` variant"), - ActionDiff::AxisPairChanged { - action: _, - id: _, - axis_pair: _, - } => panic!("Expected a `Released` variant got a `AxisPairChanged` variant"), }); } @@ -191,34 +190,33 @@ fn generate_binary_action_diffs() { fn generate_value_action_diffs() { let input_value = 0.5; let mut app = create_app(); + let entity = app.world.query_filtered::>>().single(&app.world); app.add_systems( Update, pay_da_bills(move |mut action_state| { action_state.action_data_mut(Action::PayTheBills).value = input_value; }), ) - .add_systems(PostUpdate, generate_action_diffs::) - .add_event::>(); + .add_systems(PostUpdate, generate_action_diffs::) + .add_event::>(); app.update(); - assert_action_diff_created(&mut app, |action_diff| match action_diff { - ActionDiff::ValueChanged { id, action, value } => { - assert!(*id == BankAccountId(1337)); - assert!(*action == Action::PayTheBills); - assert!(*value == input_value); - } - ActionDiff::Released { action: _, id: _ } => { - panic!("Expected a `ValueChanged` variant got a `Released` variant") - } - ActionDiff::Pressed { action: _, id: _ } => { - panic!("Expected a `ValueChanged` variant got a `Pressed` variant") + assert_action_diff_created(&mut app, |action_diff| { + assert_eq!(action_diff.owner, Some(entity)); + match action_diff.action_diff { + ActionDiff::ValueChanged { action, value } => { + assert_eq!(action, Action::PayTheBills); + assert_eq!(value, input_value); + } + ActionDiff::Released { .. } => { + panic!("Expected a `ValueChanged` variant got a `Released` variant") + } + ActionDiff::Pressed { .. } => { + panic!("Expected a `ValueChanged` variant got a `Pressed` variant") + } + ActionDiff::AxisPairChanged { .. } => panic!("Expected a `ValueChanged` variant got a `AxisPairChanged` variant"), } - ActionDiff::AxisPairChanged { - action: _, - id: _, - axis_pair: _, - } => panic!("Expected a `ValueChanged` variant got a `AxisPairChanged` variant"), }); app.update(); @@ -227,24 +225,19 @@ fn generate_value_action_diffs() { app.update(); - assert_action_diff_created(&mut app, |action_diff| match action_diff { - ActionDiff::Released { id, action } => { - assert!(*id == BankAccountId(1337)); - assert!(*action == Action::PayTheBills); - } - ActionDiff::Pressed { action: _, id: _ } => { - panic!("Expected a `Released` variant got a `Pressed` variant") + assert_action_diff_created(&mut app, |action_diff| { + assert_eq!(action_diff.owner, Some(entity)); + match action_diff.action_diff { + ActionDiff::Released { action } => { + assert_eq!(action, Action::PayTheBills); + } + ActionDiff::Pressed { .. } => { + panic!("Expected a `Released` variant got a `Pressed` variant") + } + ActionDiff::ValueChanged {.. } => panic!("Expected a `Released` variant got a `ValueChanged` variant"), + ActionDiff::AxisPairChanged {.. + } => panic!("Expected a `Released` variant got a `AxisPairChanged` variant"), } - ActionDiff::ValueChanged { - action: _, - id: _, - value: _, - } => panic!("Expected a `Released` variant got a `ValueChanged` variant"), - ActionDiff::AxisPairChanged { - action: _, - id: _, - axis_pair: _, - } => panic!("Expected a `Released` variant got a `AxisPairChanged` variant"), }); } @@ -252,6 +245,7 @@ fn generate_value_action_diffs() { fn generate_axis_action_diffs() { let input_axis_pair = Vec2 { x: 5., y: 8. }; let mut app = create_app(); + let entity = app.world.query_filtered::>>().single(&app.world); app.add_systems( Update, pay_da_bills(move |mut action_state| { @@ -259,32 +253,29 @@ fn generate_axis_action_diffs() { Some(DualAxisData::from_xy(input_axis_pair)); }), ) - .add_systems(PostUpdate, generate_action_diffs::) - .add_event::>(); + .add_systems(PostUpdate, generate_action_diffs::) + .add_event::>(); app.update(); - assert_action_diff_created(&mut app, |action_diff| match action_diff { - ActionDiff::AxisPairChanged { - id, - action, - axis_pair, - } => { - assert!(*id == BankAccountId(1337)); - assert!(*action == Action::PayTheBills); - assert!(*axis_pair == input_axis_pair); - } - ActionDiff::Released { action: _, id: _ } => { - panic!("Expected a `AxisPairChanged` variant got a `Released` variant") - } - ActionDiff::Pressed { action: _, id: _ } => { - panic!("Expected a `AxisPairChanged` variant got a `Pressed` variant") + assert_action_diff_created(&mut app, |action_diff| { + assert_eq!(action_diff.owner, Some(entity)); + match action_diff.action_diff { + ActionDiff::AxisPairChanged { + action, + axis_pair, + } => { + assert_eq!(action, Action::PayTheBills); + assert_eq!(axis_pair, input_axis_pair); + } + ActionDiff::Released { .. } => { + panic!("Expected a `AxisPairChanged` variant got a `Released` variant") + } + ActionDiff::Pressed { .. } => { + panic!("Expected a `AxisPairChanged` variant got a `Pressed` variant") + } + ActionDiff::ValueChanged { .. } => panic!("Expected a `AxisPairChanged` variant got a `ValueChanged` variant"), } - ActionDiff::ValueChanged { - action: _, - id: _, - value: _, - } => panic!("Expected a `AxisPairChanged` variant got a `ValueChanged` variant"), }); app.update(); @@ -293,103 +284,118 @@ fn generate_axis_action_diffs() { app.update(); - assert_action_diff_created(&mut app, |action_diff| match action_diff { - ActionDiff::Released { id, action } => { - assert!(*id == BankAccountId(1337)); - assert!(*action == Action::PayTheBills); - } - ActionDiff::Pressed { action: _, id: _ } => { - panic!("Expected a `Released` variant got a `Pressed` variant") + assert_action_diff_created(&mut app, |action_diff| { + assert_eq!(action_diff.owner, Some(entity)); + match action_diff.action_diff { + ActionDiff::Released { action } => { + assert_eq!(action, Action::PayTheBills); + } + ActionDiff::Pressed { .. } => { + panic!("Expected a `Released` variant got a `Pressed` variant") + } + ActionDiff::ValueChanged { .. } => panic!("Expected a `Released` variant got a `ValueChanged` variant"), + ActionDiff::AxisPairChanged { .. } => panic!("Expected a `Released` variant got a `AxisPairChanged` variant"), } - ActionDiff::ValueChanged { - action: _, - id: _, - value: _, - } => panic!("Expected a `Released` variant got a `ValueChanged` variant"), - ActionDiff::AxisPairChanged { - action: _, - id: _, - axis_pair: _, - } => panic!("Expected a `Released` variant got a `AxisPairChanged` variant"), }); } #[test] fn process_binary_action_diffs() { let mut app = create_app(); - app.add_systems(PreUpdate, process_action_diffs::); + let entity = app.world.query_filtered::>>().single(&app.world); + app.add_systems(PreUpdate, process_action_diffs::); let action_diff = ActionDiff::Pressed { - id: BankAccountId(1337), action: Action::PayTheBills, }; - send_action_diff(&mut app, action_diff.clone()); + let action_diff_event = ActionDiffEvent { + owner: Some(entity), + action_diff, + }; + send_action_diff(&mut app, action_diff_event.clone()); app.update(); - assert_action_diff_received(&mut app, action_diff); + assert_action_diff_received(&mut app, action_diff_event); let action_diff = ActionDiff::Released { - id: BankAccountId(1337), action: Action::PayTheBills, }; - send_action_diff(&mut app, action_diff.clone()); + let action_diff_event = ActionDiffEvent { + owner: Some(entity), + action_diff, + }; + send_action_diff(&mut app, action_diff_event.clone()); app.update(); - assert_action_diff_received(&mut app, action_diff); + assert_action_diff_received(&mut app, action_diff_event); } #[test] fn process_value_action_diff() { let mut app = create_app(); - app.add_systems(PreUpdate, process_action_diffs::); + let entity = app.world.query_filtered::>>().single(&app.world); + app.add_systems(PreUpdate, process_action_diffs::); let action_diff = ActionDiff::ValueChanged { - id: BankAccountId(1337), action: Action::PayTheBills, value: 0.5, }; - send_action_diff(&mut app, action_diff.clone()); + let action_diff_event = ActionDiffEvent { + owner: Some(entity), + action_diff, + }; + send_action_diff(&mut app, action_diff_event.clone()); app.update(); - assert_action_diff_received(&mut app, action_diff); + assert_action_diff_received(&mut app, action_diff_event); let action_diff = ActionDiff::Released { - id: BankAccountId(1337), action: Action::PayTheBills, }; - send_action_diff(&mut app, action_diff.clone()); + let action_diff_event = ActionDiffEvent { + owner: Some(entity), + action_diff, + }; + send_action_diff(&mut app, action_diff_event.clone()); app.update(); - assert_action_diff_received(&mut app, action_diff); + assert_action_diff_received(&mut app, action_diff_event); } #[test] fn process_axis_action_diff() { let mut app = create_app(); - app.add_systems(PreUpdate, process_action_diffs::); + let entity = app.world.query_filtered::>>().single(&app.world); + app.add_systems(PreUpdate, process_action_diffs::); let action_diff = ActionDiff::AxisPairChanged { - id: BankAccountId(1337), action: Action::PayTheBills, axis_pair: Vec2 { x: 1., y: 0. }, }; - send_action_diff(&mut app, action_diff.clone()); + let action_diff_event = ActionDiffEvent { + owner: Some(entity), + action_diff, + }; + send_action_diff(&mut app, action_diff_event.clone()); app.update(); - assert_action_diff_received(&mut app, action_diff); + assert_action_diff_received(&mut app, action_diff_event); let action_diff = ActionDiff::Released { - id: BankAccountId(1337), action: Action::PayTheBills, }; - send_action_diff(&mut app, action_diff.clone()); + let action_diff_event = ActionDiffEvent { + owner: Some(entity), + action_diff, + }; + send_action_diff(&mut app, action_diff_event.clone()); app.update(); - assert_action_diff_received(&mut app, action_diff); + assert_action_diff_received(&mut app, action_diff_event); }