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