author | unc0rr |
Fri, 15 Aug 2008 06:33:50 +0000 | |
changeset 1214 | 090f94cd663e |
parent 1211 | 94c4f20abdda |
child 1215 | 0c1c294c0f7e |
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> |
1209 | 28 |
#include <QLabel> |
29 |
#include <QListWidget> |
|
30 |
#include <QVBoxLayout> |
|
249 | 31 |
|
32 |
#include "hwconsts.h" |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
682
diff
changeset
|
33 |
#include "mapContainer.h" |
1209 | 34 |
#include "igbox.h" |
184 | 35 |
|
36 |
HWMapContainer::HWMapContainer(QWidget * parent) : |
|
37 |
QWidget(parent), mainLayout(this) |
|
38 |
{ |
|
679 | 39 |
#if QT_VERSION >= 0x040300 |
682 | 40 |
mainLayout.setContentsMargins(QApplication::style()->pixelMetric(QStyle::PM_LayoutLeftMargin), |
41 |
1, |
|
42 |
QApplication::style()->pixelMetric(QStyle::PM_LayoutRightMargin), |
|
43 |
QApplication::style()->pixelMetric(QStyle::PM_LayoutBottomMargin)); |
|
679 | 44 |
#endif |
1209 | 45 |
imageButt = new QPushButton(this); |
530 | 46 |
imageButt->setObjectName("imageButt"); |
1214 | 47 |
imageButt->setFixedSize(256 + 6, 128 + 6); |
184 | 48 |
imageButt->setFlat(true); |
452 | 49 |
imageButt->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);//QSizePolicy::Minimum, QSizePolicy::Minimum); |
1209 | 50 |
mainLayout.addWidget(imageButt, 0, 0, 1, 2); |
320 | 51 |
connect(imageButt, SIGNAL(clicked()), this, SLOT(setRandomSeed())); |
52 |
setRandomSeed(); |
|
249 | 53 |
|
1209 | 54 |
chooseMap = new QComboBox(this); |
55 |
chooseMap->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
|
1210 | 56 |
chooseMap->addItem(QComboBox::tr("generated map...")); |
57 |
chooseMap->addItems(*mapList); |
|
249 | 58 |
connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int))); |
1209 | 59 |
mainLayout.addWidget(chooseMap, 1, 1); |
249 | 60 |
|
1209 | 61 |
QLabel * lblMap = new QLabel(tr("Map"), this); |
62 |
mainLayout.addWidget(lblMap, 1, 0); |
|
63 |
||
64 |
gbThemes = new IconedGroupBox(this); |
|
65 |
gbThemes->setTitle(tr("Themes")); |
|
66 |
gbThemes->setStyleSheet("padding: 0px;"); |
|
67 |
mainLayout.addWidget(gbThemes, 0, 2, 2, 1); |
|
1210 | 68 |
|
1209 | 69 |
QVBoxLayout * gbTLayout = new QVBoxLayout(gbThemes); |
70 |
lwThemes = new QListWidget(this); |
|
1210 | 71 |
lwThemes->setMinimumHeight(30); |
1209 | 72 |
lwThemes->setFixedWidth(100); |
1211 | 73 |
for (int i = 0; i < Themes->size(); ++i) { |
74 |
QListWidgetItem * lwi = new QListWidgetItem(); |
|
75 |
lwi->setText(Themes->at(i)); |
|
76 |
lwi->setTextAlignment(Qt::AlignHCenter); |
|
77 |
lwThemes->addItem(lwi); |
|
78 |
} |
|
1210 | 79 |
|
1209 | 80 |
gbTLayout->addWidget(lwThemes); |
81 |
lwThemes->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum); |
|
82 |
||
452 | 83 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
184 | 84 |
} |
85 |
||
86 |
void HWMapContainer::setImage(const QImage newImage) |
|
87 |
{ |
|
88 |
QPixmap px(256, 128); |
|
89 |
QPixmap pxres(256, 128); |
|
90 |
QPainter p(&pxres); |
|
91 |
||
92 |
px.fill(Qt::yellow); |
|
93 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
94 |
px.setMask(bm); |
|
95 |
||
96 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 97 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
98 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 99 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
100 |
p.drawPixmap(QPoint(0, 0), px); |
|
101 |
||
102 |
imageButt->setIcon(pxres); |
|
103 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 104 |
chooseMap->setCurrentIndex(0); |
105 |
} |
|
106 |
||
107 |
void HWMapContainer::mapChanged(int index) |
|
108 |
{ |
|
109 |
if(!index) { |
|
110 |
changeImage(); |
|
111 |
return; |
|
112 |
} |
|
113 |
||
331 | 114 |
loadMap(index); |
115 |
||
116 |
emit mapChanged(chooseMap->currentText()); |
|
117 |
} |
|
118 |
||
119 |
void HWMapContainer::loadMap(int index) |
|
120 |
{ |
|
249 | 121 |
QPixmap mapImage; |
320 | 122 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 123 |
changeImage(); |
124 |
chooseMap->setCurrentIndex(0); |
|
125 |
return; |
|
126 |
} |
|
320 | 127 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
128 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
129 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
130 |
QTextStream input(&mapCfgFile); |
|
131 |
input >> theme; |
|
132 |
mapCfgFile.close(); |
|
133 |
} |
|
184 | 134 |
} |
135 |
||
136 |
void HWMapContainer::changeImage() |
|
137 |
{ |
|
1214 | 138 |
pMap = new HWMap(); |
139 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
|
140 |
pMap->getImage(m_seed.toStdString()); |
|
141 |
||
142 |
if(!Themes->size()) return; |
|
143 |
quint32 themeNum = rand() % Themes->size(); |
|
144 |
theme = Themes->at(themeNum); |
|
145 |
lwThemes->setCurrentRow(themeNum); |
|
184 | 146 |
} |
147 |
||
148 |
QString HWMapContainer::getCurrentSeed() const |
|
149 |
{ |
|
150 |
return m_seed; |
|
151 |
} |
|
152 |
||
249 | 153 |
QString HWMapContainer::getCurrentMap() const |
154 |
{ |
|
320 | 155 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 156 |
return chooseMap->currentText(); |
157 |
} |
|
158 |
||
159 |
QString HWMapContainer::getCurrentTheme() const |
|
160 |
{ |
|
320 | 161 |
return theme; |
249 | 162 |
} |
163 |
||
184 | 164 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
165 |
{ |
|
166 |
//imageButt->setIconSize(imageButt->size()); |
|
167 |
} |
|
320 | 168 |
|
169 |
void HWMapContainer::setSeed(const QString & seed) |
|
170 |
{ |
|
171 |
m_seed = seed; |
|
172 |
changeImage(); |
|
173 |
} |
|
174 |
||
175 |
void HWMapContainer::setMap(const QString & map) |
|
176 |
{ |
|
331 | 177 |
int id = chooseMap->findText(map); |
178 |
if(id >= 0) { |
|
179 |
chooseMap->setCurrentIndex(id); |
|
180 |
loadMap(id); |
|
181 |
} |
|
320 | 182 |
} |
183 |
||
184 |
void HWMapContainer::setTheme(const QString & theme) |
|
185 |
{ |
|
186 |
this->theme = theme; |
|
187 |
} |
|
188 |
||
189 |
void HWMapContainer::setRandomSeed() |
|
190 |
{ |
|
191 |
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
|
192 |
emit seedChanged(m_seed); |
320 | 193 |
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
|
194 |
emit themeChanged(theme); |
320 | 195 |
} |