/*
* This file is part of laurelin/client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::ecs::{
system::Resource,
system::{Res, ResMut},
};
use bevy_inspector_egui::bevy_egui::{egui, EguiContext};
use laurelin_shared::server::messages::Auth;
use naia_bevy_client::Client;
use crate::{
cfg::CfgDev,
util::egui::{menuwindow, password},
};
#[derive(Default, Resource)]
pub struct ConnectScreenData {
pub state: ConnectState,
pub connecting: bool,
pub username: String,
pub email: String,
pub password: String,
pub password_confirm: String,
pub error: String,
}
#[derive(Default, PartialEq)]
pub enum ConnectState {
#[default]
Login,
LoggingIn,
Register,
Registering,
}
pub fn ui(
mut egui_context: ResMut<EguiContext>,
mut data: ResMut<ConnectScreenData>,
mut client: Client,
cfg_dev: Res<CfgDev>,
) {
menuwindow(
egui_context.ctx_mut(),
"Connect",
&egui::Vec2::new(400., 600.),
|ui| match data.state {
ConnectState::Login => {
ui.horizontal(|ui| {
ui.label("Email:");
ui.text_edit_singleline(&mut data.email)
});
ui.horizontal(|ui| {
ui.label("Password:");
ui.add(password(&mut data.password));
});
if !data.error.is_empty() {
ui.label(egui::RichText::new(&data.error).color(egui::Color32::RED));
}
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
if ui.button("Login").clicked() {
data.error.clear();
data.state = ConnectState::LoggingIn;
client.auth(Auth::new(None, &data.email, &data.password));
client.connect(&cfg_dev.server_address)
}
});
ui.vertical_centered(|ui| {
ui.label("I don't have an account:");
if ui.link("Register").clicked() {
*data = ConnectScreenData::default();
data.state = ConnectState::Register;
}
});
}
ConnectState::Register => {
ui.horizontal(|ui| {
ui.label("Username:");
ui.text_edit_singleline(&mut data.username)
});
ui.horizontal(|ui| {
ui.label("Email:");
ui.text_edit_singleline(&mut data.email)
});
ui.horizontal(|ui| {
ui.label("Password:");
ui.add(password(&mut data.password));
});
ui.horizontal(|ui| {
ui.label("Confirm password:");
ui.add(password(&mut data.password_confirm));
});
if !data.error.is_empty() {
ui.label(egui::RichText::new(&data.error).color(egui::Color32::RED));
}
ui.add_enabled_ui(data.password == data.password_confirm, |ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
if ui.button("Register").clicked() {
data.error.clear();
data.state = ConnectState::Registering;
client.auth(Auth::new(
Some(data.username.clone()),
&data.email,
&data.password,
));
client.connect(&cfg_dev.server_address)
}
});
});
ui.vertical_centered(|ui| {
ui.label("I have an account:");
if ui.link("Login").clicked() {
*data = ConnectScreenData::default();
data.state = ConnectState::Login;
}
});
}
_ => {
ui.spinner();
}
},
);
}