Skip to content

Commit

Permalink
Better organize game UI elements
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmauro committed Apr 19, 2024
1 parent 1ce9b70 commit cfd179e
Showing 1 changed file with 116 additions and 108 deletions.
224 changes: 116 additions & 108 deletions src/game/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ pub struct UiPlugin;

impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::Game), setup)
app.add_systems(OnEnter(AppState::Game), game_ui)
.add_systems(Update, round_system);
}
}

pub fn setup(mut commands: Commands, settings: Res<GameSettings>, asset_server: Res<AssetServer>) {
pub fn game_ui(
mut commands: Commands,
settings: Res<GameSettings>,
asset_server: Res<AssetServer>,
) {
let font = asset_server.load("embedded://fonts/FiraSans-Bold.ttf");

commands
Expand All @@ -31,7 +35,7 @@ pub fn setup(mut commands: Commands, settings: Res<GameSettings>, asset_server:
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
padding: UiRect::all(Val::Px(20.0)),
..default()
},
..default()
Expand All @@ -45,7 +49,6 @@ pub fn setup(mut commands: Commands, settings: Res<GameSettings>, asset_server:
style: Style {
flex_grow: 1.0,
flex_direction: FlexDirection::Row,
align_items: AlignItems::Start,
justify_content: JustifyContent::SpaceBetween,
width: Val::Percent(100.0),
..default()
Expand All @@ -54,27 +57,7 @@ pub fn setup(mut commands: Commands, settings: Res<GameSettings>, asset_server:
},
OnGameScreen,
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
format!("{}-Back", settings.n),
TextStyle {
font: font.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
parent.spawn((
TextBundle::from_section(
"11/24",
TextStyle {
font: font.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
),
CurrentRoundText,
));
});
.with_children(|parent| game_info(parent, settings, font.clone()));

parent
.spawn((
Expand All @@ -83,96 +66,121 @@ pub fn setup(mut commands: Commands, settings: Res<GameSettings>, asset_server:
flex_grow: 1.0,
flex_direction: FlexDirection::Row,
align_items: AlignItems::End,
justify_content: JustifyContent::SpaceBetween,
..default()
},
..default()
},
OnGameScreen,
))
.with_children(|parent| {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyA),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Position (A)",
TextStyle {
font: font.clone(),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
.with_children(|parent| buttons(parent, font));
});
}

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyS),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Sound (S)",
TextStyle {
font: font.clone(),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
fn game_info(parent: &mut ChildBuilder, settings: Res<GameSettings>, font: Handle<Font>) {
parent.spawn(TextBundle::from_section(
format!("{}-Back", settings.n),
TextStyle {
font: font.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
parent.spawn((
TextBundle::from_section(
"",
TextStyle {
font: font.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
),
CurrentRoundText,
));
}

fn buttons(parent: &mut ChildBuilder, font: Handle<Font>) {
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyA),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Position (A)",
TextStyle {
font: font.clone(),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyD),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Color (D)",
TextStyle {
font: font.clone(),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
});
parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyS),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Sound (S)",
TextStyle {
font: font.clone(),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});

parent
.spawn(GameButtonBundle {
button: ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(3.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
border_color: button::BUTTON_BORDER_COLOR.into(),
background_color: button::NORMAL_BUTTON.into(),
..default()
},
shortcut: Shortcut(KeyCode::KeyD),
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Color (D)",
TextStyle {
font: font.clone(),
font_size: 20.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
}

0 comments on commit cfd179e

Please sign in to comment.