author | unc0rr |
Sun, 14 Jan 2007 14:08:40 +0000 | |
changeset 329 | 4c3aad46baa5 |
parent 325 | 17c860483407 |
child 331 | 6bfc326e4976 |
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 |
||
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))); |
104 |
pMap->getImage(m_seed.toStdString()); |
|
320 | 105 |
theme = (Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel"; |
184 | 106 |
} |
107 |
||
108 |
QString HWMapContainer::getCurrentSeed() const |
|
109 |
{ |
|
110 |
return m_seed; |
|
111 |
} |
|
112 |
||
249 | 113 |
QString HWMapContainer::getCurrentMap() const |
114 |
{ |
|
320 | 115 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 116 |
return chooseMap->currentText(); |
117 |
} |
|
118 |
||
119 |
QString HWMapContainer::getCurrentTheme() const |
|
120 |
{ |
|
320 | 121 |
return theme; |
249 | 122 |
} |
123 |
||
184 | 124 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
125 |
{ |
|
126 |
//imageButt->setIconSize(imageButt->size()); |
|
127 |
} |
|
320 | 128 |
|
129 |
void HWMapContainer::setSeed(const QString & seed) |
|
130 |
{ |
|
131 |
m_seed = seed; |
|
132 |
changeImage(); |
|
133 |
} |
|
134 |
||
135 |
void HWMapContainer::setMap(const QString & map) |
|
136 |
{ |
|
137 |
||
138 |
} |
|
139 |
||
140 |
void HWMapContainer::setTheme(const QString & theme) |
|
141 |
{ |
|
142 |
this->theme = theme; |
|
143 |
} |
|
144 |
||
145 |
void HWMapContainer::setRandomSeed() |
|
146 |
{ |
|
147 |
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
|
148 |
emit seedChanged(m_seed); |
320 | 149 |
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
|
150 |
emit themeChanged(theme); |
320 | 151 |
} |