use bevy_ecs::entity_disabling::Disabled;
use bevy_ecs::prelude::*;
use bevy_ecs::relationship::Relationship;
use bevy_enhanced_input::context::ExternallyMocked;
use bevy_enhanced_input::prelude::*;
#[derive(Component)]
pub struct InputMarker<C> {
marker: core::marker::PhantomData<C>,
}
impl<C> Default for InputMarker<C> {
fn default() -> Self {
Self {
marker: core::marker::PhantomData,
}
}
}
pub(crate) fn propagate_input_marker<C: Component>(
trigger: On<Add, InputMarker<C>>,
actions: Query<&Actions<C>>,
mocked: Query<(), With<ExternallyMocked>>,
mut commands: Commands,
) {
if let Ok(actions) = actions.get(trigger.entity) {
actions.iter().for_each(|action| {
if mocked.contains(action) {
return;
}
commands.entity(action).insert(InputMarker::<C>::default());
});
}
}
pub(crate) fn add_action_markers_from_context<C: Component>(
trigger: On<Add, ActionOf<C>>,
action_of: Query<&ActionOf<C>, (Without<ExternallyMocked>, Allow<Disabled>)>,
context: Query<(Has<InputMarker<C>>, Has<Disabled>), (With<C>, Allow<Disabled>)>,
mut commands: Commands,
) {
let Ok(action_of) = action_of.get(trigger.entity) else {
return;
};
let Ok((has_input_marker, disabled)) = context.get(action_of.get()) else {
return;
};
if has_input_marker {
commands
.entity(trigger.entity)
.insert(InputMarker::<C>::default());
}
if disabled {
commands.entity(trigger.entity).insert(Disabled);
}
}
pub(crate) fn add_input_marker_from_binding<C: Component>(
trigger: On<Add, Bindings>,
action: Query<
(),
(
With<ActionOf<C>>,
Without<InputMarker<C>>,
Without<ExternallyMocked>,
),
>,
mut commands: Commands,
) {
if action.get(trigger.entity).is_err() {
return;
};
commands
.entity(trigger.entity)
.insert(InputMarker::<C>::default());
}