generated from tipragot/rust
Add the main menu #31
|
@ -3,81 +3,44 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
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.
|
/// The plugin for the menu.
|
||||||
pub struct MenuPlugin;
|
pub struct MenuPlugin;
|
||||||
|
|
||||||
/// Implement a trait in order to transform the struc into a plugin.
|
/// Implement a trait in order to transform the struc into a plugin.
|
||||||
impl Plugin for MenuPlugin {
|
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) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_plugins(EguiPlugin)
|
app.add_plugins(EguiPlugin)
|
||||||
.init_resource::<Address>()
|
|
||||||
.add_systems(Update, ui_connect_button)
|
.add_systems(Update, ui_connect_button)
|
||||||
.add_systems(Update, ui_host_button);
|
.add_systems(Update, ui_host_button);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// The creation of the connection window.
|
/// Display a connect window, with a button to join a game and an input to enter the address.
|
||||||
/// example:
|
fn ui_connect_button(mut ctx: EguiContexts, mut connection_string: Local<String>) {
|
||||||
/// ```
|
|
||||||
/// 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
|
|
||||||
egui::Window::new("Connect")
|
egui::Window::new("Connect")
|
||||||
.default_width(400.0)
|
.default_width(400.0)
|
||||||
.show(egui_ctx.ctx_mut(), |ui| {
|
.show(ctx.ctx_mut(), |ui| {
|
||||||
// Create the text
|
|
||||||
ui.heading("Please enter the password of the game: ");
|
ui.heading("Please enter the password of the game: ");
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.label("Address: ");
|
ui.label("Address: ");
|
||||||
|
|
||||||
// Create the input
|
ui.text_edit_singleline(&mut *connection_string);
|
||||||
ui.text_edit_singleline(&mut address.ip);
|
|
||||||
|
|
||||||
// Create the join button
|
if ui.button("Join").clicked() {
|
||||||
let button = ui.button("Join");
|
println!("clicked");
|
||||||
if button.clicked() {
|
|
||||||
address.host = false;
|
|
||||||
// TODO: Join the game
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
/// The creation of the host window.
|
/// Display an host window, with a button to create a new game.
|
||||||
/// example:
|
fn ui_host_button(mut ctx: EguiContexts) {
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
You should use a local resource instead. You should use a local resource instead.
|
|||||||
/// ```
|
|
||||||
/// 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
|
|
||||||
egui::Window::new("Host")
|
egui::Window::new("Host")
|
||||||
.default_width(400.0)
|
.default_width(400.0)
|
||||||
.show(egui_ctx.ctx_mut(), |ui| {
|
.show(ctx.ctx_mut(), |ui| {
|
||||||
// Create a button to create a new game
|
if ui.button("Create new game").clicked() {
|
||||||
let button = ui.button("Create new game");
|
|
||||||
|
|
||||||
// Create the host button
|
|
||||||
if button.clicked() {
|
|
||||||
println!("clicked");
|
println!("clicked");
|
||||||
address.host = true;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue
This example does not use the function.