menu system: system dispatching (and the menus can now be closed)

This commit is contained in:
Schrottkatze 2025-06-02 22:20:25 +02:00
parent acbcfe5956
commit fc83ab8452
Signed by: schrottkatze
SSH key fingerprint: SHA256:FPOYVeBy3QP20FEM42uWF1Wa/Qhlk+L3S2+Wuau/Auo
7 changed files with 202 additions and 132 deletions

View file

@ -5,15 +5,24 @@ use bevy::{
state::state::FreelyMutableState,
};
use super::{ItemPosition, Menu, MenuItemInternal, MenuItemType, Menus, OnPressAction};
use super::{
ItemPosition, MenuItemInternal, MenuItemType, OnPressAction,
menus::{Menu, Menus},
};
impl<TriggerState: States, NavState: States + FreelyMutableState + Copy> Plugin
for Menus<TriggerState, NavState>
mod components;
impl<TriggerState, NavState> Plugin for Menus<TriggerState, NavState>
where
TriggerState: States,
NavState: States + FreelyMutableState + Copy,
{
fn build(&self, app: &mut App) {
app.insert_state(self.start)
.insert_resource(self.get_store())
.insert_resource(MenusClosedWhen(self.closed_when))
.add_observer(build_ui::<NavState>)
.add_observer(destroy_ui::<NavState>)
// .add_observer(destroy_ui::<NavStates>)
.add_systems(
Update,
@ -26,7 +35,7 @@ impl<TriggerState: States, NavState: States + FreelyMutableState + Copy> Plugin
}
#[derive(Event)]
struct DestroyUi<S: States>(S);
struct DestroyUi<S: States>(PhantomData<S>);
#[derive(Event)]
struct BuildUi<S: States>(S);
@ -46,6 +55,19 @@ impl<T: States, S: States> Menus<T, S> {
#[derive(Resource)]
struct MenusStore<S: States>(HashMap<S, Menu<S>>);
#[derive(Resource)]
struct MenusClosedWhen<S: States>(S);
fn destroy_ui<S: States>(
trigger: Trigger<DestroyUi<S>>,
ui_parent: Query<Entity, With<UiParent<S>>>,
mut c: Commands,
) {
trace!("destroy_ui called");
if let Ok(parent) = ui_parent.single_inner() {
c.entity(parent).despawn();
}
}
fn build_ui<S: States>(
trigger: Trigger<BuildUi<S>>,
@ -63,11 +85,7 @@ fn build_ui<S: States>(
.clone()
.into_iter()
// TODO: implement other item positions
.filter(|it| it.pos == ItemPosition::MainView);
if let Ok(parent) = ui_parent.single_inner() {
c.entity(parent).despawn();
}
.filter(|it| ItemPosition::MainView == it.pos);
c.spawn((
UiParent(PhantomData::<S>),
@ -96,75 +114,33 @@ fn build_ui<S: States>(
}
fn handle_press_actions<S: States + FreelyMutableState>(
mut c: Commands,
mut interaction_query: Query<(&Interaction, &Action<S>), Changed<Interaction>>,
mut nav_state: ResMut<NextState<S>>,
closed_when: Res<MenusClosedWhen<S>>,
) {
for (interaction, Action(action)) in interaction_query {
if *interaction != Interaction::Pressed {
continue;
}
match action {
OnPressAction::NavigateTo(to) => nav_state.set(to.clone()),
_ => todo!(),
}
action.run(&mut c, &mut nav_state, &closed_when.0);
}
}
mod components {
use bevy::prelude::*;
use crate::menus::lib::OnPressAction;
use super::Action;
pub fn text(t: String) -> impl Bundle {
(
Text::new(t),
TextFont {
// font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 33.0,
..default()
},
TextColor(Color::srgb(0.9, 0.9, 0.9)),
TextShadow::default(),
)
}
pub fn button(t: String) -> impl Bundle {
(
Button,
Node {
border: UiRect::all(Val::Px(5.0)),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
padding: UiRect::all(Val::Px(10.0)),
..default()
},
BorderColor(Color::BLACK),
BorderRadius::MAX,
BackgroundColor(Color::srgb(0.2, 0.2, 0.2)),
children![text(t)],
)
}
}
fn update_ui_trigger<S: States>(
fn update_ui_trigger<S: States + PartialEq>(
mut c: Commands,
mut trans_reader: EventReader<StateTransitionEvent<S>>,
menus_data: Res<MenusStore<S>>,
closed_when: Res<MenusClosedWhen<S>>,
) {
for trans in trans_reader.read() {
info!("{trans:?}");
// if let Some(to_delete) = trans.exited {}
c.trigger(DestroyUi(PhantomData::<S>));
if let Some(to_build) = trans.entered.clone() {
c.trigger(BuildUi(to_build));
if to_build != closed_when.0 {
c.trigger(BuildUi(to_build));
}
}
}
}
// fn build_ui<S: States>(mut c: Commands) {
// }