184
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2006 Igor Ulyanov <iulyanov@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 "mapContainer.h"
|
|
20 |
|
|
21 |
#include <QPushButton>
|
|
22 |
#include <QBuffer>
|
|
23 |
#include <QUuid>
|
|
24 |
#include <QBitmap>
|
|
25 |
#include <QPainter>
|
|
26 |
#include <QLinearGradient>
|
|
27 |
#include <QColor>
|
249
|
28 |
#include <QTextStream>
|
|
29 |
|
|
30 |
#include "hwconsts.h"
|
184
|
31 |
|
|
32 |
HWMapContainer::HWMapContainer(QWidget * parent) :
|
|
33 |
QWidget(parent), mainLayout(this)
|
|
34 |
{
|
|
35 |
imageButt=new QPushButton(this);
|
320
|
36 |
imageButt->setFixedSize(256, 128);
|
184
|
37 |
imageButt->setFlat(true);
|
|
38 |
imageButt->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
|
39 |
mainLayout.addWidget(imageButt);
|
320
|
40 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed()));
|
|
41 |
setRandomSeed();
|
249
|
42 |
|
|
43 |
chooseMap=new QComboBox(this);
|
|
44 |
QDir tmpdir;
|
|
45 |
tmpdir.cd(datadir->absolutePath());
|
|
46 |
tmpdir.cd("Maps");
|
|
47 |
tmpdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
48 |
QStringList mapList=tmpdir.entryList(QStringList("*"));
|
251
|
49 |
mapList.push_front(QComboBox::tr("generated map..."));
|
249
|
50 |
chooseMap->addItems(mapList);
|
|
51 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
|
|
52 |
|
|
53 |
mainLayout.addWidget(chooseMap);
|
184
|
54 |
}
|
|
55 |
|
|
56 |
void HWMapContainer::setImage(const QImage newImage)
|
|
57 |
{
|
|
58 |
QPixmap px(256, 128);
|
|
59 |
QPixmap pxres(256, 128);
|
|
60 |
QPainter p(&pxres);
|
|
61 |
|
|
62 |
px.fill(Qt::yellow);
|
|
63 |
QBitmap bm = QBitmap::fromImage(newImage);
|
|
64 |
px.setMask(bm);
|
|
65 |
|
|
66 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128));
|
216
|
67 |
linearGrad.setColorAt(1, QColor(0, 0, 192));
|
|
68 |
linearGrad.setColorAt(0, QColor(66, 115, 225));
|
184
|
69 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad);
|
|
70 |
p.drawPixmap(QPoint(0, 0), px);
|
|
71 |
|
|
72 |
imageButt->setIcon(pxres);
|
|
73 |
imageButt->setIconSize(QSize(256, 128));
|
249
|
74 |
chooseMap->setCurrentIndex(0);
|
|
75 |
}
|
|
76 |
|
|
77 |
void HWMapContainer::mapChanged(int index)
|
|
78 |
{
|
|
79 |
if(!index) {
|
|
80 |
changeImage();
|
|
81 |
return;
|
|
82 |
}
|
|
83 |
|
|
84 |
QPixmap mapImage;
|
320
|
85 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) {
|
249
|
86 |
changeImage();
|
|
87 |
chooseMap->setCurrentIndex(0);
|
|
88 |
return;
|
|
89 |
}
|
320
|
90 |
imageButt->setIcon(mapImage.scaled(256, 128));
|
|
91 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg");
|
|
92 |
if (mapCfgFile.open(QFile::ReadOnly)) {
|
|
93 |
QTextStream input(&mapCfgFile);
|
|
94 |
input >> theme;
|
|
95 |
mapCfgFile.close();
|
|
96 |
}
|
325
|
97 |
emit mapChanged(chooseMap->currentText());
|
184
|
98 |
}
|
|
99 |
|
|
100 |
void HWMapContainer::changeImage()
|
|
101 |
{
|
320
|
102 |
pMap = new HWMap();
|
184
|
103 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage)));
|
325
|
104 |
emit seedChanged(m_seed);
|
184
|
105 |
pMap->getImage(m_seed.toStdString());
|
320
|
106 |
theme = (Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel";
|
325
|
107 |
emit themeChanged(theme);
|
184
|
108 |
}
|
|
109 |
|
|
110 |
QString HWMapContainer::getCurrentSeed() const
|
|
111 |
{
|
|
112 |
return m_seed;
|
|
113 |
}
|
|
114 |
|
249
|
115 |
QString HWMapContainer::getCurrentMap() const
|
|
116 |
{
|
320
|
117 |
if(!chooseMap->currentIndex()) return QString();
|
249
|
118 |
return chooseMap->currentText();
|
|
119 |
}
|
|
120 |
|
|
121 |
QString HWMapContainer::getCurrentTheme() const
|
|
122 |
{
|
320
|
123 |
return theme;
|
249
|
124 |
}
|
|
125 |
|
184
|
126 |
void HWMapContainer::resizeEvent ( QResizeEvent * event )
|
|
127 |
{
|
|
128 |
//imageButt->setIconSize(imageButt->size());
|
|
129 |
}
|
320
|
130 |
|
|
131 |
void HWMapContainer::setSeed(const QString & seed)
|
|
132 |
{
|
|
133 |
m_seed = seed;
|
|
134 |
changeImage();
|
|
135 |
}
|
|
136 |
|
|
137 |
void HWMapContainer::setMap(const QString & map)
|
|
138 |
{
|
|
139 |
|
|
140 |
}
|
|
141 |
|
|
142 |
void HWMapContainer::setTheme(const QString & theme)
|
|
143 |
{
|
|
144 |
this->theme = theme;
|
|
145 |
}
|
|
146 |
|
|
147 |
void HWMapContainer::setRandomSeed()
|
|
148 |
{
|
|
149 |
m_seed = QUuid::createUuid().toString();
|
|
150 |
changeImage();
|
|
151 |
}
|