00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "qtetrix.h"
00023
00024 #include <opie2/oresource.h>
00025
00026 #include <qlabel.h>
00027 #include <qdatetime.h>
00028 #include <qlayout.h>
00029 #include <qtimer.h>
00030
00031 #include "ohighscoredlg.h"
00032
00033
00034 void drawTetrixButton( QPainter *p, int x, int y, int w, int h,
00035 const QColor *color )
00036 {
00037 QColor fc;
00038 if ( color ) {
00039 QPointArray a;
00040 a.setPoints( 3, x,y+h-1, x,y, x+w-1,y );
00041 p->setPen( color->light() );
00042 p->drawPolyline( a );
00043 a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 );
00044 p->setPen( color->dark() );
00045 p->drawPolyline( a );
00046 x++;
00047 y++;
00048 w -= 2;
00049 h -= 2;
00050 fc = *color;
00051 }
00052 else
00053 fc = p->backgroundColor();
00054 p->fillRect( x, y, w, h, fc );
00055 }
00056
00057
00058 ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name )
00059 : QFrame( parent, name )
00060 {
00061 setFrameStyle( QFrame::Panel | QFrame::Sunken );
00062 xOffset = -1;
00063 }
00064
00065 void ShowNextPiece::resizeEvent( QResizeEvent *e )
00066 {
00067 QSize sz = e->size();
00068 blockWidth = (sz.width() - 3)/5;
00069 blockHeight = (sz.height() - 3)/6;
00070 xOffset = (sz.width() - 3)/5;
00071 yOffset = (sz.height() - 3)/6;
00072 }
00073
00074
00075 void ShowNextPiece::paintEvent( QPaintEvent * )
00076 {
00077 QPainter p( this );
00078 drawFrame( &p );
00079 p.end();
00080 emit update();
00081 }
00082
00083
00084 void ShowNextPiece::drawNextSquare(int x, int y,QColor *color)
00085 {
00086 if (xOffset == -1)
00087 return;
00088
00089 QPainter paint;
00090 paint.begin(this);
00091 drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight,
00092 blockWidth, blockHeight, color );
00093 paint.end();
00094 }
00095
00096
00097 QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f )
00098 : QMainWindow( parent, name, f )
00099 {
00100 setIcon( Opie::Core::OResource::loadPixmap( "tetrix_icon" ) );
00101 setCaption( tr("Tetrix" ) );
00102
00103 QTime t = QTime::currentTime();
00104 TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/
00105 (24+60+60) );
00106
00107 QWidget *gameArea = new QWidget( this );
00108 setCentralWidget( gameArea );
00109
00110 QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 );
00111 gl->setColStretch( 1, 5 );
00112 gl->setColStretch( 2, 10 );
00113
00114 QLabel *l;
00115 l = new QLabel( tr("Next"), gameArea );
00116 gl->addWidget( l, 0, 0 );
00117 showNext = new ShowNextPiece(gameArea);
00118 showNext->setBackgroundColor(QColor(0,0,0));
00119 gl->addWidget( showNext, 0, 1 );
00120
00121 l = new QLabel( tr("Score"), gameArea );
00122 gl->addWidget( l, 1, 0 );
00123 showScore = new QLabel(gameArea);
00124 gl->addWidget( showScore, 1, 1 );
00125 l = new QLabel( tr("Level"), gameArea );
00126 gl->addWidget( l, 2, 0 );
00127 showLevel = new QLabel(gameArea);
00128 gl->addWidget( showLevel, 2, 1 );
00129 l = new QLabel( tr("Removed"), gameArea );
00130 gl->addWidget( l, 3, 0 );
00131 showLines = new QLabel(gameArea);
00132 gl->addWidget( showLines, 3, 1 );
00133
00134 board = new QTetrixBoard(gameArea);
00135 board->setBackgroundColor(QColor(0,0,0));
00136 gl->addMultiCellWidget( board, 0, 4, 2, 2 );
00137
00138 QPushButton *pb = new QPushButton( tr("Start"), gameArea );
00139 pb->setFocusPolicy( NoFocus );
00140 connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) );
00141 gl->addMultiCellWidget( pb, 4, 4, 0, 1 );
00142
00143 connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) );
00144 connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), this,
00145 SLOT(setNext(int,int,QColor*)) );
00146 connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) );
00147 connect( board, SIGNAL(updateScoreSignal(int)), showScore, SLOT(setNum(int)) );
00148 connect( board, SIGNAL(updateLevelSignal(int)), showLevel, SLOT(setNum(int)) );
00149 connect( board, SIGNAL(updateRemovedSignal(int)), showLines, SLOT(setNum(int)) );
00150
00151 showScore->setNum( 0 );
00152 showLevel->setNum( 0 );
00153 showLines->setNum( 0 );
00154 board->revealNextPiece(TRUE);
00155 board->setFocusPolicy( StrongFocus );
00156
00157 QTimer::singleShot( -1, this, SLOT(setup()) );
00158 }
00159
00160 void QTetrix::setup()
00161 {
00162 resizeEvent( 0x0 );
00163 }
00164
00165 void QTetrix::gameOver()
00166 {
00167 OHighscore *hs = new OHighscore( showScore->text().toInt() , showLevel->text().toInt() );
00168 if ( hs->isNewhighscore )
00169 hs->insertData( hs->getName(), showScore->text().toInt() , showLevel->text().toInt() );
00170 OHighscoreDialog hscdlg( hs, this, "OHighscoreDialog", true );
00171 hscdlg.exec();
00172 }
00173
00174 void QTetrix::quit()
00175 {
00176 close();
00177 }
00178
00179 void QTetrix::setNext( int x, int y, QColor *color )
00180 {
00181 resizeEvent( 0x0 );
00182 showNext->drawNextSquare( x, y, color );
00183 }
00184
00185 void QTetrix::resizeEvent( QResizeEvent * )
00186 {
00187
00188 int widthFactor = board->QFrame::width() / board->boardWidth();
00189 if ( widthFactor < 1 )
00190 widthFactor = 1;
00191 int heightFactor = board->QFrame::height() / board->boardHeight();
00192 if ( heightFactor < 1 )
00193 heightFactor = 1;
00194 widthFactor > heightFactor ? board->resize( heightFactor * board->boardWidth() + 2,
00195 heightFactor * board->boardHeight() + 2 )
00196 : board->resize( widthFactor * board->boardWidth() + 2,
00197 widthFactor * board->boardHeight() + 2 );
00198
00199
00200 widthFactor = showNext->width() / 5;
00201 if ( widthFactor < 1 )
00202 widthFactor = 1;
00203 heightFactor = showNext->height() / 6;
00204 if ( heightFactor < 1 )
00205 heightFactor = 1;
00206 widthFactor > heightFactor ? showNext->resize( heightFactor * 5 + 2, heightFactor * 6 + 2 )
00207 : showNext->resize( widthFactor * 5 + 2, widthFactor * 6 + 2 );
00208 }