Add the main menu #31

Merged
CoCo_Sol merged 25 commits from main-menu into main 2024-02-09 23:42:37 +00:00
Showing only changes of commit 9796b4f461 - Show all commits

View file

@ -3,81 +3,44 @@
use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPlugin};
/// A struc to store the address and if the game is being hosted or joined.
#[derive(Default, Resource)]
struct Address {
/// If the game is being hosted or joined.
/// True => Host, False => Join
host: bool,
/// The IP of the game.
/// The string you will share to connect to the game.
ip: String,
}
/// The plugin for the menu.
pub struct MenuPlugin;
/// Implement a trait in order to transform the struc into a plugin.
impl Plugin for MenuPlugin {
/// The fonction that been called when the plugin is added to the app.
/// A function that is called when the plugin is added to the application.
fn build(&self, app: &mut App) {
app.add_plugins(EguiPlugin)
.init_resource::<Address>()
.add_systems(Update, ui_connect_button)
.add_systems(Update, ui_host_button);
}
}
/// The creation of the connection window.
/// example:
/// ```
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugins(EguiPlugin)
/// .add_systems(Update, ui_connect_button);
/// ```
fn ui_connect_button(mut address: ResMut<Address>, mut egui_ctx: EguiContexts) {
// Create the window
/// Display a connect window, with a button to join a game and an input to enter the address.
fn ui_connect_button(mut ctx: EguiContexts, mut connection_string: Local<String>) {
egui::Window::new("Connect")
.default_width(400.0)
.show(egui_ctx.ctx_mut(), |ui| {
// Create the text
.show(ctx.ctx_mut(), |ui| {
ui.heading("Please enter the password of the game: ");
ui.horizontal(|ui| {
ui.label("Address: ");
// Create the input
ui.text_edit_singleline(&mut address.ip);
ui.text_edit_singleline(&mut *connection_string);
// Create the join button
let button = ui.button("Join");
if button.clicked() {
address.host = false;
// TODO: Join the game
if ui.button("Join").clicked() {
println!("clicked");
}
});
});
}
CoCo_Sol marked this conversation as resolved Outdated

This example does not use the function.

This example does not use the function.
/// The creation of the host window.
/// example:
/// ```
/// App::new()
/// .add_plugins(DefaultPlugins)
/// .add_plugins(EguiPlugin)
/// .add_systems(Update, ui_host_button);
fn ui_host_button(mut egui_ctx: EguiContexts, mut address: ResMut<Address>) {
// Create the window
/// Display an host window, with a button to create a new game.
fn ui_host_button(mut ctx: EguiContexts) {
CoCo_Sol marked this conversation as resolved Outdated

You should use a local resource instead.

You should use a local resource instead.
egui::Window::new("Host")
.default_width(400.0)
.show(egui_ctx.ctx_mut(), |ui| {
// Create a button to create a new game
let button = ui.button("Create new game");
// Create the host button
if button.clicked() {
.show(ctx.ctx_mut(), |ui| {
if ui.button("Create new game").clicked() {
println!("clicked");
address.host = true;
}
});
}