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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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