/*
* 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::{Res, ResMut};
use bevy_egui::{egui, EguiContexts};
use laurelin_shared::server::messages::Auth;
use naia_bevy_client::{transport::webrtc, Client};
use crate::{
cfg::CfgDev,
util::egui::{menuwindow, password},
};
use super::{ConnectScreenData, ConnectState};
pub fn ui(
mut egui_contexts: EguiContexts,
mut data: ResMut<ConnectScreenData>,
mut client: Client,
cfg_dev: Res<CfgDev>,
) {
menuwindow(
egui_contexts.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));
connect(&mut client, &cfg_dev);
}
});
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,
));
connect(&mut client, &cfg_dev);
}
});
});
ui.vertical_centered(|ui| {
ui.label("I have an account:");
if ui.link("Login").clicked() {
*data = ConnectScreenData::default();
data.state = ConnectState::Login;
}
});
}
_ => {
ui.spinner();
}
},
);
}
fn connect(client: &mut Client, cfg_dev: &CfgDev) {
let socket = webrtc::Socket::new(&cfg_dev.server_address, client.socket_config());
client.connect(socket);
}