DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 5409a8d8c9754cee5562f63c70d252c9a62095b4 deck-builder/client/src/plugins/menu/ui/connect/ui.rs -rw-r--r-- 3.9 KiB
5409a8d8Jonni Liljamo feat(client): handle create game datarequestresponse 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
/*
 * 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::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));
                        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();
            }
        },
    );
}