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