use super::components::{RonUI, UILayer, UILayerNavigationConfig};
use crate::app_state::overworld::OverworldEntity;
use crate::extra::mortar::LocaleLoaded;
use bevy::prelude::*;
pub(crate) fn spawn_backpack_ui_system(
mut commands: Commands,
overworld_ui_query: Query<&RonUI>,
locale_loaded: Option<Res<LocaleLoaded>>,
navigation_config: Res<UILayerNavigationConfig>,
) {
if locale_loaded.is_none() {
return;
}
if !overworld_ui_query.is_empty() {
return;
}
let player_data = crate::core::data::PlayerData::default();
let max_index = navigation_config
.get(&UILayer::BACKPACK_MENU)
.and_then(|rule| {
rule.max_index()
.as_ref()
.map(|bound| super::ron_ui::evaluate_index_bound(bound, &player_data))
})
.unwrap_or_else(|| UILayer::BACKPACK_MENU_OPTIONS.len());
commands.spawn((
OverworldEntity(),
RonUI::new(UILayer::BACKPACK_MENU, max_index),
Transform::from_translation(Vec3::ZERO),
Name::new("Backpack Menu UI"),
));
info!(
"Spawned backpack UI in Menu state with max_index {}",
max_index
);
}
pub(crate) fn despawn_backpack_ui_system(
mut commands: Commands,
overworld_ui_query: Query<Entity, With<RonUI>>,
) {
for entity in &overworld_ui_query {
let root = entity;
commands.queue(move |world: &mut World| {
let mut stack = vec![root];
while let Some(entity) = stack.pop() {
if let Ok(entity_ref) = world.get_entity(entity)
&& let Some(children) = entity_ref.get::<Children>()
{
for child in children.iter() {
stack.push(child);
}
}
let _ = world.despawn(entity);
}
});
info!("Despawned backpack UI");
}
}