#text-input #forms #input #bevy-ui #bevy #ui #bevy-plugin

bevy_ui_forms

A bevy plugin for creating forms

1 unstable release

0.1.0 Apr 24, 2024

#532 in Game dev

Download history 187/week @ 2024-04-22

187 downloads per month

MPL-2.0 license

270KB
972 lines

bevy_ui_forms

Xenira xenira@3.141.rip :toc: :toc-placement!: :toclevels: 2 :sectnums: :icons: font :source-highlighter: highlight.js

crates.io, link=https://crates.io/crates/bevy_ui_forms docs, link=https://docs.rs/bevy_ui_forms Following released Bevy versions, link=https://bevyengine.org/learn/book/plugin-development/#main-branch-tracking

Adds forms to bevy_ui.

This started out as a fork of bevy_simple_text_input with a slightly more ambitious single-line text input widget for bevy_ui.

NOTE: While the original text input widget works well the form logic is still in developmen, might be buggy and is subject to change. This also applies to the input, as the behaviour is changed.

It now includes some form logic in addition to the text input widget.

There also is a macro for creating forms from a struct.

animated screenshot of text input widget gaining focus and text typed and submitted

toc::[]

Features extended from the original

  • Character masking
  • Placeholder text
  • Clipboard support
  • Focus (one active text input at a time and auto-focus on click)
  • Form logic
  • Form 'derive' macro
  • Tab key to switch between text inputs
  • Enter key to submit form

Usage

IMPORTANT: Code and examples in the main branch are under development and may not be compatible with the released version on crates.io. Make sure to switch to the corresponding tag.

See examples/basic.rs.

Form macro

.Macro example

use bevy::prelude::*;
use bevy_ui_forms::prelude::*;

#[form_struct]
#[derive(Debug, Clone)]
pub struct LoginData {
    #[form_field(active)]
    #[text_box(placeholder = "Username")]
    pub username: String,
    #[text_box(placeholder = "Password", mask = '*')]
    pub password: String,
    #[form_field(optional)]
    #[text_box(placeholder = "Email")]
    pub email: Option<String>,
}

.Usage

fn setup(mut commands: Commands) {
    commands.spawn((
        LoginDataForm,
        NodeBundle {
            style: Style {
                flex_direction: FlexDirection::Column,
                row_gap: Val::Px(8.0),
                align_self: AlignSelf::Stretch,
                align_items: AlignItems::Stretch,
                ..default()
            },
            ..default()
        },
    ));
}
fn on_form_submit(
    mut ev_login_form: EventReader<LoginDataFormEvent>,
) {
    for ev in ev_login_form.read() {
        match &ev.event {
            FormEvent::Submit(data) => {
              // do something with the data
            }
            _ => {}
        }
    }
}

Compatibility

.Compatibility with Bevy versions [options="header"] |==== | bevy_ui_forms | bevy | 0.1 | 0.13 |====

Contributing

Please feel free to open a PR, but its best to open an issue first to discuss the changes you would like to make.

Please keep PRs small and scoped to a single feature or fix.

Alternatives

If you need more features, check out bevy_simple_text_input, bevy_cosmic_edit or bevy_egui.

License

This project is licensed under the Mozilla Public License (MPL) 2.0.

The original bevy_simple_text_input is licensed under the MIT or Apache 2.0 license.

Dependencies

~42–84MB
~1.5M SLoC