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