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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! text input example
use bevy::{
color::palettes::css::{LIGHT_GOLDENROD_YELLOW, MAROON, RED},
prelude::*,
};
use bevy_ui_text_input::{
TextInputQueue, TextInputBuffer, TextInputMode, TextInputNode, TextInputPlugin, TextInputPrompt,
TextInputStyle, TextSubmitEvent, actions::TextInputAction,
};
fn main() {
App::new()
.add_plugins((DefaultPlugins, TextInputPlugin))
.add_systems(Startup, setup)
.add_systems(Update, (button_system, submit))
.run();
}
#[derive(Component)]
struct OutputMarker;
fn setup(mut commands: Commands, assets: Res<AssetServer>) {
// UI camera
commands.spawn(Camera2d);
let output = commands
.spawn((Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(5.),
..Default::default()
},))
.with_children(|commands| {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
flex_direction: FlexDirection::Column,
overflow: Overflow::clip(),
..Default::default()
},
BorderColor(Color::WHITE),
BackgroundColor(Color::BLACK),
))
.with_child((
Node {
width: Val::Px(500.),
height: Val::Px(500.),
max_height: Val::Px(500.),
min_height: Val::Px(500.),
max_width: Val::Px(500.),
min_width: Val::Px(500.),
..Default::default()
},
Text::new("Nothing submitted."),
OutputMarker,
));
})
.id();
let editor = commands
.spawn((
TextInputNode {
clear_on_submit: true,
unfocus_on_submit: false,
..Default::default()
},
TextInputPrompt {
text: "The TextInputPrompt is shown when the input is empty..".to_string(),
color: Some(Color::srgb(0.3, 0.3, 0.3)),
..Default::default()
},
TextInputBuffer::default(),
TextFont {
font: assets.load("fonts/FiraSans-Bold.ttf"),
font_size: 25.,
..Default::default()
},
TextColor(LIGHT_GOLDENROD_YELLOW.into()),
Node {
width: Val::Px(500.),
height: Val::Px(500.),
..default()
},
TextInputStyle::default(),
BackgroundColor(Color::srgb(0., 0., 0.2)),
))
.id();
let submit_button = commands
.spawn((
Node {
align_self: AlignSelf::Start,
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE),
Button,
))
.with_child(Text::new("Submit"))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut TextInputQueue>| {
query.get_mut(editor).unwrap().add(TextInputAction::Submit);
},
)
.id();
let control_panel = commands
.spawn((Node {
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::SpaceBetween,
..Default::default()
},))
.add_child(submit_button)
.with_children(|commands| {
commands
.spawn(Node {
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::Start,
align_content: AlignContent::Start,
width: Val::Auto,
height: Val::Auto,
flex_grow: 0.0,
flex_shrink: 0.0,
column_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.with_child(Text::new("sans"))
.observe(
move |_: Trigger<Pointer<Click>>,
mut query: Query<&mut TextFont>,
assets: Res<AssetServer>| {
if let Ok(mut text_font) = query.get_mut(editor) {
text_font.font = assets.load("fonts/FiraSans-Bold.ttf");
}
},
);
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>,
mut query: Query<&mut TextFont>,
assets: Res<AssetServer>| {
if let Ok(mut text_font) = query.get_mut(editor) {
text_font.font = assets.load("fonts/FiraMono-Medium.ttf");
}
},
)
.with_child(Text::new("mono"));
});
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut TextFont>| {
if let Ok(mut text_font) = query.get_mut(editor) {
text_font.font_size = 16.;
}
},
)
.with_child(Text::new("16"));
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut TextFont>| {
if let Ok(mut text_font) = query.get_mut(editor) {
text_font.font_size = 25.;
}
},
)
.with_child(Text::new("25"));
});
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
for w in [250, 500] {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut Node>| {
if let Ok(mut node) = query.get_mut(editor) {
node.width = Val::Px(w as f32);
}
},
)
.with_child(Text::new(format!("{w}w")));
}
});
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
for h in [250, 500] {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut Node>| {
if let Ok(mut node) = query.get_mut(editor) {
node.height = Val::Px(h as f32);
}
},
)
.with_child(Text::new(format!("{h}h")));
}
});
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut TextInputNode>| {
if let Ok(mut input) = query.get_mut(editor) {
let wrap = match input.mode.wrap() {
bevy::text::cosmic_text::Wrap::None => bevy::text::cosmic_text::Wrap::Glyph,
bevy::text::cosmic_text::Wrap::Glyph => bevy::text::cosmic_text::Wrap::Word,
bevy::text::cosmic_text::Wrap::Word => bevy::text::cosmic_text::Wrap::WordOrGlyph,
bevy::text::cosmic_text::Wrap::WordOrGlyph => bevy::text::cosmic_text::Wrap::None,
};
input.mode = TextInputMode::MultiLine { wrap };
}
},
)
.with_child(Text::new(format!("wrap")));
});
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
row_gap: Val::Px(4.),
..Default::default()
})
.with_children(|commands| {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE), Button
))
.observe(
move |_: Trigger<Pointer<Click>>, mut query: Query<&mut TextInputNode>| {
if let Ok(mut input) = query.get_mut(editor) {
input.justification = match input.justification {
JustifyText::Left => JustifyText::Center,
JustifyText::Center => JustifyText::Right,
JustifyText::Right => JustifyText::Justified,
JustifyText::Justified => JustifyText::Left,
}
}
},
)
.with_child(Text::new(format!("align")));
});
});
})
.id();
let editor_panel = commands
.spawn(Node {
width: Val::Px(504.),
justify_content: JustifyContent::Start,
align_items: AlignItems::Start,
..Default::default()
})
.with_children(|commands| {
commands
.spawn((
Node {
border: UiRect::all(Val::Px(2.)),
padding: UiRect::all(Val::Px(2.)),
..Default::default()
},
BorderColor(Color::WHITE),
BackgroundColor(Color::BLACK),
))
.add_child(editor);
})
.id();
commands
.spawn(Node {
width: Val::Percent(100.),
height: Val::Percent(100.),
align_items: AlignItems::Center,
flex_direction: FlexDirection::Column,
margin: UiRect::top(Val::Px(10.)),
row_gap: Val::Px(10.),
column_gap: Val::Px(10.),
..Default::default()
})
.with_child(Text::new("Text Input Example".to_string()))
.with_children(|commands| {
commands
.spawn(Node {
display: Display::Grid,
grid_template_columns: vec![GridTrack::auto(), GridTrack::auto()],
column_gap: Val::Px(10.),
row_gap: Val::Px(10.),
..Default::default()
})
.add_child(editor_panel)
.add_child(output)
.add_child(control_panel)
.with_child(Text::new(
"Press Shift + Enter or click the button to submit",
));
});
}
fn button_system(
mut interaction_query: Query<
(&Interaction, &mut BorderColor, &Children),
(Changed<Interaction>, With<Button>),
>,
mut text_query: Query<&mut TextColor>,
) {
for (interaction, mut border_color, children) in &mut interaction_query {
let mut color = text_query.get_mut(children[0]).unwrap();
match *interaction {
Interaction::Pressed => {
color.0 = MAROON.into();
border_color.0 = MAROON.into();
}
Interaction::Hovered => {
color.0 = RED.into();
border_color.0 = RED.into();
}
Interaction::None => {
color.0 = Color::WHITE;
border_color.0 = Color::WHITE;
}
}
}
}
fn submit(
mut events: EventReader<TextSubmitEvent>,
mut query: Query<&mut Text, With<OutputMarker>>,
) {
for event in events.read() {
for mut text in query.iter_mut() {
text.0 = event.text.clone();
}
}
}