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