summaryrefslogtreecommitdiff
path: root/noncore/games/fifteen/fifteen.cpp
authorzecke <zecke>2004-07-24 15:02:13 (UTC)
committer zecke <zecke>2004-07-24 15:02:13 (UTC)
commitc94b8aee4873dc382d3fe2673c7ac1db7080fe0e (patch) (unidiff)
treeda5d98b9e429ad845651e731e93a978cea54a65b /noncore/games/fifteen/fifteen.cpp
parent2da1cf47a5c1429d8ad010880ff1da3e97b437ad (diff)
downloadopie-c94b8aee4873dc382d3fe2673c7ac1db7080fe0e.zip
opie-c94b8aee4873dc382d3fe2673c7ac1db7080fe0e.tar.gz
opie-c94b8aee4873dc382d3fe2673c7ac1db7080fe0e.tar.bz2
Custom Image as the background for the board tiles.
This is later needed for the puzzle game mode
Diffstat (limited to 'noncore/games/fifteen/fifteen.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/fifteen/fifteen.cpp211
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
@@ -22,4 +22,9 @@
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
@@ -31,2 +36,3 @@
31#include <qmenubar.h> 36#include <qmenubar.h>
37#include <qimage.h>
32 38
@@ -38,2 +44,3 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
38{ 44{
45
39 // random seed 46 // random seed
@@ -66,2 +73,8 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
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.
@@ -76,4 +89,11 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
76 89
90
91
92
93///////////////
94/////// Pieces table Implementation
95///////
77PiecesTable::PiecesTable(QWidget* parent, const char* name ) 96PiecesTable::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{
@@ -92,9 +112,5 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
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
100PiecesTable::~PiecesTable() 116PiecesTable::~PiecesTable()
@@ -102,2 +118,3 @@ PiecesTable::~PiecesTable()
102 writeConfig(); 118 writeConfig();
119 clear();
103} 120}
@@ -113,2 +130,3 @@ void PiecesTable::writeConfig()
113 cfg.writeEntry("Randomized", _randomized ); 130 cfg.writeEntry("Randomized", _randomized );
131 cfg.writeEntry("Image", _image );
114} 132}
@@ -121,2 +139,3 @@ void PiecesTable::readConfig()
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;
@@ -127,2 +146,117 @@ void PiecesTable::readConfig()
127 } 146 }
147
148}
149
150
151void 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 */
163void 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 */
246void 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}
@@ -133,4 +267,2 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
133 int h = cellHeight(); 267 int h = cellHeight();
134 int x2 = w - 1;
135 int y2 = h - 1;
136 268
@@ -139,23 +271,13 @@ void PiecesTable::paintCell(QPainter *p, int row, int col)
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}
@@ -169,25 +291,6 @@ void PiecesTable::resizeEvent(QResizeEvent *e)
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}
@@ -386 +489,11 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
386} 489}
490
491void 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}