Diffstat (limited to 'noncore/games/fifteen/fifteen.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/fifteen/fifteen.cpp | 211 |
1 files changed, 162 insertions, 49 deletions
diff --git a/noncore/games/fifteen/fifteen.cpp b/noncore/games/fifteen/fifteen.cpp index 506e87a..f425e06 100644 --- a/noncore/games/fifteen/fifteen.cpp +++ b/noncore/games/fifteen/fifteen.cpp | |||
@@ -21,6 +21,11 @@ | |||
21 | #include "fifteen.h" | 21 | #include "fifteen.h" |
22 | 22 | ||
23 | #include "fifteenconfigdialog.h" | ||
24 | |||
25 | #include <opie2/ofileselector.h> | ||
26 | |||
23 | #include <qtopia/resource.h> | 27 | #include <qtopia/resource.h> |
24 | #include <qtopia/config.h> | 28 | #include <qtopia/config.h> |
29 | #include <qtopia/qpeapplication.h> | ||
25 | 30 | ||
26 | #include <qvbox.h> | 31 | #include <qvbox.h> |
@@ -30,4 +35,5 @@ | |||
30 | #include <qtoolbar.h> | 35 | #include <qtoolbar.h> |
31 | #include <qmenubar.h> | 36 | #include <qmenubar.h> |
37 | #include <qimage.h> | ||
32 | 38 | ||
33 | #include <stdlib.h> | 39 | #include <stdlib.h> |
@@ -37,4 +43,5 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f | |||
37 | : QMainWindow( parent, name, fl ) | 43 | : QMainWindow( parent, name, fl ) |
38 | { | 44 | { |
45 | |||
39 | // random seed | 46 | // random seed |
40 | srand(time(0)); | 47 | srand(time(0)); |
@@ -65,4 +72,10 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f | |||
65 | a->addTo( toolbar ); | 72 | a->addTo( toolbar ); |
66 | 73 | ||
74 | |||
75 | a = new QAction( tr("Configure"), Resource::loadPixmap( "SettingsIcon" ), | ||
76 | QString::null, 0, this, 0 ); | ||
77 | connect( a, SIGNAL( activated()), table, SLOT( slotConfigure()) ); | ||
78 | a->addTo( game ); | ||
79 | |||
67 | /* This is pointless and confusing. | 80 | /* This is pointless and confusing. |
68 | a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ), | 81 | a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ), |
@@ -75,6 +88,13 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f | |||
75 | } | 88 | } |
76 | 89 | ||
90 | |||
91 | |||
92 | |||
93 | /////////////// | ||
94 | /////// Pieces table Implementation | ||
95 | /////// | ||
77 | PiecesTable::PiecesTable(QWidget* parent, const char* name ) | 96 | PiecesTable::PiecesTable(QWidget* parent, const char* name ) |
78 | : QTableView(parent, name), _menu(0), _randomized(false) | 97 | : QTableView(parent, name), _menu(0), _randomized(false), |
98 | _dialog( 0l ) | ||
79 | { | 99 | { |
80 | // setup table view | 100 | // setup table view |
@@ -91,14 +111,11 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name ) | |||
91 | initColors(); | 111 | initColors(); |
92 | 112 | ||
93 | // set font | ||
94 | QFont f = font(); | ||
95 | f.setPixelSize(18); | ||
96 | f.setBold( TRUE ); | ||
97 | setFont(f); | ||
98 | } | 113 | } |
99 | 114 | ||
115 | |||
100 | PiecesTable::~PiecesTable() | 116 | PiecesTable::~PiecesTable() |
101 | { | 117 | { |
102 | writeConfig(); | 118 | writeConfig(); |
119 | clear(); | ||
103 | } | 120 | } |
104 | 121 | ||
@@ -112,4 +129,5 @@ void PiecesTable::writeConfig() | |||
112 | cfg.writeEntry("Map", map, '-'); | 129 | cfg.writeEntry("Map", map, '-'); |
113 | cfg.writeEntry("Randomized", _randomized ); | 130 | cfg.writeEntry("Randomized", _randomized ); |
131 | cfg.writeEntry("Image", _image ); | ||
114 | } | 132 | } |
115 | 133 | ||
@@ -120,4 +138,5 @@ void PiecesTable::readConfig() | |||
120 | QStringList map = cfg.readListEntry("Map", '-'); | 138 | QStringList map = cfg.readListEntry("Map", '-'); |
121 | _randomized = cfg.readBoolEntry( "Randomized", FALSE ); | 139 | _randomized = cfg.readBoolEntry( "Randomized", FALSE ); |
140 | _image = cfg.readEntry( "Image", QString::null ); | ||
122 | int i = 0; | 141 | int i = 0; |
123 | for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) { | 142 | for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) { |
@@ -126,4 +145,119 @@ void PiecesTable::readConfig() | |||
126 | if ( i > 15 ) break; | 145 | if ( i > 15 ) break; |
127 | } | 146 | } |
147 | |||
148 | } | ||
149 | |||
150 | |||
151 | void PiecesTable::clear() { | ||
152 | /* clean up and resize */ | ||
153 | for (uint i = 0; i < _pixmap.count(); ++i ) | ||
154 | delete _pixmap[i]; | ||
155 | _pixmap.resize( 16 ); | ||
156 | } | ||
157 | |||
158 | /* | ||
159 | * Let us pre-render the tiles. Either we've a Custom Image as | ||
160 | * background or we use the drawRect to fill the background and | ||
161 | * last we put the number on it | ||
162 | */ | ||
163 | void PiecesTable::slotCustomImage( const QString& _str , bool upd ) { | ||
164 | QString str = _str; | ||
165 | |||
166 | |||
167 | /* couldn't load image fall back to plain tiles*/ | ||
168 | QImage img = QImage(str); | ||
169 | if(img.isNull()) | ||
170 | str = QString::null; | ||
171 | else | ||
172 | img = img.smoothScale( width(),height() ); | ||
173 | |||
174 | QPixmap pix; | ||
175 | pix.convertFromImage( img ); | ||
176 | |||
177 | uint image=0; | ||
178 | |||
179 | clear(); | ||
180 | |||
181 | /* used variables */ | ||
182 | int cols = numCols(); | ||
183 | int rows = numRows(); | ||
184 | int cellW = cellWidth(); | ||
185 | int cellH = cellHeight(); | ||
186 | int x2 = cellW-1; | ||
187 | int y2 = cellH-1; | ||
188 | bool empty = str.isEmpty(); | ||
189 | double bw = empty ? 0.9 : 0.98; | ||
190 | int x_offset = cellW - int(cellW * bw);// 10% should be enough | ||
191 | inty_offset = cellH - int(cellH * bw); | ||
192 | |||
193 | /* border polygon */ | ||
194 | initPolygon(cellW, cellH, x_offset, y_offset ); | ||
195 | |||
196 | if ( cellW == 0 || cellH == 0 ) { | ||
197 | _pixmap.resize( 0 ); | ||
198 | return; | ||
199 | } | ||
200 | |||
201 | QFont f = font(); | ||
202 | f.setPixelSize(18); | ||
203 | f.setBold( TRUE ); | ||
204 | |||
205 | /* for every tile */ | ||
206 | for(int row = 0; row < rows; ++row ) { | ||
207 | for(int col= 0; col < cols; ++col) { | ||
208 | QPixmap *pip = new QPixmap(cellW, cellH ); | ||
209 | QPainter *p = new QPainter(pip ); | ||
210 | p->setFont( f ); | ||
211 | |||
212 | /* draw the tradional tile or a part of the pixmap*/ | ||
213 | if(empty) { | ||
214 | p->setBrush(_colors[image]); | ||
215 | p->setPen(NoPen); | ||
216 | p->drawRect(0,0,cellW,cellH); | ||
217 | }else | ||
218 | p->drawPixmap(0, 0, pix,col*cellW, row*cellH, cellW, cellH ); | ||
219 | |||
220 | // draw borders | ||
221 | if (height() > 40) { | ||
222 | p->setBrush(_colors[image].light(130)); | ||
223 | p->drawPolygon(light_border); | ||
224 | |||
225 | p->setBrush(_colors[image].dark(130)); | ||
226 | p->drawPolygon(dark_border); | ||
227 | } | ||
228 | |||
229 | // draw number | ||
230 | p->setPen(black); | ||
231 | p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(image+1)); | ||
232 | |||
233 | delete p; | ||
234 | _pixmap[image++] = pip; | ||
235 | } | ||
236 | } | ||
237 | _image = str; | ||
238 | |||
239 | if ( upd ) | ||
240 | update(); | ||
241 | } | ||
242 | |||
243 | /* | ||
244 | * Calculate 3d-effect borders | ||
245 | */ | ||
246 | void PiecesTable::initPolygon(int cell_w, int cell_h, int x_offset, int y_offset ) { | ||
247 | light_border.setPoints(6, | ||
248 | 0, 0, | ||
249 | cell_w, 0, | ||
250 | cell_w - x_offset, y_offset, | ||
251 | x_offset, y_offset, | ||
252 | x_offset, cell_h - y_offset, | ||
253 | 0, cell_h); | ||
254 | |||
255 | dark_border.setPoints(6, | ||
256 | cell_w, 0, | ||
257 | cell_w, cell_h, | ||
258 | 0, cell_h, | ||
259 | x_offset, cell_h - y_offset, | ||
260 | cell_w - x_offset, cell_h - y_offset, | ||
261 | cell_w - x_offset, y_offset); | ||
128 | } | 262 | } |
129 | 263 | ||
@@ -132,31 +266,19 @@ void PiecesTable::paintCell(QPainter *p, int row, int col) | |||
132 | int w = cellWidth(); | 266 | int w = cellWidth(); |
133 | int h = cellHeight(); | 267 | int h = cellHeight(); |
134 | int x2 = w - 1; | ||
135 | int y2 = h - 1; | ||
136 | 268 | ||
137 | int number = _map[col + row * numCols()] + 1; | 269 | int number = _map[col + row * numCols()] + 1; |
138 | 270 | ||
139 | // draw cell background | 271 | // draw cell background |
140 | if(number == 16) | 272 | if(number == 16) { |
141 | p->setBrush(colorGroup().background()); | 273 | p->setBrush(colorGroup().background()); |
142 | else | 274 | p->setPen(NoPen); |
143 | p->setBrush(_colors[number-1]); | 275 | p->drawRect(0, 0, w, h); |
144 | p->setPen(NoPen); | 276 | return; |
145 | p->drawRect(0, 0, w, h); | ||
146 | |||
147 | if (number == 16) return; | ||
148 | |||
149 | // draw borders | ||
150 | if (height() > 40) { | ||
151 | p->setBrush(_colors[number-1].light(130)); | ||
152 | p->drawPolygon(light_border); | ||
153 | |||
154 | p->setBrush(_colors[number-1].dark(130)); | ||
155 | p->drawPolygon(dark_border); | ||
156 | } | 277 | } |
157 | 278 | ||
158 | // draw number | 279 | if( _pixmap.count() == 0 ) |
159 | p->setPen(black); | 280 | return; |
160 | p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(number)); | 281 | |
282 | p->drawPixmap(0, 0, *(_pixmap[(number-1 )]) ); | ||
161 | } | 283 | } |
162 | 284 | ||
@@ -168,27 +290,8 @@ void PiecesTable::resizeEvent(QResizeEvent *e) | |||
168 | setCellHeight(contentsRect().height() / numCols()); | 290 | setCellHeight(contentsRect().height() / numCols()); |
169 | 291 | ||
170 | // | 292 | |
171 | // Calculate 3d-effect borders | 293 | /* update the image and calculate border*/ |
172 | // | 294 | slotCustomImage( _image ); |
173 | intcell_w = cellWidth(); | 295 | |
174 | intcell_h = cellHeight(); | ||
175 | int x_offset = cell_w - int(cell_w * 0.9);// 10% should be enough | ||
176 | inty_offset = cell_h - int(cell_h * 0.9); | ||
177 | |||
178 | light_border.setPoints(6, | ||
179 | 0, 0, | ||
180 | cell_w, 0, | ||
181 | cell_w - x_offset, y_offset, | ||
182 | x_offset, y_offset, | ||
183 | x_offset, cell_h - y_offset, | ||
184 | 0, cell_h); | ||
185 | |||
186 | dark_border.setPoints(6, | ||
187 | cell_w, 0, | ||
188 | cell_w, cell_h, | ||
189 | 0, cell_h, | ||
190 | x_offset, cell_h - y_offset, | ||
191 | cell_w - x_offset, cell_h - y_offset, | ||
192 | cell_w - x_offset, y_offset); | ||
193 | } | 296 | } |
194 | 297 | ||
@@ -385,2 +488,12 @@ void PiecesTable::mousePressEvent(QMouseEvent* e) | |||
385 | } | 488 | } |
386 | } | 489 | } |
490 | |||
491 | void PiecesTable::slotConfigure() { | ||
492 | if ( !_dialog ) | ||
493 | _dialog = new FifteenConfigDialog(this, "Fifteen Configure Dialog", true ); | ||
494 | |||
495 | |||
496 | _dialog->setImageSrc( _image ); | ||
497 | if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted ) | ||
498 | slotCustomImage( _dialog->imageSrc(), true ); | ||
499 | } | ||