From 359e4ae5f7e096f9b9dd2c532a285b2f48dd4dfd Mon Sep 17 00:00:00 2001 From: Rich Churcher Date: Sat, 21 Dec 2024 13:00:41 +1300 Subject: [PATCH] ui_root --- src/lib.rs | 1 + src/screens/loading.rs | 15 ++++++++++++++- src/ui.rs | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/ui.rs diff --git a/src/lib.rs b/src/lib.rs index 991bc9a..ee7e696 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ mod assets; #[cfg(feature = "dev")] mod dev_tools; mod screens; +mod ui; use bevy::{ asset::AssetMetaCheck, diff --git a/src/screens/loading.rs b/src/screens/loading.rs index 3b544a8..45edda0 100644 --- a/src/screens/loading.rs +++ b/src/screens/loading.rs @@ -1,3 +1,16 @@ use bevy::prelude::*; -pub fn plugin(app: &mut App) {} +use crate::{screens::Screen, ui::Containers}; + +pub(super) fn plugin(app: &mut App) { + app.add_systems(OnEnter(Screen::Loading), spawn_loading_screen); +} + +fn spawn_loading_screen(mut commands: Commands) { + commands + .ui_root() + .insert(StateScoped(Screen::Loading)) + .with_children(|p| { + p.spawn(Text::new("Loading...")); + }); +} diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..aae16ab --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,23 @@ +use bevy::{ecs::system::EntityCommands, prelude::*, ui::Val::*}; + +/// An extension trait for spawning UI containers. +pub trait Containers { + /// Spawns a root node that covers the full screen + /// and centers its content horizontally and vertically. + fn ui_root(&mut self) -> EntityCommands; +} + +impl Containers for Commands<'_, '_> { + fn ui_root(&mut self) -> EntityCommands { + self.spawn((Name::new("UI Root"), Node { + align_items: AlignItems::Center, + flex_direction: FlexDirection::Column, + height: Percent(100.0), + justify_content: JustifyContent::Center, + position_type: PositionType::Absolute, + row_gap: Px(10.0), + width: Percent(100.0), + ..default() + })) + } +}