Skip to content

Commit 00d7cdb

Browse files
committed
feat: in-game overlay connect and reconnect, smaller ui pieces
1 parent e7d7d6d commit 00d7cdb

File tree

1 file changed

+121
-50
lines changed

1 file changed

+121
-50
lines changed

src/overlay/mod.rs

Lines changed: 121 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use pocket_relay_client_shared::{
66
api::{lookup_server, LookupData},
77
ctx::ClientContext,
88
reqwest::Client,
9+
servers::{has_server_tasks, stop_server_tasks},
910
};
1011
use tokio::{runtime::Runtime, task::AbortHandle};
1112

@@ -19,6 +20,8 @@ pub struct OverlayRenderLoop {
1920

2021
initial_startup_screen: InitialStartupScreen,
2122

23+
connection_state: Arc<Mutex<ConnectionState>>,
24+
2225
/// Http client for sending requests
2326
http_client: Client,
2427

@@ -33,8 +36,8 @@ impl OverlayRenderLoop {
3336
remember_url: config.is_some(),
3437
target_url: config.map(|value| value.connection_url).unwrap_or_default(),
3538
connect_task: None,
36-
connection_state: Default::default(),
3739
},
40+
connection_state: Default::default(),
3841
http_client,
3942
runtime,
4043
}
@@ -45,15 +48,30 @@ impl ImguiRenderLoop for OverlayRenderLoop {
4548
fn render(&mut self, ui: &mut imgui::Ui) {
4649
match self.screen {
4750
OverlayScreen::InitialStartup => render_startup_screen(self, ui),
48-
OverlayScreen::Game => {}
51+
OverlayScreen::Game => {
52+
let io = ui.io();
53+
54+
if io.key_ctrl && io.key_shift && io.keys_down[imgui::Key::P as usize] {
55+
self.screen = OverlayScreen::GameOverlay;
56+
}
57+
}
58+
OverlayScreen::GameOverlay => {
59+
let io = ui.io();
60+
61+
if io.keys_down[imgui::Key::Escape as usize] {
62+
self.screen = OverlayScreen::Game;
63+
}
64+
65+
render_game_overlay(self, ui)
66+
}
4967
}
5068
}
5169

5270
fn message_filter(&self, io: &imgui::Io) -> MessageFilter {
5371
let mut filter = MessageFilter::empty();
5472

5573
match &self.screen {
56-
OverlayScreen::InitialStartup => {
74+
OverlayScreen::InitialStartup | OverlayScreen::GameOverlay => {
5775
if io.want_capture_mouse {
5876
filter |= MessageFilter::InputMouse;
5977
filter |= MessageFilter::InputKeyboard;
@@ -77,6 +95,9 @@ pub enum OverlayScreen {
7795

7896
/// User is playing the game, don't show anything
7997
Game,
98+
99+
/// User has opened the game overlay manually
100+
GameOverlay,
80101
}
81102

82103
pub struct InitialStartupScreen {
@@ -89,8 +110,6 @@ pub struct InitialStartupScreen {
89110

90111
/// Background task for connecting
91112
connect_task: Option<AbortHandle>,
92-
93-
connection_state: Arc<Mutex<ConnectionState>>,
94113
}
95114

96115
#[derive(Default)]
@@ -118,6 +137,38 @@ fn overlay_window(ui: &mut imgui::Ui, display_size: [f32; 2]) {
118137
.build(|| {});
119138
}
120139

140+
pub fn render_game_overlay(parent: &mut OverlayRenderLoop, ui: &mut imgui::Ui) {
141+
let display_size = ui.io().display_size;
142+
overlay_window(ui, display_size);
143+
144+
ui.window("Pocket Relay")
145+
.resizable(false)
146+
.size([450.0, 350.0], imgui::Condition::Always)
147+
.build(|| {
148+
let is_connected;
149+
let allowed_connect;
150+
151+
{
152+
let state = &*parent.connection_state.lock();
153+
allowed_connect = !matches!(state, ConnectionState::Connecting);
154+
is_connected = matches!(state, ConnectionState::Connected(_));
155+
status_text(ui, state);
156+
}
157+
158+
if is_connected {
159+
let disconnect_pressed = ui.button("Disconnect");
160+
if disconnect_pressed {
161+
on_click_disconnect(parent);
162+
}
163+
} else {
164+
let connect_pressed = connect_button(ui, allowed_connect);
165+
if connect_pressed {
166+
on_click_connect(parent);
167+
}
168+
}
169+
});
170+
}
171+
121172
pub fn render_startup_screen(parent: &mut OverlayRenderLoop, ui: &mut imgui::Ui) {
122173
let display_size = ui.io().display_size;
123174

@@ -130,7 +181,7 @@ pub fn render_startup_screen(parent: &mut OverlayRenderLoop, ui: &mut imgui::Ui)
130181
];
131182

132183
let window = ui
133-
.window("Pocket Relay")
184+
.window("Pocket Relay Introduction")
134185
.no_decoration()
135186
.title_bar(false)
136187
.movable(false)
@@ -154,53 +205,15 @@ pub fn render_startup_screen(parent: &mut OverlayRenderLoop, ui: &mut imgui::Ui)
154205

155206
ui.text_wrapped("If you don't want to connect to a Pocket Relay server press 'Cancel'");
156207

157-
let mut allowed_connect = true;
208+
let allowed_connect;
158209

159210
{
160-
let state = &*parent.initial_startup_screen.connection_state.lock();
161-
match state {
162-
ConnectionState::Initial => {}
163-
ConnectionState::Connecting => {
164-
allowed_connect = false;
165-
ui.text("Connecting...")
166-
}
167-
ConnectionState::Connected(data) => {
168-
ui.text("Connected:");
169-
ui.same_line();
170-
ui.text_wrapped(data.url.as_str());
171-
ui.same_line();
172-
ui.text_wrapped(" version ");
173-
ui.same_line();
174-
ui.text_wrapped(data.version.to_string());
175-
}
176-
ConnectionState::Error(error) => {
177-
ui.text_wrapped("Failed to connect");
178-
ui.same_line();
179-
ui.text_wrapped(error);
180-
}
181-
}
211+
let state = &*parent.connection_state.lock();
212+
allowed_connect = !matches!(state, ConnectionState::Connecting);
213+
status_text(ui, state);
182214
};
183215

184-
let connect_pressed = {
185-
let (button_color, button_hovered_color, button_active_color) = if allowed_connect {
186-
(
187-
[0.2, 0.5, 1.0, 1.0],
188-
[0.3, 0.6, 1.0, 1.0],
189-
[0.1, 0.4, 0.9, 1.0],
190-
)
191-
} else {
192-
(
193-
[0.3, 0.3, 0.3, 1.0],
194-
[0.3, 0.3, 0.3, 1.0],
195-
[0.3, 0.3, 0.3, 1.0],
196-
)
197-
};
198-
199-
let _bc = ui.push_style_color(StyleColor::Button, button_color);
200-
let _bhc = ui.push_style_color(StyleColor::ButtonHovered, button_hovered_color);
201-
let _bac = ui.push_style_color(StyleColor::ButtonActive, button_active_color);
202-
ui.button("Connect")
203-
};
216+
let connect_pressed = connect_button(ui, allowed_connect);
204217

205218
ui.same_line();
206219

@@ -216,6 +229,50 @@ pub fn render_startup_screen(parent: &mut OverlayRenderLoop, ui: &mut imgui::Ui)
216229
}
217230
}
218231

232+
const DISABLED_BUTTON_COLOR: [f32; 4] = [0.3, 0.3, 0.3, 1.0];
233+
234+
fn status_text(ui: &imgui::Ui, state: &ConnectionState) {
235+
match state {
236+
ConnectionState::Initial => ui.text("Not connected."),
237+
ConnectionState::Connecting => ui.text("Connecting..."),
238+
ConnectionState::Connected(data) => {
239+
ui.text("Connected:");
240+
ui.same_line();
241+
ui.text_wrapped(data.url.as_str());
242+
ui.same_line();
243+
ui.text_wrapped(" version ");
244+
ui.same_line();
245+
ui.text_wrapped(data.version.to_string());
246+
}
247+
ConnectionState::Error(error) => {
248+
ui.text_wrapped("Failed to connect");
249+
ui.same_line();
250+
ui.text_wrapped(error);
251+
}
252+
}
253+
}
254+
255+
fn connect_button(ui: &imgui::Ui, allowed_connect: bool) -> bool {
256+
let (button_color, button_hovered_color, button_active_color) = if allowed_connect {
257+
(
258+
[0.2, 0.5, 1.0, 1.0],
259+
[0.3, 0.6, 1.0, 1.0],
260+
[0.1, 0.4, 0.9, 1.0],
261+
)
262+
} else {
263+
(
264+
DISABLED_BUTTON_COLOR,
265+
DISABLED_BUTTON_COLOR,
266+
DISABLED_BUTTON_COLOR,
267+
)
268+
};
269+
270+
let _bc = ui.push_style_color(StyleColor::Button, button_color);
271+
let _bhc = ui.push_style_color(StyleColor::ButtonHovered, button_hovered_color);
272+
let _bac = ui.push_style_color(StyleColor::ButtonActive, button_active_color);
273+
ui.button("Connect")
274+
}
275+
219276
fn on_click_cancel(parent: &mut OverlayRenderLoop) {
220277
parent.screen = OverlayScreen::Game;
221278
}
@@ -226,7 +283,7 @@ fn on_click_connect(parent: &mut OverlayRenderLoop) {
226283
abort_handle.abort();
227284
}
228285

229-
let state = parent.initial_startup_screen.connection_state.clone();
286+
let state = parent.connection_state.clone();
230287
let url = parent.initial_startup_screen.target_url.clone();
231288
let http_client = parent.http_client.clone();
232289
let remember = parent.initial_startup_screen.remember_url;
@@ -274,4 +331,18 @@ fn on_click_connect(parent: &mut OverlayRenderLoop) {
274331
parent.initial_startup_screen.connect_task = Some(abort_handle);
275332
}
276333

334+
fn on_click_disconnect(parent: &mut OverlayRenderLoop) {
335+
// Abort existing task
336+
if let Some(abort_handle) = parent.initial_startup_screen.connect_task.take() {
337+
abort_handle.abort();
338+
}
339+
340+
// Handle disconnecting
341+
if has_server_tasks() {
342+
stop_server_tasks();
343+
}
344+
345+
*parent.connection_state.lock() = ConnectionState::Initial;
346+
}
347+
277348
pub struct GameScreen;

0 commit comments

Comments
 (0)