author | displacer |
Wed, 19 Dec 2007 07:37:17 +0000 | |
changeset 679 | 8187912107ac |
parent 677 | 9d0bcc3c903a |
child 682 | a37b6966de84 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
486 | 3 |
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> |
184 | 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> |
677
9d0bcc3c903a
Save some vertical pixels using fewer margin in HWMapContainer
unc0rr
parents:
530
diff
changeset
|
29 |
#include <QApplication> |
249 | 30 |
|
31 |
#include "hwconsts.h" |
|
184 | 32 |
|
33 |
HWMapContainer::HWMapContainer(QWidget * parent) : |
|
34 |
QWidget(parent), mainLayout(this) |
|
35 |
{ |
|
679 | 36 |
#if QT_VERSION >= 0x040300 |
677
9d0bcc3c903a
Save some vertical pixels using fewer margin in HWMapContainer
unc0rr
parents:
530
diff
changeset
|
37 |
mainLayout.setContentsMargins (0, 1, 0, QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin)); |
679 | 38 |
#endif |
184 | 39 |
imageButt=new QPushButton(this); |
530 | 40 |
imageButt->setObjectName("imageButt"); |
320 | 41 |
imageButt->setFixedSize(256, 128); |
184 | 42 |
imageButt->setFlat(true); |
452 | 43 |
imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum); |
184 | 44 |
mainLayout.addWidget(imageButt); |
320 | 45 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed())); |
46 |
setRandomSeed(); |
|
249 | 47 |
|
48 |
chooseMap=new QComboBox(this); |
|
49 |
QDir tmpdir; |
|
50 |
tmpdir.cd(datadir->absolutePath()); |
|
51 |
tmpdir.cd("Maps"); |
|
52 |
tmpdir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); |
|
53 |
QStringList mapList=tmpdir.entryList(QStringList("*")); |
|
251 | 54 |
mapList.push_front(QComboBox::tr("generated map...")); |
249 | 55 |
chooseMap->addItems(mapList); |
56 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int))); |
|
57 |
||
58 |
mainLayout.addWidget(chooseMap); |
|
452 | 59 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
184 | 60 |
} |
61 |
||
62 |
void HWMapContainer::setImage(const QImage newImage) |
|
63 |
{ |
|
64 |
QPixmap px(256, 128); |
|
65 |
QPixmap pxres(256, 128); |
|
66 |
QPainter p(&pxres); |
|
67 |
||
68 |
px.fill(Qt::yellow); |
|
69 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
70 |
px.setMask(bm); |
|
71 |
||
72 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 73 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
74 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 75 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
76 |
p.drawPixmap(QPoint(0, 0), px); |
|
77 |
||
78 |
imageButt->setIcon(pxres); |
|
79 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 80 |
chooseMap->setCurrentIndex(0); |
81 |
} |
|
82 |
||
83 |
void HWMapContainer::mapChanged(int index) |
|
84 |
{ |
|
85 |
if(!index) { |
|
86 |
changeImage(); |
|
87 |
return; |
|
88 |
} |
|
89 |
||
331 | 90 |
loadMap(index); |
91 |
||
92 |
emit mapChanged(chooseMap->currentText()); |
|
93 |
} |
|
94 |
||
95 |
void HWMapContainer::loadMap(int index) |
|
96 |
{ |
|
249 | 97 |
QPixmap mapImage; |
320 | 98 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 99 |
changeImage(); |
100 |
chooseMap->setCurrentIndex(0); |
|
101 |
return; |
|
102 |
} |
|
320 | 103 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
104 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
105 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
106 |
QTextStream input(&mapCfgFile); |
|
107 |
input >> theme; |
|
108 |
mapCfgFile.close(); |
|
109 |
} |
|
184 | 110 |
} |
111 |
||
112 |
void HWMapContainer::changeImage() |
|
113 |
{ |
|
320 | 114 |
pMap = new HWMap(); |
184 | 115 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
116 |
pMap->getImage(m_seed.toStdString()); |
|
320 | 117 |
theme = (Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel"; |
184 | 118 |
} |
119 |
||
120 |
QString HWMapContainer::getCurrentSeed() const |
|
121 |
{ |
|
122 |
return m_seed; |
|
123 |
} |
|
124 |
||
249 | 125 |
QString HWMapContainer::getCurrentMap() const |
126 |
{ |
|
320 | 127 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 128 |
return chooseMap->currentText(); |
129 |
} |
|
130 |
||
131 |
QString HWMapContainer::getCurrentTheme() const |
|
132 |
{ |
|
320 | 133 |
return theme; |
249 | 134 |
} |
135 |
||
184 | 136 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
137 |
{ |
|
138 |
//imageButt->setIconSize(imageButt->size()); |
|
139 |
} |
|
320 | 140 |
|
141 |
void HWMapContainer::setSeed(const QString & seed) |
|
142 |
{ |
|
143 |
m_seed = seed; |
|
144 |
changeImage(); |
|
145 |
} |
|
146 |
||
147 |
void HWMapContainer::setMap(const QString & map) |
|
148 |
{ |
|
331 | 149 |
int id = chooseMap->findText(map); |
150 |
if(id >= 0) { |
|
151 |
chooseMap->setCurrentIndex(id); |
|
152 |
loadMap(id); |
|
153 |
} |
|
320 | 154 |
} |
155 |
||
156 |
void HWMapContainer::setTheme(const QString & theme) |
|
157 |
{ |
|
158 |
this->theme = theme; |
|
159 |
} |
|
160 |
||
161 |
void HWMapContainer::setRandomSeed() |
|
162 |
{ |
|
163 |
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
|
164 |
emit seedChanged(m_seed); |
320 | 165 |
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
|
166 |
emit themeChanged(theme); |
320 | 167 |
} |