20
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*/
|
|
33 |
|
|
34 |
#include <QtGui>
|
|
35 |
#include <QStringList>
|
|
36 |
#include <QProcess>
|
|
37 |
#include <QDir>
|
|
38 |
#include <QPixmap>
|
|
39 |
#include <QRegExp>
|
|
40 |
#include <QIcon>
|
|
41 |
#include <QFile>
|
|
42 |
#include <QTextStream>
|
|
43 |
|
|
44 |
#include "hwform.h"
|
|
45 |
#include "sdlkeys.h"
|
|
46 |
#include "hwconsts.h"
|
25
|
47 |
//#include "gamecmds.h"
|
20
|
48 |
|
|
49 |
HWForm::HWForm(QWidget *parent)
|
|
50 |
: QMainWindow(parent)
|
|
51 |
{
|
|
52 |
ui.setupUi(this);
|
|
53 |
TeamNameEdit = new QLineEdit(ui.GBoxTeam);
|
|
54 |
TeamNameEdit->setGeometry(QRect(10, 20, 141, 20));
|
|
55 |
TeamNameEdit->setMaxLength(15);
|
|
56 |
for(int i = 0; i < 8; i++)
|
|
57 |
{
|
|
58 |
HHNameEdit[i] = new QLineEdit(ui.GBoxHedgehogs);
|
|
59 |
HHNameEdit[i]->setGeometry(QRect(10, 20 + i * 30, 141, 20));
|
|
60 |
HHNameEdit[i]->setMaxLength(15);
|
|
61 |
}
|
25
|
62 |
|
20
|
63 |
QStringList binds;
|
|
64 |
for(int i = 0; strlen(sdlkeys[i][1]) > 0; i++)
|
|
65 |
{
|
|
66 |
binds << sdlkeys[i][1];
|
|
67 |
}
|
25
|
68 |
|
26
|
69 |
quint16 widind = 0, top = 0;
|
|
70 |
for(quint8 i = 0; i < BINDS_NUMBER; i++)
|
20
|
71 |
{
|
26
|
72 |
LBind[i] = new QLabel(ui.BindsBox->widget(widind));
|
|
73 |
LBind[i]->setGeometry(QRect(10, top + 3, 60, 20));
|
20
|
74 |
LBind[i]->setText(cbinds[i].name);
|
|
75 |
LBind[i]->setAlignment(Qt::AlignRight);
|
26
|
76 |
CBBind[i] = new QComboBox(ui.BindsBox->widget(widind));
|
|
77 |
CBBind[i]->setGeometry(QRect(80, top, 80, 20));
|
20
|
78 |
CBBind[i]->addItems(binds);
|
26
|
79 |
if (cbinds[i].chwidget)
|
|
80 |
{
|
|
81 |
top = 0;
|
|
82 |
widind++;
|
|
83 |
} else
|
|
84 |
{
|
|
85 |
top += 30;
|
|
86 |
}
|
20
|
87 |
}
|
25
|
88 |
|
20
|
89 |
QDir tmpdir;
|
|
90 |
tmpdir.cd(DATA_PATH);
|
|
91 |
tmpdir.cd("Forts");
|
|
92 |
tmpdir.setFilter(QDir::Files);
|
25
|
93 |
|
20
|
94 |
ui.CBFort->addItems(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L.png"), "\\1"));
|
25
|
95 |
|
20
|
96 |
tmpdir.cd("../Graphics/Graves");
|
|
97 |
QStringList list = tmpdir.entryList(QStringList("*.png"));
|
|
98 |
for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
|
|
99 |
{
|
|
100 |
ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
|
|
101 |
}
|
|
102 |
|
|
103 |
cfgdir.setPath(cfgdir.homePath());
|
|
104 |
if (!cfgdir.exists(".hedgewars"))
|
|
105 |
{
|
|
106 |
if (!cfgdir.mkdir(".hedgewars"))
|
|
107 |
{
|
25
|
108 |
QMessageBox::critical(this,
|
20
|
109 |
tr("Error"),
|
|
110 |
tr("Cannot create directory %s").arg("/.hedgewars"),
|
|
111 |
tr("Quit"));
|
|
112 |
}
|
|
113 |
return ;
|
|
114 |
}
|
|
115 |
cfgdir.cd(".hedgewars");
|
25
|
116 |
|
21
|
117 |
list = cfgdir.entryList(QStringList("*.cfg"));
|
|
118 |
for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
|
|
119 |
{
|
|
120 |
ui.CBTeamName->addItem((*it).replace(QRegExp("^(.*).cfg"), "\\1"));
|
|
121 |
}
|
25
|
122 |
|
|
123 |
|
20
|
124 |
QFile settings(cfgdir.absolutePath() + "/options");
|
25
|
125 |
if (settings.open(QIODevice::ReadOnly))
|
20
|
126 |
{
|
25
|
127 |
QTextStream stream(&settings);
|
|
128 |
stream.setCodec("UTF-8");
|
|
129 |
QString str;
|
|
130 |
|
|
131 |
while (!stream.atEnd())
|
20
|
132 |
{
|
25
|
133 |
str = stream.readLine();
|
|
134 |
if (str.startsWith(";")) continue;
|
|
135 |
if (str.startsWith("resolution "))
|
|
136 |
{
|
|
137 |
str.remove(0, 11);
|
|
138 |
ui.CBResolution->setCurrentIndex(str.toLong());
|
|
139 |
} else
|
|
140 |
if (str.startsWith("fullscreen "))
|
|
141 |
{
|
|
142 |
str.remove(0, 11);
|
|
143 |
ui.CBFullscreen->setChecked(str.toLong());
|
|
144 |
}
|
20
|
145 |
}
|
25
|
146 |
settings.close();
|
20
|
147 |
}
|
26
|
148 |
|
28
|
149 |
connect(ui.BtnSPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
|
150 |
connect(ui.BtnDemosBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
|
151 |
connect(ui.BtnSetupBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
|
152 |
connect(ui.BtnMPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
|
153 |
connect(ui.BtnNetBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
|
154 |
connect(ui.BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer()));
|
|
155 |
connect(ui.BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup()));
|
|
156 |
connect(ui.BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer()));
|
|
157 |
connect(ui.BtnDemos, SIGNAL(clicked()), this, SLOT(GoToDemos()));
|
|
158 |
connect(ui.BtnNet, SIGNAL(clicked()), this, SLOT(GoToNet()));
|
|
159 |
connect(ui.BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam()));
|
|
160 |
connect(ui.BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam()));
|
|
161 |
connect(ui.BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave()));
|
|
162 |
connect(ui.BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard()));
|
|
163 |
connect(ui.BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame()));
|
|
164 |
connect(ui.BtnSaveOptions, SIGNAL(clicked()), this, SLOT(SaveOptions()));
|
|
165 |
connect(ui.BtnPlayDemo, SIGNAL(clicked()), this, SLOT(PlayDemo()));
|
|
166 |
connect(ui.BtnNetConnect, SIGNAL(clicked()), this, SLOT(NetConnect()));
|
|
167 |
connect(ui.BtnNetChatDisconnect, SIGNAL(clicked()), this, SLOT(NetDisconnect()));
|
|
168 |
connect(ui.CBGrave, SIGNAL(activated(const QString &)), this, SLOT(CBGrave_activated(const QString &)));
|
|
169 |
connect(ui.CBFort, SIGNAL(activated(const QString &)), this, SLOT(CBFort_activated(const QString &)));
|
20
|
170 |
ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
|
|
171 |
}
|
|
172 |
|
|
173 |
void HWForm::GoToMain()
|
|
174 |
{
|
|
175 |
ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
|
|
176 |
}
|
|
177 |
|
|
178 |
void HWForm::GoToSinglePlayer()
|
|
179 |
{
|
|
180 |
ui.Pages->setCurrentIndex(ID_PAGE_SINGLEPLAYER);
|
|
181 |
}
|
|
182 |
|
|
183 |
void HWForm::GoToSetup()
|
|
184 |
{
|
|
185 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
186 |
}
|
|
187 |
|
25
|
188 |
void HWForm::GoToMultiplayer()
|
|
189 |
{
|
|
190 |
ui.Pages->setCurrentIndex(ID_PAGE_MULTIPLAYER);
|
|
191 |
}
|
26
|
192 |
|
|
193 |
void HWForm::GoToDemos()
|
|
194 |
{
|
|
195 |
QDir tmpdir;
|
|
196 |
tmpdir.cd(DATA_PATH);
|
|
197 |
tmpdir.cd("Demos");
|
|
198 |
tmpdir.setFilter(QDir::Files);
|
28
|
199 |
ui.DemosList->clear();
|
26
|
200 |
ui.DemosList->addItems(tmpdir.entryList(QStringList("*.hwd_1")).replaceInStrings(QRegExp("^(.*).hwd_1"), "\\1"));
|
|
201 |
ui.Pages->setCurrentIndex(ID_PAGE_DEMOS);
|
|
202 |
}
|
|
203 |
|
28
|
204 |
void HWForm::GoToNet()
|
|
205 |
{
|
|
206 |
ui.Pages->setCurrentIndex(ID_PAGE_NET);
|
|
207 |
}
|
|
208 |
|
|
209 |
void HWForm::GoToNetChat()
|
|
210 |
{
|
|
211 |
ui.Pages->setCurrentIndex(ID_PAGE_NETCHAT);
|
|
212 |
}
|
|
213 |
|
20
|
214 |
void HWForm::NewTeam()
|
|
215 |
{
|
25
|
216 |
tmpTeam = new HWTeam("unnamed");
|
21
|
217 |
tmpTeam->SetCfgDir(cfgdir.absolutePath());
|
|
218 |
tmpTeam->SetToPage(this);
|
20
|
219 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
|
|
220 |
}
|
|
221 |
|
|
222 |
void HWForm::EditTeam()
|
|
223 |
{
|
25
|
224 |
tmpTeam = new HWTeam(ui.CBTeamName->currentText());
|
21
|
225 |
tmpTeam->SetCfgDir(cfgdir.absolutePath());
|
|
226 |
tmpTeam->LoadFromFile();
|
|
227 |
tmpTeam->SetToPage(this);
|
20
|
228 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
|
|
229 |
}
|
|
230 |
|
|
231 |
void HWForm::TeamSave()
|
|
232 |
{
|
21
|
233 |
tmpTeam->GetFromPage(this);
|
|
234 |
tmpTeam->SaveToFile();
|
|
235 |
delete tmpTeam;
|
20
|
236 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
237 |
}
|
|
238 |
|
|
239 |
void HWForm::TeamDiscard()
|
|
240 |
{
|
|
241 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
242 |
}
|
|
243 |
|
|
244 |
void HWForm::SimpleGame()
|
|
245 |
{
|
26
|
246 |
game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
|
20
|
247 |
game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
|
|
248 |
game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
|
26
|
249 |
game->Start();
|
20
|
250 |
}
|
|
251 |
|
|
252 |
void HWForm::CBGrave_activated(const QString & gravename)
|
|
253 |
{
|
|
254 |
QPixmap pix(QString(DATA_PATH) + "/Graphics/Graves/" + gravename + ".png");
|
|
255 |
ui.GravePreview->setPixmap(pix.copy(0, 0, 32, 32));
|
|
256 |
}
|
|
257 |
|
|
258 |
void HWForm::CBFort_activated(const QString & fortname)
|
|
259 |
{
|
|
260 |
QPixmap pix(QString(DATA_PATH) + "/Forts/" + fortname + "L.png");
|
|
261 |
ui.FortPreview->setPixmap(pix);
|
|
262 |
}
|
|
263 |
|
|
264 |
void HWForm::SaveOptions()
|
|
265 |
{
|
|
266 |
QFile settings(cfgdir.absolutePath() + "/options");
|
|
267 |
if (!settings.open(QIODevice::WriteOnly))
|
|
268 |
{
|
25
|
269 |
QMessageBox::critical(this,
|
20
|
270 |
tr("Error"),
|
|
271 |
tr("Cannot save options to file %s").arg(settings.fileName()),
|
|
272 |
tr("Quit"));
|
|
273 |
return ;
|
|
274 |
}
|
|
275 |
QTextStream stream(&settings);
|
|
276 |
stream.setCodec("UTF-8");
|
|
277 |
stream << "; Generated by Hedgewars, do not modify" << endl;
|
|
278 |
stream << "resolution " << ui.CBResolution->currentIndex() << endl;
|
|
279 |
stream << "fullscreen " << ui.CBFullscreen->isChecked() << endl;
|
|
280 |
settings.close();
|
|
281 |
}
|
26
|
282 |
|
|
283 |
void HWForm::PlayDemo()
|
|
284 |
{
|
|
285 |
QListWidgetItem * curritem = ui.DemosList->currentItem();
|
|
286 |
if (!curritem)
|
|
287 |
{
|
|
288 |
QMessageBox::critical(this,
|
|
289 |
tr("Error"),
|
|
290 |
tr("Please, select demo from the list above"),
|
|
291 |
tr("OK"));
|
|
292 |
return ;
|
|
293 |
}
|
|
294 |
game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
|
|
295 |
game->PlayDemo(QString(DATA_PATH) + "/Demos/" + curritem->text() + ".hwd_1");
|
|
296 |
}
|
|
297 |
|
28
|
298 |
void HWForm::NetConnect()
|
|
299 |
{
|
|
300 |
hwnet = new HWNet();
|
|
301 |
hwnet->Connect("localhost", 6667);
|
|
302 |
connect(hwnet, SIGNAL(Connected()), this, SLOT(GoToNetChat()));
|
|
303 |
connect(hwnet, SIGNAL(AddGame(const QString &)), this, SLOT(AddGame(const QString &)));
|
|
304 |
}
|
26
|
305 |
|
28
|
306 |
void HWForm::NetDisconnect()
|
|
307 |
{
|
|
308 |
hwnet->Disconnect();
|
|
309 |
GoToNet();
|
|
310 |
}
|
|
311 |
|
|
312 |
void HWForm::AddGame(const QString & chan)
|
|
313 |
{
|
|
314 |
ui.ChannelsList->addItem(chan);
|
|
315 |
}
|