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