| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: qtetrixb.cpp 160 2006-12-11 20:15:57Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Copyright (C) 1992-1998 Trolltech AS.  All rights reserved. | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of an example program for Qt.  This example | 
|---|
| 7 | ** program may be used, distributed and modified without limitation. | 
|---|
| 8 | ** | 
|---|
| 9 | *****************************************************************************/ | 
|---|
| 10 |  | 
|---|
| 11 | #include "qtetrixb.h" | 
|---|
| 12 | #include "qtetrix.h" | 
|---|
| 13 | #include <qtimer.h> | 
|---|
| 14 | #include <qpainter.h> | 
|---|
| 15 |  | 
|---|
| 16 | const int waitAfterLineTime = 500; | 
|---|
| 17 |  | 
|---|
| 18 | QTetrixBoard::QTetrixBoard( QWidget *p, const char *name ) | 
|---|
| 19 | : QFrame( p, name ) | 
|---|
| 20 | { | 
|---|
| 21 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); | 
|---|
| 22 | paint = 0; | 
|---|
| 23 | paint_widget = 0; | 
|---|
| 24 | timer = new QTimer(this); | 
|---|
| 25 | connect( timer, SIGNAL(timeout()), SLOT(timeout()) ); | 
|---|
| 26 |  | 
|---|
| 27 | colors[0].setRgb(200,100,100); | 
|---|
| 28 | colors[1].setRgb(100,200,100); | 
|---|
| 29 | colors[2].setRgb(100,100,200); | 
|---|
| 30 | colors[3].setRgb(200,200,100); | 
|---|
| 31 | colors[4].setRgb(200,100,200); | 
|---|
| 32 | colors[5].setRgb(100,200,200); | 
|---|
| 33 | colors[6].setRgb(218,170,  0); | 
|---|
| 34 |  | 
|---|
| 35 | xOffset          = -1;      // -1 until a resizeEvent is received. | 
|---|
| 36 | blockWidth       = 20; | 
|---|
| 37 | yOffset          = 30; | 
|---|
| 38 | blockHeight      = 20; | 
|---|
| 39 | noGame           = TRUE; | 
|---|
| 40 | isPaused         = FALSE; | 
|---|
| 41 | waitingAfterLine = FALSE; | 
|---|
| 42 | updateTimeoutTime();   // Sets timeoutTime | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | void QTetrixBoard::startGame(int gameType,int fillRandomLines) | 
|---|
| 46 | { | 
|---|
| 47 | if ( isPaused ) | 
|---|
| 48 | return;         // ignore if game is paused | 
|---|
| 49 | noGame = FALSE; | 
|---|
| 50 | GenericTetrix::startGame( gameType, fillRandomLines ); | 
|---|
| 51 | // Note that the timer is started by updateLevel! | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | void QTetrixBoard::pause() | 
|---|
| 56 | { | 
|---|
| 57 | if ( noGame )                       // game not active | 
|---|
| 58 | return; | 
|---|
| 59 | isPaused = !isPaused; | 
|---|
| 60 | if ( isPaused ) { | 
|---|
| 61 | timer->stop(); | 
|---|
| 62 | hideBoard(); | 
|---|
| 63 | } | 
|---|
| 64 | else | 
|---|
| 65 | timer->start(timeoutTime); | 
|---|
| 66 | update(); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | void QTetrixBoard::drawSquare(int x,int y,int value) | 
|---|
| 71 | { | 
|---|
| 72 | if (xOffset == -1)    // Before first resizeEvent? | 
|---|
| 73 | return; | 
|---|
| 74 |  | 
|---|
| 75 | const int X = xOffset  + x*blockWidth; | 
|---|
| 76 | const int Y = yOffset  + (y - 1)*blockHeight; | 
|---|
| 77 |  | 
|---|
| 78 | bool localPainter = paint == 0; | 
|---|
| 79 | QPainter *p; | 
|---|
| 80 | QWidget *w; | 
|---|
| 81 | if ( localPainter ) { | 
|---|
| 82 | p = new QPainter( this ); | 
|---|
| 83 | w = this; | 
|---|
| 84 | } else { | 
|---|
| 85 | p = paint; | 
|---|
| 86 | w = paint_widget; | 
|---|
| 87 | } | 
|---|
| 88 | drawTetrixButton( p, X, Y, blockWidth, blockHeight, | 
|---|
| 89 | value == 0 ? 0 : &colors[value-1], w); | 
|---|
| 90 | /* | 
|---|
| 91 | if ( value != 0 ) { | 
|---|
| 92 | QColor tc, bc; | 
|---|
| 93 | tc = colors[value-1].light(); | 
|---|
| 94 | bc = colors[value-1].dark(); | 
|---|
| 95 | p->drawShadePanel( X, Y, blockWidth, blockHeight, | 
|---|
| 96 | tc, bc, 1, colors[value-1], TRUE ); | 
|---|
| 97 | } | 
|---|
| 98 | else | 
|---|
| 99 | p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() ); | 
|---|
| 100 | */ | 
|---|
| 101 | if ( localPainter ) | 
|---|
| 102 | delete p; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void QTetrixBoard::drawNextSquare( int x, int y, int value ) | 
|---|
| 106 | { | 
|---|
| 107 | if ( value == 0 ) | 
|---|
| 108 | emit drawNextSquareSignal (x, y, 0 ); | 
|---|
| 109 | else | 
|---|
| 110 | emit drawNextSquareSignal( x, y, &colors[value-1] ); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void QTetrixBoard::updateRemoved( int noOfLines ) | 
|---|
| 114 | { | 
|---|
| 115 | if ( noOfLines > 0 ) { | 
|---|
| 116 | timer->stop(); | 
|---|
| 117 | timer->start( waitAfterLineTime ); | 
|---|
| 118 | waitingAfterLine = TRUE; | 
|---|
| 119 | } | 
|---|
| 120 | emit updateRemovedSignal( noOfLines ); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | void QTetrixBoard::updateScore( int newScore ) | 
|---|
| 124 | { | 
|---|
| 125 | emit updateScoreSignal( newScore ); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void QTetrixBoard::updateLevel( int newLevel ) | 
|---|
| 129 | { | 
|---|
| 130 | timer->stop(); | 
|---|
| 131 | updateTimeoutTime(); | 
|---|
| 132 | timer->start( timeoutTime ); | 
|---|
| 133 | emit updateLevelSignal( newLevel ); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | void QTetrixBoard::pieceDropped(int) | 
|---|
| 137 | { | 
|---|
| 138 | if ( waitingAfterLine ) // give player a break if a line has been removed | 
|---|
| 139 | return; | 
|---|
| 140 | newPiece(); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | void QTetrixBoard::gameOver() | 
|---|
| 144 | { | 
|---|
| 145 | timer->stop(); | 
|---|
| 146 | noGame = TRUE; | 
|---|
| 147 | emit gameOverSignal(); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | void QTetrixBoard::timeout() | 
|---|
| 151 | { | 
|---|
| 152 | if ( waitingAfterLine ) { | 
|---|
| 153 | timer->stop(); | 
|---|
| 154 | waitingAfterLine = FALSE; | 
|---|
| 155 | newPiece(); | 
|---|
| 156 | timer->start( timeoutTime ); | 
|---|
| 157 | } else { | 
|---|
| 158 | oneLineDown(); | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void QTetrixBoard::drawContents( QPainter *p ) | 
|---|
| 163 | { | 
|---|
| 164 | const char *text = "Press \"Pause\""; | 
|---|
| 165 | QRect r = contentsRect(); | 
|---|
| 166 | paint = p;                          // set widget painter | 
|---|
| 167 | if ( isPaused ) { | 
|---|
| 168 | p->drawText( r, AlignCenter | AlignVCenter, text ); | 
|---|
| 169 | return; | 
|---|
| 170 | } | 
|---|
| 171 | int x1,y1,x2,y2; | 
|---|
| 172 | x1 = (r.left() - xOffset) / blockWidth; | 
|---|
| 173 | if (x1 < 0) | 
|---|
| 174 | x1 = 0; | 
|---|
| 175 | if (x1 >= boardWidth()) | 
|---|
| 176 | x1 = boardWidth() - 1; | 
|---|
| 177 |  | 
|---|
| 178 | x2 = (r.right() - xOffset) / blockWidth; | 
|---|
| 179 | if (x2 < 0) | 
|---|
| 180 | x2 = 0; | 
|---|
| 181 | if (x2 >= boardWidth()) | 
|---|
| 182 | x2 = boardWidth() - 1; | 
|---|
| 183 |  | 
|---|
| 184 | y1 = (r.top() - yOffset) / blockHeight; | 
|---|
| 185 | if (y1 < 0) | 
|---|
| 186 | y1 = 0; | 
|---|
| 187 | if (y1 >= boardHeight()) | 
|---|
| 188 | y1 = boardHeight() - 1; | 
|---|
| 189 |  | 
|---|
| 190 | y2 = (r.bottom() - yOffset) / blockHeight; | 
|---|
| 191 | if (y2 < 0) | 
|---|
| 192 | y2 = 0; | 
|---|
| 193 | if (y2 >= boardHeight()) | 
|---|
| 194 | y2 = boardHeight() - 1; | 
|---|
| 195 |  | 
|---|
| 196 | updateBoard( x1, y1, x2, y2, TRUE ); | 
|---|
| 197 | paint = 0;                          // reset widget painter | 
|---|
| 198 | return; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | void QTetrixBoard::resizeEvent(QResizeEvent *e) | 
|---|
| 202 | { | 
|---|
| 203 | QSize sz = e->size(); | 
|---|
| 204 | blockWidth  = (sz.width() - 3)/10; | 
|---|
| 205 | blockHeight = (sz.height() - 3)/22; | 
|---|
| 206 | xOffset     = 1; | 
|---|
| 207 | yOffset     = 1; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | void QTetrixBoard::keyPressEvent( QKeyEvent *e ) | 
|---|
| 211 | { | 
|---|
| 212 | if ( noGame || isPaused || waitingAfterLine ) | 
|---|
| 213 | return; | 
|---|
| 214 | switch( e->key() ) { | 
|---|
| 215 | case Key_Left : | 
|---|
| 216 | moveLeft(); | 
|---|
| 217 | break; | 
|---|
| 218 | case Key_Right : | 
|---|
| 219 | moveRight(); | 
|---|
| 220 | break; | 
|---|
| 221 | case Key_Down : | 
|---|
| 222 | rotateRight(); | 
|---|
| 223 | break; | 
|---|
| 224 | case Key_Up : | 
|---|
| 225 | rotateLeft(); | 
|---|
| 226 | break; | 
|---|
| 227 | case Key_Space : | 
|---|
| 228 | dropDown(); | 
|---|
| 229 | break; | 
|---|
| 230 | case Key_D : | 
|---|
| 231 | oneLineDown(); | 
|---|
| 232 | break; | 
|---|
| 233 | default: | 
|---|
| 234 | return; | 
|---|
| 235 | } | 
|---|
| 236 | e->accept(); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | void QTetrixBoard::updateTimeoutTime() | 
|---|
| 240 | { | 
|---|
| 241 | timeoutTime = 1000/(1 + getLevel()); | 
|---|
| 242 | } | 
|---|