bevy_basic_ui/
systems.rs

1use bevy::{app::AppExit, prelude::*};
2
3use crate::UiState;
4
5use super::{
6    components::{QuitButton, Screen, SettingsButton},
7    styles::{HOVERED_BUTTON_COLOR, NORMAL_BUTTON_COLOR, PRESSED_BUTTON_COLOR},
8};
9
10pub fn interact_with_quit_button(
11    commands: Commands,
12    mut button_q: Query<
13        (&Interaction, &mut BackgroundColor),
14        (Changed<Interaction>, With<QuitButton>),
15    >,
16    state: Res<State<UiState>>,
17    screen_q: Query<Entity, With<Screen>>,
18    mut next_state: ResMut<NextState<UiState>>,
19    mut app_exit_event_writer: EventWriter<AppExit>,
20) {
21    if let Ok((interaction, mut background_color)) = button_q.get_single_mut() {
22        match *interaction {
23            Interaction::Pressed => {
24                *background_color = PRESSED_BUTTON_COLOR.into();
25                despawn_screens(commands, screen_q);
26                match state.get() {
27                    UiState::Hud => {
28                        next_state.set(UiState::MainMenu);
29                    }
30                    UiState::MainMenu => {
31                        app_exit_event_writer.send(AppExit);
32                    }
33                    UiState::Settings => {
34                        next_state.set(UiState::MainMenu);
35                    }
36                    _ => (),
37                }
38            }
39            Interaction::Hovered => {
40                *background_color = HOVERED_BUTTON_COLOR.into();
41            }
42            Interaction::None => {
43                *background_color = NORMAL_BUTTON_COLOR.into();
44            }
45        }
46    }
47}
48
49pub fn interact_with_settings_button(
50    mut button_q: Query<
51        (&Interaction, &mut BackgroundColor),
52        (Changed<Interaction>, With<SettingsButton>),
53    >,
54    mut settings_state: ResMut<NextState<UiState>>,
55) {
56    if let Ok((interaction, mut background_color)) = button_q.get_single_mut() {
57        match *interaction {
58            Interaction::Pressed => {
59                *background_color = PRESSED_BUTTON_COLOR.into();
60                settings_state.set(UiState::Settings);
61            }
62            Interaction::Hovered => {
63                *background_color = HOVERED_BUTTON_COLOR.into();
64            }
65            Interaction::None => {
66                *background_color = NORMAL_BUTTON_COLOR.into();
67            }
68        }
69    }
70}
71
72// pub fn interact_with_respawn_button(
73//     mut button_q: Query<
74//         (&Interaction, &mut BackgroundColor),
75//         (Changed<Interaction>, With<RespawnButton>),
76//     >,
77//     mut spawn_ev: EventWriter<SpawnEvent>,
78//     mut state: ResMut<NextState<SimulationState>>,
79//     cam_q: Query<&CameraTarget, With<Camera>>,
80//     commands: Commands,
81//     respawn_q: Query<Entity, With<Screen>>,
82//     SimulationState maps: Res<Assets<Map>>,
83//     map: Res<MapHandle>,
84// ) {
85//     if let Ok(cam) = cam_q.get_single() {
86//         if let Some(map) = maps.get(map.0.id()) {
87//             if let Ok((interaction, mut background_color)) = button_q.get_single_mut() {
88//                 match *interaction {
89//                     Interaction::Pressed => {
90//                         *background_color = PRESSED_BUTTON_COLOR.into();
91//                         despawn_screens(commands, respawn_q);
92//                         state.set(SimulationState::Running);
93//                         spawn_ev.send(SpawnEvent {
94//                             player_number: cam.target,
95//                             transform: Vec3 {
96//                                 x: 5.0,
97//                                 y: map.map_height as f32 * 2.0 + 1.0,
98//                                 z: 0.0,
99//                             },
100//                         });
101//                     }
102//                     Interaction::Hovered => {
103//                         *background_color = HOVERED_BUTTON_COLOR.into();
104//                     }
105//                     Interaction::None => {
106//                         *background_color = NORMAL_BUTTON_COLOR.into();
107//                     }
108//                 }
109//             }
110//         }
111//     }
112// }
113
114pub fn despawn_screens(mut commands: Commands, mut screen_q: Query<Entity, With<Screen>>) {
115    for ent in screen_q.iter_mut() {
116        commands.entity(ent).despawn_recursive();
117    }
118}
119
120pub fn switch_to_menu(mut state: ResMut<NextState<UiState>>) {
121    state.set(UiState::MainMenu);
122}