Crate bevy_text_edit

Source
Expand description

§Plugin

Add plugin TextEditPlugin to the app and define which states it will run in:

use bevy::prelude::*;
use bevy_text_edit::TextEditPlugin;

#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, States)]
enum GameState {
    #[default]
    Menu,
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // Add the plugin
        .add_plugins(TextEditPlugin::new(vec![GameState::Menu]))
        .run();
}

If you don’t care to game state and want to always run input text, use TextEditPluginAnyState:

use bevy::prelude::*;
use bevy_text_edit::TextEditPluginAnyState;

App::new()
    .add_plugins(DefaultPlugins)
    // Add the plugin
    .add_plugins(TextEditPluginAnyState::any())
    .run();

§Component

Insert the component TextEditable into any text entity that needs to be editable:

use bevy::prelude::*;
use bevy_text_edit::TextEditable;

fn setup(mut commands: Commands) {
    commands.spawn((
        TextEditable::default(), // Mark text is editable
        Text::new("Input Text 1"),
    ));
}

Only text that is focused by clicking gets keyboard input.

It is also possible to limit which characters are allowed to enter through filter_in and filter_out attribute (regex is supported):

use bevy::prelude::*;
use bevy_text_edit::TextEditable;

fn setup(mut commands: Commands) {
    commands.spawn((
        TextEditable {
            filter_in: vec!["[0-9]".into(), " ".into()], // Only allow number and space
            filter_out: vec!["5".into()],                // Ignore number 5
            ..default()
        },
        Text::new("Input Text 1"),
    ));
}

§Get text

The edited text can be retrieved from event TextEdited.

use bevy::prelude::*;
use bevy_text_edit::TextEdited;

fn get_text(
    mut event: EventReader<TextEdited>,
) {
    for e in event.read() {
        info!("Entity {}: {}", e.entity, e.text);
    }
}

Modules§

virtual_keyboard

Structs§

BlinkInterval
Text cursor blink interval in millisecond.
CursorPosition
Current position of cursor in the text.
DisplayTextCursor
The text that will be displayed as cursor. Default is |.
TextEditConfig
TextEditFocus
Mark a text entity is focused. Normally done by mouse click.
TextEditPlugin
The main plugin
TextEditPluginAnyState
Use this if you don’t care to state and want this plugin’s systems always run.
TextEditable
Mark a text is editable.
You can limit which characters are allowed to enter through filter_in and filter_out attribute (regex is supported):
TextEdited

Enums§

DummyState

Functions§

listen_changing_focus