author | unc0rr |
Mon, 08 Jan 2007 20:51:22 +0000 | |
changeset 312 | c36d0b34ac3d |
parent 311 | b8905423f19f |
child 314 | 83773ccf4f09 |
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" |
|
37 |
||
38 |
HWForm::HWForm(QWidget *parent) |
|
39 |
: QMainWindow(parent) |
|
40 |
{ |
|
41 |
ui.setupUi(this); |
|
42 |
||
301 | 43 |
config = new GameUIConfig(this, cfgdir->absolutePath() + "/hedgewars.ini"); |
184 | 44 |
|
245 | 45 |
UpdateTeamsLists(); |
184 | 46 |
|
47 |
connect(ui.pageMain->BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer())); |
|
48 |
connect(ui.pageMain->BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup())); |
|
49 |
connect(ui.pageMain->BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer())); |
|
50 |
connect(ui.pageMain->BtnDemos, SIGNAL(clicked()), this, SLOT(GoToDemos())); |
|
51 |
connect(ui.pageMain->BtnNet, SIGNAL(clicked()), this, SLOT(GoToNet())); |
|
187 | 52 |
connect(ui.pageMain->BtnInfo, SIGNAL(clicked()), this, SLOT(GoToInfo())); |
184 | 53 |
connect(ui.pageMain->BtnExit, SIGNAL(clicked()), this, SLOT(close())); |
54 |
||
289 | 55 |
connect(ui.pageLocalGame->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 56 |
connect(ui.pageLocalGame->BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame())); |
57 |
||
58 |
connect(ui.pageEditTeam->BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave())); |
|
59 |
connect(ui.pageEditTeam->BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard())); |
|
60 |
||
289 | 61 |
connect(ui.pageMultiplayer->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 62 |
connect(ui.pageMultiplayer->BtnStartMPGame, SIGNAL(clicked()), this, SLOT(StartMPGame())); |
63 |
||
289 | 64 |
connect(ui.pagePlayDemo->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 65 |
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
|
66 |
connect(ui.pagePlayDemo->DemosList, SIGNAL(doubleClicked (const QModelIndex &)), this, SLOT(PlayDemo())); |
184 | 67 |
|
289 | 68 |
connect(ui.pageOptions->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 69 |
connect(ui.pageOptions->BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam())); |
70 |
connect(ui.pageOptions->BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam())); |
|
71 |
connect(ui.pageOptions->BtnSaveOptions, SIGNAL(clicked()), config, SLOT(SaveOptions())); |
|
312 | 72 |
connect(ui.pageOptions->BtnSaveOptions, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 73 |
|
289 | 74 |
connect(ui.pageNet->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 75 |
connect(ui.pageNet->BtnNetConnect, SIGNAL(clicked()), this, SLOT(NetConnect())); |
76 |
||
289 | 77 |
connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
184 | 78 |
connect(ui.pageNetGame->BtnAddTeam, SIGNAL(clicked()), this, SLOT(NetAddTeam())); |
79 |
connect(ui.pageNetGame->BtnGo, SIGNAL(clicked()), this, SLOT(NetStartGame())); |
|
80 |
||
81 |
connect(ui.pageNetChat->BtnDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect())); |
|
82 |
connect(ui.pageNetChat->BtnJoin, SIGNAL(clicked()), this, SLOT(NetJoin())); |
|
83 |
connect(ui.pageNetChat->BtnCreate, SIGNAL(clicked()), this, SLOT(NetCreate())); |
|
84 |
||
289 | 85 |
connect(ui.pageInfo->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
187 | 86 |
|
306 | 87 |
connect(ui.pageGameStats->BtnBack, SIGNAL(clicked()), this, SLOT(GoBack())); |
88 |
||
311 | 89 |
connect(ui.pageMultiplayer->teamsSelect, SIGNAL(NewTeam()), this, SLOT(NewTeam())); |
90 |
||
290 | 91 |
GoToPage(ID_PAGE_MAIN); |
184 | 92 |
} |
93 |
||
245 | 94 |
void HWForm::UpdateTeamsLists() |
95 |
{ |
|
96 |
QStringList teamslist = config->GetTeamsList(); |
|
97 |
||
98 |
if(teamslist.empty()) { |
|
99 |
HWTeam defaultTeam("DefaultTeam"); |
|
100 |
defaultTeam.SaveToFile(); |
|
101 |
teamslist.push_back("DefaultTeam"); |
|
102 |
} |
|
103 |
||
104 |
ui.pageOptions->CBTeamName->clear(); |
|
105 |
ui.pageOptions->CBTeamName->addItems(teamslist); |
|
106 |
} |
|
107 |
||
184 | 108 |
void HWForm::GoToMain() |
109 |
{ |
|
289 | 110 |
GoToPage(ID_PAGE_MAIN); |
184 | 111 |
} |
112 |
||
113 |
void HWForm::GoToSinglePlayer() |
|
114 |
{ |
|
289 | 115 |
GoToPage(ID_PAGE_SINGLEPLAYER); |
184 | 116 |
} |
117 |
||
118 |
void HWForm::GoToSetup() |
|
119 |
{ |
|
289 | 120 |
GoToPage(ID_PAGE_SETUP); |
184 | 121 |
} |
122 |
||
187 | 123 |
void HWForm::GoToInfo() |
124 |
{ |
|
289 | 125 |
GoToPage(ID_PAGE_INFO); |
187 | 126 |
} |
127 |
||
184 | 128 |
void HWForm::GoToMultiplayer() |
129 |
{ |
|
290 | 130 |
GoToPage(ID_PAGE_MULTIPLAYER); |
184 | 131 |
} |
132 |
||
133 |
void HWForm::GoToDemos() |
|
134 |
{ |
|
135 |
QDir tmpdir; |
|
136 |
tmpdir.cd(cfgdir->absolutePath()); |
|
137 |
tmpdir.cd("Demos"); |
|
138 |
tmpdir.setFilter(QDir::Files); |
|
139 |
ui.pagePlayDemo->DemosList->clear(); |
|
271 | 140 |
ui.pagePlayDemo->DemosList->addItems(tmpdir.entryList(QStringList("*.hwd_" + cProtoVer)) |
141 |
.replaceInStrings(QRegExp("^(.*).hwd_" + cProtoVer), "\\1")); |
|
290 | 142 |
GoToPage(ID_PAGE_DEMOS); |
184 | 143 |
} |
144 |
||
145 |
void HWForm::GoToNet() |
|
146 |
{ |
|
289 | 147 |
GoToPage(ID_PAGE_NET); |
184 | 148 |
} |
149 |
||
150 |
void HWForm::GoToNetChat() |
|
151 |
{ |
|
289 | 152 |
GoToPage(ID_PAGE_NETCHAT); |
153 |
} |
|
154 |
||
311 | 155 |
void HWForm::OnPageShown(quint8 id) |
156 |
{ |
|
157 |
if (id == ID_PAGE_MULTIPLAYER) { |
|
158 |
QStringList tmNames=config->GetTeamsList(); |
|
159 |
QList<HWTeam> teamsList; |
|
160 |
for(QStringList::iterator it=tmNames.begin(); it!=tmNames.end(); it++) { |
|
161 |
HWTeam team(*it); |
|
162 |
team.LoadFromFile(); |
|
163 |
teamsList.push_back(team); |
|
164 |
} |
|
165 |
ui.pageMultiplayer->teamsSelect->resetPlayingTeams(teamsList); |
|
166 |
} |
|
167 |
} |
|
168 |
||
289 | 169 |
void HWForm::GoToPage(quint8 id) |
170 |
{ |
|
171 |
PagesStack.push(ui.Pages->currentIndex()); |
|
311 | 172 |
OnPageShown(id); |
289 | 173 |
ui.Pages->setCurrentIndex(id); |
174 |
} |
|
175 |
||
176 |
void HWForm::GoBack() |
|
177 |
{ |
|
178 |
quint8 id = PagesStack.isEmpty() ? ID_PAGE_MAIN : PagesStack.pop(); |
|
311 | 179 |
OnPageShown(id); |
289 | 180 |
ui.Pages->setCurrentIndex(id); |
184 | 181 |
} |
182 |
||
183 |
void HWForm::NewTeam() |
|
184 |
{ |
|
245 | 185 |
editedTeam = new HWTeam("unnamed"); |
186 |
editedTeam->SetToPage(this); |
|
290 | 187 |
GoToPage(ID_PAGE_SETUP_TEAM); |
184 | 188 |
} |
189 |
||
190 |
void HWForm::EditTeam() |
|
191 |
{ |
|
245 | 192 |
editedTeam = new HWTeam(ui.pageOptions->CBTeamName->currentText()); |
193 |
editedTeam->LoadFromFile(); |
|
194 |
editedTeam->SetToPage(this); |
|
290 | 195 |
GoToPage(ID_PAGE_SETUP_TEAM); |
184 | 196 |
} |
197 |
||
198 |
void HWForm::TeamSave() |
|
199 |
{ |
|
245 | 200 |
editedTeam->GetFromPage(this); |
201 |
editedTeam->SaveToFile(); |
|
202 |
delete editedTeam; |
|
203 |
UpdateTeamsLists(); |
|
290 | 204 |
GoBack(); |
184 | 205 |
} |
206 |
||
207 |
void HWForm::TeamDiscard() |
|
208 |
{ |
|
245 | 209 |
delete editedTeam; |
290 | 210 |
GoBack(); |
184 | 211 |
} |
212 |
||
213 |
void HWForm::SimpleGame() |
|
214 |
{ |
|
306 | 215 |
CreateGame(ui.pageLocalGame->gameCFG); |
184 | 216 |
game->StartQuick(); |
217 |
} |
|
218 |
||
219 |
void HWForm::PlayDemo() |
|
220 |
{ |
|
221 |
QListWidgetItem * curritem = ui.pagePlayDemo->DemosList->currentItem(); |
|
222 |
if (!curritem) |
|
223 |
{ |
|
224 |
QMessageBox::critical(this, |
|
225 |
tr("Error"), |
|
226 |
tr("Please, select demo from the list above"), |
|
227 |
tr("OK")); |
|
228 |
return ; |
|
229 |
} |
|
306 | 230 |
CreateGame(0); |
271 | 231 |
game->PlayDemo(cfgdir->absolutePath() + "/Demos/" + curritem->text() + ".hwd_" + cProtoVer); |
184 | 232 |
} |
233 |
||
234 |
void HWForm::NetConnect() |
|
235 |
{ |
|
236 |
hwnet = new HWNet(config); |
|
237 |
connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat())); |
|
238 |
connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &))); |
|
239 |
connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter())); |
|
240 |
connect(hwnet, SIGNAL(ChangeInTeams(const QStringList &)), this, SLOT(ChangeInNetTeams(const QStringList &))); |
|
241 |
hwnet->Connect(ui.pageNet->editIP->text(), 6667, ui.pageNet->editNetNick->text()); |
|
242 |
config->SaveOptions(); |
|
243 |
} |
|
244 |
||
245 |
void HWForm::NetDisconnect() |
|
246 |
{ |
|
247 |
hwnet->Disconnect(); |
|
290 | 248 |
GoBack(); |
184 | 249 |
} |
250 |
||
251 |
void HWForm::AddGame(const QString & chan) |
|
252 |
{ |
|
253 |
ui.pageNetChat->ChannelsList->addItem(chan); |
|
254 |
} |
|
255 |
||
256 |
void HWForm::NetGameEnter() |
|
257 |
{ |
|
290 | 258 |
GoToPage(ID_PAGE_NETCFG); |
184 | 259 |
} |
260 |
||
261 |
void HWForm::NetJoin() |
|
262 |
{ |
|
263 |
hwnet->JoinGame("#hw"); |
|
264 |
} |
|
265 |
||
266 |
void HWForm::NetCreate() |
|
267 |
{ |
|
268 |
hwnet->JoinGame("#hw"); |
|
269 |
} |
|
270 |
||
271 |
void HWForm::NetAddTeam() |
|
272 |
{ |
|
273 |
HWTeam team("DefaultTeam"); |
|
274 |
team.LoadFromFile(); |
|
275 |
hwnet->AddTeam(team); |
|
276 |
} |
|
277 |
||
278 |
void HWForm::NetStartGame() |
|
279 |
{ |
|
280 |
hwnet->StartGame(); |
|
281 |
} |
|
282 |
||
283 |
void HWForm::ChangeInNetTeams(const QStringList & teams) |
|
284 |
{ |
|
285 |
ui.pageNetGame->listNetTeams->clear(); |
|
286 |
ui.pageNetGame->listNetTeams->addItems(teams); |
|
287 |
} |
|
288 |
||
289 |
void HWForm::StartMPGame() |
|
290 |
{ |
|
306 | 291 |
CreateGame(ui.pageMultiplayer->gameCFG); |
292 |
||
184 | 293 |
list<HWTeam> teamslist=ui.pageMultiplayer->teamsSelect->getPlayingTeams(); |
294 |
for (list<HWTeam>::const_iterator it = teamslist.begin(); it != teamslist.end(); ++it ) { |
|
207 | 295 |
HWTeamTempParams params=ui.pageMultiplayer->teamsSelect->getTeamParams(it->TeamName); |
213 | 296 |
game->AddTeam(it->TeamName, params); |
184 | 297 |
} |
298 |
game->StartLocal(); |
|
299 |
} |
|
306 | 300 |
|
301 |
void HWForm::GameStateChanged(GameState gameState) |
|
302 |
{ |
|
303 |
if (gameState == gsStarted) |
|
307 | 304 |
{ |
306 | 305 |
GoToPage(ID_PAGE_GAMESTATS); |
307 | 306 |
ui.pageGameStats->labelGameStats->setText(""); |
307 |
} |
|
308 |
} |
|
309 |
||
310 |
void HWForm::AddStatText(const QString & msg) |
|
311 |
{ |
|
312 |
ui.pageGameStats->labelGameStats->setText( |
|
313 |
ui.pageGameStats->labelGameStats->text() + msg); |
|
306 | 314 |
} |
315 |
||
316 |
void HWForm::GameStats(char type, const QString & info) |
|
317 |
{ |
|
318 |
switch(type) { |
|
307 | 319 |
case 'r' : { |
320 |
AddStatText(QString("<h1 align=\"center\">%1</h1>").arg(info)); |
|
321 |
break; |
|
322 |
} |
|
323 |
case 'D' : { |
|
324 |
int i = info.indexOf(' '); |
|
325 |
QString message = QLabel::tr("<p>The best shot award was won by <b>%1</b> with <b>%2</b> pts.</p>") |
|
326 |
.arg(info.mid(i + 1), info.left(i)); |
|
327 |
AddStatText(message); |
|
328 |
break; |
|
329 |
} |
|
330 |
case 'K' : { |
|
331 |
QString message = QLabel::tr("<p>A total of <b>%1</b> Hedgehog(s) were killed during this round.</p>").arg(info); |
|
332 |
AddStatText(message); |
|
333 |
break; |
|
334 |
} |
|
306 | 335 |
} |
336 |
} |
|
337 |
||
338 |
void HWForm::CreateGame(GameCFGWidget * gamecfg) |
|
339 |
{ |
|
340 |
game = new HWGame(config, gamecfg); |
|
341 |
connect(game, SIGNAL(GameStateChanged(GameState)), this, SLOT(GameStateChanged(GameState))); |
|
342 |
connect(game, SIGNAL(GameStats(char, const QString &)), this, SLOT(GameStats(char, const QString &))); |
|
343 |
} |