DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 38e1dbbfe8864ed9083d38b96e226c1033a807d7 deck-builder/sdbclient/src/plugins/menu/play/ui.rs -rw-r--r-- 10.4 KiB
38e1dbbfJonni Liljamo feat(sdbclient): add my_games API endpoint 1 year, 10 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * This file is part of sdbclient
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use bevy::prelude::*;
use bevy_inspector_egui::bevy_egui::{egui, EguiContext};
use iyes_loopless::prelude::*;

use crate::{
    cfg::CfgUser,
    plugins::menu::MenuState,
    runtime::menu::{BrowseMenuTab, PlayMenuUIState, RTDMenu},
};

use super::{AllFormingEvent, CreateGameEvent, JoinGameEvent, PlayMenuState};

pub(super) fn show(
    mut commands: Commands,
    mut egui_context: ResMut<EguiContext>,
    mut rtdmenu: ResMut<RTDMenu>,
    cfg_user: Res<CfgUser>,
    mut creategame_ev_w: EventWriter<CreateGameEvent>,
    mut allforming_ev_w: EventWriter<AllFormingEvent>,
    mut joingame_ev_w: EventWriter<JoinGameEvent>,
) {
    egui::Window::new(egui::RichText::new(rtdmenu.play_menu_ui_state.display()).size(32.))
        .resizable(false)
        .collapsible(false)
        .anchor(egui::Align2::CENTER_CENTER, egui::Vec2::ZERO)
        .fixed_size(egui::Vec2::new(800., 600.))
        .show(egui_context.ctx_mut(), |ui| {
            // Override egui style for this scope.
            let mut egui_style = (*ui.style_mut()).clone();
            egui_style.text_styles = [
                (
                    egui::TextStyle::Heading,
                    egui::FontId::new(30.0, egui::FontFamily::Proportional),
                ),
                (
                    egui::TextStyle::Body,
                    egui::FontId::new(18.0, egui::FontFamily::Proportional),
                ),
                (
                    egui::TextStyle::Monospace,
                    egui::FontId::new(14.0, egui::FontFamily::Proportional),
                ),
                (
                    egui::TextStyle::Button,
                    egui::FontId::new(24.0, egui::FontFamily::Proportional),
                ),
                (
                    egui::TextStyle::Small,
                    egui::FontId::new(10.0, egui::FontFamily::Proportional),
                ),
            ]
            .into();
            ui.set_style(egui_style);

            match rtdmenu.play_menu_ui_state {
                PlayMenuUIState::Main => {
                    egui::SidePanel::left("play_side_panel")
                        .resizable(false)
                        .show_inside(ui, |ui| {
                            ui.vertical_centered(|ui| {
                                if ui.button("Create").clicked() {
                                    rtdmenu.play_menu_ui_state = PlayMenuUIState::CreateGame
                                }

                                if ui.button("Back").clicked() {
                                    commands.insert_resource(NextState(PlayMenuState::None));
                                    commands.insert_resource(NextState(MenuState::Main));
                                }
                            });
                        });

                    egui::TopBottomPanel::top("play_top_panel").resizable(false).show_inside(ui, |ui| {
                        ui.horizontal(|ui| {
                            ui.label(egui::RichText::new("Browse: ").size(24.));

                            ui.selectable_value(&mut rtdmenu.browse_tab, BrowseMenuTab::Forming, "Forming");
                            ui.selectable_value(&mut rtdmenu.browse_tab, BrowseMenuTab::InProgress, "In Progress");
                            ui.selectable_value(&mut rtdmenu.browse_tab, BrowseMenuTab::Finished, "Finished");

                            ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                                if ui.button("Refresh").clicked() {
                                    // refresh the current tab
                                    // oh and also, if the tab content in rtdmenu is empty, should
                                    // fetch when switching to it
                                    match rtdmenu.browse_tab {
                                        BrowseMenuTab::Forming => {
                                            if !rtdmenu.waiting_for_all_forming_call {
                                                allforming_ev_w.send(AllFormingEvent);
                                            }
                                        }
                                        BrowseMenuTab::InProgress => todo!(),
                                        BrowseMenuTab::Finished => todo!(),
                                    }
                                }
                            });
                        });
                    });

                    ui.vertical_centered(|ui| {
                        egui::ScrollArea::vertical().show(ui, |ui| {
                            match rtdmenu.browse_tab {
                                BrowseMenuTab::Forming => {
                                    if rtdmenu.waiting_for_all_forming_call {
                                        ui.horizontal(|ui| {
                                            ui.spinner();
                                            ui.label("loading...");
                                        });
                                    } else {
                                        if rtdmenu.all_forming_games.is_empty() {
                                            ui.label("No forming games found.");
                                        } else {
                                            for game in rtdmenu.all_forming_games.clone() {
                                                egui::Frame::none()
                                                    .fill(egui::Color32::BLACK)
                                                    .rounding(4.)
                                                    .outer_margin(4.)
                                                    .inner_margin(4.)
                                                    .show(ui, |ui| {
                                                        ui.horizontal(|ui| {
                                                            ui.label(format!(
                                                                "Host: {}",
                                                                game.host.username
                                                            ));
                                                            ui.with_layout(
                                                                egui::Layout::right_to_left(
                                                                    egui::Align::Center,
                                                                ),
                                                                |ui| {
                                                                    if game.guest_id == cfg_user.id {
                                                                        ui.add_enabled(false, egui::Button::new("Joined"));
                                                                        if ui.button("Inspect").clicked() {
                                                                            rtdmenu.cur_game = Some(game.clone());
                                                                            rtdmenu.play_menu_ui_state = PlayMenuUIState::InLobbyGuest;
                                                                        }
                                                                    } else if game.host_id == cfg_user.id {
                                                                        ui.add_enabled(false, egui::Button::new("Host"));
                                                                        if ui.button("Inspect").clicked() {
                                                                            rtdmenu.cur_game = Some(game.clone());
                                                                            rtdmenu.play_menu_ui_state = PlayMenuUIState::InLobbyHost;
                                                                        }
                                                                    } else {
                                                                        if ui.button("Join").clicked() {
                                                                            rtdmenu.cur_game = Some(game.clone());
                                                                            joingame_ev_w.send(JoinGameEvent);
                                                                        }
                                                                    }
                                                                },
                                                            );
                                                        });
                                                    });
                                            }
                                        }
                                    }
                                }
                                BrowseMenuTab::InProgress => {

                                }
                                BrowseMenuTab::Finished => {

                                }
                            }
                        });
                    });
                }
                PlayMenuUIState::CreateGame => {
                    ui.vertical_centered(|ui| {
                        ui.add_enabled_ui(!rtdmenu.waiting_for_create_game_call, |ui| {
                            if ui.button("Confirm").clicked() {
                                creategame_ev_w.send(CreateGameEvent);
                            }

                            if ui.button("Cancel").clicked() {
                                rtdmenu.play_menu_ui_state = PlayMenuUIState::Main
                            }
                        });
                    });
                }
                PlayMenuUIState::InLobbyHost => {
                    if ui.button("Start").clicked() {
                        // patch state to "in-progress"
                        todo!();
                    }

                    if ui.button("Back").clicked() {
                        rtdmenu.play_menu_ui_state = PlayMenuUIState::Main
                    }
                }
                PlayMenuUIState::InLobbyGuest => {
                    ui.label(egui::RichText::new(format!("Host: {}", rtdmenu.cur_game.as_ref().unwrap().host.username)));

                    if ui.button("Back").clicked() {
                        rtdmenu.play_menu_ui_state = PlayMenuUIState::Main
                    }
                }
            }
        });
}