2023-01-29 20:27:29 +00:00
|
|
|
//! 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.
|
|
|
|
|
2024-02-19 16:51:27 +00:00
|
|
|
use std::mem;
|
|
|
|
|
2024-04-30 00:49:41 +00:00
|
|
|
use bevy::{
|
|
|
|
input::{
|
|
|
|
keyboard::{Key, KeyboardInput},
|
|
|
|
ButtonState,
|
|
|
|
},
|
|
|
|
prelude::*,
|
|
|
|
};
|
2023-01-29 20:27:29 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
2023-03-18 01:45:34 +00:00
|
|
|
.add_systems(Startup, setup_scene)
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
(
|
|
|
|
toggle_ime,
|
|
|
|
listen_ime_events,
|
|
|
|
listen_keyboard_input_events,
|
|
|
|
bubbling_text,
|
|
|
|
),
|
|
|
|
)
|
2023-01-29 20:27:29 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
|
|
|
|
let font = asset_server.load("fonts/FiraMono-Medium.ttf");
|
|
|
|
|
|
|
|
commands.spawn(
|
|
|
|
TextBundle::from_sections([
|
|
|
|
TextSection {
|
|
|
|
value: "IME Enabled: ".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font: font.clone_weak(),
|
|
|
|
font_size: 20.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
TextSection {
|
|
|
|
value: "false\n".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font: font.clone_weak(),
|
|
|
|
font_size: 30.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
TextSection {
|
|
|
|
value: "IME Active: ".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font: font.clone_weak(),
|
|
|
|
font_size: 20.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
TextSection {
|
|
|
|
value: "false\n".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font: font.clone_weak(),
|
|
|
|
font_size: 30.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
TextSection {
|
|
|
|
value: "click to toggle IME, press return to start a new line\n\n".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font: font.clone_weak(),
|
|
|
|
font_size: 18.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
TextSection {
|
|
|
|
value: "".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
font,
|
|
|
|
font_size: 25.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
.with_style(Style {
|
|
|
|
position_type: PositionType::Absolute,
|
2023-03-13 15:17:00 +00:00
|
|
|
top: Val::Px(10.0),
|
|
|
|
left: Val::Px(10.0),
|
2023-01-29 20:27:29 +00:00
|
|
|
..default()
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
commands.spawn(Text2dBundle {
|
|
|
|
text: Text::from_section(
|
|
|
|
"".to_string(),
|
|
|
|
TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
|
|
font_size: 100.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-01-29 20:27:29 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn toggle_ime(
|
2023-12-06 20:32:34 +00:00
|
|
|
input: Res<ButtonInput<MouseButton>>,
|
2023-01-29 20:27:29 +00:00
|
|
|
mut windows: Query<&mut Window>,
|
|
|
|
mut text: Query<&mut Text, With<Node>>,
|
|
|
|
) {
|
|
|
|
if input.just_pressed(MouseButton::Left) {
|
|
|
|
let mut window = windows.single_mut();
|
|
|
|
|
2023-04-05 22:32:36 +00:00
|
|
|
window.ime_position = window.cursor_position().unwrap();
|
2023-01-29 20:27:29 +00:00
|
|
|
window.ime_enabled = !window.ime_enabled;
|
|
|
|
|
|
|
|
let mut text = text.single_mut();
|
|
|
|
text.sections[1].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>)>,
|
|
|
|
) {
|
2023-08-30 14:20:03 +00:00
|
|
|
for event in events.read() {
|
2023-01-29 20:27:29 +00:00
|
|
|
match event {
|
|
|
|
Ime::Preedit { value, cursor, .. } if !cursor.is_none() => {
|
|
|
|
status_text.single_mut().sections[5].value = format!("IME buffer: {value}");
|
|
|
|
}
|
|
|
|
Ime::Preedit { cursor, .. } if cursor.is_none() => {
|
|
|
|
status_text.single_mut().sections[5].value = "".to_string();
|
|
|
|
}
|
|
|
|
Ime::Commit { value, .. } => {
|
|
|
|
edit_text.single_mut().sections[0].value.push_str(value);
|
|
|
|
}
|
|
|
|
Ime::Enabled { .. } => {
|
|
|
|
status_text.single_mut().sections[3].value = "true\n".to_string();
|
|
|
|
}
|
|
|
|
Ime::Disabled { .. } => {
|
|
|
|
status_text.single_mut().sections[3].value = "false\n".to_string();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn listen_keyboard_input_events(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut events: EventReader<KeyboardInput>,
|
2024-02-19 16:51:27 +00:00
|
|
|
mut edit_text: Query<&mut Text, (Without<Node>, Without<Bubble>)>,
|
2023-01-29 20:27:29 +00:00
|
|
|
) {
|
2023-08-30 14:20:03 +00:00
|
|
|
for event in events.read() {
|
2024-04-30 00:49:41 +00:00
|
|
|
// Only trigger changes when the key is first pressed.
|
|
|
|
if event.state == ButtonState::Released {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match &event.logical_key {
|
|
|
|
Key::Enter => {
|
2024-02-19 16:51:27 +00:00
|
|
|
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),
|
|
|
|
},
|
|
|
|
));
|
2023-01-29 20:27:29 +00:00
|
|
|
}
|
2024-04-30 00:49:41 +00:00
|
|
|
Key::Space => {
|
|
|
|
edit_text.single_mut().sections[0].value.push(' ');
|
|
|
|
}
|
|
|
|
Key::Backspace => {
|
2024-02-19 16:51:27 +00:00
|
|
|
edit_text.single_mut().sections[0].value.pop();
|
2023-01-29 20:27:29 +00:00
|
|
|
}
|
2024-04-30 00:49:41 +00:00
|
|
|
Key::Character(character) => {
|
|
|
|
edit_text.single_mut().sections[0].value.push_str(character);
|
|
|
|
}
|
2023-01-29 20:27:29 +00:00
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|