use bevy::ecs::component::Component;
use bevy::input::keyboard::KeyCode;
use bevy::reflect::Reflect;
use super::common::{
CONTROLLER_DURATION_BEFORE_FIRST_REPEAT, CONTROLLER_DURATION_BETWEEN_REPEAT, OtsUiActions,
OtsUiValues,
};
use crate::keyboard::binding::KeyboardBinding;
use crate::keyboard::plugin::KeyboardPlugin;
use crate::keyboard::triggers::{ButtonPressed, TriggerTrait};
pub type OtsUiKeyboardPlugin = KeyboardPlugin<OtsUiActions, OtsUiValues, OtsUiKeyboardBinding>;
#[derive(Component, Reflect)]
pub struct OtsUiKeyboardBinding {
enabled: bool,
up: ButtonPressed,
down: ButtonPressed,
left: ButtonPressed,
right: ButtonPressed,
validate: ButtonPressed,
back: ButtonPressed,
}
impl OtsUiKeyboardBinding {
pub fn enable(&mut self) {
self.enabled = true;
}
pub fn disable(&mut self) {
self.enabled = false;
}
pub fn set_enable(&mut self, enable: bool) {
self.enabled = enable;
}
pub fn new() -> Self {
Self {
enabled: true,
up: ButtonPressed::new(KeyCode::ArrowUp).with_repeat(
CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
CONTROLLER_DURATION_BETWEEN_REPEAT,
),
down: ButtonPressed::new(KeyCode::ArrowDown).with_repeat(
CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
CONTROLLER_DURATION_BETWEEN_REPEAT,
),
left: ButtonPressed::new(KeyCode::ArrowLeft).with_repeat(
CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
CONTROLLER_DURATION_BETWEEN_REPEAT,
),
right: ButtonPressed::new(KeyCode::ArrowRight).with_repeat(
CONTROLLER_DURATION_BEFORE_FIRST_REPEAT,
CONTROLLER_DURATION_BETWEEN_REPEAT,
),
validate: ButtonPressed::new(KeyCode::Enter),
back: ButtonPressed::new(KeyCode::Backspace),
}
}
}
impl KeyboardBinding<OtsUiActions, OtsUiValues> for OtsUiKeyboardBinding {
fn get_action_bindings<'a>(&'a mut self) -> Vec<(&'a mut dyn TriggerTrait, OtsUiActions)> {
if !self.enabled {
return vec![];
}
return vec![
(&mut self.up, OtsUiActions::Up),
(&mut self.down, OtsUiActions::Down),
(&mut self.left, OtsUiActions::Left),
(&mut self.right, OtsUiActions::Right),
(&mut self.validate, OtsUiActions::Validate),
(&mut self.back, OtsUiActions::Back),
];
}
}