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