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 |
|
20
|
149 |
connect(ui.BtnSPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
26
|
150 |
connect(ui.BtnDemosBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
20
|
151 |
connect(ui.BtnSetupBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
25
|
152 |
connect(ui.BtnMPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
20
|
153 |
connect(ui.BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer()));
|
|
154 |
connect(ui.BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup()));
|
25
|
155 |
connect(ui.BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer()));
|
26
|
156 |
connect(ui.BtnDemos, SIGNAL(clicked()), this, SLOT(GoToDemos()));
|
20
|
157 |
connect(ui.BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam()));
|
|
158 |
connect(ui.BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam()));
|
|
159 |
connect(ui.BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave()));
|
|
160 |
connect(ui.BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard()));
|
|
161 |
connect(ui.BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame()));
|
|
162 |
connect(ui.BtnSaveOptions, SIGNAL(clicked()), this, SLOT(SaveOptions()));
|
26
|
163 |
connect(ui.BtnPlayDemo, SIGNAL(clicked()), this, SLOT(PlayDemo()));
|
20
|
164 |
connect(ui.CBGrave, SIGNAL(activated(const QString &)), this, SLOT(CBGrave_activated(const QString &)));
|
|
165 |
connect(ui.CBFort, SIGNAL(activated(const QString &)), this, SLOT(CBFort_activated(const QString &)));
|
|
166 |
ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
|
|
167 |
}
|
|
168 |
|
|
169 |
void HWForm::GoToMain()
|
|
170 |
{
|
|
171 |
ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
|
|
172 |
}
|
|
173 |
|
|
174 |
void HWForm::GoToSinglePlayer()
|
|
175 |
{
|
|
176 |
ui.Pages->setCurrentIndex(ID_PAGE_SINGLEPLAYER);
|
|
177 |
}
|
|
178 |
|
|
179 |
void HWForm::GoToSetup()
|
|
180 |
{
|
|
181 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
182 |
}
|
|
183 |
|
25
|
184 |
void HWForm::GoToMultiplayer()
|
|
185 |
{
|
|
186 |
ui.Pages->setCurrentIndex(ID_PAGE_MULTIPLAYER);
|
|
187 |
}
|
26
|
188 |
|
|
189 |
void HWForm::GoToDemos()
|
|
190 |
{
|
|
191 |
QDir tmpdir;
|
|
192 |
tmpdir.cd(DATA_PATH);
|
|
193 |
tmpdir.cd("Demos");
|
|
194 |
tmpdir.setFilter(QDir::Files);
|
|
195 |
ui.DemosList->addItems(tmpdir.entryList(QStringList("*.hwd_1")).replaceInStrings(QRegExp("^(.*).hwd_1"), "\\1"));
|
|
196 |
ui.Pages->setCurrentIndex(ID_PAGE_DEMOS);
|
|
197 |
}
|
|
198 |
|
20
|
199 |
void HWForm::NewTeam()
|
|
200 |
{
|
25
|
201 |
tmpTeam = new HWTeam("unnamed");
|
21
|
202 |
tmpTeam->SetCfgDir(cfgdir.absolutePath());
|
|
203 |
tmpTeam->SetToPage(this);
|
20
|
204 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
|
|
205 |
}
|
|
206 |
|
|
207 |
void HWForm::EditTeam()
|
|
208 |
{
|
25
|
209 |
tmpTeam = new HWTeam(ui.CBTeamName->currentText());
|
21
|
210 |
tmpTeam->SetCfgDir(cfgdir.absolutePath());
|
|
211 |
tmpTeam->LoadFromFile();
|
|
212 |
tmpTeam->SetToPage(this);
|
20
|
213 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
|
|
214 |
}
|
|
215 |
|
|
216 |
void HWForm::TeamSave()
|
|
217 |
{
|
21
|
218 |
tmpTeam->GetFromPage(this);
|
|
219 |
tmpTeam->SaveToFile();
|
|
220 |
delete tmpTeam;
|
20
|
221 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
222 |
}
|
|
223 |
|
|
224 |
void HWForm::TeamDiscard()
|
|
225 |
{
|
|
226 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
227 |
}
|
|
228 |
|
|
229 |
void HWForm::SimpleGame()
|
|
230 |
{
|
26
|
231 |
game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
|
20
|
232 |
game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
|
|
233 |
game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
|
26
|
234 |
game->Start();
|
20
|
235 |
}
|
|
236 |
|
|
237 |
void HWForm::CBGrave_activated(const QString & gravename)
|
|
238 |
{
|
|
239 |
QPixmap pix(QString(DATA_PATH) + "/Graphics/Graves/" + gravename + ".png");
|
|
240 |
ui.GravePreview->setPixmap(pix.copy(0, 0, 32, 32));
|
|
241 |
}
|
|
242 |
|
|
243 |
void HWForm::CBFort_activated(const QString & fortname)
|
|
244 |
{
|
|
245 |
QPixmap pix(QString(DATA_PATH) + "/Forts/" + fortname + "L.png");
|
|
246 |
ui.FortPreview->setPixmap(pix);
|
|
247 |
}
|
|
248 |
|
|
249 |
void HWForm::SaveOptions()
|
|
250 |
{
|
|
251 |
QFile settings(cfgdir.absolutePath() + "/options");
|
|
252 |
if (!settings.open(QIODevice::WriteOnly))
|
|
253 |
{
|
25
|
254 |
QMessageBox::critical(this,
|
20
|
255 |
tr("Error"),
|
|
256 |
tr("Cannot save options to file %s").arg(settings.fileName()),
|
|
257 |
tr("Quit"));
|
|
258 |
return ;
|
|
259 |
}
|
|
260 |
QTextStream stream(&settings);
|
|
261 |
stream.setCodec("UTF-8");
|
|
262 |
stream << "; Generated by Hedgewars, do not modify" << endl;
|
|
263 |
stream << "resolution " << ui.CBResolution->currentIndex() << endl;
|
|
264 |
stream << "fullscreen " << ui.CBFullscreen->isChecked() << endl;
|
|
265 |
settings.close();
|
|
266 |
}
|
26
|
267 |
|
|
268 |
void HWForm::PlayDemo()
|
|
269 |
{
|
|
270 |
QListWidgetItem * curritem = ui.DemosList->currentItem();
|
|
271 |
if (!curritem)
|
|
272 |
{
|
|
273 |
QMessageBox::critical(this,
|
|
274 |
tr("Error"),
|
|
275 |
tr("Please, select demo from the list above"),
|
|
276 |
tr("OK"));
|
|
277 |
return ;
|
|
278 |
}
|
|
279 |
game = new HWGame(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
|
|
280 |
game->PlayDemo(QString(DATA_PATH) + "/Demos/" + curritem->text() + ".hwd_1");
|
|
281 |
}
|
|
282 |
|
|
283 |
|