Skip to content

Commit

Permalink
ui_root
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-ie committed Dec 21, 2024
1 parent 845d992 commit 359e4ae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod assets;
#[cfg(feature = "dev")]
mod dev_tools;
mod screens;
mod ui;

use bevy::{
asset::AssetMetaCheck,
Expand Down
15 changes: 14 additions & 1 deletion src/screens/loading.rs
Original file line number Diff line number Diff line change
@@ -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..."));
});
}
23 changes: 23 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -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()
}))
}
}

0 comments on commit 359e4ae

Please sign in to comment.