1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
#ifdef _MSC_VER
#include "StdAfx.h"
#endif
#include "SelectMenu.h"
#include <SDL_keysym.h>
#include <SDL_timer.h>
#include <boost/bind.hpp>
#include <sstream>
#ifndef _WIN32
#include <unistd.h>
#define EXECLP execlp
#else
#include <process.h>
#define EXECLP _execlp
#endif
#include <stack>
#include <boost/cstdint.hpp>
#include "LobbyConnection.h"
#include "Game/ClientSetup.h"
#include "SelectionWidget.h"
#include "Game/PreGame.h"
#include "Rendering/glFont.h"
#include "LogOutput.h"
#include "Exceptions.h"
#include "TdfParser.h"
#include "Util.h"
#include "FileSystem/ArchiveScanner.h"
#include "FileSystem/FileHandler.h"
#include "FileSystem/VFSHandler.h"
#include "FileSystem/FileSystem.h"
#include "ConfigHandler.h"
#include "InputHandler.h"
#include "Game/StartScripts/ScriptHandler.h"
#include "Game/StartScripts/SkirmishAITestScript.h"
#include "aGui/Gui.h"
#include "aGui/VerticalLayout.h"
#include "aGui/HorizontalLayout.h"
#include "aGui/Button.h"
#include "aGui/LineEdit.h"
#include "aGui/TextElement.h"
#include "aGui/Window.h"
#include "aGui/Picture.h"
#include "aGui/List.h"
using std::string;
using agui::Button;
using agui::HorizontalLayout;
extern boost::uint8_t* keys;
extern bool globalQuit;
class ConnectWindow : public agui::Window
{
public:
ConnectWindow() : agui::Window("Connect to server")
{
agui::gui->AddElement(this);
SetPos(0.5, 0.5);
SetSize(0.4, 0.2);
agui::VerticalLayout* wndLayout = new agui::VerticalLayout(this);
HorizontalLayout* input = new HorizontalLayout(wndLayout);
/*agui::TextElement* label = */new agui::TextElement("Address:", input); // will be deleted in input
address = new agui::LineEdit(input);
address->DefaultAction.connect(boost::bind(&ConnectWindow::Finish, this, true));
address->SetFocus(true);
address->SetContent(configHandler->GetString("address", ""));
HorizontalLayout* buttons = new HorizontalLayout(wndLayout);
Button* connect = new Button("Connect", buttons);
connect->Clicked.connect(boost::bind(&ConnectWindow::Finish, this, true));
Button* close = new Button("Close", buttons);
close->Clicked.connect(boost::bind(&ConnectWindow::Finish, this, false));
GeometryChange();
}
boost::signal<void (std::string)> Connect;
agui::LineEdit* address;
private:
void Finish(bool connect)
{
if (connect)
Connect(address->GetContent());
else
WantClose();
};
};
class SettingsWindow : public agui::Window
{
public:
SettingsWindow(std::string &name) : agui::Window(name)
{
agui::gui->AddElement(this);
SetPos(0.5, 0.5);
SetSize(0.4, 0.2);
agui::VerticalLayout* wndLayout = new agui::VerticalLayout(this);
HorizontalLayout* input = new HorizontalLayout(wndLayout);
/*agui::TextElement* value_label = */new agui::TextElement("Value:", input); // will be deleted in input
value = new agui::LineEdit(input);
value->DefaultAction.connect(boost::bind(&SettingsWindow::Finish, this, true));
value->SetFocus(true);
value->SetContent(configHandler->GetString(name, ""));
HorizontalLayout* buttons = new HorizontalLayout(wndLayout);
Button* ok = new Button("OK", buttons);
ok->Clicked.connect(boost::bind(&SettingsWindow::Finish, this, true));
Button* close = new Button("Cancel", buttons);
close->Clicked.connect(boost::bind(&SettingsWindow::Finish, this, false));
GeometryChange();
}
boost::signal<void (std::string)> OK;
agui::LineEdit* value;
private:
void Finish(bool set)
{
if (set)
OK(title + " = " + value->GetContent());
else
WantClose();
};
};
std::string CreateDefaultSetup(const std::string& map, const std::string& mod, const std::string& script,
const std::string& playername)
{
TdfParser::TdfSection setup;
TdfParser::TdfSection* game = setup.construct_subsection("GAME");
game->add_name_value("Mapname", map);
game->add_name_value("Gametype", mod);
TdfParser::TdfSection* modopts = game->construct_subsection("MODOPTIONS");
modopts->AddPair("GameMode", 3);
modopts->AddPair("MaxSpeed", 20);
modopts->add_name_value("Scriptname", script);
game->AddPair("IsHost", 1);
game->add_name_value("MyPlayerName", playername);
game->AddPair("NoHelperAIs", configHandler->Get("NoHelperAIs", 0));
TdfParser::TdfSection* player0 = game->construct_subsection("PLAYER0");
player0->add_name_value("Name", playername);
player0->AddPair("Team", 0);
const bool isSkirmishAITestScript =
(script.substr(0, CSkirmishAITestScript::SCRIPT_NAME_PRELUDE.size())
== CSkirmishAITestScript::SCRIPT_NAME_PRELUDE);
if (!isSkirmishAITestScript) {
TdfParser::TdfSection* player1 = game->construct_subsection("PLAYER1");
player1->add_name_value("Name", "Enemy");
player1->AddPair("Team", 1);
}
TdfParser::TdfSection* team0 = game->construct_subsection("TEAM0");
team0->AddPair("TeamLeader", 0);
team0->AddPair("AllyTeam", 0);
TdfParser::TdfSection* team1 = game->construct_subsection("TEAM1");
if (isSkirmishAITestScript) {
team1->AddPair("TeamLeader", 0);
} else {
team1->AddPair("TeamLeader", 1);
}
team1->AddPair("AllyTeam", 1);
TdfParser::TdfSection* ally0 = game->construct_subsection("ALLYTEAM0");
ally0->AddPair("NumAllies", 0);
TdfParser::TdfSection* ally1 = game->construct_subsection("ALLYTEAM1");
ally1->AddPair("NumAllies", 0);
std::ostringstream str;
setup.print(str);
return str.str();
}
SelectMenu::SelectMenu(bool server) : GuiElement(NULL), conWindow(NULL), updWindow(NULL), settingsWindow(NULL), curSelect(NULL)
{
SetPos(0,0);
SetSize(1,1);
agui::gui->AddElement(this, true);
mySettings = new ClientSetup();
mySettings->isHost = server;
mySettings->myPlayerName = configHandler->GetString("name", "UnnamedPlayer");
if (mySettings->myPlayerName.empty()) {
mySettings->myPlayerName = "UnnamedPlayer";
} else {
mySettings->myPlayerName = StringReplaceInPlace(mySettings->myPlayerName, ' ', '_');
}
{ // GUI stuff
agui::Picture* background = new agui::Picture(this);;
{
std::string archive = archiveScanner->ModNameToModArchive("Spring Bitmaps");
std::string archivePath = archiveScanner->GetArchivePath(archive)+archive;
vfsHandler->AddArchive(archivePath, false);
background->Load("bitmaps/ui/background.jpg");
vfsHandler->RemoveArchive(archivePath);
}
selw = new SelectionWidget(this);
agui::VerticalLayout* menu = new agui::VerticalLayout(this);
menu->SetPos(0.1, 0.5);
menu->SetSize(0.4, 0.4);
menu->SetBorder(1.2f);
/*agui::TextElement* title = */new agui::TextElement("Spring", menu); // will be deleted in menu
Button* single = new Button("Test the Game", menu);
single->Clicked.connect(boost::bind(&SelectMenu::Single, this));
Button* multi = new Button("Start the Lobby", menu);
multi->Clicked.connect(boost::bind(&SelectMenu::Multi, this));
Button* update = new Button("Lobby connect (WIP)", menu);
update->Clicked.connect(boost::bind(&SelectMenu::ShowUpdateWindow, this, true));
userSetting = configHandler->GetString("LastSelectedSetting", "");
Button* editsettings = new Button("Edit settings", menu);
editsettings->Clicked.connect(boost::bind(&SelectMenu::ShowSettingsList, this));
Button* settings = new Button("Start SpringSettings", menu);
settings->Clicked.connect(boost::bind(&SelectMenu::Settings, this));
Button* direct = new Button("Direct connect", menu);
direct->Clicked.connect(boost::bind(&SelectMenu::ShowConnectWindow, this, true));
Button* quit = new Button("Quit", menu);
quit->Clicked.connect(boost::bind(&SelectMenu::Quit, this));
background->GeometryChange();
}
if (!mySettings->isHost) {
ShowConnectWindow(true);
}
}
SelectMenu::~SelectMenu()
{
ShowConnectWindow(false);
CleanWindow();
delete updWindow;
}
bool SelectMenu::Draw()
{
SDL_Delay(10); // milliseconds
ClearScreen();
agui::gui->Draw();
return true;
}
bool SelectMenu::Update()
{
if (updWindow)
{
updWindow->Poll();
if (updWindow->WantClose())
{
delete updWindow;
updWindow = NULL;
}
}
return true;
}
void SelectMenu::Single()
{
static bool once = false;
if (selw->userMod == SelectionWidget::NoModSelect)
{
selw->ShowModList();
}
else if (selw->userMap == SelectionWidget::NoMapSelect)
{
selw->ShowMapList();
}
else if (selw->userScript == SelectionWidget::NoScriptSelect)
{
selw->ShowScriptList();
}
else if (!once) // in case of double-click
{
once = true;
mySettings->isHost = true;
pregame = new CPreGame(mySettings);
pregame->LoadSetupscript(CreateDefaultSetup(selw->userMap, selw->userMod, selw->userScript, mySettings->myPlayerName));
agui::gui->RmElement(this);
}
}
void SelectMenu::Settings()
{
#ifdef __unix__
const std::string settingsProgram = "springsettings";
#else
const std::string settingsProgram = "springsettings.exe";
#endif
EXECLP(settingsProgram.c_str(), Quote(settingsProgram).c_str(), NULL);
}
void SelectMenu::Multi()
{
#ifdef __unix__
const std::string defLobby = configHandler->GetString("DefaultLobby", "springlobby");
#else
const std::string defLobby = configHandler->GetString("DefaultLobby", "springlobby.exe");
#endif
EXECLP(defLobby.c_str(), Quote(defLobby).c_str(), NULL);
}
void SelectMenu::Quit()
{
globalQuit = true;
}
void SelectMenu::ShowConnectWindow(bool show)
{
if (show && !conWindow)
{
conWindow = new ConnectWindow();
conWindow->Connect.connect(boost::bind(&SelectMenu::DirectConnect, this, _1));
conWindow->WantClose.connect(boost::bind(&SelectMenu::ShowConnectWindow, this, false));
}
else if (!show && conWindow)
{
agui::gui->RmElement(conWindow);
conWindow = NULL;
}
}
void SelectMenu::ShowSettingsWindow(bool show, std::string name)
{
if (show)
{
if(settingsWindow) {
agui::gui->RmElement(settingsWindow);
settingsWindow = NULL;
}
settingsWindow = new SettingsWindow(name);
settingsWindow->OK.connect(boost::bind(&SelectMenu::ShowSettingsWindow, this, false, _1));
settingsWindow->WantClose.connect(boost::bind(&SelectMenu::ShowSettingsWindow, this, false, ""));
}
else if (!show && settingsWindow)
{
agui::gui->RmElement(settingsWindow);
settingsWindow = NULL;
int p = name.find(" = ");
if(p != std::string::npos) {
configHandler->SetString(name.substr(0,p), name.substr(p + 3));
ShowSettingsList();
}
if(curSelect)
curSelect->list->SetFocus(true);
}
}
void SelectMenu::ShowUpdateWindow(bool show)
{
if (show)
{
if (!updWindow)
updWindow = new LobbyConnection();
updWindow->ConnectDialog(true);
}
else if (!show && updWindow)
{
delete updWindow;
updWindow = NULL;
}
}
void SelectMenu::ShowSettingsList()
{
if (!curSelect) {
curSelect = new ListSelectWnd("Select setting");
curSelect->Selected.connect(boost::bind(&SelectMenu::SelectSetting, this, _1));
curSelect->WantClose.connect(boost::bind(&SelectMenu::CleanWindow, this));
}
curSelect->list->RemoveAllItems();
const std::map<std::string, std::string> &data = configHandler->GetData();
for(std::map<std::string,std::string>::const_iterator iter = data.begin(); iter != data.end(); ++iter)
curSelect->list->AddItem(iter->first + " = " + iter->second, "");
if(data.find(userSetting) != data.end())
curSelect->list->SetCurrentItem(userSetting + " = " + configHandler->GetString(userSetting, ""));
curSelect->list->RefreshQuery();
}
void SelectMenu::SelectSetting(std::string setting) {
int p = setting.find(" = ");
if(p != std::string::npos)
setting = setting.substr(0, p);
userSetting = setting;
configHandler->SetString("LastSelectedSetting", userSetting);
ShowSettingsWindow(true, userSetting);
}
void SelectMenu::CleanWindow() {
if (curSelect) {
ShowSettingsWindow(false, "");
agui::gui->RmElement(curSelect);
curSelect = NULL;
}
}
void SelectMenu::DirectConnect(const std::string& addr)
{
configHandler->SetString("address", addr);
mySettings->hostip = addr;
mySettings->isHost = false;
pregame = new CPreGame(mySettings);
agui::gui->RmElement(this);
}
bool SelectMenu::HandleEventSelf(const SDL_Event& ev)
{
switch (ev.type) {
case SDL_KEYDOWN: {
if (ev.key.keysym.sym == SDLK_ESCAPE)
{
logOutput.Print("User exited");
globalQuit=true;
}
else if (ev.key.keysym.sym == SDLK_RETURN)
{
Single();
return true;
}
break;
}
}
return false;
}
|