From e0cd574a5126e53fe043e1818c3764f4c38bd6b1 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 28 Aug 2023 08:02:36 -0700 Subject: [PATCH] Little cleanup with title bars code duplication --- bin/editor.rs | 34 +++------------------------------ src/ui.rs | 53 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/bin/editor.rs b/bin/editor.rs index 7bc86ed..c8f5992 100644 --- a/bin/editor.rs +++ b/bin/editor.rs @@ -260,22 +260,7 @@ fn initialize_ui(mut commands: Commands) { }) .id(); parent.spawn(( - NodeBundle { - style: Style { - border: UiRect::all(Val::Px(1.0)), - margin: UiRect::all(Val::Px(1.0)), - padding: UiRect::all(Val::Px(1.0)), - flex_direction: FlexDirection::Row, - overflow: Overflow::clip(), - align_items: AlignItems::Center, - align_content: AlignContent::Center, - justify_content: JustifyContent::SpaceBetween, - ..default() - }, - background_color: Color::WHITE.into(), - border_color: Color::BLACK.into(), - ..default() - }, + ui::TitleBarBase::new(Color::WHITE).bundle(), ui::Title { text: "Assets".into(), ..default() @@ -1087,20 +1072,7 @@ mod monologues { }) .with_children(|parent| { parent.spawn(( - NodeBundle { - style: Style { - padding: UiRect::all(Val::Px(1.0)), - margin: UiRect::all(Val::Px(1.0)), - border: UiRect::all(Val::Px(1.0)), - flex_direction: FlexDirection::Row, - align_items: AlignItems::Center, - justify_content: JustifyContent::SpaceBetween, - ..default() - }, - background_color: Color::VIOLET.into(), - border_color: Color::BLACK.into(), - ..default() - }, + ui::TitleBarBase::new(Color::VIOLET).bundle(), ui::Title { text: "Monologue".into(), ..default() @@ -1123,7 +1095,7 @@ mod monologues { )); }); }); - }) + }); } // TODO: Sync Handle and TextStyle components to automagically generate and sync text diff --git a/src/ui.rs b/src/ui.rs index 16ae171..22bdcb1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -61,6 +61,35 @@ mod title { pub font: Option>, } + #[derive(Debug)] + pub struct TitleBarBase { + bg_color: Color, + } + + impl TitleBarBase { + pub fn new(bg_color: Color) -> Self { + Self { bg_color } + } + + pub fn bundle(&self) -> NodeBundle { + NodeBundle { + style: Style { + padding: UiRect::all(Val::Px(1.0)), + margin: UiRect::all(Val::Px(1.0)), + border: UiRect::all(Val::Px(1.0)), + flex_direction: FlexDirection::Row, + align_items: AlignItems::Center, + align_content: AlignContent::Center, + justify_content: JustifyContent::SpaceBetween, + ..default() + }, + background_color: self.bg_color.into(), + border_color: Color::BLACK.into(), + ..default() + } + } + } + #[derive(Debug, Component)] pub struct Note { pub text: String, @@ -579,25 +608,13 @@ pub mod alert { justify_self: JustifySelf::Center, ..default() }, + background_color: Color::WHITE.into(), border_color: color.into(), ..default() }) .with_children(|parent| { parent.spawn(( - NodeBundle { - style: Style { - padding: UiRect::all(Val::Px(1.0)), - margin: UiRect::all(Val::Px(1.0)), - border: UiRect::all(Val::Px(1.0)), - flex_direction: FlexDirection::Row, - align_items: AlignItems::Center, - align_content: AlignContent::Center, - justify_content: JustifyContent::SpaceBetween, - ..default() - }, - background_color: color.into(), - ..default() - }, + TitleBarBase::new(color).bundle(), Title { text: "Alert".into(), ..default() @@ -607,7 +624,13 @@ pub mod alert { }, Sorting(0), )); - parent.spawn(TextBundle::from_section(text, TextStyle { ..default() })); + parent.spawn(TextBundle::from_section( + text, + TextStyle { + color: Color::BLACK.into(), + ..default() + }, + )); }); }); });