|
| 1 | +//! Virtual keyboard example |
| 2 | +
|
| 3 | +use bevy::{ |
| 4 | + color::palettes::css::NAVY, |
| 5 | + core_widgets::{Activate, CoreWidgetsPlugins}, |
| 6 | + ecs::relationship::RelatedSpawnerCommands, |
| 7 | + feathers::{ |
| 8 | + controls::virtual_keyboard, dark_theme::create_dark_theme, theme::UiTheme, FeathersPlugin, |
| 9 | + }, |
| 10 | + input_focus::{tab_navigation::TabNavigationPlugin, InputDispatchPlugin}, |
| 11 | + prelude::*, |
| 12 | + winit::WinitSettings, |
| 13 | +}; |
| 14 | + |
| 15 | +fn main() { |
| 16 | + App::new() |
| 17 | + .add_plugins(( |
| 18 | + DefaultPlugins, |
| 19 | + CoreWidgetsPlugins, |
| 20 | + InputDispatchPlugin, |
| 21 | + TabNavigationPlugin, |
| 22 | + FeathersPlugin, |
| 23 | + )) |
| 24 | + .insert_resource(UiTheme(create_dark_theme())) |
| 25 | + // Only run the app when there is user input. This will significantly reduce CPU/GPU use. |
| 26 | + .insert_resource(WinitSettings::desktop_app()) |
| 27 | + .add_systems(Startup, setup) |
| 28 | + .run(); |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Component)] |
| 32 | +struct VirtualKey(String); |
| 33 | + |
| 34 | +fn on_virtual_key_pressed( |
| 35 | + In(Activate(virtual_key_entity)): In<Activate>, |
| 36 | + virtual_key_query: Query<&VirtualKey>, |
| 37 | +) { |
| 38 | + if let Ok(VirtualKey(label)) = virtual_key_query.get(virtual_key_entity) { |
| 39 | + println!("key pressed: {label}"); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn setup(mut commands: Commands) { |
| 44 | + // ui camera |
| 45 | + commands.spawn(Camera2d); |
| 46 | + let callback = commands.register_system(on_virtual_key_pressed); |
| 47 | + |
| 48 | + let layout = [ |
| 49 | + vec!["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", ","], |
| 50 | + vec!["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"], |
| 51 | + vec!["A", "S", "D", "F", "G", "H", "J", "K", "L", "'"], |
| 52 | + vec!["Z", "X", "C", "V", "B", "N", "M", "-", "/"], |
| 53 | + vec!["space", "enter", "backspace"], |
| 54 | + vec!["left", "right", "up", "down", "home", "end"], |
| 55 | + ]; |
| 56 | + |
| 57 | + let keys_iter = layout.into_iter().map(|row| { |
| 58 | + row.into_iter() |
| 59 | + .map(|label| { |
| 60 | + let label_string = label.to_string(); |
| 61 | + (label_string.clone(), VirtualKey(label_string)) |
| 62 | + }) |
| 63 | + .collect() |
| 64 | + }); |
| 65 | + |
| 66 | + commands |
| 67 | + .spawn(Node { |
| 68 | + width: Val::Percent(100.0), |
| 69 | + height: Val::Percent(100.0), |
| 70 | + align_items: AlignItems::End, |
| 71 | + justify_content: JustifyContent::Center, |
| 72 | + ..default() |
| 73 | + }) |
| 74 | + .with_children(|parent: &mut RelatedSpawnerCommands<ChildOf>| { |
| 75 | + parent |
| 76 | + .spawn(( |
| 77 | + Node { |
| 78 | + flex_direction: FlexDirection::Column, |
| 79 | + border: Val::Px(5.).into(), |
| 80 | + row_gap: Val::Px(5.), |
| 81 | + padding: Val::Px(5.).into(), |
| 82 | + align_items: AlignItems::Center, |
| 83 | + margin: Val::Px(25.).into(), |
| 84 | + ..Default::default() |
| 85 | + }, |
| 86 | + BackgroundColor(NAVY.into()), |
| 87 | + BorderColor::all(Color::WHITE), |
| 88 | + BorderRadius::all(Val::Px(10.)), |
| 89 | + )) |
| 90 | + .with_children(|parent: &mut RelatedSpawnerCommands<ChildOf>| { |
| 91 | + parent.spawn(Text::new("virtual keyboard")); |
| 92 | + parent.spawn(virtual_keyboard(keys_iter, callback)); |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
0 commit comments