From 96c76da7f62fd4dee16ef615dd917612ca1f3c37 Mon Sep 17 00:00:00 2001 From: demenik Date: Fri, 28 Nov 2025 15:48:59 +0100 Subject: [PATCH] feat: Add text UI component (#56) --- src/features/hud/mod.rs | 26 +++--------------- src/features/hud/ui/settings.rs | 38 ++++++-------------------- src/features/hud/ui/timer_settings.rs | 13 ++------- src/features/inventory/ui/inventory.rs | 6 +--- src/features/inventory/ui/item.rs | 14 ++++------ src/features/savegame/ui/load.rs | 29 ++++++++------------ src/features/start_screen/mod.rs | 6 +--- src/features/ui/ui/button.rs | 12 ++------ src/features/ui/ui/mod.rs | 2 ++ src/features/ui/ui/texts.rs | 19 +++++++++++++ 10 files changed, 56 insertions(+), 109 deletions(-) create mode 100644 src/features/ui/ui/texts.rs diff --git a/src/features/hud/mod.rs b/src/features/hud/mod.rs index 92f5bbe..bafd140 100644 --- a/src/features/hud/mod.rs +++ b/src/features/hud/mod.rs @@ -39,37 +39,19 @@ fn setup(mut commands: Commands) { }, BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)), children![ - ( - TextType::Phase, - Text::new("..."), - TextFont::from_font_size(16.0), - TextColor(Color::WHITE) - ), - ( - TextType::Timer, - Text::new("--:--"), - TextFont::from_font_size(16.0), - TextColor(Color::WHITE) - ), + text_with_component(TextType::Phase, "...", 16.0, Color::WHITE), + text_with_component(TextType::Timer, "...", 16.0, Color::WHITE), ( Button, inventory::components::ButtonType::InventoryOpen, Node::default(), - children![( - Text::new("Inventar"), - TextFont::from_font_size(16.0), - TextColor(Color::WHITE) - )] + children![text("Inventar", 16.0, Color::WHITE)] ), ( Button, ButtonType::SettingsOpen, Node::default(), - children![( - Text::new("Einstellungen"), - TextFont::from_font_size(16.0), - TextColor(Color::WHITE) - )] + children![text("Einstellungen", 16.0, Color::WHITE)] ) ], )); diff --git a/src/features/hud/ui/settings.rs b/src/features/hud/ui/settings.rs index 4883089..dd8868d 100644 --- a/src/features/hud/ui/settings.rs +++ b/src/features/hud/ui/settings.rs @@ -33,11 +33,7 @@ pub fn open_settings(commands: &mut Commands) { ..Node::hstack(px(20)) }, children![ - ( - Text::new("Spiel Einstellungen"), - TextFont::from_font_size(40.0), - TextColor(Color::WHITE), - ), + text("Spiel Einstellungen", 40.0, Color::WHITE), pill_button( ButtonType::SettingsClose, ButtonVariant::Destructive, @@ -89,16 +85,12 @@ pub fn open_settings(commands: &mut Commands) { ..Node::vstack(px(10)) }, children![ - ( - Text::new("Timer Einstellungen"), - TextFont::from_font_size(18.0), - TextColor(Color::WHITE), + text("Spiel Einstellungen", 18.0, Color::WHITE), + text( + "Tipp: Benutze [Umstellen] um in 10er Schritten zu inkrementieren oder dekrementieren!", + 16.0, + Color::WHITE ), - ( - Text::new("Tipp: Benutze [Umstellen] um in 10er Schritten zu inkrementieren oder dekrementieren!"), - TextFont::from_font_size(16.0), - TextColor(Color::WHITE) - ) ] ), ( @@ -107,11 +99,7 @@ pub fn open_settings(commands: &mut Commands) { ..Node::vstack(px(10)) }, children![ - ( - Text::new("Fokus Phase"), - TextFont::from_font_size(12.0), - TextColor(Color::WHITE) - ), + text("Fokus Phase", 12.0, Color::WHITE), timer_settings(TimerType::Focus) ] ), @@ -121,11 +109,7 @@ pub fn open_settings(commands: &mut Commands) { ..Node::vstack(px(10)) }, children![ - ( - Text::new("Kurze Pause"), - TextFont::from_font_size(12.0), - TextColor(Color::WHITE) - ), + text("Kurze Pause", 12.0, Color::WHITE), timer_settings(TimerType::ShortBreak) ] ), @@ -135,11 +119,7 @@ pub fn open_settings(commands: &mut Commands) { ..Node::vstack(px(10)) }, children![ - ( - Text::new("Lange Pause"), - TextFont::from_font_size(12.0), - TextColor(Color::WHITE) - ), + text("Lange Pause", 12.0, Color::WHITE), timer_settings(TimerType::LongBreak) ] ) diff --git a/src/features/hud/ui/timer_settings.rs b/src/features/hud/ui/timer_settings.rs index e7d599c..49fce78 100644 --- a/src/features/hud/ui/timer_settings.rs +++ b/src/features/hud/ui/timer_settings.rs @@ -9,11 +9,7 @@ pub fn timer_settings(timer_type: TimerType) -> impl Bundle { }, children![ timer_settings_part(SettingsTimerInput::Minutes(timer_type.clone()), 1), - ( - Text::new(":"), - TextFont::from_font_size(24.0), - TextColor(Color::WHITE) - ), + text(":", 24.0, Color::WHITE), timer_settings_part(SettingsTimerInput::Seconds(timer_type.clone()), 1), ], ) @@ -36,12 +32,7 @@ fn timer_settings_part(input: SettingsTimerInput, amount: u32) -> impl Bundle { "+", 12.0 ), - ( - input.clone(), - Text::new("--"), - TextFont::from_font_size(24.0), - TextColor(Color::WHITE) - ), + text_with_component(input.clone(), "--", 24.0, Color::WHITE), button( ButtonType::SettingsTimerChange { input: input.clone(), diff --git a/src/features/inventory/ui/inventory.rs b/src/features/inventory/ui/inventory.rs index d00514d..e77d9cc 100644 --- a/src/features/inventory/ui/inventory.rs +++ b/src/features/inventory/ui/inventory.rs @@ -33,11 +33,7 @@ pub fn open_inventory(commands: &mut Commands, items: Query<&ItemStack>) { ..Node::hstack(px(20)) }, children![ - ( - Text::new("Inventar"), - TextFont::from_font_size(40.0), - TextColor(Color::WHITE), - ), + text("Inventar", 40.0, Color::WHITE), pill_button( ButtonType::InventoryClose, ButtonVariant::Destructive, diff --git a/src/features/inventory/ui/item.rs b/src/features/inventory/ui/item.rs index 950e7b1..cb9517b 100644 --- a/src/features/inventory/ui/item.rs +++ b/src/features/inventory/ui/item.rs @@ -31,16 +31,12 @@ pub fn list_itemstack(itemstack: &ItemStack) -> impl Bundle { ..Node::vstack(px(4)) }, children![ - ( - Text::new(format!("{} ({})", name, itemstack.amount)), - TextFont::from_font_size(14.0), - TextColor(Color::WHITE) + text( + format!("{} ({})", name, itemstack.amount), + 14.0, + Color::WHITE ), - ( - Text::new(itemstack.item_type.description()), - TextFont::from_font_size(10.0), - TextColor(Color::WHITE) - ) + text(itemstack.item_type.description(), 10.0, Color::WHITE) ] ) ], diff --git a/src/features/savegame/ui/load.rs b/src/features/savegame/ui/load.rs index ff71740..80b405e 100644 --- a/src/features/savegame/ui/load.rs +++ b/src/features/savegame/ui/load.rs @@ -37,11 +37,7 @@ pub fn spawn_load_popup(commands: &mut Commands) { ..default() }, children![ - ( - Text::new("Spielstand Auswahl"), - TextFont::from_font_size(40.0), - TextColor(Color::WHITE), - ), + text("Spielstand Auswahl", 40.0, Color::WHITE), pill_button( ButtonType::PopupClose, ButtonVariant::Destructive, @@ -93,23 +89,20 @@ pub fn spawn_load_popup(commands: &mut Commands) { ..default() }, children![ - ( - Text::new(format!( - "Spielstand {}", - savegame.index + 1 - )), - TextFont::from_font_size(24.0), - TextColor(Color::srgb(0.9, 0.9, 0.9)) + text( + format!("Spielstand {}", savegame.index + 1), + 24.0, + Color::WHITE ), - ( - Text::new(format!( + text( + format!( "Beeren: {}, Fokusphasen abgeschlossen: {}", savegame.total_berries, savegame.completed_focus - )), - TextFont::from_font_size(18.0), - TextColor(Color::srgb(0.9, 0.9, 0.9)) - ) + ), + 18.0, + Color::WHITE, + ), ] ), pill_button( diff --git a/src/features/start_screen/mod.rs b/src/features/start_screen/mod.rs index 78d2de6..afcfdff 100644 --- a/src/features/start_screen/mod.rs +++ b/src/features/start_screen/mod.rs @@ -25,11 +25,7 @@ fn setup(mut commands: Commands) { ..Node::center() }, children![ - ( - Text::new("Pomonon Garten"), - TextFont::from_font_size(64.0), - TextColor(Color::srgb(0.9, 0.9, 0.9)) - ), + text("Pomomon Garden", 64.0, Color::WHITE), button( ButtonType::LoadGame, ButtonVariant::Primary, diff --git a/src/features/ui/ui/button.rs b/src/features/ui/ui/button.rs index b4cb220..11ae87e 100644 --- a/src/features/ui/ui/button.rs +++ b/src/features/ui/ui/button.rs @@ -17,11 +17,7 @@ pub fn button( node, BackgroundColor(variant.normal_background()), BorderRadius::all(px(10)), - children![( - Text::new(title), - TextFont::from_font_size(font_size), - TextColor(variant.text_color()) - )], + children![text(title, font_size, variant.text_color())], ) } @@ -42,11 +38,7 @@ pub fn pill_button( node, BackgroundColor(variant.normal_background()), BorderRadius::MAX, - children![( - Text::new(title), - TextFont::from_font_size(font_size), - TextColor(variant.text_color()) - )], + children![text(title, font_size, variant.text_color())], ) } diff --git a/src/features/ui/ui/mod.rs b/src/features/ui/ui/mod.rs index 637968e..b349580 100644 --- a/src/features/ui/ui/mod.rs +++ b/src/features/ui/ui/mod.rs @@ -1,5 +1,7 @@ pub mod button; pub mod flexbox; +pub mod texts; pub use button::{button, pill_button}; pub use flexbox::Flexbox; +pub use texts::{text, text_with_component}; diff --git a/src/features/ui/ui/texts.rs b/src/features/ui/ui/texts.rs new file mode 100644 index 0000000..70b19df --- /dev/null +++ b/src/features/ui/ui/texts.rs @@ -0,0 +1,19 @@ +pub use crate::prelude::*; + +pub fn text(content: impl Into, size: f32, color: Color) -> (Text, TextFont, TextColor) { + ( + Text::new(content), + TextFont::from_font_size(size), + TextColor(color), + ) +} + +pub fn text_with_component( + component: impl Component, + content: impl Into, + size: f32, + color: Color, +) -> impl Bundle { + let (a, b, c) = text(content, size, color); + (component, a, b, c) +}