Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_feathers/src/controls/virtual_keyboard.rs
6596 views
1
use bevy_core_widgets::{Activate, Callback};
2
use bevy_ecs::{
3
bundle::Bundle,
4
component::Component,
5
hierarchy::{ChildOf, Children},
6
relationship::RelatedSpawner,
7
spawn::{Spawn, SpawnRelated, SpawnWith},
8
system::{In, SystemId},
9
};
10
use bevy_input_focus::tab_navigation::TabGroup;
11
use bevy_ui::Node;
12
use bevy_ui::Val;
13
use bevy_ui::{widget::Text, FlexDirection};
14
15
use crate::controls::{button, ButtonProps};
16
17
/// Function to spawn a virtual keyboard
18
pub fn virtual_keyboard<T>(
19
keys: impl Iterator<Item = Vec<(String, T)>> + Send + Sync + 'static,
20
on_key_press: SystemId<In<Activate>>,
21
) -> impl Bundle
22
where
23
T: Component,
24
{
25
(
26
Node {
27
flex_direction: FlexDirection::Column,
28
row_gap: Val::Px(4.),
29
..Default::default()
30
},
31
TabGroup::new(0),
32
Children::spawn((SpawnWith(move |parent: &mut RelatedSpawner<ChildOf>| {
33
for row in keys {
34
parent.spawn((
35
Node {
36
flex_direction: FlexDirection::Row,
37
column_gap: Val::Px(4.),
38
..Default::default()
39
},
40
Children::spawn(SpawnWith(move |parent: &mut RelatedSpawner<ChildOf>| {
41
for (label, key_id) in row.into_iter() {
42
parent.spawn(button(
43
ButtonProps {
44
on_click: Callback::System(on_key_press),
45
..Default::default()
46
},
47
(key_id,),
48
Spawn(Text::new(label)),
49
));
50
}
51
})),
52
));
53
}
54
}),)),
55
)
56
}
57
58