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