author | unc0rr |
Fri, 26 Jan 2007 22:36:21 +0000 | |
changeset 369 | 2aed85310727 |
parent 331 | 6bfc326e4976 |
child 452 | 45fbb9df0c99 |
permissions | -rw-r--r-- |
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 |
||
331 | 84 |
loadMap(index); |
85 |
||
86 |
emit mapChanged(chooseMap->currentText()); |
|
87 |
} |
|
88 |
||
89 |
void HWMapContainer::loadMap(int index) |
|
90 |
{ |
|
249 | 91 |
QPixmap mapImage; |
320 | 92 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 93 |
changeImage(); |
94 |
chooseMap->setCurrentIndex(0); |
|
95 |
return; |
|
96 |
} |
|
320 | 97 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
98 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
99 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
100 |
QTextStream input(&mapCfgFile); |
|
101 |
input >> theme; |
|
102 |
mapCfgFile.close(); |
|
103 |
} |
|
184 | 104 |
} |
105 |
||
106 |
void HWMapContainer::changeImage() |
|
107 |
{ |
|
320 | 108 |
pMap = new HWMap(); |
184 | 109 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
110 |
pMap->getImage(m_seed.toStdString()); |
|
320 | 111 |
theme = (Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel"; |
184 | 112 |
} |
113 |
||
114 |
QString HWMapContainer::getCurrentSeed() const |
|
115 |
{ |
|
116 |
return m_seed; |
|
117 |
} |
|
118 |
||
249 | 119 |
QString HWMapContainer::getCurrentMap() const |
120 |
{ |
|
320 | 121 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 122 |
return chooseMap->currentText(); |
123 |
} |
|
124 |
||
125 |
QString HWMapContainer::getCurrentTheme() const |
|
126 |
{ |
|
320 | 127 |
return theme; |
249 | 128 |
} |
129 |
||
184 | 130 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
131 |
{ |
|
132 |
//imageButt->setIconSize(imageButt->size()); |
|
133 |
} |
|
320 | 134 |
|
135 |
void HWMapContainer::setSeed(const QString & seed) |
|
136 |
{ |
|
137 |
m_seed = seed; |
|
138 |
changeImage(); |
|
139 |
} |
|
140 |
||
141 |
void HWMapContainer::setMap(const QString & map) |
|
142 |
{ |
|
331 | 143 |
int id = chooseMap->findText(map); |
144 |
if(id >= 0) { |
|
145 |
chooseMap->setCurrentIndex(id); |
|
146 |
loadMap(id); |
|
147 |
} |
|
320 | 148 |
} |
149 |
||
150 |
void HWMapContainer::setTheme(const QString & theme) |
|
151 |
{ |
|
152 |
this->theme = theme; |
|
153 |
} |
|
154 |
||
155 |
void HWMapContainer::setRandomSeed() |
|
156 |
{ |
|
157 |
m_seed = QUuid::createUuid().toString(); |
|
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
325
diff
changeset
|
158 |
emit seedChanged(m_seed); |
320 | 159 |
changeImage(); |
329
4c3aad46baa5
Send game parameters by net... Currently it leads to infinite loop, flooding traffic with messages about changes
unc0rr
parents:
325
diff
changeset
|
160 |
emit themeChanged(theme); |
320 | 161 |
} |