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) (side-by-side diff)
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) (show whitespace changes)
-rw-r--r--noncore/games/fifteen/fifteen.cpp187
1 files changed, 150 insertions, 37 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
@@ -20,8 +20,13 @@
#include "fifteen.h"
+#include "fifteenconfigdialog.h"
+
+#include <opie2/ofileselector.h>
+
#include <qtopia/resource.h>
#include <qtopia/config.h>
+#include <qtopia/qpeapplication.h>
#include <qvbox.h>
#include <qaction.h>
@@ -29,6 +34,7 @@
#include <qmessagebox.h>
#include <qtoolbar.h>
#include <qmenubar.h>
+#include <qimage.h>
#include <stdlib.h>
#include <time.h>
@@ -36,6 +42,7 @@
FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags fl)
: QMainWindow( parent, name, fl )
{
+
// random seed
srand(time(0));
setCaption( tr("Fifteen Pieces") );
@@ -64,6 +71,12 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
a->addTo( game );
a->addTo( toolbar );
+
+ a = new QAction( tr("Configure"), Resource::loadPixmap( "SettingsIcon" ),
+ QString::null, 0, this, 0 );
+ connect( a, SIGNAL( activated()), table, SLOT( slotConfigure()) );
+ a->addTo( game );
+
/* This is pointless and confusing.
a = new QAction( tr( "Solve" ), Resource::loadIconSet( "repeat" ),
QString::null, 0, this, 0 );
@@ -74,8 +87,15 @@ FifteenMainWindow::FifteenMainWindow(QWidget *parent, const char* name, WFlags f
menubar->insertItem( tr( "Game" ), game );
}
+
+
+
+///////////////
+/////// Pieces table Implementation
+///////
PiecesTable::PiecesTable(QWidget* parent, const char* name )
- : QTableView(parent, name), _menu(0), _randomized(false)
+ : QTableView(parent, name), _menu(0), _randomized(false),
+ _dialog( 0l )
{
// setup table view
setFrameStyle(StyledPanel | Sunken);
@@ -90,16 +110,13 @@ PiecesTable::PiecesTable(QWidget* parent, const char* name )
readConfig();
initColors();
- // set font
- QFont f = font();
- f.setPixelSize(18);
- f.setBold( TRUE );
- setFont(f);
}
+
PiecesTable::~PiecesTable()
{
writeConfig();
+ clear();
}
void PiecesTable::writeConfig()
@@ -111,6 +128,7 @@ void PiecesTable::writeConfig()
map.append( QString::number( _map[i] ) );
cfg.writeEntry("Map", map, '-');
cfg.writeEntry("Randomized", _randomized );
+ cfg.writeEntry("Image", _image );
}
void PiecesTable::readConfig()
@@ -119,62 +137,113 @@ void PiecesTable::readConfig()
cfg.setGroup("Game");
QStringList map = cfg.readListEntry("Map", '-');
_randomized = cfg.readBoolEntry( "Randomized", FALSE );
+ _image = cfg.readEntry( "Image", QString::null );
int i = 0;
for ( QStringList::Iterator it = map.begin(); it != map.end(); ++it ) {
_map[i] = (*it).toInt();
i++;
if ( i > 15 ) break;
}
+
}
-void PiecesTable::paintCell(QPainter *p, int row, int col)
-{
- int w = cellWidth();
- int h = cellHeight();
- int x2 = w - 1;
- int y2 = h - 1;
- int number = _map[col + row * numCols()] + 1;
+void PiecesTable::clear() {
+ /* clean up and resize */
+ for (uint i = 0; i < _pixmap.count(); ++i )
+ delete _pixmap[i];
+ _pixmap.resize( 16 );
+}
- // draw cell background
- if(number == 16)
- p->setBrush(colorGroup().background());
+/*
+ * Let us pre-render the tiles. Either we've a Custom Image as
+ * background or we use the drawRect to fill the background and
+ * last we put the number on it
+ */
+void PiecesTable::slotCustomImage( const QString& _str , bool upd ) {
+ QString str = _str;
+
+
+ /* couldn't load image fall back to plain tiles*/
+ QImage img = QImage(str);
+ if(img.isNull())
+ str = QString::null;
else
- p->setBrush(_colors[number-1]);
- p->setPen(NoPen);
- p->drawRect(0, 0, w, h);
+ img = img.smoothScale( width(),height() );
+
+ QPixmap pix;
+ pix.convertFromImage( img );
+
+ uint image=0;
+
+ clear();
+
+ /* used variables */
+ int cols = numCols();
+ int rows = numRows();
+ int cellW = cellWidth();
+ int cellH = cellHeight();
+ int x2 = cellW-1;
+ int y2 = cellH-1;
+ bool empty = str.isEmpty();
+ double bw = empty ? 0.9 : 0.98;
+ int x_offset = cellW - int(cellW * bw); // 10% should be enough
+ int y_offset = cellH - int(cellH * bw);
- if (number == 16) return;
+ /* border polygon */
+ initPolygon(cellW, cellH, x_offset, y_offset );
+
+ if ( cellW == 0 || cellH == 0 ) {
+ _pixmap.resize( 0 );
+ return;
+ }
+
+ QFont f = font();
+ f.setPixelSize(18);
+ f.setBold( TRUE );
+
+ /* for every tile */
+ for(int row = 0; row < rows; ++row ) {
+ for(int col= 0; col < cols; ++col) {
+ QPixmap *pip = new QPixmap(cellW, cellH );
+ QPainter *p = new QPainter(pip );
+ p->setFont( f );
+
+ /* draw the tradional tile or a part of the pixmap*/
+ if(empty) {
+ p->setBrush(_colors[image]);
+ p->setPen(NoPen);
+ p->drawRect(0,0,cellW,cellH);
+ }else
+ p->drawPixmap(0, 0, pix,col*cellW, row*cellH, cellW, cellH );
// draw borders
if (height() > 40) {
- p->setBrush(_colors[number-1].light(130));
+ p->setBrush(_colors[image].light(130));
p->drawPolygon(light_border);
- p->setBrush(_colors[number-1].dark(130));
+ p->setBrush(_colors[image].dark(130));
p->drawPolygon(dark_border);
}
// draw number
p->setPen(black);
- p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(number));
-}
+ p->drawText(0, 0, x2, y2, AlignHCenter | AlignVCenter, QString::number(image+1));
-void PiecesTable::resizeEvent(QResizeEvent *e)
-{
- QTableView::resizeEvent(e);
-
- setCellWidth(contentsRect().width()/ numRows());
- setCellHeight(contentsRect().height() / numCols());
+ delete p;
+ _pixmap[image++] = pip;
+ }
+ }
+ _image = str;
- //
- // Calculate 3d-effect borders
- //
- int cell_w = cellWidth();
- int cell_h = cellHeight();
- int x_offset = cell_w - int(cell_w * 0.9); // 10% should be enough
- int y_offset = cell_h - int(cell_h * 0.9);
+ if ( upd )
+ update();
+}
+/*
+ * Calculate 3d-effect borders
+ */
+void PiecesTable::initPolygon(int cell_w, int cell_h, int x_offset, int y_offset ) {
light_border.setPoints(6,
0, 0,
cell_w, 0,
@@ -192,6 +261,40 @@ void PiecesTable::resizeEvent(QResizeEvent *e)
cell_w - x_offset, y_offset);
}
+void PiecesTable::paintCell(QPainter *p, int row, int col)
+{
+ int w = cellWidth();
+ int h = cellHeight();
+
+ int number = _map[col + row * numCols()] + 1;
+
+ // draw cell background
+ if(number == 16) {
+ p->setBrush(colorGroup().background());
+ p->setPen(NoPen);
+ p->drawRect(0, 0, w, h);
+ return;
+ }
+
+ if( _pixmap.count() == 0 )
+ return;
+
+ p->drawPixmap(0, 0, *(_pixmap[(number-1 )]) );
+}
+
+void PiecesTable::resizeEvent(QResizeEvent *e)
+{
+ QTableView::resizeEvent(e);
+
+ setCellWidth(contentsRect().width()/ numRows());
+ setCellHeight(contentsRect().height() / numCols());
+
+
+ /* update the image and calculate border*/
+ slotCustomImage( _image );
+
+}
+
void PiecesTable::initColors()
{
_colors.resize(numRows() * numCols());
@@ -384,3 +487,13 @@ void PiecesTable::mousePressEvent(QMouseEvent* e)
checkwin();
}
}
+
+void PiecesTable::slotConfigure() {
+ if ( !_dialog )
+ _dialog = new FifteenConfigDialog(this, "Fifteen Configure Dialog", true );
+
+
+ _dialog->setImageSrc( _image );
+ if ( QPEApplication::execDialog(_dialog) == QDialog::Accepted )
+ slotCustomImage( _dialog->imageSrc(), true );
+}