Implement PopupQueue

This commit is contained in:
Sofia 2026-07-23 19:38:05 +03:00
parent 2f02ab7d5b
commit d2c4bfa9de
4 changed files with 70 additions and 8 deletions

View File

@ -99,8 +99,8 @@ impl GameManager {
if let Some(mut game) = self.game.take() { if let Some(mut game) = self.game.take() {
game.bind_mut().finish_game(); game.bind_mut().finish_game();
game.queue_free(); game.queue_free();
self.go_to_main_menu();
} }
self.go_to_main_menu();
} }
pub fn go_to_finish_screen(&self) { pub fn go_to_finish_screen(&self) {

View File

@ -10,6 +10,7 @@ pub mod map_resource;
pub mod net; pub mod net;
pub mod player; pub mod player;
pub mod player_hand_remover; pub mod player_hand_remover;
pub mod popup_queue;
pub mod soldier_mesh; pub mod soldier_mesh;
pub mod team_resource; pub mod team_resource;
pub mod ui; pub mod ui;

27
rust/src/popup_queue.rs Normal file
View File

@ -0,0 +1,27 @@
use std::collections::VecDeque;
use godot::prelude::*;
pub struct Popup {
pub title: String,
pub message: String,
pub ok: bool,
}
#[derive(GodotClass)]
#[class(base=Node, init, singleton)]
pub struct PopupQueue {
popups: VecDeque<Popup>,
base: Base<Node>,
}
impl PopupQueue {
pub fn queue(&mut self, popup: Popup) {
self.popups.push_back(popup);
}
pub fn poll(&mut self) -> Option<Popup> {
self.popups.pop_front()
}
}

View File

@ -4,7 +4,10 @@ use godot::{
register::property::SimpleVar, register::property::SimpleVar,
}; };
use crate::net::network_manager::NetworkManager; use crate::{
net::network_manager::NetworkManager,
popup_queue::{Popup, PopupQueue},
};
#[derive(Debug, Clone, GodotConvert, Default, Export)] #[derive(Debug, Clone, GodotConvert, Default, Export)]
#[godot(via = u8)] #[godot(via = u8)]
@ -66,19 +69,31 @@ impl INode3D for MainMenu {
.bind_mut() .bind_mut()
.signals() .signals()
.on_client_disconnected() .on_client_disconnected()
.connect_other(self, |s, msg| { .connect_other(self, |_, msg| {
if msg.is_empty() { if msg.is_empty() {
s.show_popup("Disconnected", "disconnected", true); PopupQueue::singleton().bind_mut().queue(Popup {
title: "Disconnected".to_owned(),
message: "disconnected".to_owned(),
ok: true,
});
} else { } else {
s.show_popup("Error", &msg.to_string(), true); PopupQueue::singleton().bind_mut().queue(Popup {
title: "Error".to_owned(),
message: msg.to_string(),
ok: true,
});
} }
}); });
NetworkManager::singleton() NetworkManager::singleton()
.bind_mut() .bind_mut()
.signals() .signals()
.on_error_hosting_server() .on_error_hosting_server()
.connect_other(self, |s, msg| { .connect_other(self, |_, msg| {
s.show_popup("Error Hosting Server", &msg.to_string(), true); PopupQueue::singleton().bind_mut().queue(Popup {
title: "Error hosting server".to_owned(),
message: msg.to_string(),
ok: true,
});
}); });
} }
@ -139,6 +154,16 @@ impl INode3D for MainMenu {
} }
} }
} }
if let Some(popup_panel) = self.popup_panel.clone()
&& let Some(button) = self.popup_button.clone()
{
if !popup_panel.is_visible() || !button.is_visible() {
if let Some(popup) = PopupQueue::singleton().bind_mut().poll() {
self.show_popup(&popup.title, &popup.message, popup.ok);
}
}
}
} }
} }
@ -157,12 +182,21 @@ impl MainMenu {
pub fn join_game(&mut self) { pub fn join_game(&mut self) {
if let Some(addr) = self.join_addr.clone() { if let Some(addr) = self.join_addr.clone() {
self.show_popup("Joining", "joining game", false); self.show_popup("Joining", "joining game", false);
PopupQueue::singleton().bind_mut().queue(Popup {
title: "Joining".to_owned(),
message: "joining game".to_owned(),
ok: false,
});
match addr.get_text().to_string().parse() { match addr.get_text().to_string().parse() {
Ok(addr) => { Ok(addr) => {
NetworkManager::singleton().bind_mut().join(addr); NetworkManager::singleton().bind_mut().join(addr);
} }
Err(err) => { Err(err) => {
self.show_popup("Error", &format!("Error joining: {}", err), true); PopupQueue::singleton().bind_mut().queue(Popup {
title: "Error".to_owned(),
message: format!("Error joining: {}", err),
ok: true,
});
} }
} }
} }