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 |
|
20
|
69 |
for(int i = 0; i < BINDS_NUMBER; i++)
|
|
70 |
{
|
|
71 |
LBind[i] = new QLabel(ui.GBoxBinds);
|
|
72 |
LBind[i]->setGeometry(QRect(10, 23 + i * 30, 60, 20));
|
|
73 |
LBind[i]->setText(cbinds[i].name);
|
|
74 |
LBind[i]->setAlignment(Qt::AlignRight);
|
|
75 |
CBBind[i] = new QComboBox(ui.GBoxBinds);
|
|
76 |
CBBind[i]->setGeometry(QRect(80, 20 + i * 30, 80, 20));
|
|
77 |
CBBind[i]->addItems(binds);
|
|
78 |
}
|
25
|
79 |
|
20
|
80 |
QDir tmpdir;
|
|
81 |
tmpdir.cd(DATA_PATH);
|
|
82 |
tmpdir.cd("Forts");
|
|
83 |
tmpdir.setFilter(QDir::Files);
|
25
|
84 |
|
20
|
85 |
ui.CBFort->addItems(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L.png"), "\\1"));
|
25
|
86 |
|
20
|
87 |
tmpdir.cd("../Graphics/Graves");
|
|
88 |
QStringList list = tmpdir.entryList(QStringList("*.png"));
|
|
89 |
for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
|
|
90 |
{
|
|
91 |
ui.CBGrave->addItem((*it).replace(QRegExp("^(.*).png"), "\\1"));
|
|
92 |
}
|
|
93 |
|
|
94 |
cfgdir.setPath(cfgdir.homePath());
|
|
95 |
if (!cfgdir.exists(".hedgewars"))
|
|
96 |
{
|
|
97 |
if (!cfgdir.mkdir(".hedgewars"))
|
|
98 |
{
|
25
|
99 |
QMessageBox::critical(this,
|
20
|
100 |
tr("Error"),
|
|
101 |
tr("Cannot create directory %s").arg("/.hedgewars"),
|
|
102 |
tr("Quit"));
|
|
103 |
}
|
|
104 |
return ;
|
|
105 |
}
|
|
106 |
cfgdir.cd(".hedgewars");
|
25
|
107 |
|
21
|
108 |
list = cfgdir.entryList(QStringList("*.cfg"));
|
|
109 |
for (QStringList::Iterator it = list.begin(); it != list.end(); ++it )
|
|
110 |
{
|
|
111 |
ui.CBTeamName->addItem((*it).replace(QRegExp("^(.*).cfg"), "\\1"));
|
|
112 |
}
|
25
|
113 |
|
|
114 |
|
20
|
115 |
QFile settings(cfgdir.absolutePath() + "/options");
|
25
|
116 |
if (settings.open(QIODevice::ReadOnly))
|
20
|
117 |
{
|
25
|
118 |
QTextStream stream(&settings);
|
|
119 |
stream.setCodec("UTF-8");
|
|
120 |
QString str;
|
|
121 |
|
|
122 |
while (!stream.atEnd())
|
20
|
123 |
{
|
25
|
124 |
str = stream.readLine();
|
|
125 |
if (str.startsWith(";")) continue;
|
|
126 |
if (str.startsWith("resolution "))
|
|
127 |
{
|
|
128 |
str.remove(0, 11);
|
|
129 |
ui.CBResolution->setCurrentIndex(str.toLong());
|
|
130 |
} else
|
|
131 |
if (str.startsWith("fullscreen "))
|
|
132 |
{
|
|
133 |
str.remove(0, 11);
|
|
134 |
ui.CBFullscreen->setChecked(str.toLong());
|
|
135 |
}
|
20
|
136 |
}
|
25
|
137 |
settings.close();
|
20
|
138 |
}
|
|
139 |
connect(ui.BtnSPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
|
140 |
connect(ui.BtnSetupBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
25
|
141 |
connect(ui.BtnMPBack, SIGNAL(clicked()), this, SLOT(GoToMain()));
|
20
|
142 |
connect(ui.BtnSinglePlayer, SIGNAL(clicked()), this, SLOT(GoToSinglePlayer()));
|
|
143 |
connect(ui.BtnSetup, SIGNAL(clicked()), this, SLOT(GoToSetup()));
|
25
|
144 |
connect(ui.BtnMultiplayer, SIGNAL(clicked()), this, SLOT(GoToMultiplayer()));
|
20
|
145 |
connect(ui.BtnNewTeam, SIGNAL(clicked()), this, SLOT(NewTeam()));
|
|
146 |
connect(ui.BtnEditTeam, SIGNAL(clicked()), this, SLOT(EditTeam()));
|
|
147 |
connect(ui.BtnTeamSave, SIGNAL(clicked()), this, SLOT(TeamSave()));
|
|
148 |
connect(ui.BtnTeamDiscard, SIGNAL(clicked()), this, SLOT(TeamDiscard()));
|
|
149 |
connect(ui.BtnSimpleGame, SIGNAL(clicked()), this, SLOT(SimpleGame()));
|
|
150 |
connect(ui.BtnSaveOptions, SIGNAL(clicked()), this, SLOT(SaveOptions()));
|
|
151 |
connect(ui.CBGrave, SIGNAL(activated(const QString &)), this, SLOT(CBGrave_activated(const QString &)));
|
|
152 |
connect(ui.CBFort, SIGNAL(activated(const QString &)), this, SLOT(CBFort_activated(const QString &)));
|
|
153 |
ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
|
|
154 |
}
|
|
155 |
|
|
156 |
void HWForm::GoToMain()
|
|
157 |
{
|
|
158 |
ui.Pages->setCurrentIndex(ID_PAGE_MAIN);
|
|
159 |
}
|
|
160 |
|
|
161 |
void HWForm::GoToSinglePlayer()
|
|
162 |
{
|
|
163 |
ui.Pages->setCurrentIndex(ID_PAGE_SINGLEPLAYER);
|
|
164 |
}
|
|
165 |
|
|
166 |
void HWForm::GoToSetup()
|
|
167 |
{
|
|
168 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
169 |
}
|
|
170 |
|
25
|
171 |
void HWForm::GoToMultiplayer()
|
|
172 |
{
|
|
173 |
ui.Pages->setCurrentIndex(ID_PAGE_MULTIPLAYER);
|
|
174 |
}
|
20
|
175 |
void HWForm::NewTeam()
|
|
176 |
{
|
25
|
177 |
tmpTeam = new HWTeam("unnamed");
|
21
|
178 |
tmpTeam->SetCfgDir(cfgdir.absolutePath());
|
|
179 |
tmpTeam->SetToPage(this);
|
20
|
180 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
|
|
181 |
}
|
|
182 |
|
|
183 |
void HWForm::EditTeam()
|
|
184 |
{
|
25
|
185 |
tmpTeam = new HWTeam(ui.CBTeamName->currentText());
|
21
|
186 |
tmpTeam->SetCfgDir(cfgdir.absolutePath());
|
|
187 |
tmpTeam->LoadFromFile();
|
|
188 |
tmpTeam->SetToPage(this);
|
20
|
189 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP_TEAM);
|
|
190 |
}
|
|
191 |
|
|
192 |
void HWForm::TeamSave()
|
|
193 |
{
|
21
|
194 |
tmpTeam->GetFromPage(this);
|
|
195 |
tmpTeam->SaveToFile();
|
|
196 |
delete tmpTeam;
|
20
|
197 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
198 |
}
|
|
199 |
|
|
200 |
void HWForm::TeamDiscard()
|
|
201 |
{
|
|
202 |
ui.Pages->setCurrentIndex(ID_PAGE_SETUP);
|
|
203 |
}
|
|
204 |
|
|
205 |
void HWForm::SimpleGame()
|
|
206 |
{
|
|
207 |
game = new HWGame();
|
|
208 |
game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
|
|
209 |
game->AddTeam(cfgdir.absolutePath() + "/team.cfg");
|
21
|
210 |
game->Start(ui.CBResolution->currentIndex(), ui.CBFullscreen->isChecked());
|
20
|
211 |
}
|
|
212 |
|
|
213 |
void HWForm::CBGrave_activated(const QString & gravename)
|
|
214 |
{
|
|
215 |
QPixmap pix(QString(DATA_PATH) + "/Graphics/Graves/" + gravename + ".png");
|
|
216 |
ui.GravePreview->setPixmap(pix.copy(0, 0, 32, 32));
|
|
217 |
}
|
|
218 |
|
|
219 |
void HWForm::CBFort_activated(const QString & fortname)
|
|
220 |
{
|
|
221 |
QPixmap pix(QString(DATA_PATH) + "/Forts/" + fortname + "L.png");
|
|
222 |
ui.FortPreview->setPixmap(pix);
|
|
223 |
}
|
|
224 |
|
|
225 |
void HWForm::SaveOptions()
|
|
226 |
{
|
|
227 |
QFile settings(cfgdir.absolutePath() + "/options");
|
|
228 |
if (!settings.open(QIODevice::WriteOnly))
|
|
229 |
{
|
25
|
230 |
QMessageBox::critical(this,
|
20
|
231 |
tr("Error"),
|
|
232 |
tr("Cannot save options to file %s").arg(settings.fileName()),
|
|
233 |
tr("Quit"));
|
|
234 |
return ;
|
|
235 |
}
|
|
236 |
QTextStream stream(&settings);
|
|
237 |
stream.setCodec("UTF-8");
|
|
238 |
stream << "; Generated by Hedgewars, do not modify" << endl;
|
|
239 |
stream << "resolution " << ui.CBResolution->currentIndex() << endl;
|
|
240 |
stream << "fullscreen " << ui.CBFullscreen->isChecked() << endl;
|
|
241 |
settings.close();
|
|
242 |
}
|