Crates.io | bevy_state_ui |
lib.rs | bevy_state_ui |
version | |
source | src |
created_at | 2024-10-25 21:35:00.107175 |
updated_at | 2024-12-03 10:43:15.990152 |
description | A simple UI library for rendering a UI from a given state |
homepage | |
repository | https://github.com/ironpeak/bevy_state_ui |
max_upload_size | |
id | 1423266 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This library provides a simple and flexible way to render UI elements in Bevy based on a given application state. Using bevy_state_ui, you can bind UI elements to state properties and update them in real-time as the state changes.
Add bevy_state_ui to your Cargo.toml dependencies:
[dependencies]
bevy = "0.14.2"
bevy_state_ui = "0.3.0"
Here's how you can set up a simple UI with a button that changes color when hovered.
Example:
use bevy::prelude::*;
use bevy_state_ui::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, update_button_interactions)
.register_ui_state::<State>()
.add_systems(
Update,
render
.run_if(ui_state_changed::<State>),
)
.run();
}
#[derive(Resource, Hash)]
pub struct State {
pub hovered: bool,
}
fn render(mut commands: Commands, state: Res<State>) {
commands.with_children(|parent| {
parent.spawn(ButtonBundle {
style: Style { width: Val::Percent(40.0), height: Val::Percent(15.0), ..default() },
background_color: if state.hovered { Color::WHITE.into() } else { Color::BLACK.into() },
..default()
}).with_children(|parent| {
parent.spawn(TextBundle::from_section("I am a button", TextStyle { font_size: 40.0, ..default() }));
});
});
}
fn update_button_interactions(mut state: ResMut<State>, q: Query<&Interaction, (Changed<Interaction>, With<Button>)>) {
if let Some(&interaction) = q.iter().next() {
state.hovered = matches!(interaction, Interaction::Hovered);
}
}