use bevy::{
input::{keyboard::KeyboardInput, ButtonState},
prelude::*,
};
#[derive(Copy, Clone)]
pub(super) struct KeyCondition {
control: bool,
shift: bool,
key: KeyCode,
}
impl KeyCondition {
pub(super) fn single(key: KeyCode) -> Self {
Self {
control: false,
shift: false,
key,
}
}
pub(super) fn with_ctrl(mut self) -> Self {
self.control = true;
self
}
pub(super) fn with_shift(mut self) -> Self {
self.shift = true;
self
}
pub(super) fn build(
self,
) -> impl Fn(Res<ButtonInput<KeyCode>>, EventReader<KeyboardInput>) -> bool {
move |keys: Res<ButtonInput<KeyCode>>, mut events: EventReader<KeyboardInput>| {
let proper_key = events
.read()
.filter(|k| k.state == ButtonState::Pressed && k.key_code == self.key)
.count()
> 0;
let control = keys.pressed(KeyCode::ControlLeft) || keys.pressed(KeyCode::ControlRight);
let shift = keys.pressed(KeyCode::ShiftLeft) || keys.pressed(KeyCode::ShiftRight);
self.control == control && shift == self.shift && proper_key
}
}
}