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