Add connect-now equivalent to online screen

Specify port to allow to connect to one of multiple servers in same machine
This commit is contained in:
Benau 2018-04-15 14:03:26 +08:00
parent 8c72ce66e3
commit 2c6883842e
2 changed files with 69 additions and 2 deletions

View File

@ -23,10 +23,13 @@
<buttonbar id="menu_toprow" proportion="3" width="90%" align="center">
<icon-button id="lan" width="128" height="128"
icon="gui/menu_multi.png" focus_icon="gui/menu_multi_focus.png"
I18N="Networking menu button" text="Local Networking"/>
I18N="Networking menu button" text="Local networking"/>
<icon-button id="wan" width="128" height="128"
icon="gui/menu_multi.png" focus_icon="gui/menu_multi_focus.png"
I18N="Networking menu button" text="Global Networking"/>
I18N="Networking menu button" text="Global networking"/>
<icon-button id="enter-address" width="128" height="128"
icon="gui/online/menu_quick_play.png" focus_icon="gui/online/menu_quick_play_hover.png"
I18N="Networking menu button" text="Enter server address"/>
<icon-button id="online" width="128" height="128"
icon="gui/menu_online.png" focus_icon="gui/menu_online_focus.png"
I18N="Networking menu button" text="Your profile"/>

View File

@ -20,19 +20,26 @@
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "guiengine/message_queue.hpp"
#include "guiengine/widgets/check_box_widget.hpp"
#include "guiengine/widgets/label_widget.hpp"
#include "guiengine/widgets/list_widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "input/device_manager.hpp"
#include "network/protocols/connect_to_server.hpp"
#include "network/protocols/client_lobby.hpp"
#include "network/network_config.hpp"
#include "network/server.hpp"
#include "network/stk_host.hpp"
#include "online/request_manager.hpp"
#include "states_screens/networking_lobby.hpp"
#include "states_screens/online_lan.hpp"
#include "states_screens/online_profile_achievements.hpp"
#include "states_screens/online_profile_servers.hpp"
#include "states_screens/online_screen.hpp"
#include "states_screens/state_manager.hpp"
#include "states_screens/user_screen.hpp"
#include "states_screens/dialogs/general_text_field_dialog.hpp"
#include "states_screens/dialogs/message_dialog.hpp"
#include "utils/string_utils.hpp"
@ -134,6 +141,13 @@ void OnlineScreen::onUpdate(float delta)
m_online->setLabel(PlayerManager::getCurrentOnlineId() ? m_online_string
: m_login_string);
// In case for entering server address finished
if (auto lb = LobbyProtocol::get<LobbyProtocol>())
{
NetworkingLobby::getInstance()->setJoinedServer(nullptr);
StateManager::get()->resetAndSetStack(
NetworkConfig::get()->getResetScreens(true/*lobby*/).data());
}
} // onUpdate
// ----------------------------------------------------------------------------
@ -209,6 +223,56 @@ void OnlineScreen::eventCallback(Widget* widget, const std::string& name,
UserScreen::getInstance()->push();
}
}
else if (selection == "enter-address")
{
if (NetworkConfig::get()->isAddingNetworkPlayers())
{
core::stringw msg =
_("No player available for connecting to server.");
MessageQueue::add(MessageQueue::MT_ERROR, msg);
return;
}
core::stringw instruction =
_("Enter the server address with IP followed by : and then port.");
new GeneralTextFieldDialog(instruction.c_str(),
[](const irr::core::stringw& text) {},
[](GUIEngine::LabelWidget* lw, GUIEngine::TextBoxWidget* tb)->bool
{
TransportAddress server_addr(
StringUtils::wideToUtf8(tb->getText()));
if (server_addr.isUnset())
{
core::stringw err = _("Invalid server address: %s.",
tb->getText());
lw->setText(err, true);
return false;
}
NetworkConfig::get()->setIsWAN();
NetworkConfig::get()->setIsServer(false);
NetworkConfig::get()->setPassword("");
auto server = std::make_shared<Server>(0, L"", 0, 0, 0, 0,
server_addr, false);
STKHost::create();
auto cts = std::make_shared<ConnectToServer>(server);
cts->setup();
Log::info("OnlineScreen", "Trying to connect to server '%s'.",
server_addr.toString().c_str());
if (!cts->handleDirectConnect(10000))
{
core::stringw err = _("Cannot connect to server %s.",
server_addr.toString().c_str());
STKHost::get()->shutdown();
NetworkConfig::get()->unsetNetworking();
lw->setText(err, true);
return false;
}
auto cl = LobbyProtocol::create<ClientLobby>();
cl->setAddress(server_addr);
cl->requestStart();
return true;
});
}
} // eventCallback
// ----------------------------------------------------------------------------