19 releases (5 breaking)

new 0.5.2 Apr 11, 2025
0.5.0 Mar 25, 2025
0.4.0 Dec 3, 2024
0.3.1 Oct 7, 2024
0.2.0 Jul 23, 2024

#157 in Game dev

Download history 128/week @ 2025-02-10 151/week @ 2025-02-17 166/week @ 2025-02-24 56/week @ 2025-03-03 2/week @ 2025-03-10 107/week @ 2025-03-24 10/week @ 2025-03-31 230/week @ 2025-04-07

347 downloads per month

MIT/Apache

170KB
1K SLoC

bevy_text_edit

crates.io docs.rs dependency status pipeline status

Gitlab Github

A very easy to use plugin for input text in Bevy. Good enough for game setting and command console.

Features:

  • Switchable between multiple text boxes.
  • Moving text cursor using arrow keys and Home/End.
  • Limit input length.
  • Filter input text with regex.
  • Placeholder.
  • In-game virtual keyboard.
    • Repeated key.

Not support:

  • IME.
  • Multi-language.
  • Select text.
  • Copy/paste.

Quickstart

Plugin

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

#[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 TextEditPluginNoState:

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // Add the plugin
        .add_plugins(TextEditPluginNoState)
        .add_systems(Startup, setup)
        .run();
}

Component

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

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):

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 or observe trigger TextEdited.

fn get_text(
    mut event: EventReader<TextEdited>,
) {
    for e in event.read() {
        info!("Entity {}: {}", e.entity, e.text);
    }
}
fn setup(mut commands: Commands) {
    commands.spawn((
        TextEditable::default(),
        Text::new("Input Text"),
    )).observe(get_text);
}

fn get_text(
    trigger: Trigger<TextEdited>,
) {
    let text = trigger.text.as_str();
    info!("{}", text);
}

License

Please see LICENSE.

Compatible Bevy Versions

bevy bevy_text_edit
0.15 0.4-0.5, branch master
0.14 0.1-0.3
0.13 0.0.1-0.0.5

Dependencies

~57–89MB
~1.5M SLoC