use crate::{construct::*, prelude::*, string_cursor::*};
use bevy::prelude::*;
use std::borrow::Cow;
pub(crate) fn plugin(_app: &mut App) {}
#[derive(Debug, Clone, Component, Reflect)]
pub struct Password;
unsafe impl Submitter for Password {
type Out = String;
}
impl Construct for Password {
type Props = Cow<'static, str>;
fn construct(
context: &mut ConstructContext,
props: Self::Props,
) -> Result<Self, ConstructError> {
let input_state = StringCursor::default();
let mut commands = context.world.commands();
commands
.entity(context.id)
.insert(Prompt(props))
.insert(input_state)
.insert(Focusable::default());
context.world.flush();
Ok(Password)
}
}
#[cfg(test)]
mod test {
use super::*;
use bevy::input::{
ButtonState,
keyboard::{Key, KeyboardInput},
};
#[test]
fn test_password_key_presses() {
let mut app = App::new();
app.add_plugins(MinimalPlugins)
.add_plugins(AskyPlugin)
.add_message::<KeyboardInput>()
.init_resource::<bevy::input::ButtonInput<bevy::input::keyboard::KeyCode>>()
.init_resource::<bevy::input_focus::InputFocus>();
let entity = app
.world_mut()
.spawn((
Password,
StringCursor::default(),
Focusable::default(),
Prompt(Cow::Borrowed("Password: ")),
))
.id();
app.update();
fn send_key_event(app: &mut App, event: KeyboardInput) {
app.world_mut()
.resource_mut::<Messages<KeyboardInput>>()
.write(event);
}
fn create_char_event(c: char) -> KeyboardInput {
use bevy::input::keyboard::KeyCode;
let key_code = match c {
'P' => KeyCode::KeyP,
'a' => KeyCode::KeyA,
's' => KeyCode::KeyS,
'w' => KeyCode::KeyW,
'o' => KeyCode::KeyO,
'r' => KeyCode::KeyR,
'd' => KeyCode::KeyD,
'1' => KeyCode::Digit1,
'2' => KeyCode::Digit2,
'3' => KeyCode::Digit3,
'@' => KeyCode::Digit2, '#' => KeyCode::Digit3, _ => KeyCode::KeyA, };
KeyboardInput {
logical_key: Key::Character(c.to_string().into()),
state: ButtonState::Pressed,
window: Entity::PLACEHOLDER,
key_code,
text: Some(c.to_string().into()),
repeat: false,
}
}
fn create_key_event(key: Key) -> KeyboardInput {
use bevy::input::keyboard::KeyCode;
let key_code = match key {
Key::Backspace => KeyCode::Backspace,
Key::Delete => KeyCode::Delete,
Key::ArrowLeft => KeyCode::ArrowLeft,
Key::ArrowRight => KeyCode::ArrowRight,
Key::Enter => KeyCode::Enter,
Key::Escape => KeyCode::Escape,
_ => KeyCode::Backspace, };
KeyboardInput {
logical_key: key,
state: ButtonState::Pressed,
window: Entity::PLACEHOLDER,
key_code,
text: None,
repeat: false,
}
}
send_key_event(&mut app, create_char_event('P'));
send_key_event(&mut app, create_char_event('a'));
send_key_event(&mut app, create_char_event('s'));
send_key_event(&mut app, create_char_event('s'));
send_key_event(&mut app, create_char_event('w'));
send_key_event(&mut app, create_char_event('o'));
send_key_event(&mut app, create_char_event('r'));
send_key_event(&mut app, create_char_event('d'));
send_key_event(&mut app, create_char_event('1'));
send_key_event(&mut app, create_char_event('2'));
send_key_event(&mut app, create_char_event('3'));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password123");
assert_eq!(cursor.index, 11);
send_key_event(&mut app, create_key_event(Key::Backspace));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password12");
assert_eq!(cursor.index, 10);
send_key_event(&mut app, create_key_event(Key::ArrowLeft));
send_key_event(&mut app, create_key_event(Key::ArrowLeft));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password12");
assert_eq!(cursor.index, 8);
send_key_event(&mut app, create_key_event(Key::Delete));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password2");
assert_eq!(cursor.index, 8);
send_key_event(&mut app, create_key_event(Key::ArrowRight));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.index, 9);
send_key_event(&mut app, create_char_event('@'));
send_key_event(&mut app, create_char_event('#'));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password2@#");
assert_eq!(cursor.index, 11);
send_key_event(&mut app, create_key_event(Key::Enter));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password2@#");
send_key_event(&mut app, create_key_event(Key::Escape));
app.update();
let cursor = app.world().get::<StringCursor>(entity).unwrap();
assert_eq!(cursor.value, "Password2@#");
}
}