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§
Structs§
- Blink
Interval - Text cursor blink interval in millisecond.
- Cursor
Position - Current position of cursor in the text.
- Display
Text Cursor - The text that will be displayed as cursor. Default is
|
. - Text
Edit Config - Text
Edit Focus - Mark a text entity is focused. Normally done by mouse click.
- Text
Edit Plugin - The main plugin
- Text
Edit Plugin AnyState - Use this if you don’t care to state and want this plugin’s systems always run.
- Text
Editable - Mark a text is editable.
You can limit which characters are allowed to enter throughfilter_in
andfilter_out
attribute (regex is supported): - Text
Edited