From 7cd07ad958a1f5c1d10533878acd7855b13d7eac Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sun, 6 Oct 2024 08:51:49 +0200 Subject: [PATCH] button --- src/main.rs | 127 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 43 deletions(-) diff --git a/src/main.rs b/src/main.rs index 62c8d6d..615c4af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,8 +66,8 @@ fn main() { .add_systems(Startup, star_setup) .add_systems(Startup, cons_setup) .add_systems(Startup, ui_setup) - //.add_systems(Update, button_interaction) .add_systems(Update, player_rotate) + .add_systems(Update, button_system) .run(); } @@ -236,51 +236,92 @@ fn player_rotate( } } -fn ui_setup( - mut commands: Commands, - //asset_server: Res, - //mut materials: ResMut> -) { - let text = "Hello world!"; - - commands.spawn( - TextBundle::from_section( - text, - TextStyle { - font_size: 100.0, - color: Color::WHITE, - ..default() - }, - ) // Set the alignment of the Text - .with_text_justify(JustifyText::Center) - // Set the style of the TextBundle itself. - .with_style(Style { - position_type: PositionType::Absolute, - bottom: Val::Px(5.0), - right: Val::Px(5.0), +const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); +const PRESSED_BUTTON: Color = Color::srgb(0.50, 0.15, 0.15); + +fn ui_setup(mut commands: Commands) { + let container_node = NodeBundle { + style: Style { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + align_items: AlignItems::Center, + justify_content: JustifyContent::Center, ..default() - }) + }, + ..default() + }; + + let button_node = ButtonBundle { + style: Style { + width: Val::Px(150.0), + height: Val::Px(65.0), + border: UiRect::all(Val::Px(5.0)), + // horizontally center child text + justify_content: JustifyContent::Center, + // vertically center child text + align_items: AlignItems::Center, + ..default() + }, + border_color: BorderColor(Color::BLACK), + background_color: NORMAL_BUTTON.into(), + ..default() + }; + + let button_text_node = TextBundle::from_section( + "Button", + TextStyle { + font_size: 40.0, + color: Color::srgb(0.9, 0.9, 0.9), + ..default() + }, ); + + let container = commands.spawn(container_node).id(); + let button = commands.spawn(button_node).id(); + let button_text = commands.spawn(button_text_node).id(); + + commands.entity(button).push_children(&[button_text]); + commands.entity(container).push_children(&[button]); } -// System to handle button interaction -// fn button_interaction( -// mut interaction_query: Query<(&Interaction, &mut BackgroundColor), (Changed, With