bevy_notify/lib.rs
1//! # bevy-notify
2//!
3//! `bevy-notify` is a bevy plugin wrapping [egui_notify](https://crates.io/crates/egui-notify) and
4//! adding an event receiver to your bevy app. Toast notifications can then be send
5//! in your bevy app at any point.
6//! # Examples
7//!
8//! ```
9//! fn main() {
10//! App::new()
11//! .add_plugins(DefaultPlugins)
12//! .add_plugin(NotifyPlugin)
13//! .add_plugin(EguiPlugin)
14//! .insert_resource(Notifications(Toasts::default()))
15//! .add_system(notify_example_system)
16//! .run();
17//! }
18//!
19//! fn notify_example_system(key_input: Res<Input<KeyCode>>, mut events: ResMut<Events<Toast>>) {
20//! if key_input.just_pressed(KeyCode::Space) {
21//! events.send(Toast::success("Space pressed"));
22//! }
23//! }
24//! ```
25use bevy::prelude::{App, Events, Plugin, ResMut, Resource};
26use bevy_egui::EguiContext;
27pub use egui_notify::*;
28
29/// Adds an event resource for Toast nofications to your bevy app.
30/// A system is added that will show received toast events through an egui context.
31///
32/// # Examples
33///
34/// ```
35/// App::new().add_plugin(NotifyPlugin)
36/// .add_plugin(EguiPlugin)
37/// .insert_resource(Notifications(Toasts::default()));
38/// ```
39pub struct NotifyPlugin;
40
41impl Plugin for NotifyPlugin {
42 fn build(&self, app: &mut App) {
43 app.insert_resource(Events::<Toast>::default())
44 .add_system(update_toasts);
45 }
46}
47
48#[derive(Resource)]
49pub struct Notifications(pub Toasts);
50
51fn update_toasts(
52 mut egui_context: ResMut<EguiContext>,
53 mut toasts: ResMut<Notifications>,
54 mut events: ResMut<Events<Toast>>,
55) {
56 events.update();
57 for event in events.drain() {
58 toasts.0.add(event);
59 }
60 toasts.0.show(egui_context.ctx_mut());
61}