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