author | displacer |
Wed, 10 Jan 2007 23:21:05 +0000 | |
changeset 314 | 83773ccf4f09 |
parent 312 | c36d0b34ac3d |
child 322 | e7175ae57945 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include <QtGui> |
|
20 |
#include <QStringList> |
|
21 |
#include <QProcess> |
|
22 |
#include <QDir> |
|
23 |
#include <QPixmap> |
|
24 |
#include <QRegExp> |
|
25 |
#include <QIcon> |
|
26 |
#include <QFile> |
|
27 |
#include <QTextStream> |
|
28 |
||
29 |
#include "hwform.h" |
|
30 |
#include "game.h" |
|
31 |
#include "team.h" |
|
32 |
#include "netclient.h" |
|
33 |
#include "teamselect.h" |
|
34 |
#include "gameuiconfig.h" |
|
35 |
#include "pages.h" |
|
36 |
#include "hwconsts.h" |
|
314 | 37 |
#include "newnetclient.h" |
184 | 38 |
|
39 |
HWForm::HWForm(QWidget *parent) |
|
314 | 40 |
: QMainWindow(parent), pnetserver(0) |
184 | 41 |
{ |
42 |
ui.setupUi(this); |
|
43 |
||
301 | 44 |
config = new GameUIConfig(this, cfgdir->absolutePath() + "/hedgewars.ini"); |
184 | 45 |
|
245 | 46 |
UpdateTeamsLists(); |
184 | 47 |
|
48 |
connect(ui.pageMain->BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer())); |
|
49 |
connect(ui.pageMain->BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup())); |
|
50 |
connect(ui.pageMain->BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer())); |
|
51 |
connect(ui.pageMain->BtnDemos, SIGNAL(clicked()), this, SLOT(GoToDemos())); |
|
52 |
connect(ui.pageMain->BtnNet, SIGNAL(clicked()), this, SLOT(GoToNet())); |
|
187 | 53 |
connect(ui.pageMain->BtnInfo, SIGNAL(clicked()), this, SLOT(GoToInfo())); |
184 | 54 |
connect(ui.pageMain->BtnExit, SIGNAL(clicked()), this, SLOT(close())); |
55 |
||
289 | 56 |
connect(ui.pageLocalGame->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 57 |
connect(ui.pageLocalGame->BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame())); |
58 |
||
59 |
connect(ui.pageEditTeam->BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave())); |
|
60 |
connect(ui.pageEditTeam->BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard())); |
|
61 |
||
289 | 62 |
connect(ui.pageMultiplayer->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 63 |
connect(ui.pageMultiplayer->BtnStartMPGame, SIGNAL(clicked()), this, SLOT(StartMPGame())); |
64 |
||
289 | 65 |
connect(ui.pagePlayDemo->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 66 |
connect(ui.pagePlayDemo->BtnPlayDemo, SIGNAL(clicked()), this, SLOT(PlayDemo())); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
245
diff
changeset
|
67 |
connect(ui.pagePlayDemo->DemosList, SIGNAL(doubleClicked (const QModelIndex &)), this, SLOT(PlayDemo())); |
184 | 68 |
|
289 | 69 |
connect(ui.pageOptions->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 70 |
connect(ui.pageOptions->BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam())); |
71 |
connect(ui.pageOptions->BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam())); |
|
72 |
connect(ui.pageOptions->BtnSaveOptions, SIGNAL(clicked()), config, SLOT(SaveOptions())); |
|
312 | 73 |
connect(ui.pageOptions->BtnSaveOptions, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 74 |
|
289 | 75 |
connect(ui.pageNet->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 76 |
connect(ui.pageNet->BtnNetConnect, SIGNAL(clicked()), this, SLOT(NetConnect())); |
314 | 77 |
connect(ui.pageNet->BtnNetSvrStart, SIGNAL(clicked()), this, SLOT(NetStartServer())); |
184 | 78 |
|
289 | 79 |
connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 80 |
connect(ui.pageNetGame->BtnAddTeam, SIGNAL(clicked()), this, SLOT(NetAddTeam())); |
81 |
connect(ui.pageNetGame->BtnGo, SIGNAL(clicked()), this, SLOT(NetStartGame())); |
|
82 |
||
83 |
connect(ui.pageNetChat->BtnDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect())); |
|
84 |
connect(ui.pageNetChat->BtnJoin, SIGNAL(clicked()), this, SLOT(NetJoin())); |
|
85 |
connect(ui.pageNetChat->BtnCreate, SIGNAL(clicked()), this, SLOT(NetCreate())); |
|
86 |
||
289 | 87 |
connect(ui.pageInfo->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
187 | 88 |
|
306 | 89 |
connect(ui.pageGameStats->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
90 |
||
311 | 91 |
connect(ui.pageMultiplayer->teamsSelect, SIGNAL(NewTeam()), this, SLOT(NewTeam())); |
92 |
||
290 | 93 |
GoToPage(ID_PAGE_MAIN); |
184 | 94 |
} |
95 |
||
245 | 96 |
void HWForm::UpdateTeamsLists() |
97 |
{ |
|
98 |
QStringList teamslist = config->GetTeamsList(); |
|
99 |
||
100 |
if(teamslist.empty()) { |
|
101 |
HWTeam defaultTeam("DefaultTeam"); |
|
102 |
defaultTeam.SaveToFile(); |
|
103 |
teamslist.push_back("DefaultTeam"); |
|
104 |
} |
|
105 |
||
106 |
ui.pageOptions->CBTeamName->clear(); |
|
107 |
ui.pageOptions->CBTeamName->addItems(teamslist); |
|
108 |
} |
|
109 |
||
184 | 110 |
void HWForm::GoToMain() |
111 |
{ |
|
289 | 112 |
GoToPage(ID_PAGE_MAIN); |
184 | 113 |
} |
114 |
||
115 |
void HWForm::GoToSinglePlayer() |
|
116 |
{ |
|
289 | 117 |
GoToPage(ID_PAGE_SINGLEPLAYER); |
184 | 118 |
} |
119 |
||
120 |
void HWForm::GoToSetup() |
|
121 |
{ |
|
289 | 122 |
GoToPage(ID_PAGE_SETUP); |
184 | 123 |
} |
124 |
||
187 | 125 |
void HWForm::GoToInfo() |
126 |
{ |
|
289 | 127 |
GoToPage(ID_PAGE_INFO); |
187 | 128 |
} |
129 |
||
184 | 130 |
void HWForm::GoToMultiplayer() |
131 |
{ |
|
290 | 132 |
GoToPage(ID_PAGE_MULTIPLAYER); |
184 | 133 |
} |
134 |
||
135 |
void HWForm::GoToDemos() |
|
136 |
{ |
|
137 |
QDir tmpdir; |
|
138 |
tmpdir.cd(cfgdir->absolutePath()); |
|
139 |
tmpdir.cd("Demos"); |
|
140 |
tmpdir.setFilter(QDir::Files); |
|
141 |
ui.pagePlayDemo->DemosList->clear(); |
|
271 | 142 |
ui.pagePlayDemo->DemosList->addItems(tmpdir.entryList(QStringList("*.hwd_" + cProtoVer)) |
143 |
.replaceInStrings(QRegExp("^(.*).hwd_" + cProtoVer), "\\1")); |
|
290 | 144 |
GoToPage(ID_PAGE_DEMOS); |
184 | 145 |
} |
146 |
||
147 |
void HWForm::GoToNet() |
|
148 |
{ |
|
289 | 149 |
GoToPage(ID_PAGE_NET); |
184 | 150 |
} |
151 |
||
152 |
void HWForm::GoToNetChat() |
|
153 |
{ |
|
289 | 154 |
GoToPage(ID_PAGE_NETCHAT); |
155 |
} |
|
156 |
||
311 | 157 |
void HWForm::OnPageShown(quint8 id) |
158 |
{ |
|
159 |
if (id == ID_PAGE_MULTIPLAYER) { |
|
160 |
QStringList tmNames=config->GetTeamsList(); |
|
161 |
QList<HWTeam> teamsList; |
|
162 |
for(QStringList::iterator it=tmNames.begin(); it!=tmNames.end(); it++) { |
|
163 |
HWTeam team(*it); |
|
164 |
team.LoadFromFile(); |
|
165 |
teamsList.push_back(team); |
|
166 |
} |
|
167 |
ui.pageMultiplayer->teamsSelect->resetPlayingTeams(teamsList); |
|
168 |
} |
|
169 |
} |
|
170 |
||
289 | 171 |
void HWForm::GoToPage(quint8 id) |
172 |
{ |
|
173 |
PagesStack.push(ui.Pages->currentIndex()); |
|
311 | 174 |
OnPageShown(id); |
289 | 175 |
ui.Pages->setCurrentIndex(id); |
176 |
} |
|
177 |
||
178 |
void HWForm::GoBack() |
|
179 |
{ |
|
180 |
quint8 id = PagesStack.isEmpty() ? ID_PAGE_MAIN : PagesStack.pop(); |
|
311 | 181 |
OnPageShown(id); |
289 | 182 |
ui.Pages->setCurrentIndex(id); |
184 | 183 |
} |
184 |
||
185 |
void HWForm::NewTeam() |
|
186 |
{ |
|
245 | 187 |
editedTeam = new HWTeam("unnamed"); |
188 |
editedTeam->SetToPage(this); |
|
290 | 189 |
GoToPage(ID_PAGE_SETUP_TEAM); |
184 | 190 |
} |
191 |
||
192 |
void HWForm::EditTeam() |
|
193 |
{ |
|
245 | 194 |
editedTeam = new HWTeam(ui.pageOptions->CBTeamName->currentText()); |
195 |
editedTeam->LoadFromFile(); |
|
196 |
editedTeam->SetToPage(this); |
|
290 | 197 |
GoToPage(ID_PAGE_SETUP_TEAM); |
184 | 198 |
} |
199 |
||
200 |
void HWForm::TeamSave() |
|
201 |
{ |
|
245 | 202 |
editedTeam->GetFromPage(this); |
203 |
editedTeam->SaveToFile(); |
|
204 |
delete editedTeam; |
|
205 |
UpdateTeamsLists(); |
|
290 | 206 |
GoBack(); |
184 | 207 |
} |
208 |
||
209 |
void HWForm::TeamDiscard() |
|
210 |
{ |
|
245 | 211 |
delete editedTeam; |
290 | 212 |
GoBack(); |
184 | 213 |
} |
214 |
||
215 |
void HWForm::SimpleGame() |
|
216 |
{ |
|
306 | 217 |
CreateGame(ui.pageLocalGame->gameCFG); |
184 | 218 |
game->StartQuick(); |
219 |
} |
|
220 |
||
221 |
void HWForm::PlayDemo() |
|
222 |
{ |
|
223 |
QListWidgetItem * curritem = ui.pagePlayDemo->DemosList->currentItem(); |
|
224 |
if (!curritem) |
|
225 |
{ |
|
226 |
QMessageBox::critical(this, |
|
227 |
tr("Error"), |
|
228 |
tr("Please, select demo from the list above"), |
|
229 |
tr("OK")); |
|
230 |
return ; |
|
231 |
} |
|
306 | 232 |
CreateGame(0); |
271 | 233 |
game->PlayDemo(cfgdir->absolutePath() + "/Demos/" + curritem->text() + ".hwd_" + cProtoVer); |
184 | 234 |
} |
235 |
||
314 | 236 |
void HWForm::_NetConnect(const QString & hostName, quint16 port, const QString & nick) |
184 | 237 |
{ |
314 | 238 |
hwnet = new HWNewNet(config); |
184 | 239 |
connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat())); |
240 |
connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &))); |
|
241 |
connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter())); |
|
242 |
connect(hwnet, SIGNAL(ChangeInTeams(const QStringList &)), this, SLOT(ChangeInNetTeams(const QStringList &))); |
|
314 | 243 |
hwnet->Connect(hostName, port, nick); |
184 | 244 |
config->SaveOptions(); |
245 |
} |
|
246 |
||
314 | 247 |
void HWForm::NetConnect() |
248 |
{ |
|
249 |
_NetConnect(ui.pageNet->editIP->text(), 46631, ui.pageNet->editNetNick->text()); |
|
250 |
} |
|
251 |
||
252 |
void HWForm::NetStartServer() |
|
253 |
{ |
|
254 |
pnetserver = new HWNetServer; |
|
255 |
pnetserver->StartServer(); |
|
256 |
_NetConnect(pnetserver->getRunningHostName(), pnetserver->getRunningPort(), ui.pageNet->editNetNick->text()); |
|
257 |
} |
|
258 |
||
184 | 259 |
void HWForm::NetDisconnect() |
260 |
{ |
|
314 | 261 |
hwnet->Disconnect(); |
262 |
GoBack(); |
|
263 |
delete hwnet; |
|
264 |
hwnet=0; |
|
265 |
if(pnetserver) { |
|
266 |
pnetserver->StopServer(); |
|
267 |
delete pnetserver; |
|
268 |
pnetserver=0; |
|
269 |
} |
|
184 | 270 |
} |
271 |
||
272 |
void HWForm::AddGame(const QString & chan) |
|
273 |
{ |
|
274 |
ui.pageNetChat->ChannelsList->addItem(chan); |
|
275 |
} |
|
276 |
||
277 |
void HWForm::NetGameEnter() |
|
278 |
{ |
|
290 | 279 |
GoToPage(ID_PAGE_NETCFG); |
184 | 280 |
} |
281 |
||
282 |
void HWForm::NetJoin() |
|
283 |
{ |
|
284 |
hwnet->JoinGame("#hw"); |
|
285 |
} |
|
286 |
||
287 |
void HWForm::NetCreate() |
|
288 |
{ |
|
289 |
hwnet->JoinGame("#hw"); |
|
290 |
} |
|
291 |
||
292 |
void HWForm::NetAddTeam() |
|
293 |
{ |
|
294 |
HWTeam team("DefaultTeam"); |
|
295 |
team.LoadFromFile(); |
|
296 |
hwnet->AddTeam(team); |
|
297 |
} |
|
298 |
||
299 |
void HWForm::NetStartGame() |
|
300 |
{ |
|
301 |
hwnet->StartGame(); |
|
302 |
} |
|
303 |
||
304 |
void HWForm::ChangeInNetTeams(const QStringList & teams) |
|
305 |
{ |
|
306 |
ui.pageNetGame->listNetTeams->clear(); |
|
307 |
ui.pageNetGame->listNetTeams->addItems(teams); |
|
308 |
} |
|
309 |
||
310 |
void HWForm::StartMPGame() |
|
311 |
{ |
|
306 | 312 |
CreateGame(ui.pageMultiplayer->gameCFG); |
313 |
||
184 | 314 |
list<HWTeam> teamslist=ui.pageMultiplayer->teamsSelect->getPlayingTeams(); |
315 |
for (list<HWTeam>::const_iterator it = teamslist.begin(); it != teamslist.end(); ++it ) { |
|
207 | 316 |
HWTeamTempParams params=ui.pageMultiplayer->teamsSelect->getTeamParams(it->TeamName); |
213 | 317 |
game->AddTeam(it->TeamName, params); |
184 | 318 |
} |
319 |
game->StartLocal(); |
|
320 |
} |
|
306 | 321 |
|
322 |
void HWForm::GameStateChanged(GameState gameState) |
|
323 |
{ |
|
324 |
if (gameState == gsStarted) |
|
307 | 325 |
{ |
306 | 326 |
GoToPage(ID_PAGE_GAMESTATS); |
307 | 327 |
ui.pageGameStats->labelGameStats->setText(""); |
328 |
} |
|
329 |
} |
|
330 |
||
331 |
void HWForm::AddStatText(const QString & msg) |
|
332 |
{ |
|
333 |
ui.pageGameStats->labelGameStats->setText( |
|
334 |
ui.pageGameStats->labelGameStats->text() + msg); |
|
306 | 335 |
} |
336 |
||
337 |
void HWForm::GameStats(char type, const QString & info) |
|
338 |
{ |
|
339 |
switch(type) { |
|
307 | 340 |
case 'r' : { |
341 |
AddStatText(QString("<h1 align=\"center\">%1</h1>").arg(info)); |
|
342 |
break; |
|
343 |
} |
|
344 |
case 'D' : { |
|
345 |
int i = info.indexOf(' '); |
|
346 |
QString message = QLabel::tr("<p>The best shot award was won by <b>%1</b> with <b>%2</b> pts.</p>") |
|
347 |
.arg(info.mid(i + 1), info.left(i)); |
|
348 |
AddStatText(message); |
|
349 |
break; |
|
350 |
} |
|
351 |
case 'K' : { |
|
352 |
QString message = QLabel::tr("<p>A total of <b>%1</b> Hedgehog(s) were killed during this round.</p>").arg(info); |
|
353 |
AddStatText(message); |
|
354 |
break; |
|
355 |
} |
|
306 | 356 |
} |
357 |
} |
|
358 |
||
359 |
void HWForm::CreateGame(GameCFGWidget * gamecfg) |
|
360 |
{ |
|
361 |
game = new HWGame(config, gamecfg); |
|
362 |
connect(game, SIGNAL(GameStateChanged(GameState)), this, SLOT(GameStateChanged(GameState))); |
|
363 |
connect(game, SIGNAL(GameStats(char, const QString &)), this, SLOT(GameStats(char, const QString &))); |
|
364 |
} |