fix: Improve button/pill_button UI element to allow any child

This commit is contained in:
demenik
2025-12-01 14:26:16 +01:00
parent 7a608516eb
commit f908192c0e
8 changed files with 86 additions and 94 deletions

View File

@@ -48,8 +48,7 @@ fn setup(mut commands: Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Shop", |color| text("Shop", 16.0, color)
16.0
), ),
button( button(
inventory::components::ButtonType::InventoryOpen, inventory::components::ButtonType::InventoryOpen,
@@ -58,8 +57,7 @@ fn setup(mut commands: Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Inventar", |color| text("Inventar", 16.0, color)
16.0
), ),
button( button(
ButtonType::SettingsOpen, ButtonType::SettingsOpen,
@@ -68,8 +66,7 @@ fn setup(mut commands: Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Einstellungen", |color| text("Einstellungen", 16.0, color)
16.0
) )
], ],
)); ));

View File

@@ -43,8 +43,7 @@ pub fn open_settings(commands: &mut Commands) {
height: px(40), height: px(40),
..default() ..default()
}, },
"X", |color| text("X", 24.0, color)
24.0
), ),
], ],
)); ));
@@ -59,8 +58,7 @@ pub fn open_settings(commands: &mut Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Spiel verlassen", |color| text("Spiel verlassen", 24.0, color)
24.0,
)); ));
parent.spawn(button( parent.spawn(button(
@@ -70,8 +68,7 @@ pub fn open_settings(commands: &mut Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Spiel speichern", |color| text("Spiel speichern", 24.0, color)
24.0,
)); ));
parent.spawn(( parent.spawn((

View File

@@ -29,8 +29,7 @@ fn timer_settings_part(input: SettingsTimerInput, amount: u32) -> impl Bundle {
width: percent(100), width: percent(100),
..default() ..default()
}, },
"+", |color| text("+", 12.0, color)
12.0
), ),
text_with_component(input.clone(), "--", 24.0, Color::WHITE), text_with_component(input.clone(), "--", 24.0, Color::WHITE),
button( button(
@@ -43,8 +42,7 @@ fn timer_settings_part(input: SettingsTimerInput, amount: u32) -> impl Bundle {
width: percent(100), width: percent(100),
..default() ..default()
}, },
"-", |color| text("-", 12.0, color)
12.0
), ),
], ],
) )

View File

@@ -2,7 +2,11 @@ use super::super::components::{ButtonType, RootMarker};
use crate::prelude::GameConfig; use crate::prelude::GameConfig;
use crate::{features::inventory::ui::list_itemstack, prelude::*}; use crate::{features::inventory::ui::list_itemstack, prelude::*};
pub fn open_inventory(commands: &mut Commands, items: Query<&ItemStack>, game_config: &Res<GameConfig>) { pub fn open_inventory(
commands: &mut Commands,
items: Query<&ItemStack>,
game_config: &Res<GameConfig>,
) {
commands commands
.spawn(( .spawn((
RootMarker::Inventory, RootMarker::Inventory,
@@ -44,8 +48,7 @@ pub fn open_inventory(commands: &mut Commands, items: Query<&ItemStack>, game_co
height: px(40), height: px(40),
..default() ..default()
}, },
"X", |color| text("X", 24.0, color)
24.0
), ),
], ],
)); ));

View File

@@ -47,8 +47,7 @@ pub fn spawn_load_popup(commands: &mut Commands) {
height: px(40), height: px(40),
..default() ..default()
}, },
"X", |color| text("X", 24.0, color)
24.0
) )
], ],
)); ));
@@ -64,63 +63,61 @@ pub fn spawn_load_popup(commands: &mut Commands) {
}) })
.with_children(|parent| { .with_children(|parent| {
for savegame in SavegamePath::list() { for savegame in SavegamePath::list() {
parent.spawn(( parent.spawn(
Button, button(
ButtonType::SavegameLoad { ButtonType::SavegameLoad { savegame_path: savegame.path.clone() },
savegame_path: savegame.path.clone(), ButtonVariant::Secondary,
}, Node {
ButtonVariant::Secondary, width: percent(100),
Node { padding: UiRect::all(px(10)),
width: percent(100), ..Node::center()
height: px(80), },
flex_direction: FlexDirection::Row, |color| (
column_gap: px(10.0),
padding: UiRect::horizontal(px(10.0)),
..Node::center()
},
BackgroundColor(ButtonVariant::Secondary.normal_background()),
BorderRadius::all(px(10)),
children![
(
Node { Node {
width: percent(100), width: percent(100),
height: percent(100), align_items: AlignItems::Center,
flex_direction: FlexDirection::Column, ..Node::hstack(px(10))
justify_content: JustifyContent::Center,
..default()
}, },
children![ children![(
text( Node {
format!("Spielstand {}", savegame.index + 1), width: percent(100),
24.0, height: percent(100),
Color::WHITE flex_direction: FlexDirection::Column,
), justify_content: JustifyContent::Center,
text( ..default()
format!( },
"Beeren: {}, Fokusphasen abgeschlossen: {}", children![
savegame.total_berries, text(
savegame.completed_focus format!("Spielstand {}", savegame.index + 1),
24.0,
color
), ),
18.0, text(
Color::WHITE, format!(
), "Beeren: {}, Fokusphasen abgeschlossen: {}",
] savegame.total_berries,
), savegame.completed_focus
pill_button( ),
ButtonType::SavegameDelete { 18.0,
savegame_path: savegame.path.clone() Color::WHITE,
}, ),
ButtonVariant::Destructive, ]
Node { ),
width: px(40), pill_button(
height: px(40), ButtonType::SavegameDelete {
..default() savegame_path: savegame.path.clone()
}, },
"X", ButtonVariant::Destructive,
24.0 Node {
), width: px(40),
], height: px(40),
)); ..default()
},
|color| text("X", 24.0, color)
)]
)
),
);
} }
}); });
}); });

View File

@@ -42,8 +42,7 @@ pub fn open_shop(commands: &mut Commands) {
height: px(40), height: px(40),
..default() ..default()
}, },
"X", |color| text("X", 24.0, color)
24.0
), ),
], ],
)); ));

View File

@@ -34,8 +34,7 @@ fn setup(mut commands: Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Spiel laden", |color| text("Spiel laden", 33.0, color)
33.0
), ),
button( button(
ButtonType::NewGame, ButtonType::NewGame,
@@ -45,8 +44,7 @@ fn setup(mut commands: Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Neues Spiel", |color| text("Neues Spiel", 33.0, color)
33.0,
), ),
button( button(
ButtonType::Settings, ButtonType::Settings,
@@ -56,8 +54,7 @@ fn setup(mut commands: Commands) {
padding: UiRect::all(px(10)), padding: UiRect::all(px(10)),
..default() ..default()
}, },
"Einstellungen", |color| text("Einstellungen", 33.0, color)
33.0
), ),
], ],
)); ));

View File

@@ -1,14 +1,15 @@
use crate::prelude::*; use crate::prelude::*;
pub fn button( pub fn button<C, R>(
button_type: impl Component, button_type: impl Component,
variant: ButtonVariant, variant: ButtonVariant,
mut node: Node, mut node: Node,
title: impl Into<String>, child: C,
font_size: f32, ) -> impl Bundle
) -> impl Bundle { where
C: FnOnce(Color) -> R,
R: Bundle,
{
node.justify_content = JustifyContent::Center; node.justify_content = JustifyContent::Center;
node.align_items = AlignItems::Center; node.align_items = AlignItems::Center;
@@ -19,17 +20,20 @@ pub fn button(
node, node,
BackgroundColor(variant.normal_background()), BackgroundColor(variant.normal_background()),
BorderRadius::all(px(10)), BorderRadius::all(px(10)),
children![text(title, font_size, variant.text_color())], children![child(variant.text_color())],
) )
} }
pub fn pill_button( pub fn pill_button<C, R>(
button_type: impl Component, button_type: impl Component,
variant: ButtonVariant, variant: ButtonVariant,
mut node: Node, mut node: Node,
title: impl Into<String>, child: C,
font_size: f32, ) -> impl Bundle
) -> impl Bundle { where
C: FnOnce(Color) -> R,
R: Bundle,
{
node.justify_content = JustifyContent::Center; node.justify_content = JustifyContent::Center;
node.align_items = AlignItems::Center; node.align_items = AlignItems::Center;
@@ -40,7 +44,7 @@ pub fn pill_button(
node, node,
BackgroundColor(variant.normal_background()), BackgroundColor(variant.normal_background()),
BorderRadius::MAX, BorderRadius::MAX,
children![text(title, font_size, variant.text_color())], children![child(variant.text_color())],
) )
} }