@@ -23,11 +23,9 @@ static void file_log(std::string str) {
2323#ifdef GHOST_GUI
2424# include < QVector>
2525# define GHOST_LOG (x ) (file_log(x), emit this ->OnNewEvent (QString::fromStdString(x)))
26- # define UI_EVENT (x ) (emit this ->UIEvent (x))
2726#else
2827# include < stdio.h>
2928# define GHOST_LOG (x ) (file_log(x), printf(" [LOG] %s\n " , std::string(x).c_str()))
30- # define UI_EVENT (x ) ((void )0 )
3129#endif
3230
3331#define HEARTBEAT_RATE 5000
@@ -235,7 +233,6 @@ void NetworkManager::DisconnectPlayer(Client& c, const char *reason)
235233 if (toErase != -1 ) {
236234 this ->clients .erase (this ->clients .begin () + toErase);
237235 UI_EVENT (" client_change" );
238- OnConnectedClientsChanged ();
239236 }
240237}
241238
@@ -373,11 +370,11 @@ void NetworkManager::CheckConnection()
373370 c.tcpSocket ->send (packet_notify_all);
374371 }
375372
376- UI_EVENT (" client_change" );
377373 GHOST_LOG (" Connection: " + client.name + " (" + (client.spectator ? " spectator" : " player" ) + " ) @ " + client.IP .toString () + " :" + std::to_string (client.port ));
378- OnConnectedClientsChanged ();
379374
380375 this ->clients .push_back (std::move (client));
376+
377+ UI_EVENT (" client_change" );
381378}
382379
383380void NetworkManager::ReceiveUDPUpdates (std::vector<std::tuple<sf::Packet, sf::IpAddress, unsigned short >>& buffer)
@@ -661,13 +658,18 @@ bool NetworkManager::IsOnWhitelist(std::string name, sf::IpAddress IP) {
661658 return index != whitelist.end ();
662659}
663660
664- void NetworkManager::OnConnectedClientsChanged () {
665- for (auto const & [id, callback] : connectedClientsChangedCallbacks) {
666- callback ();
661+
662+ void NetworkManager::UI_EVENT (std::string event) {
663+ for (auto item : uiEventCallbacks) {
664+ auto [type, callback] = item.second ;
665+ if (type == event) callback ();
667666 }
667+ #ifdef GHOST_GUI
668+ emit this ->UIEvent (event);
669+ #endif
668670}
669671
670- int NetworkManager::RegisterConnectedClientsChangedCallback ( std::function<void ()> callback) {
671- connectedClientsChangedCallbacks .insert ({connectedClientsChangedCallbackId ++, callback});
672- return connectedClientsChangedCallbackId - 1 ;
673- }
672+ int NetworkManager::RegisterEventCallback (std::string type, std::function<void ()> callback) {
673+ uiEventCallbacks .insert ({uiEventCallbackId ++, {type, callback} });
674+ return uiEventCallbackId - 1 ;
675+ }
0 commit comments