Skip to content

Commit

Permalink
RMG: make Netplay Session dialog stay open while in-game
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Jan 21, 2025
1 parent 26b35eb commit c0ebf11
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
19 changes: 16 additions & 3 deletions Source/RMG/UserInterface/Dialog/Netplay/NetplaySessionDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ NetplaySessionDialog::NetplaySessionDialog(QWidget *parent, QWebSocket* webSocke

NetplaySessionDialog::~NetplaySessionDialog(void)
{
if (this->webSocket->isValid())
{
this->webSocket->close();
}
}

void NetplaySessionDialog::on_webSocket_textMessageReceived(QString message)
Expand Down Expand Up @@ -106,7 +110,6 @@ void NetplaySessionDialog::on_webSocket_textMessageReceived(QString message)
else if (type == "reply_begin_game")
{
emit OnPlayGame(this->sessionFile, this->webSocket->peerAddress().toString(), this->sessionPort, this->sessionNumber);
QDialog::accept();
}
else if (type == "reply_motd")
{
Expand All @@ -133,7 +136,7 @@ void NetplaySessionDialog::on_sendPushButton_clicked(void)
json.insert("room", session);
NetplayCommon::AddCommonJson(json);

webSocket->sendTextMessage(QJsonDocument(json).toJson());
this->webSocket->sendTextMessage(QJsonDocument(json).toJson());
this->chatLineEdit->clear();
}

Expand All @@ -149,5 +152,15 @@ void NetplaySessionDialog::accept()
json.insert("room", session);
NetplayCommon::AddCommonJson(json);

webSocket->sendTextMessage(QJsonDocument(json).toJson());
this->webSocket->sendTextMessage(QJsonDocument(json).toJson());
}

void NetplaySessionDialog::reject(void)
{
if (this->webSocket->isValid())
{
this->webSocket->close();
}

QDialog::reject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class NetplaySessionDialog : public QDialog, private Ui::NetplaySessionDialog
void on_sendPushButton_clicked(void);

void accept(void) Q_DECL_OVERRIDE;
void reject(void) Q_DECL_OVERRIDE;

signals:
void OnPlayGame(QString file, QString address, int port, int player);
Expand Down
66 changes: 54 additions & 12 deletions Source/RMG/UserInterface/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,17 @@ void MainWindow::launchEmulationThread(QString cartRom, QString address, int por
}

this->emulationThread->SetNetplay(address, port, player);
this->launchEmulationThread(cartRom);
this->launchEmulationThread(cartRom, "", false, -1, true);
}

void MainWindow::launchEmulationThread(QString cartRom, QString diskRom, bool refreshRomListAfterEmulation, int slot)
void MainWindow::launchEmulationThread(QString cartRom, QString diskRom, bool refreshRomListAfterEmulation, int slot, bool netplay)
{
if (this->netplaySessionDialog != nullptr && !netplay)
{
this->showErrorMessage("EmulationThread::run Failed", "Cannot start emulation when netplay session is active");
return;
}

CoreSettingsSave();

if (this->emulationThread->isRunning())
Expand Down Expand Up @@ -795,8 +801,8 @@ void MainWindow::updateActions(bool inEmulation, bool isPaused)
this->action_View_Log->setShortcut(QKeySequence(keyBinding));
this->action_View_ClearRomCache->setEnabled(!inEmulation);

this->action_Netplay_CreateSession->setEnabled(!inEmulation);
this->action_Netplay_JoinSession->setEnabled(!inEmulation);
this->action_Netplay_CreateSession->setEnabled(!inEmulation && this->netplaySessionDialog == nullptr);
this->action_Netplay_JoinSession->setEnabled(!inEmulation && this->netplaySessionDialog == nullptr);

keyBinding = QString::fromStdString(CoreSettingsGetStringValue(SettingsID::KeyBinding_IncreaseVolume));
this->action_Audio_IncreaseVolume->setShortcut(QKeySequence(keyBinding));
Expand Down Expand Up @@ -1179,6 +1185,25 @@ void MainWindow::checkForUpdates(bool silent, bool force)
}
#endif // UPDATER

#ifdef NETPLAY
void MainWindow::showNetplaySessionBrowser(QWebSocket* webSocket, QJsonObject json, QString sessionFile)
{
if (this->netplaySessionDialog != nullptr)
{
this->netplaySessionDialog->deleteLater();
this->netplaySessionDialog = nullptr;
}

this->netplaySessionDialog = new Dialog::NetplaySessionDialog(this, webSocket, json, sessionFile);
connect(this->netplaySessionDialog, &Dialog::NetplaySessionDialog::OnPlayGame, this, &MainWindow::on_Netplay_PlayGame);
connect(this->netplaySessionDialog, &Dialog::NetplaySessionDialog::rejected, this, &MainWindow::on_NetplaySessionBrowser_rejected);
this->netplaySessionDialog->show();

// force refresh of actions
this->updateActions(false, false);
}
#endif // NETPLAY

void MainWindow::timerEvent(QTimerEvent *event)
{
int timerId = event->timerId();
Expand Down Expand Up @@ -1891,31 +1916,27 @@ void MainWindow::on_Action_View_Log(void)
void MainWindow::on_Action_Netplay_CreateSession(void)
{
#ifdef NETPLAY
QWebSocket webSocket;
static QWebSocket webSocket;

Dialog::CreateNetplaySessionDialog dialog(this, &webSocket, this->ui_Widget_RomBrowser->GetModelData());
int ret = dialog.exec();
if (ret == QDialog::Accepted)
{
Dialog::NetplaySessionDialog sessionDialog(this, &webSocket, dialog.GetSessionJson(), dialog.GetSessionFile());
connect(&sessionDialog, &Dialog::NetplaySessionDialog::OnPlayGame, this, &MainWindow::on_Netplay_PlayGame);
sessionDialog.exec();
this->showNetplaySessionBrowser(&webSocket, dialog.GetSessionJson(), dialog.GetSessionFile());
}
#endif // NETPLAY
}

void MainWindow::on_Action_Netplay_JoinSession(void)
{
#ifdef NETPLAY
QWebSocket webSocket;
static QWebSocket webSocket;

Dialog::NetplaySessionBrowserDialog dialog(this, &webSocket, this->ui_Widget_RomBrowser->GetModelData());
int ret = dialog.exec();
if (ret == QDialog::Accepted)
{
Dialog::NetplaySessionDialog sessionDialog(this, &webSocket, dialog.GetSessionJson(), dialog.GetSessionFile());
connect(&sessionDialog, &Dialog::NetplaySessionDialog::OnPlayGame, this, &MainWindow::on_Netplay_PlayGame);
sessionDialog.exec();
this->showNetplaySessionBrowser(&webSocket, dialog.GetSessionJson(), dialog.GetSessionFile());
}
#endif // NETPLAY
}
Expand Down Expand Up @@ -1977,6 +1998,12 @@ void MainWindow::on_Emulation_Finished(bool ret)
this->ui_NoSwitchToRomBrowser = false;
}

if (this->netplaySessionDialog != nullptr)
{
this->netplaySessionDialog->deleteLater();
this->netplaySessionDialog = nullptr;
}

if (!this->ui_QuitAfterEmulation &&
!this->ui_NoSwitchToRomBrowser &&
this->ui_RefreshRomListAfterEmulation)
Expand Down Expand Up @@ -2204,6 +2231,21 @@ void MainWindow::on_Netplay_PlayGame(QString file, QString address, int port, in
this->launchEmulationThread(file, address, port, player);
}

void MainWindow::on_NetplaySessionBrowser_rejected()
{
bool isRunning = CoreIsEmulationRunning();
bool isPaused = CoreIsEmulationPaused();

if (this->netplaySessionDialog != nullptr)
{
this->netplaySessionDialog->deleteLater();
this->netplaySessionDialog = nullptr;
}

// force refresh of actions
this->updateActions(isRunning, isPaused);
}

void MainWindow::on_VidExt_Init(VidExtRenderMode renderMode)
{
this->ui_VidExtRenderMode = renderMode;
Expand Down
13 changes: 12 additions & 1 deletion Source/RMG/UserInterface/MainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "Widget/Render/OGLWidget.hpp"
#include "Widget/Render/VKWidget.hpp"

#ifdef NETPLAY
#include "Dialog/Netplay/NetplaySessionDialog.hpp"
#endif // NETPLAY
#include "Dialog/LogDialog.hpp"

#ifdef UPDATER
Expand Down Expand Up @@ -114,6 +117,9 @@ class MainWindow : public QMainWindow, private Ui::MainWindow
QString ui_WindowTitle;

Dialog::LogDialog logDialog;
#ifdef NETPLAY
Dialog::NetplaySessionDialog* netplaySessionDialog = nullptr;
#endif // NETPLAY

void closeEvent(QCloseEvent *) Q_DECL_OVERRIDE;

Expand All @@ -132,7 +138,7 @@ class MainWindow : public QMainWindow, private Ui::MainWindow
void initializeEmulationThread(void);
void connectEmulationThreadSignals(void);
void launchEmulationThread(QString cartRom, QString address, int port, int player);
void launchEmulationThread(QString cartRom, QString diskRom = "", bool refreshRomListAfterEmulation = false, int slot = -1);
void launchEmulationThread(QString cartRom, QString diskRom = "", bool refreshRomListAfterEmulation = false, int slot = -1, bool netplay = false);

QString getSaveStateSlotDateTimeText(QAction* action);
QString getSaveStateSlotText(QAction* action, int slot);
Expand All @@ -153,6 +159,10 @@ class MainWindow : public QMainWindow, private Ui::MainWindow
#ifdef UPDATER
void checkForUpdates(bool silent, bool force);
#endif // UPDATER

#ifdef NETPLAY
void showNetplaySessionBrowser(QWebSocket* webSocket, QJsonObject json, QString sessionFile);
#endif // NETPLAY
protected:
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;

Expand Down Expand Up @@ -225,6 +235,7 @@ class MainWindow : public QMainWindow, private Ui::MainWindow
void on_RomBrowser_Cheats(QString file);

void on_Netplay_PlayGame(QString file, QString address, int port, int player);
void on_NetplaySessionBrowser_rejected(void);

public slots:

Expand Down

0 comments on commit c0ebf11

Please sign in to comment.