From 083e1180fb4c8a45f896e029ea65feb0c1542e18 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 7 Dec 2018 00:54:48 +0800 Subject: [PATCH] Add the GUI to update server config in lobby --- .../online/server_configuration_dialog.stkgui | 44 +++++ sources.cmake | 2 +- src/network/protocols/client_lobby.cpp | 10 +- src/network/protocols/client_lobby.hpp | 2 + .../dialogs/server_configuration_dialog.cpp | 183 ++++++++++++++++++ .../dialogs/server_configuration_dialog.hpp | 117 +++++++++++ .../online/networking_lobby.cpp | 14 +- src/states_screens/race_result_gui.cpp | 1 - 8 files changed, 368 insertions(+), 5 deletions(-) create mode 100644 data/gui/dialogs/online/server_configuration_dialog.stkgui create mode 100644 src/states_screens/dialogs/server_configuration_dialog.cpp create mode 100644 src/states_screens/dialogs/server_configuration_dialog.hpp diff --git a/data/gui/dialogs/online/server_configuration_dialog.stkgui b/data/gui/dialogs/online/server_configuration_dialog.stkgui new file mode 100644 index 000000000..db8adc441 --- /dev/null +++ b/data/gui/dialogs/online/server_configuration_dialog.stkgui @@ -0,0 +1,44 @@ + + +
+
+ + + +
+
diff --git a/sources.cmake b/sources.cmake index d4f28ae4d..ba4868d71 100644 --- a/sources.cmake +++ b/sources.cmake @@ -1,5 +1,5 @@ # Modify this file to change the last-modified date when you add/remove a file. -# This will then trigger a new cmake run automatically. +# This will then trigger a new cmake run automatically. file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp") file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp") file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*") diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index d0cdb6a54..2ddd7c4b4 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -88,6 +88,7 @@ ClientLobby::ClientLobby(const TransportAddress& a, std::shared_ptr s) m_disconnected_msg[PDI_KICK] = _("You were kicked from the server."); m_disconnected_msg[PDI_BAD_CONNECTION] = _("Bad network connection is detected."); + m_first_connect = true; } // ClientLobby //----------------------------------------------------------------------------- @@ -582,6 +583,13 @@ void ClientLobby::handleServerInfo(Event* event) // At least 6 bytes should remain now if (!checkDataSize(event, 6)) return; + if (!m_first_connect) + { + NetworkingLobby::getInstance() + ->addMoreServerInfo(L"--------------------"); + } + m_first_connect = false; + NetworkString &data = event->data(); // Add server info core::stringw str, each_line; @@ -676,8 +684,6 @@ void ClientLobby::updatePlayerList(Event* event) if (m_waiting_for_game && !waiting) { // The waiting game finished - NetworkingLobby::getInstance() - ->addMoreServerInfo(L"--------------------"); SFXManager::get()->quickSound("wee"); } diff --git a/src/network/protocols/client_lobby.hpp b/src/network/protocols/client_lobby.hpp index dff4675f0..6fcb9e447 100644 --- a/src/network/protocols/client_lobby.hpp +++ b/src/network/protocols/client_lobby.hpp @@ -77,6 +77,8 @@ private: bool m_auto_started; + bool m_first_connect; + uint64_t m_auto_back_to_lobby_time; /** The state of the finite state machine. */ diff --git a/src/states_screens/dialogs/server_configuration_dialog.cpp b/src/states_screens/dialogs/server_configuration_dialog.cpp new file mode 100644 index 000000000..b8d7a844e --- /dev/null +++ b/src/states_screens/dialogs/server_configuration_dialog.cpp @@ -0,0 +1,183 @@ +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2018 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "states_screens/dialogs/server_configuration_dialog.hpp" + +#include "config/user_config.hpp" +#include "guiengine/widgets/icon_button_widget.hpp" +#include "guiengine/widgets/label_widget.hpp" +#include "guiengine/widgets/ribbon_widget.hpp" +#include "guiengine/widgets/spinner_widget.hpp" +#include "network/protocols/lobby_protocol.hpp" +#include "network/stk_host.hpp" +#include "states_screens/state_manager.hpp" + +using namespace GUIEngine; + +// ---------------------------------------------------------------------------- +void ServerConfigurationDialog::beforeAddingWidgets() +{ + m_more_options_text = getWidget("more-options"); + assert(m_more_options_text != NULL); + m_more_options_spinner = getWidget("more-options-spinner"); + assert(m_more_options_spinner != NULL); + + m_options_widget = getWidget("options"); + assert(m_options_widget != NULL); + m_game_mode_widget = getWidget("gamemode"); + assert(m_game_mode_widget != NULL); + m_difficulty_widget = getWidget("difficulty"); + assert(m_difficulty_widget != NULL); + m_ok_widget = getWidget("ok"); + assert(m_ok_widget != NULL); + m_cancel_widget = getWidget("cancel"); + assert(m_cancel_widget != NULL); +} // beforeAddingWidgets + +// ---------------------------------------------------------------------------- +void ServerConfigurationDialog::init() +{ + ModalDialog::init(); + + RibbonWidget* difficulty = getWidget("difficulty"); + assert(difficulty != NULL); + difficulty->setSelection((int)race_manager->getDifficulty(), + PLAYER_ID_GAME_MASTER); + + RibbonWidget* gamemode = getWidget("gamemode"); + assert(gamemode != NULL); + gamemode->setSelection(m_prev_mode, PLAYER_ID_GAME_MASTER); + + updateMoreOption(m_prev_mode); + m_options_widget->setFocusForPlayer(PLAYER_ID_GAME_MASTER); + m_options_widget->select("cancel", PLAYER_ID_GAME_MASTER); +} // init + +// ---------------------------------------------------------------------------- +GUIEngine::EventPropagation + ServerConfigurationDialog::processEvent(const std::string& source) +{ + if (source == m_options_widget->m_properties[PROP_ID]) + { + const std::string& selection = + m_options_widget->getSelectionIDString(PLAYER_ID_GAME_MASTER); + if (selection == m_cancel_widget->m_properties[PROP_ID]) + { + m_self_destroy = true; + return GUIEngine::EVENT_BLOCK; + } + else if (selection == m_ok_widget->m_properties[PROP_ID]) + { + m_self_destroy = true; + NetworkString change(PROTOCOL_LOBBY_ROOM); + change.addUInt8(LobbyProtocol::LE_CONFIG_SERVER); + change.addUInt8((uint8_t)m_difficulty_widget + ->getSelection(PLAYER_ID_GAME_MASTER)); + switch (m_game_mode_widget->getSelection(PLAYER_ID_GAME_MASTER)) + { + case 0: + { + change.addUInt8(3).addUInt8(0); + break; + } + case 1: + { + change.addUInt8(4).addUInt8(0); + break; + } + case 2: + { + int v = m_more_options_spinner->getValue(); + if (v == 0) + change.addUInt8(7).addUInt8(0); + else + change.addUInt8(8).addUInt8(0); + break; + } + case 3: + { + int v = m_more_options_spinner->getValue(); + change.addUInt8(6).addUInt8((uint8_t)v); + break; + } + default: + { + break; + } + } + STKHost::get()->sendToServer(&change, true); + return GUIEngine::EVENT_BLOCK; + } + } + else if (source == m_game_mode_widget->m_properties[PROP_ID]) + { + const int selection = + m_game_mode_widget->getSelection(PLAYER_ID_GAME_MASTER); + m_prev_value = 0; + updateMoreOption(selection); + m_prev_mode = selection; + return GUIEngine::EVENT_BLOCK; + } + return GUIEngine::EVENT_LET; +} // eventCallback + +// ---------------------------------------------------------------------------- +void ServerConfigurationDialog::updateMoreOption(int game_mode) +{ + switch (game_mode) + { + case 0: + case 1: + { + m_more_options_text->setVisible(false); + m_more_options_spinner->setVisible(false); + break; + } + case 2: + { + m_more_options_text->setVisible(true); + m_more_options_spinner->setVisible(true); + m_more_options_spinner->clearLabels(); + m_more_options_text->setText(_("Battle mode"), false); + m_more_options_spinner->setVisible(true); + m_more_options_spinner->clearLabels(); + m_more_options_spinner->addLabel(_("Free-For-All")); + m_more_options_spinner->addLabel(_("Capture The Flag")); + m_more_options_spinner->setValue(m_prev_value); + break; + } + case 3: + { + m_more_options_text->setVisible(true); + m_more_options_spinner->setVisible(true); + m_more_options_spinner->clearLabels(); + m_more_options_text->setText(_("Soccer game type"), false); + m_more_options_spinner->setVisible(true); + m_more_options_spinner->clearLabels(); + m_more_options_spinner->addLabel(_("Time limit")); + m_more_options_spinner->addLabel(_("Goals limit")); + m_more_options_spinner->setValue(m_prev_value); + break; + } + default: + { + m_more_options_text->setVisible(false); + m_more_options_spinner->setVisible(false); + break; + } + } +} // updateMoreOption diff --git a/src/states_screens/dialogs/server_configuration_dialog.hpp b/src/states_screens/dialogs/server_configuration_dialog.hpp new file mode 100644 index 000000000..469890679 --- /dev/null +++ b/src/states_screens/dialogs/server_configuration_dialog.hpp @@ -0,0 +1,117 @@ +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2018 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_SERVER_CONFIGURATION_DIALOG_HPP +#define HEADER_SERVER_CONFIGURATION_DIALOG_HPP + +#include "guiengine/modaldialog.hpp" +#include "race/race_manager.hpp" + +namespace GUIEngine +{ + class SpinnerWidget; + class LabelWidget; + class RibbonWidget; + class IconButtonWidget; +} + +class ServerConfigurationDialog : public GUIEngine::ModalDialog +{ +private: + int m_prev_mode; + int m_prev_value; + bool m_self_destroy; + + GUIEngine::SpinnerWidget* m_more_options_spinner; + + GUIEngine::LabelWidget* m_more_options_text; + + GUIEngine::RibbonWidget* m_difficulty_widget; + GUIEngine::RibbonWidget* m_game_mode_widget; + GUIEngine::RibbonWidget* m_options_widget; + GUIEngine::IconButtonWidget* m_ok_widget; + GUIEngine::IconButtonWidget* m_cancel_widget; + + void updateMoreOption(int game_mode); +public: + ServerConfigurationDialog(bool soccer_goal) : ModalDialog(0.8f, 0.8f) + { + m_self_destroy = false; + switch (race_manager->getMinorMode()) + { + case RaceManager::MINOR_MODE_NORMAL_RACE: + { + m_prev_mode = 0; + m_prev_value = 0; + break; + } + case RaceManager::MINOR_MODE_TIME_TRIAL: + { + m_prev_mode = 1; + m_prev_value = 0; + break; + } + case RaceManager::MINOR_MODE_FREE_FOR_ALL: + { + m_prev_mode = 2; + m_prev_value = 0; + break; + } + case RaceManager::MINOR_MODE_CAPTURE_THE_FLAG: + { + m_prev_mode = 2; + m_prev_value = 1; + break; + } + case RaceManager::MINOR_MODE_SOCCER: + { + m_prev_mode = 3; + m_prev_value = soccer_goal ? 1 : 0; + break; + } + default: + { + m_prev_mode = 0; + m_prev_value = 0; + break; + } + } + loadFromFile("online/server_configuration_dialog.stkgui"); + } + // ------------------------------------------------------------------------ + void beforeAddingWidgets(); + // ------------------------------------------------------------------------ + GUIEngine::EventPropagation processEvent(const std::string& source); + // ------------------------------------------------------------------------ + void init(); + // ------------------------------------------------------------------------ + void onEnterPressedInternal() { m_self_destroy = true; } + // ------------------------------------------------------------------------ + bool onEscapePressed() + { + m_self_destroy = true; + return false; + } + // ------------------------------------------------------------------------ + void onUpdate(float dt) + { + if (m_self_destroy) + ModalDialog::dismiss(); + } +}; // class ServerConfigurationDialog + +#endif diff --git a/src/states_screens/online/networking_lobby.cpp b/src/states_screens/online/networking_lobby.cpp index 01727a489..f79b6c869 100644 --- a/src/states_screens/online/networking_lobby.cpp +++ b/src/states_screens/online/networking_lobby.cpp @@ -35,14 +35,16 @@ #include "input/device_manager.hpp" #include "input/input_manager.hpp" #include "io/file_manager.hpp" +#include "network/game_setup.hpp" #include "network/network_config.hpp" #include "network/protocols/connect_to_server.hpp" #include "network/protocols/client_lobby.hpp" #include "network/server.hpp" #include "network/stk_host.hpp" #include "states_screens/dialogs/splitscreen_player_dialog.hpp" -#include "states_screens/state_manager.hpp" #include "states_screens/dialogs/network_user_dialog.hpp" +#include "states_screens/dialogs/server_configuration_dialog.hpp" +#include "states_screens/state_manager.hpp" #include "utils/translation.hpp" #include @@ -451,6 +453,16 @@ void NetworkingLobby::eventCallback(Widget* widget, const std::string& name, start.addUInt8(LobbyProtocol::LE_REQUEST_BEGIN); STKHost::get()->sendToServer(&start, true); } + else if (name == m_config_button->m_properties[PROP_ID]) + { + auto cl = LobbyProtocol::get(); + if (cl) + { + new ServerConfigurationDialog( + race_manager->isSoccerMode() && + cl->getGameSetup()->isSoccerGoalTarget()); + } + } } // eventCallback // ---------------------------------------------------------------------------- diff --git a/src/states_screens/race_result_gui.cpp b/src/states_screens/race_result_gui.cpp index 5dc41674e..95bcfeb7f 100644 --- a/src/states_screens/race_result_gui.cpp +++ b/src/states_screens/race_result_gui.cpp @@ -471,7 +471,6 @@ void RaceResultGUI::backToLobby() cleanupGPProgress(); StateManager::get()->resetAndSetStack( NetworkConfig::get()->getResetScreens(true/*lobby*/).data()); - NetworkingLobby::getInstance()->addMoreServerInfo(L"--------------------"); } // backToLobby //-----------------------------------------------------------------------------