DEVELOPMENT ENVIRONMENT

~liljamo/bevy_dice

ref: 01253fe1583ba20eebbb57a9395631b3e395903f bevy_dice/crates/bevy_dice_demo/src/toast.rs -rw-r--r-- 3.2 KiB
01253fe1Jonni Liljamo feat(demo): naive toast system for results 4 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under MIT, see LICENSE for more information.
 */

use std::time::Duration;
#[cfg(not(feature = "wasm"))]
use std::time::Instant;
#[cfg(feature = "wasm")]
use web_time::Instant;

use bevy::{color::palettes::tailwind::*, prelude::*};

pub struct ToastPlugin;

impl Plugin for ToastPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Startup, setup)
            .add_systems(Update, cleanup_toasts);

        app.add_event::<ShowResultToast>()
            .add_observer(show_result_toast);
    }
}

#[derive(Component)]
struct ToastUI;

#[derive(Component)]
struct Toast {
    spawned: Instant,
}

fn setup(mut commands: Commands) {
    commands.spawn((
        Node {
            width: Val::Percent(100.0),
            height: Val::Percent(100.0),
            flex_direction: FlexDirection::Column,
            align_items: AlignItems::FlexEnd,
            padding: UiRect::all(Val::Px(8.0)),
            overflow: Overflow::scroll_y(),
            ..Default::default()
        },
        ToastUI,
    ));
}

#[derive(Event)]
pub struct ShowResultToast(pub u32, pub u8);

fn show_result_toast(
    trigger: Trigger<ShowResultToast>,
    mut commands: Commands,
    q_toast_ui: Query<Entity, With<ToastUI>>,
) {
    if let Ok(toast_ui) = q_toast_ui.single() {
        let toast = commands
            .spawn((
                Toast {
                    spawned: Instant::now(),
                },
                Node {
                    width: Val::Px(180.0),
                    height: Val::Px(60.0),
                    border: UiRect::all(Val::Px(2.0)),
                    flex_direction: FlexDirection::Column,
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    ..Default::default()
                },
                BackgroundColor(Color::from(EMERALD_400)),
                BorderColor(Color::from(EMERALD_700)),
                BorderRadius::all(Val::Px(10.0)),
                BoxShadow::new(
                    Color::BLACK.with_alpha(0.8),
                    Val::Px(4.0),
                    Val::Px(4.0),
                    Val::DEFAULT,
                    Val::DEFAULT,
                ),
                children![
                    (
                        Text::new(format!("Die {}", trigger.0)),
                        TextFont {
                            font_size: 18.0,
                            ..Default::default()
                        },
                        TextColor(Color::BLACK),
                    ),
                    (
                        Text::new(format!("Result: {}", trigger.1)),
                        TextFont {
                            font_size: 20.0,
                            ..Default::default()
                        },
                        TextColor(Color::WHITE),
                    )
                ],
            ))
            .id();

        commands.entity(toast_ui).add_child(toast);
    }
}

fn cleanup_toasts(mut commands: Commands, q_toast: Query<(Entity, &Toast)>) {
    for (entity, toast) in q_toast.iter() {
        if toast.spawned.elapsed() > Duration::from_secs(2) {
            commands.entity(entity).despawn();
        }
    }
}