bevy/examples/input/text_input.rs
Joona Aalto 25bfa80e60
Migrate cameras to required components (#15641)
# Objective

Yet another PR for migrating stuff to required components. This time,
cameras!

## Solution

As per the [selected
proposal](https://hackmd.io/tsYID4CGRiWxzsgawzxG_g#Combined-Proposal-1-Selected),
deprecate `Camera2dBundle` and `Camera3dBundle` in favor of `Camera2d`
and `Camera3d`.

Adding a `Camera` without `Camera2d` or `Camera3d` now logs a warning,
as suggested by Cart [on
Discord](https://discord.com/channels/691052431525675048/1264881140007702558/1291506402832945273).
I would personally like cameras to work a bit differently and be split
into a few more components, to avoid some footguns and confusing
semantics, but that is more controversial, and shouldn't block this core
migration.

## Testing

I ran a few 2D and 3D examples, and tried cameras with and without
render graphs.

---

## Migration Guide

`Camera2dBundle` and `Camera3dBundle` have been deprecated in favor of
`Camera2d` and `Camera3d`. Inserting them will now also insert the other
components required by them automatically.
2024-10-05 01:59:52 +00:00

176 lines
5.3 KiB
Rust

//! Simple text input support
//!
//! Return creates a new line, backspace removes the last character.
//! Clicking toggle IME (Input Method Editor) support, but the font used as limited support of characters.
//! You should change the provided font with another one to test other languages input.
use std::mem;
use bevy::{
input::keyboard::{Key, KeyboardInput},
prelude::*,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup_scene)
.add_systems(
Update,
(
toggle_ime,
listen_ime_events,
listen_keyboard_input_events,
bubbling_text,
),
)
.run();
}
fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
// The default font has a limited number of glyphs, so use the full version for
// sections that will hold text input.
let font = asset_server.load("fonts/FiraMono-Medium.ttf");
commands.spawn(
TextBundle::from_sections([
TextSection::from("Click to toggle IME. Press return to start a new line.\n\n"),
TextSection::from("IME Enabled: "),
TextSection::from("false\n"),
TextSection::from("IME Active: "),
TextSection::from("false\n"),
TextSection::from("IME Buffer: "),
TextSection {
value: "\n".to_string(),
style: TextStyle {
font: font.clone(),
..default()
},
},
])
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
commands.spawn(Text2dBundle {
text: Text::from_section(
"".to_string(),
TextStyle {
font,
font_size: 100.0,
..default()
},
),
..default()
});
}
fn toggle_ime(
input: Res<ButtonInput<MouseButton>>,
mut windows: Query<&mut Window>,
mut text: Query<&mut Text, With<Node>>,
) {
if input.just_pressed(MouseButton::Left) {
let mut window = windows.single_mut();
window.ime_position = window.cursor_position().unwrap();
window.ime_enabled = !window.ime_enabled;
let mut text = text.single_mut();
text.sections[2].value = format!("{}\n", window.ime_enabled);
}
}
#[derive(Component)]
struct Bubble {
timer: Timer,
}
fn bubbling_text(
mut commands: Commands,
mut bubbles: Query<(Entity, &mut Transform, &mut Bubble)>,
time: Res<Time>,
) {
for (entity, mut transform, mut bubble) in bubbles.iter_mut() {
if bubble.timer.tick(time.delta()).just_finished() {
commands.entity(entity).despawn();
}
transform.translation.y += time.delta_seconds() * 100.0;
}
}
fn listen_ime_events(
mut events: EventReader<Ime>,
mut status_text: Query<&mut Text, With<Node>>,
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
) {
for event in events.read() {
match event {
Ime::Preedit { value, cursor, .. } if !cursor.is_none() => {
status_text.single_mut().sections[6].value = format!("{value}\n");
}
Ime::Preedit { cursor, .. } if cursor.is_none() => {
status_text.single_mut().sections[6].value = "\n".to_string();
}
Ime::Commit { value, .. } => {
edit_text.single_mut().sections[0].value.push_str(value);
}
Ime::Enabled { .. } => {
status_text.single_mut().sections[4].value = "true\n".to_string();
}
Ime::Disabled { .. } => {
status_text.single_mut().sections[4].value = "false\n".to_string();
}
_ => (),
}
}
}
fn listen_keyboard_input_events(
mut commands: Commands,
mut events: EventReader<KeyboardInput>,
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
) {
for event in events.read() {
// Only trigger changes when the key is first pressed.
if !event.state.is_pressed() {
continue;
}
match &event.logical_key {
Key::Enter => {
let mut text = edit_text.single_mut();
if text.sections[0].value.is_empty() {
continue;
}
let old_value = mem::take(&mut text.sections[0].value);
commands.spawn((
Text2dBundle {
text: Text::from_section(old_value, text.sections[0].style.clone()),
..default()
},
Bubble {
timer: Timer::from_seconds(5.0, TimerMode::Once),
},
));
}
Key::Space => {
edit_text.single_mut().sections[0].value.push(' ');
}
Key::Backspace => {
edit_text.single_mut().sections[0].value.pop();
}
Key::Character(character) => {
edit_text.single_mut().sections[0].value.push_str(character);
}
_ => continue,
}
}
}