forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_text_inputs.rs
More file actions
241 lines (223 loc) · 8.38 KB
/
multiple_text_inputs.rs
File metadata and controls
241 lines (223 loc) · 8.38 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Demonstrates multiple text inputs
//!
//! This example arranges three text inputs in a 3x3 grid layout. The first column of each row is an [`EditableText`] text input node, the second column is a `Text` node
//! that is kept synchronized with the [`EditableText`]'s contents by the [`synchronize_output_text`] system, and the third column is updated
//! by the [`submit_text`] system when the user submits the [`EditableText`]'s text by pressing `Ctrl` + `Enter`.
use bevy::color::palettes::tailwind::SLATE_300;
use bevy::input::keyboard::Key;
use bevy::input_focus::AutoFocus;
use bevy::input_focus::{
tab_navigation::{TabGroup, TabIndex, TabNavigationPlugin},
InputFocus,
};
use bevy::prelude::*;
use bevy::text::{EditableText, TextCursorStyle};
fn main() {
App::new()
// `EditableTextInputPlugin` is part of `DefaultPlugins`
.add_plugins((DefaultPlugins, TabNavigationPlugin))
.add_systems(Startup, setup)
.add_systems(
Update,
(
synchronize_output_text,
submit_text,
update_row_border_colors,
),
)
.run();
}
#[derive(Component)]
struct TextOutput;
#[derive(Component)]
struct SubmitOutput;
#[derive(Component)]
struct TextInputRow(usize);
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
let font = TextFont {
font: asset_server.load("fonts/FiraMono-Medium.ttf").into(),
font_size: FontSize::Px(24.),
..default()
};
commands
.spawn((
Node {
width: percent(100.),
height: percent(100.),
display: Display::Grid,
justify_content: JustifyContent::Center,
align_content: AlignContent::Center,
grid_template_columns: RepeatedGridTrack::px(3, 320.),
grid_template_rows: RepeatedGridTrack::auto(6),
row_gap: px(8.),
column_gap: px(8.),
..default()
},
TabGroup::default(),
))
.with_children(|parent| {
parent.spawn((
Text::new("Multiple Text Inputs Example"),
Node {
grid_column: GridPlacement::span(3),
justify_self: JustifySelf::Center,
margin: px(16).bottom(),
..default()
},
TextColor::WHITE,
font.clone(),
));
let label_font = font.clone().with_font_size(14.);
for label in ["EditableText", "value", "submission"] {
parent.spawn((
Text::new(label),
label_font.clone(),
Node {
justify_self: JustifySelf::Center,
margin: px(-4).bottom(),
..default()
},
));
}
for row in 0..3 {
let mut input = parent.spawn((
Node {
border: px(4.).all(),
padding: px(4.).all(),
..default()
},
EditableText::new(format!("Initial text {row}")),
TextCursorStyle::default(),
font.clone(),
BackgroundColor(bevy::color::palettes::css::DARK_GREY.into()),
TextInputRow(row),
TabIndex(row as i32),
BorderColor::all(SLATE_300),
));
if row == 0 {
input.insert(AutoFocus);
}
parent.spawn((
Text::default(),
TextLayout {
linebreak: LineBreak::AnyCharacter,
..default()
},
Node {
border: px(4.).all(),
padding: px(4.).all(),
..default()
},
font.clone(),
BackgroundColor(bevy::color::palettes::css::DARK_SLATE_GRAY.into()),
BorderColor::all(Color::WHITE),
TextInputRow(row),
TextOutput,
));
parent.spawn((
Text::default(),
TextLayout {
linebreak: LineBreak::AnyCharacter,
..default()
},
Node {
border: px(4.).all(),
padding: px(4.).all(),
..default()
},
font.clone(),
BackgroundColor(bevy::color::palettes::css::DARK_SLATE_BLUE.into()),
BorderColor::all(Color::WHITE),
TextInputRow(row),
SubmitOutput,
));
}
parent.spawn((
Text::new("Press Ctrl + Enter to submit"),
Node {
grid_column: GridPlacement::span(3),
justify_self: JustifySelf::Center,
margin: px(16).top(),
..default()
},
font.clone(),
));
});
}
/// This system keeps the text of the [`TextOutput`] [`Text`] nodes synchronized with the text
/// of the [`EditableText`] node on the same row.
fn synchronize_output_text(
changed_inputs: Query<(&EditableText, &TextInputRow), Changed<EditableText>>,
mut outputs: Query<(&mut Text, &TextInputRow), With<TextOutput>>,
) {
for (editable_text, input_row) in &changed_inputs {
for (mut text, output_row) in &mut outputs {
if output_row.0 == input_row.0 {
// `EditableText::value()` returns a `SplitString` because Parley may keep IME preedit text
// in a contiguous range of the editor’s internal `String` buffer during composition.
// The returned `SplitString` omits that preedit range, exposing only the text before and after it.
//
// To avoid allocating a new `String`, we reserve the total length of the `SplitString`'s slices,
// then append them to the output `Text`.
text.0.clear();
text.0
.reserve(editable_text.value().into_iter().map(str::len).sum());
for sub_str in editable_text.value() {
text.0.push_str(sub_str);
}
}
}
}
}
// Submit the focused input's text when Ctrl+Enter is pressed.
fn submit_text(
input_focus: Res<InputFocus>,
keyboard_input: Res<ButtonInput<Key>>,
mut text_input: Query<(&mut EditableText, &TextInputRow)>,
mut text_output: Query<(&mut Text, &TextInputRow), With<SubmitOutput>>,
) {
if keyboard_input.just_pressed(Key::Enter)
&& keyboard_input.pressed(Key::Control)
&& let Some(focused_entity) = input_focus.get()
&& let Ok((mut editable_text, input_row)) = text_input.get_mut(focused_entity)
{
for (mut text, output_row) in &mut text_output {
if input_row.0 == output_row.0 {
text.0.clear();
text.0
.reserve(editable_text.value().into_iter().map(str::len).sum());
for sub_str in editable_text.value() {
text.0.push_str(sub_str);
}
break;
}
}
editable_text.clear();
}
}
/// Dim a row's border colors when its [`EditableText`] does not have input focus.
fn update_row_border_colors(
input_focus: Res<InputFocus>,
input_rows: Query<&TextInputRow, With<EditableText>>,
mut row_borders: Query<(&TextInputRow, &mut BorderColor, Has<EditableText>)>,
) {
if !input_focus.is_changed() {
return;
}
let focused_row = input_focus
.get()
.and_then(|focused_entity| input_rows.get(focused_entity).ok())
.map(|row| row.0);
for (row, mut border_color, is_input) in &mut row_borders {
let mut color = if is_input {
SLATE_300.into()
} else {
Color::WHITE
};
if Some(row.0) != focused_row {
color = color.darker(0.75);
}
border_color.set_all(color);
}
}