#bevy-ui #alert #ui #bevy #gamedev #game

bevy_ui_mod_alerts

Easy-spawn UIs that look like little popups

3 unstable releases

0.2.0 Jul 15, 2024
0.1.1 Jul 1, 2024
0.1.0 Jul 1, 2024

#1945 in Game dev

41 downloads per month
Used in bevy_anyhow_alert

MIT/Apache

32KB
559 lines

bevy_ui_mod_alerts

Crates.io Docs

A quick-and-dirty implementation of some "toast" UI element represented by an Alert component. Call the Alert::bundle constructor to build an AlertBundle, or pipe a system that reutnrs Vec<String> into the AlertsPlugin::alert function, and a toast ui node will spawn (and eventually disappear if a lifetime is specified).

See examples for more.


lib.rs:

bevy_ui_mod_alerts provides a "toast"-like alert UI which can be used to help manage errors using a convenient UI.

Alerts can be spawned by directly spawning AlertBundles using AlertBundle or Alert::bundle, or by piping a Vec<String> of alert messages into the AlertsPlugin::alert system.

Examples

This example pipes some arbitrary system into the AlertsPlugin::alert system:

use bevy::prelude::*;
use bevy_ui_mod_alerts::{AlertsPlugin};

fn main() {
    let mut app = App::new();
    app.add_plugins(MinimalPlugins);
    app.add_plugins(AlertsPlugin::new());
    app.add_systems(Update, do_stuff_and_maybe_alert.pipe(AlertsPlugin::alert));
    // app.run();
}

#[derive(Component)]
struct MyComponent;

fn do_stuff_and_maybe_alert(my_query: Query<&MyComponent>) -> Vec<String> {
    vec![]
}

The resulting UI is somewhat restylable but may not fit every application. Users can restyle the alerts with the AlertElements resource:

use bevy::prelude::*;
use bevy_ui_mod_alerts::{AlertMarker, AlertElements};

let mut app = App::new();
// ...
app.insert_resource(AlertElements::<AlertMarker> {
    // root: NodeBundle
    // alert: NodeBundle
    // header: NodeBundle
    // body: NodeBundle
    // text: TextStyle
    ..Default::default()
});

Or make tweaks from the default:

use bevy::prelude::Color;
use bevy::color::palettes;
use bevy_ui_mod_alerts::AlertElements;
let mut elements = AlertElements::new();
elements.header.background_color.0 = Color::Srgba(palettes::css::GREEN);

...but it is not the most convenient to do so yet.

Additionally, if users want multiple different alert styles to exist simultaneously, the type parameter M can be set to a custom component. Typically, the default AlertMarker is used.

use bevy::prelude::*;
use bevy_ui_mod_alerts::AlertsPlugin;

#[derive(Component, Default, Reflect)]
struct MyAlert;

let mut app = App::new();
app.add_plugins(AlertsPlugin::<MyAlert>::default());
app.add_systems(Update, (|| { vec![] }).pipe(AlertsPlugin::<MyAlert>::custom_alert));

Dependencies

~39–76MB
~1.5M SLoC