DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 03fb82e2b1d3a8b4dd29ebb891c9e7b53f01bf1d deck-builder/client/src/plugins/menu/ui/connect/ui.rs -rw-r--r-- 4.3 KiB
03fb82e2Jonni Liljamo feat(server): handle connect, disconnect and error events 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * 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();
            }
        },
    );
}