author | unc0rr |
Fri, 23 Feb 2007 14:26:53 +0000 | |
changeset 487 | 7ef6a98f0ede |
parent 486 | 7ea71cd3acd5 |
child 530 | f0b962214436 |
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); |
|
320 | 36 |
imageButt->setFixedSize(256, 128); |
184 | 37 |
imageButt->setFlat(true); |
452 | 38 |
imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum); |
184 | 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); |
|
452 | 54 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
184 | 55 |
} |
56 |
||
57 |
void HWMapContainer::setImage(const QImage newImage) |
|
58 |
{ |
|
59 |
QPixmap px(256, 128); |
|
60 |
QPixmap pxres(256, 128); |
|
61 |
QPainter p(&pxres); |
|
62 |
||
63 |
px.fill(Qt::yellow); |
|
64 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
65 |
px.setMask(bm); |
|
66 |
||
67 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 68 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
69 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 70 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
71 |
p.drawPixmap(QPoint(0, 0), px); |
|
72 |
||
73 |
imageButt->setIcon(pxres); |
|
74 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 75 |
chooseMap->setCurrentIndex(0); |
76 |
} |
|
77 |
||
78 |
void HWMapContainer::mapChanged(int index) |
|
79 |
{ |
|
80 |
if(!index) { |
|
81 |
changeImage(); |
|
82 |
return; |
|
83 |
} |
|
84 |
||
331 | 85 |
loadMap(index); |
86 |
||
87 |
emit mapChanged(chooseMap->currentText()); |
|
88 |
} |
|
89 |
||
90 |
void HWMapContainer::loadMap(int index) |
|
91 |
{ |
|
249 | 92 |
QPixmap mapImage; |
320 | 93 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 94 |
changeImage(); |
95 |
chooseMap->setCurrentIndex(0); |
|
96 |
return; |
|
97 |
} |
|
320 | 98 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
99 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
100 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
101 |
QTextStream input(&mapCfgFile); |
|
102 |
input >> theme; |
|
103 |
mapCfgFile.close(); |
|
104 |
} |
|
184 | 105 |
} |
106 |
||
107 |
void HWMapContainer::changeImage() |
|
108 |
{ |
|
320 | 109 |
pMap = new HWMap(); |
184 | 110 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
111 |
pMap->getImage(m_seed.toStdString()); |
|
320 | 112 |
theme = (Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel"; |
184 | 113 |
} |
114 |
||
115 |
QString HWMapContainer::getCurrentSeed() const |
|
116 |
{ |
|
117 |
return m_seed; |
|
118 |
} |
|
119 |
||
249 | 120 |
QString HWMapContainer::getCurrentMap() const |
121 |
{ |
|
320 | 122 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 123 |
return chooseMap->currentText(); |
124 |
} |
|
125 |
||
126 |
QString HWMapContainer::getCurrentTheme() const |
|
127 |
{ |
|
320 | 128 |
return theme; |
249 | 129 |
} |
130 |
||
184 | 131 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
132 |
{ |
|
133 |
//imageButt->setIconSize(imageButt->size()); |
|
134 |
} |
|
320 | 135 |
|
136 |
void HWMapContainer::setSeed(const QString & seed) |
|
137 |
{ |
|
138 |
m_seed = seed; |
|
139 |
changeImage(); |
|
140 |
} |
|
141 |
||
142 |
void HWMapContainer::setMap(const QString & map) |
|
143 |
{ |
|
331 | 144 |
int id = chooseMap->findText(map); |
145 |
if(id >= 0) { |
|
146 |
chooseMap->setCurrentIndex(id); |
|
147 |
loadMap(id); |
|
148 |
} |
|
320 | 149 |
} |
150 |
||
151 |
void HWMapContainer::setTheme(const QString & theme) |
|
152 |
{ |
|
153 |
this->theme = theme; |
|
154 |
} |
|
155 |
||
156 |
void HWMapContainer::setRandomSeed() |
|
157 |
{ |
|
158 |
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
|
159 |
emit seedChanged(m_seed); |
320 | 160 |
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
|
161 |
emit themeChanged(theme); |
320 | 162 |
} |