author | unc0rr |
Thu, 14 Aug 2008 14:24:45 +0000 | |
changeset 1211 | 94c4f20abdda |
parent 1210 | bfed1b89bc85 |
child 1214 | 090f94cd663e |
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"); |
1164 | 47 |
imageButt->setFixedSize(256 + 8, 128 + 8); |
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 |
||
83 |
||
452 | 84 |
mainLayout.setSizeConstraint(QLayout::SetFixedSize);//SetMinimumSize |
184 | 85 |
} |
86 |
||
87 |
void HWMapContainer::setImage(const QImage newImage) |
|
88 |
{ |
|
89 |
QPixmap px(256, 128); |
|
90 |
QPixmap pxres(256, 128); |
|
91 |
QPainter p(&pxres); |
|
92 |
||
93 |
px.fill(Qt::yellow); |
|
94 |
QBitmap bm = QBitmap::fromImage(newImage); |
|
95 |
px.setMask(bm); |
|
96 |
||
97 |
QLinearGradient linearGrad(QPoint(128, 0), QPoint(128, 128)); |
|
216 | 98 |
linearGrad.setColorAt(1, QColor(0, 0, 192)); |
99 |
linearGrad.setColorAt(0, QColor(66, 115, 225)); |
|
184 | 100 |
p.fillRect(QRect(0, 0, 256, 128), linearGrad); |
101 |
p.drawPixmap(QPoint(0, 0), px); |
|
102 |
||
103 |
imageButt->setIcon(pxres); |
|
104 |
imageButt->setIconSize(QSize(256, 128)); |
|
249 | 105 |
chooseMap->setCurrentIndex(0); |
106 |
} |
|
107 |
||
108 |
void HWMapContainer::mapChanged(int index) |
|
109 |
{ |
|
110 |
if(!index) { |
|
111 |
changeImage(); |
|
112 |
return; |
|
113 |
} |
|
114 |
||
331 | 115 |
loadMap(index); |
116 |
||
117 |
emit mapChanged(chooseMap->currentText()); |
|
118 |
} |
|
119 |
||
120 |
void HWMapContainer::loadMap(int index) |
|
121 |
{ |
|
249 | 122 |
QPixmap mapImage; |
320 | 123 |
if(!mapImage.load(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.png")) { |
249 | 124 |
changeImage(); |
125 |
chooseMap->setCurrentIndex(0); |
|
126 |
return; |
|
127 |
} |
|
320 | 128 |
imageButt->setIcon(mapImage.scaled(256, 128)); |
129 |
QFile mapCfgFile(datadir->absolutePath() + "/Maps/" + chooseMap->currentText() + "/map.cfg"); |
|
130 |
if (mapCfgFile.open(QFile::ReadOnly)) { |
|
131 |
QTextStream input(&mapCfgFile); |
|
132 |
input >> theme; |
|
133 |
mapCfgFile.close(); |
|
134 |
} |
|
184 | 135 |
} |
136 |
||
137 |
void HWMapContainer::changeImage() |
|
138 |
{ |
|
320 | 139 |
pMap = new HWMap(); |
184 | 140 |
connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage))); |
141 |
pMap->getImage(m_seed.toStdString()); |
|
320 | 142 |
theme = (Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel"; |
184 | 143 |
} |
144 |
||
145 |
QString HWMapContainer::getCurrentSeed() const |
|
146 |
{ |
|
147 |
return m_seed; |
|
148 |
} |
|
149 |
||
249 | 150 |
QString HWMapContainer::getCurrentMap() const |
151 |
{ |
|
320 | 152 |
if(!chooseMap->currentIndex()) return QString(); |
249 | 153 |
return chooseMap->currentText(); |
154 |
} |
|
155 |
||
156 |
QString HWMapContainer::getCurrentTheme() const |
|
157 |
{ |
|
320 | 158 |
return theme; |
249 | 159 |
} |
160 |
||
184 | 161 |
void HWMapContainer::resizeEvent ( QResizeEvent * event ) |
162 |
{ |
|
163 |
//imageButt->setIconSize(imageButt->size()); |
|
164 |
} |
|
320 | 165 |
|
166 |
void HWMapContainer::setSeed(const QString & seed) |
|
167 |
{ |
|
168 |
m_seed = seed; |
|
169 |
changeImage(); |
|
170 |
} |
|
171 |
||
172 |
void HWMapContainer::setMap(const QString & map) |
|
173 |
{ |
|
331 | 174 |
int id = chooseMap->findText(map); |
175 |
if(id >= 0) { |
|
176 |
chooseMap->setCurrentIndex(id); |
|
177 |
loadMap(id); |
|
178 |
} |
|
320 | 179 |
} |
180 |
||
181 |
void HWMapContainer::setTheme(const QString & theme) |
|
182 |
{ |
|
183 |
this->theme = theme; |
|
184 |
} |
|
185 |
||
186 |
void HWMapContainer::setRandomSeed() |
|
187 |
{ |
|
188 |
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
|
189 |
emit seedChanged(m_seed); |
320 | 190 |
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
|
191 |
emit themeChanged(theme); |
320 | 192 |
} |