@@ -8,6 +8,7 @@ use winit::{event::WindowEvent, event_loop::ActiveEventLoop};
8
8
9
9
use crate :: { fsm, globals} ;
10
10
11
+ /// GUI layer for all dialog boxes and the gameplay log output window.
11
12
pub struct Gui {
12
13
egui_glow : EguiGlow ,
13
14
log_messages : String ,
@@ -37,17 +38,20 @@ impl Gui {
37
38
}
38
39
}
39
40
41
+ /// Forward native window events like input to egui.
40
42
pub fn handle_events ( & mut self , window : & winit:: window:: Window , event : & WindowEvent ) {
41
43
let _ = self . egui_glow . on_window_event ( & window, & event) ;
42
44
}
43
45
46
+ /// Execute UI code and populate batch before draw call
44
47
pub fn prepare_frame (
45
48
& mut self ,
46
49
window : & winit:: window:: Window ,
47
50
state_machine : & mut fsm:: StateMachine ,
48
51
) {
49
52
self . egui_glow
50
53
. run ( & window, |ctx| match state_machine. peek ( ) {
54
+ // Starter connection menu
51
55
Some ( fsm:: State :: Menu ) | Some ( fsm:: State :: Connecting { .. } ) => show_menu (
52
56
ctx,
53
57
state_machine,
@@ -56,33 +60,40 @@ impl Gui {
56
60
& mut self . status_text ,
57
61
& mut self . status_color ,
58
62
) ,
63
+ // Gameplay state
59
64
Some ( fsm:: State :: Playing ) => show_log ( ctx, & self . log_messages ) ,
65
+ // Disconnect dialog
60
66
Some ( fsm:: State :: Disconnected ) => show_disconnected_dialog (
61
67
ctx,
62
68
state_machine,
63
69
& mut self . log_messages ,
64
70
& mut self . status_text ,
65
71
& mut self . status_color ,
66
72
) ,
73
+ // Quit confirm dialog
67
74
Some ( fsm:: State :: QuitDialog ) => show_quit_dialog ( ctx, state_machine) ,
68
75
_ => { }
69
76
} ) ;
70
77
}
71
78
79
+ /// Issue batched draw call
72
80
pub fn draw ( & mut self , window : & winit:: window:: Window ) {
73
81
self . egui_glow . paint ( & window) ;
74
82
}
75
83
84
+ /// Redirect message to gameplay log window
76
85
pub fn log ( & mut self , msg : String ) {
77
86
self . log_messages += & format ! ( "{msg}\n " ) ;
78
87
}
79
88
89
+ /// Error status on connection menu and Disconnected message dialog
80
90
pub fn set_error_status ( & mut self , msg : String ) {
81
91
self . status_color = Color32 :: RED ;
82
92
self . status_text = msg;
83
93
}
84
94
}
85
95
96
+ /// Starter connection menu
86
97
fn show_menu (
87
98
ctx : & egui:: Context ,
88
99
state_machine : & mut fsm:: StateMachine ,
@@ -102,20 +113,23 @@ fn show_menu(
102
113
. num_columns ( 2 )
103
114
. spacing ( [ 10.0 , 10.0 ] )
104
115
. show ( ui, |ui| {
116
+ // Server address textbox
105
117
ui. label ( "Server address:" ) ;
106
118
ui. add ( TextEdit :: singleline ( server_hostname) . desired_width ( 150.0 ) ) ;
107
119
ui. end_row ( ) ;
108
120
121
+ // Server port number textbox
109
122
ui. label ( "Port:" ) ;
110
123
ui. add ( TextEdit :: singleline ( server_port) . desired_width ( 150.0 ) ) ;
111
124
ui. end_row ( ) ;
112
125
126
+ // Disable "Connect" button while client is trying to connect
113
127
let connect_buttons_enabled =
114
128
!matches ! ( state_machine. peek( ) , Some ( fsm:: State :: Connecting { .. } ) ) ;
129
+
130
+ // "Create server" button
115
131
let create_button =
116
132
ui. add_enabled ( connect_buttons_enabled, Button :: new ( "Create server" ) ) ;
117
- let join_button =
118
- ui. add_enabled ( connect_buttons_enabled, Button :: new ( "Join server" ) ) ;
119
133
if create_button. clicked ( ) {
120
134
match verify_address_format ( server_hostname, server_port) {
121
135
Ok ( _) => {
@@ -132,6 +146,10 @@ fn show_menu(
132
146
}
133
147
}
134
148
}
149
+
150
+ // "Join server" button
151
+ let join_button =
152
+ ui. add_enabled ( connect_buttons_enabled, Button :: new ( "Join server" ) ) ;
135
153
if join_button. clicked ( ) {
136
154
match verify_address_format ( server_hostname, server_port) {
137
155
Ok ( _) => {
@@ -148,9 +166,12 @@ fn show_menu(
148
166
}
149
167
}
150
168
}
169
+
170
+ // Status label
151
171
ui. colored_label ( * status_color, status_text) ;
152
172
ui. end_row ( ) ;
153
173
174
+ // "Quit" button
154
175
if ui. button ( "Quit" ) . clicked ( ) {
155
176
state_machine. push ( fsm:: State :: QuitDialog ) ;
156
177
}
0 commit comments