1use bevy::color::palettes::css::DARK_GREY;
26use bevy::color::palettes::tailwind::SLATE_300;
27use bevy::input_focus::AutoFocus;
28use bevy::input_focus::{
29 tab_navigation::{TabGroup, TabIndex, TabNavigationPlugin},
30 InputFocus,
31};
32use bevy::prelude::*;
33use bevy::text::{EditableText, TextCursorStyle};
34use bevy::ui_widgets::TextInput;
35
36fn main() {
37 App::new()
38 .add_plugins(DefaultPlugins)
39 .add_plugins(TabNavigationPlugin)
40 .add_systems(Startup, setup)
41 .add_systems(Update, text_submission)
42 .run();
43}
44
45#[derive(Component)]
46struct TextOutput;
47
48fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
49 commands.spawn(Camera2d);
50
51 let root = commands
52 .spawn(Node {
53 align_items: AlignItems::Center,
54 flex_direction: FlexDirection::Column,
55 padding: px(20).all(),
56 row_gap: px(16),
57 margin: auto().all(),
58 ..default()
59 })
60 .id();
61
62 let text_instructions = commands
63 .spawn((
64 Text::new("Enter to submit text\nTab to switch inputs"),
65 TextFont {
66 font: asset_server.load("fonts/FiraSans-Bold.ttf").into(),
67 font_size: FontSize::Px(25.0),
68 ..default()
69 },
70 ))
71 .id();
72
73 let text_input_left = build_input_text(&mut commands, true, 24.0);
74 let text_input_right = build_input_text(&mut commands, false, 24.0);
75
76 let input_container = commands
77 .spawn((
78 Node {
79 column_gap: px(16),
80 ..default()
81 },
82 AutoFocus,
83 TabGroup::new(0),
84 ))
85 .id();
86
87 let text_output = commands
89 .spawn((
90 Node {
91 width: px(400),
92 border: px(2).all(),
93 padding: px(8).all(),
94 ..Default::default()
95 },
96 BorderColor::from(Color::from(SLATE_300)),
97 Text::new(""),
98 TextOutput,
99 TextLayout {
100 linebreak: LineBreak::WordOrCharacter,
101 ..default()
102 },
103 TextFont {
104 font_size: FontSize::Px(24.0),
105 ..default()
106 },
107 ))
108 .id();
109
110 commands
111 .entity(input_container)
112 .add_children(&[text_input_left, text_input_right]);
113
114 commands
115 .entity(root)
116 .add_children(&[text_instructions, input_container, text_output]);
117}
118
119fn build_input_text(commands: &mut Commands, is_left: bool, font_size: f32) -> Entity {
120 commands
121 .spawn((
122 Node {
123 border: px(2).all(),
124 ..Default::default()
125 },
126 BorderColor::from(Color::from(SLATE_300)),
127 Name::new(if is_left { "Left" } else { "Right" }),
128 TextInput,
129 EditableText {
130 visible_width: Some(10.),
131 allow_newlines: false,
132 ..Default::default()
133 },
134 TextLayout::no_wrap(),
135 TextFont {
136 font_size: FontSize::Px(font_size),
137 ..default()
138 },
139 TextCursorStyle::default(),
140 TabIndex(if is_left { 0 } else { 1 }),
141 BackgroundColor(DARK_GREY.into()),
142 ))
143 .id()
144}
145
146fn text_submission(
148 input_focus: Res<InputFocus>,
149 keyboard_input: Res<ButtonInput<KeyCode>>,
150 mut text_input: Query<(&mut EditableText, &Name)>,
151 mut text_output: Single<&mut Text, With<TextOutput>>,
152) {
153 if keyboard_input.just_pressed(KeyCode::Enter)
154 && let Some(focused_entity) = input_focus.get()
155 && let Ok((mut text_input, name)) = text_input.get_mut(focused_entity)
156 {
157 text_output.0 = format!("{:}: {:}", name, text_input.value());
158
159 text_input.clear();
160 }
161}