awq<z
Some checks failed
Rust Checks / checks (push) Failing after 8s

This commit is contained in:
Raphaël 2024-02-17 18:00:29 +01:00
parent 6abd0f0c87
commit e8b6f99852
2 changed files with 129 additions and 68 deletions

View file

@ -17,3 +17,14 @@ pub enum CurrentScene {
/// When we play this wonderful game. /// When we play this wonderful game.
Game, Game,
} }
pub fn change_scaling(mut ui_scale: ResMut<UiScale>, window: Query<&Window>) {
// Calculates the ui_scale depending on the size of the main node
let window = window.single();
let (a, b) = (
window.resolution.width() / 1280.,
window.resolution.height() / 720.,
);
ui_scale.0 = if a < b { a } else { b } as f64
}

View file

@ -2,7 +2,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::CurrentScene; use crate::{CurrentScene, change_scaling};
/// The plugin for the menu. /// The plugin for the menu.
pub struct MenuPlugin; pub struct MenuPlugin;
@ -10,52 +11,47 @@ pub struct MenuPlugin;
impl Plugin for MenuPlugin { impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, menu_ui.run_if(in_state(CurrentScene::Menu))); app.add_systems(Startup, menu_ui.run_if(in_state(CurrentScene::Menu)));
app.add_systems(Update, change_scaling);
} }
} }
/// Display the UI of the menu to host a game or join one. /// Display the UI of the menu to host a game or join one.
fn menu_ui(mut commands: Commands) { fn menu_ui(mut commands: Commands) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
// Green
// Main node
commands commands
.spawn(NodeBundle { .spawn(NodeBundle {
style: Style { style: Style {
margin: UiRect { margin: UiRect::all(Val::Auto),
left: (Val::Auto), width: Val::Px(1280.),
right: (Val::Auto), height: Val::Px(720.),
top: (Val::Px(0.)),
bottom: (Val::Px(0.)),
},
width: Val::Percent(55.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
..default() ..default()
}, },
z_index: ZIndex::Local(0),
..default() ..default()
}) })
// Text
.with_children(|parent| { // Spawn Border War Text
parent.spawn( .with_children(|main_node| {
NodeBundle{ main_node.spawn(NodeBundle {
style: Style { style: Style {
margin: UiRect { margin: UiRect {
left: (Val::Auto), left: (Val::Auto),
right: (Val::Auto), right: (Val::Auto),
top: (Val::Percent(5.)), top: (Val::Px(25.)),
bottom: (Val::Percent(15.)), bottom: (Val::Px(25.)),
}, },
width: Val::Percent(90.), width: Val::Px(650.),
height: Val::Percent(25.), height: Val::Px(300.),
..default() ..default()
}, },
background_color: BackgroundColor(Color::RED), background_color: BackgroundColor(Color::RED),
..default() ..default()
} });
); main_node
})
// BLUE
.with_children(|parent| {
parent
.spawn(NodeBundle { .spawn(NodeBundle {
style: Style { style: Style {
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
@ -63,49 +59,103 @@ fn menu_ui(mut commands: Commands) {
left: (Val::Auto), left: (Val::Auto),
right: (Val::Auto), right: (Val::Auto),
top: (Val::Auto), top: (Val::Auto),
bottom: (Val::Percent(5.)), bottom: (Val::Px(25.)),
}, },
width: Val::Percent(85.), width: Val::Px(552.5),
height: Val::Percent(70.), height: Val::Percent(70.),
..default() ..default()
}, },
..default() ..default()
}) })
// YELLOW_GREEN 1
.with_children(|parent| { .with_children(|container| {
parent.spawn(NodeBundle {
style: Style { container
width: Val::Percent(100.), .spawn(NodeBundle {
height: Val::Percent(45.), style: default_style(),
margin: UiRect {
left: (Val::Auto),
right: (Val::Auto),
top: (Val::Auto),
bottom: (Val::Auto),
},
..default() ..default()
},
background_color: BackgroundColor(Color::YELLOW_GREEN),
..default()
});
}) })
// YELLOW_GREEN 2
.with_children(|parent| { .with_children(|host| {
parent.spawn(NodeBundle {
host.spawn(NodeBundle {
style: default_style(),
background_color: BackgroundColor(Color::YELLOW),
..default()
});
host.spawn(NodeBundle {
style: default_style(),
background_color: BackgroundColor(Color::YELLOW),
..default()
});
});
container.spawn(NodeBundle {
style: default_style(),
..default()
})
.with_children(|join| {
join.spawn(NodeBundle {
style: default_style(),
background_color: BackgroundColor(Color::YELLOW),
..default()
});
join.spawn(NodeBundle {
style: default_style(),
background_color: BackgroundColor(Color::YELLOW),
..default()
});
});
});
});
// Spawn Settings button
commands.spawn(NodeBundle {
style: Style { style: Style {
width: Val::Percent(100.), width: Val::Px(75.),
height: Val::Percent(45.), aspect_ratio: Some(1.),
margin: UiRect { margin: UiRect {
left: (Val::Auto), left: Val::Px(25.),
right: (Val::Auto), right: Val::Auto,
top: (Val::Auto), top: Val::Px(25.),
bottom: (Val::Auto), bottom: Val::Auto,
}, },
..default() ..default()
}, },
background_color: BackgroundColor(Color::YELLOW_GREEN), z_index: ZIndex::Local(1),
background_color: BackgroundColor(Color::BLUE),
..default() ..default()
}); });
});
// Spawn Info button
commands.spawn(NodeBundle {
style: Style {
width: Val::Px(75.),
aspect_ratio: Some(1.),
margin: UiRect {
left: Val::Auto,
right: Val::Px(25.),
top: Val::Px(25.),
bottom: Val::Auto,
},
..default()
},
z_index: ZIndex::Local(1),
background_color: BackgroundColor(Color::BLUE),
..default()
}); });
} }
fn default_style() -> Style {
Style {
flex_direction: FlexDirection::Column,
width: Val::Percent(100.),
height: Val::Percent(45.),
margin: UiRect::all(Val::Auto),
..default()
}
}