Crates.io | bevy_button_released_plugin |
lib.rs | bevy_button_released_plugin |
version | |
source | src |
created_at | 2023-04-06 16:17:17.718947 |
updated_at | 2024-12-01 11:22:07.776764 |
description | Bevy helper crate that allows to react to button being released. |
homepage | https://github.com/Leinnan/bevy_button_released_plugin |
repository | https://github.com/Leinnan/bevy_button_released_plugin |
max_upload_size | |
id | 832303 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | 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 crate makes Bevy application aware of the release of the button instead of reacting right after clicking. I think it will be addressed in next release but until then it could be helpful for some people.
cargo add bevy_button_released_plugin
Add ButtonsReleasedPlugin
during app creation process, then the GameButton
component will be added to buttons and it is possible to react to it like in button_system
function in example below. Auto adding GameButton
can be disabled by disabling default "auto_add" feature.
use bevy::{color::palettes::css::*, prelude::*};
use bevy_button_released_plugin::{ButtonsReleasedPlugin, OnButtonReleased};
pub fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(ButtonsReleasedPlugin)
.add_systems(Startup, setup)
.run();
}
fn on_button(trigger: Trigger<OnButtonReleased>, q: Query<&Name>, mut q_text: Query<&mut Text>) {
let button_name = q.get(trigger.entity()).expect("Missing Name!");
**q_text.single_mut() = format!("Last button released: {}", button_name);
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn(root()).with_children(|parent| {
parent.spawn((
Text::new("Press any button below"),
TextFont {
font_size: 40.0,
..default()
},
node(50.0),
));
for (text, color) in [("Green", GREEN), ("Red", RED), ("Yellow", YELLOW)] {
parent
.spawn((
Button,
node(80.0),
BackgroundColor(color.into()),
Name::new(text),
))
.observe(on_button);
}
});
}
fn root() -> impl Bundle {
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
padding: UiRect::all(Val::Px(50.0)),
justify_content: JustifyContent::SpaceEvenly,
flex_direction: FlexDirection::Column,
..default()
}
}
fn node(height: f32) -> Node {
Node {
height: Val::Px(height),
..default()
}
}
Bevy version | Crate version |
---|---|
0.15 | 0.8 |
0.14 | 0.6, 0.7 |
0.13 | 0.5 |
0.12 | 0.3,0.4 |
0.11 | 0.2 |
0.10 | 0.1 |