https://github.com/dothanhtrung/bevy-text-edit
Bevy plugin for easier input text
https://github.com/dothanhtrung/bevy-text-edit
bevy bevy-plugin game-development rust
Last synced: 8 months ago
JSON representation
Bevy plugin for easier input text
- Host: GitHub
- URL: https://github.com/dothanhtrung/bevy-text-edit
- Owner: dothanhtrung
- License: other
- Created: 2024-06-10T15:43:24.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-01-17T10:00:17.000Z (over 1 year ago)
- Last Synced: 2025-01-30T18:48:23.306Z (over 1 year ago)
- Topics: bevy, bevy-plugin, game-development, rust
- Language: Rust
- Homepage: https://gitlab.com/kimtinh/bevy-text-edit
- Size: 66.4 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
bevy_text_edit
==============
[](https://crates.io/crates/bevy_text_edit)
[](https://docs.rs/bevy_text_edit)
[](https://deps.rs/repo/gitlab/kimtinh/bevy-text-edit)
[](https://gitlab.com/kimtinh/bevy-text-edit/-/commits/master)
[](https://gitlab.com/kimtinh/bevy-text-edit)
[](https://github.com/dothanhtrung/bevy-text-edit)


A very easy-to-use plugin for input text in Bevy. Good enough for game setting and command console.
Features:
* [x] Switchable between multiple text boxes.
* [x] Moving the text cursor using arrow keys and Home/End.
* [x] Limit input length.
* [x] Filter input text with regex.
* [x] Placeholder.
* [x] Paste with `Ctrl+v`.
* [x] In-game virtual keyboard.
* [x] Repeated key.
Not support:
* [ ] IME.
* In-game virtual keyboard.
* [ ] Multi-language.
* [ ] Select text.
* [ ] Copy.
Quickstart
----------
### Plugin
Add plugin `TextEditPlugin` to the app and define which states it will run in:
```rust
#[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`:
```rust
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// Add the plugin
.add_plugins(TextEditPluginAnyState::any())
.add_systems(Startup, setup)
.run();
}
```
### Component
Insert component `TextEditable` into any text entity that needs to be editable:
```rust
fn setup(mut commands: Commands) {
commands.spawn((
TextEditable::default(), // Mark text is editable
Text::new("Input Text 1"),
));
}
```
Only text 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):
```rust
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`.
```rust
// Get by event
fn get_text(
mut event: MessageReader,
) {
for e in event.read() {
info!("Entity {}: {}", e.entity, e.text);
}
}
```
```rust
// Get by observing
fn setup(mut commands: Commands) {
commands.spawn((
TextEditable::default(),
Text::new("Input Text"),
)).observe(get_text);
}
fn get_text(
trigger: On,
) {
let text = trigger.text.as_str();
info!("{}", text);
}
```
License
-------
Please see [LICENSE](./LICENSE).
Compatible Bevy Versions
------------------------
| bevy | bevy_text_edit |
|------|----------------|
| 0.17 | 0.7 |
| 0.16 | 0.6 |
| 0.15 | 0.4-0.5 |
| 0.14 | 0.1-0.3 |
| 0.13 | 0.0.1-0.0.5 |
---------
