File indexing completed on 2024-09-15 06:37:37
0001 /* 0002 SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "simplemenu.h" 0008 0009 #include <QTcpSocket> 0010 #include <QIcon> 0011 0012 #include <KLocalizedString> 0013 0014 #include "button.h" 0015 #include "chatwidget.h" 0016 #include "controller.h" 0017 #include "networkdialog.h" 0018 #include "playerentity.h" 0019 #include "protocol.h" 0020 #include "networkentity.h" 0021 #include "seaview.h" 0022 #include "settings.h" 0023 #include "aientity.h" 0024 #include "welcomescreen.h" 0025 0026 const char* SimpleMenu::iconLocal = "user-identity"; 0027 const char* SimpleMenu::iconServer = "network-server"; 0028 const char* SimpleMenu::iconClient = "network-connect"; 0029 0030 SimpleMenu::SimpleMenu(QWidget* parent, WelcomeScreen* screen) 0031 : QObject(parent) 0032 , m_screen(screen) 0033 , m_protocol(nullptr) 0034 , m_state(READY) 0035 , m_player1(nullptr) 0036 , m_player2(nullptr) 0037 { 0038 if (m_screen) { 0039 // create buttons 0040 m_local_game_btn = m_screen->addButton(0, 0, QIcon::fromTheme( QLatin1String( iconLocal) ), i18n("Single Player" )); 0041 m_server_btn = m_screen->addButton(0, 1, QIcon::fromTheme( QLatin1String( iconServer) ), i18n("Host Network Game" )); 0042 m_client_btn = m_screen->addButton(0, 2, QIcon::fromTheme( QLatin1String( iconClient) ), i18n("Connect to Network Game" )); 0043 0044 // create connections 0045 connect(m_local_game_btn, &Button::clicked, this, &SimpleMenu::localGame); 0046 connect(m_server_btn, &Button::clicked, this, &SimpleMenu::createServer); 0047 connect(m_client_btn, &Button::clicked, this, &SimpleMenu::createClient); 0048 } 0049 } 0050 0051 void SimpleMenu::finalize(State state, const QString& nickname, QTcpSocket* socket) 0052 { 0053 m_state = state; 0054 m_nickname = nickname; 0055 if (socket) { 0056 m_protocol = new Protocol(socket); 0057 m_protocol->setParent(this); 0058 } 0059 else { 0060 m_protocol = nullptr; 0061 } 0062 Q_EMIT done(); 0063 } 0064 0065 void SimpleMenu::localGame() 0066 { 0067 finalize(DONE_LOCAL_GAME, Settings::findNick()); 0068 } 0069 0070 void SimpleMenu::createServer() 0071 { 0072 QWidget* parent_widget = qobject_cast<QWidget*>(parent()); 0073 Q_ASSERT(parent_widget); 0074 QPointer<NetworkDialog> dialog = new NetworkDialog(false, parent_widget); 0075 if (dialog->exec()) { 0076 if (dialog) { 0077 finalize(DONE_SERVER, dialog->nickname(), dialog->socket()); 0078 } 0079 } 0080 delete dialog; 0081 } 0082 0083 void SimpleMenu::createClientWithUrl(const QUrl& url) 0084 { 0085 QWidget* parent_widget = qobject_cast<QWidget*>(parent()); 0086 Q_ASSERT(parent_widget); 0087 QPointer<NetworkDialog> dialog = new NetworkDialog(true, parent_widget,&url); 0088 if (dialog->exec() == QDialog::Accepted) { 0089 if (dialog) { 0090 finalize(DONE_CLIENT, dialog->nickname(), dialog->socket()); 0091 } 0092 } 0093 } 0094 0095 void SimpleMenu::createClient() 0096 { 0097 QWidget* parent_widget = qobject_cast<QWidget*>(parent()); 0098 Q_ASSERT(parent_widget); 0099 QPointer<NetworkDialog> dialog = new NetworkDialog(true, parent_widget); 0100 if (dialog->exec() == QDialog::Accepted) { 0101 if (dialog) { 0102 finalize(DONE_CLIENT, dialog->nickname(), dialog->socket()); 0103 } 0104 } 0105 } 0106 0107 void SimpleMenu::setupController(Controller* controller, Entity* old_opponent, SeaView* sea, 0108 ChatWidget* chat) 0109 { 0110 switch (m_state) { 0111 case DONE_LOCAL_GAME: { 0112 m_player1 = controller->createPlayer(Sea::Player(0), sea, chat, m_nickname); 0113 sea->setStats(Sea::Player(0), QStringLiteral("score_mouse"), m_nickname, m_player1->stats()); 0114 m_player2 = controller->createAI(Sea::Player(1), sea); 0115 sea->setStats(Sea::Player(1), QStringLiteral("score_ai"), 0116 QStringLiteral("Computer"), m_player2->stats()); 0117 chat->hide(); 0118 break; 0119 } 0120 case DONE_SERVER: { 0121 Q_ASSERT(m_protocol); 0122 m_player1 = controller->createPlayer(Sea::Player(0), sea, chat, m_nickname); 0123 sea->setStats(Sea::Player(0), QStringLiteral("score_mouse"), 0124 m_nickname, m_player1->stats()); 0125 m_player2 = controller->createRemotePlayer(Sea::Player(1), sea, m_protocol, false); 0126 if (old_opponent) { 0127 m_player1->setCompatibilityLevel(old_opponent->compatibilityLevel()); 0128 m_player2->setCompatibilityLevel(old_opponent->compatibilityLevel()); 0129 m_player2->setNick(old_opponent->nick()); 0130 } 0131 sea->setStats(Sea::Player(1), QStringLiteral("score_network"), 0132 m_player2->nick().isEmpty() ? i18n("Remote player") : m_player2->nick(), 0133 m_player2->stats()); 0134 chat->bindTo(m_player1); 0135 break; 0136 } 0137 case DONE_CLIENT: { 0138 Q_ASSERT(m_protocol); 0139 m_player1 = controller->createPlayer(Sea::Player(0), sea, chat, m_nickname); 0140 sea->setStats(Sea::Player(0), QStringLiteral("score_mouse"), 0141 m_nickname, m_player1->stats()); 0142 m_player2 = controller->createRemotePlayer(Sea::Player(1), sea, m_protocol, true); 0143 if (old_opponent) { 0144 m_player1->setCompatibilityLevel(old_opponent->compatibilityLevel()); 0145 m_player2->setCompatibilityLevel(old_opponent->compatibilityLevel()); 0146 m_player2->setNick(old_opponent->nick()); 0147 } 0148 sea->setStats(Sea::Player(1), QStringLiteral("score_network"), 0149 m_player2->nick().isEmpty() ? i18n("Remote player") : m_player2->nick(), 0150 m_player2->stats()); 0151 chat->bindTo(m_player1); 0152 break; 0153 } 0154 default: 0155 return; 0156 } 0157 0158 controller->start(sea); 0159 } 0160 0161 #include "moc_simplemenu.cpp"