//! 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 bevy::{input::keyboard::KeyboardInput, prelude::*}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_startup_system(setup_scene) .add_system(toggle_ime) .add_system(listen_ime_events) .add_system(listen_received_character_events) .add_system(listen_keyboard_input_events) .add_system(bubbling_text) .run(); } fn setup_scene(mut commands: Commands, asset_server: Res) { 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, color: Color::WHITE, }, }, TextSection { value: "false\n".to_string(), style: TextStyle { font: font.clone_weak(), font_size: 30.0, color: Color::WHITE, }, }, TextSection { value: "IME Active: ".to_string(), style: TextStyle { font: font.clone_weak(), font_size: 20.0, color: Color::WHITE, }, }, TextSection { value: "false\n".to_string(), style: TextStyle { font: font.clone_weak(), font_size: 30.0, color: Color::WHITE, }, }, 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, color: Color::WHITE, }, }, TextSection { value: "".to_string(), style: TextStyle { font, font_size: 25.0, color: Color::WHITE, }, }, ]) .with_style(Style { position_type: PositionType::Absolute, position: UiRect { top: Val::Px(10.0), left: Val::Px(10.0), ..default() }, ..default() }), ); commands.spawn(Text2dBundle { text: Text::from_section( "".to_string(), TextStyle { font: asset_server.load("fonts/FiraMono-Medium.ttf"), font_size: 100.0, color: Color::WHITE, }, ), ..default() }); } fn toggle_ime( input: Res>, mut windows: Query<&mut Window>, mut text: Query<&mut Text, With>, ) { if input.just_pressed(MouseButton::Left) { let mut window = windows.single_mut(); window.ime_position = window .cursor_position() .map(|p| Vec2::new(p.x, window.height() - p.y)) .unwrap(); 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, } #[derive(Component)] struct ImePreedit; fn bubbling_text( mut commands: Commands, mut bubbles: Query<(Entity, &mut Transform, &mut Bubble)>, time: Res