-rw-r--r-- | noncore/games/tetrix/qtetrix.cpp | 58 | ||||
-rw-r--r-- | noncore/games/tetrix/qtetrix.h | 8 | ||||
-rw-r--r-- | noncore/games/tetrix/qtetrixb.cpp | 4 |
3 files changed, 58 insertions, 12 deletions
diff --git a/noncore/games/tetrix/qtetrix.cpp b/noncore/games/tetrix/qtetrix.cpp index 20cf1a7..a6a5f34 100644 --- a/noncore/games/tetrix/qtetrix.cpp +++ b/noncore/games/tetrix/qtetrix.cpp | |||
@@ -1,174 +1,208 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | 21 | ||
22 | #include "qtetrix.h" | 22 | #include "qtetrix.h" |
23 | 23 | ||
24 | #include <qpe/resource.h> | 24 | #include <qpe/resource.h> |
25 | 25 | ||
26 | #include <qlabel.h> | 26 | #include <qlabel.h> |
27 | #include <qdatetime.h> | 27 | #include <qdatetime.h> |
28 | #include <qlayout.h> | 28 | #include <qlayout.h> |
29 | #include <qtimer.h> | ||
29 | 30 | ||
30 | #include "ohighscoredlg.h" | 31 | #include "ohighscoredlg.h" |
31 | 32 | ||
32 | 33 | ||
33 | void drawTetrixButton( QPainter *p, int x, int y, int w, int h, | 34 | void drawTetrixButton( QPainter *p, int x, int y, int w, int h, |
34 | const QColor *color ) | 35 | const QColor *color ) |
35 | { | 36 | { |
36 | QColor fc; | 37 | QColor fc; |
37 | if ( color ) { | 38 | if ( color ) { |
38 | QPointArray a; | 39 | QPointArray a; |
39 | a.setPoints( 3, x,y+h-1, x,y, x+w-1,y ); | 40 | a.setPoints( 3, x,y+h-1, x,y, x+w-1,y ); |
40 | p->setPen( color->light() ); | 41 | p->setPen( color->light() ); |
41 | p->drawPolyline( a ); | 42 | p->drawPolyline( a ); |
42 | a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 ); | 43 | a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 ); |
43 | p->setPen( color->dark() ); | 44 | p->setPen( color->dark() ); |
44 | p->drawPolyline( a ); | 45 | p->drawPolyline( a ); |
45 | x++; | 46 | x++; |
46 | y++; | 47 | y++; |
47 | w -= 2; | 48 | w -= 2; |
48 | h -= 2; | 49 | h -= 2; |
49 | fc = *color; | 50 | fc = *color; |
50 | } | 51 | } |
51 | else | 52 | else |
52 | fc = p->backgroundColor(); | 53 | fc = p->backgroundColor(); |
53 | p->fillRect( x, y, w, h, fc ); | 54 | p->fillRect( x, y, w, h, fc ); |
54 | } | 55 | } |
55 | 56 | ||
56 | 57 | ||
57 | ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name ) | 58 | ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name ) |
58 | : QFrame( parent, name ) | 59 | : QFrame( parent, name ) |
59 | { | 60 | { |
60 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); | 61 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); |
61 | xOffset = -1; // -1 until first resizeEvent. | 62 | xOffset = -1; // -1 until first resizeEvent. |
62 | } | 63 | } |
63 | 64 | ||
64 | void ShowNextPiece::resizeEvent( QResizeEvent *e ) | 65 | void ShowNextPiece::resizeEvent( QResizeEvent *e ) |
65 | { | 66 | { |
66 | QSize sz = e->size(); | 67 | QSize sz = e->size(); |
67 | blockWidth = (sz.width() - 3)/5; | 68 | blockWidth = (sz.width() - 3)/5; |
68 | blockHeight = (sz.height() - 3)/6; | 69 | blockHeight = (sz.height() - 3)/6; |
69 | xOffset = (sz.width() - 3)/5; | 70 | xOffset = (sz.width() - 3)/5; |
70 | yOffset = (sz.height() - 3)/6; | 71 | yOffset = (sz.height() - 3)/6; |
71 | } | 72 | } |
72 | 73 | ||
73 | 74 | ||
74 | void ShowNextPiece::paintEvent( QPaintEvent * ) | 75 | void ShowNextPiece::paintEvent( QPaintEvent * ) |
75 | { | 76 | { |
76 | QPainter p( this ); | 77 | QPainter p( this ); |
77 | drawFrame( &p ); | 78 | drawFrame( &p ); |
78 | p.end(); // explicit end() so any slots can paint too | 79 | p.end(); // explicit end() so any slots can paint too |
79 | emit update(); | 80 | emit update(); |
80 | } | 81 | } |
81 | 82 | ||
82 | 83 | ||
83 | void ShowNextPiece::drawNextSquare(int x, int y,QColor *color) | 84 | void ShowNextPiece::drawNextSquare(int x, int y,QColor *color) |
84 | { | 85 | { |
85 | if (xOffset == -1) // Before first resizeEvent? | 86 | if (xOffset == -1) // Before first resizeEvent? |
86 | return; | 87 | return; |
87 | 88 | ||
88 | QPainter paint; | 89 | QPainter paint; |
89 | paint.begin(this); | 90 | paint.begin(this); |
90 | drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight, | 91 | drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight, |
91 | blockWidth, blockHeight, color ); | 92 | blockWidth, blockHeight, color ); |
92 | paint.end(); | 93 | paint.end(); |
93 | } | 94 | } |
94 | 95 | ||
95 | 96 | ||
96 | QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f ) | 97 | QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f ) |
97 | : QMainWindow( parent, name, f ) | 98 | : QMainWindow( parent, name, f ) |
98 | { | 99 | { |
99 | setIcon( Resource::loadPixmap( "tetrix_icon" ) ); | 100 | setIcon( Resource::loadPixmap( "tetrix_icon" ) ); |
100 | setCaption( tr("Tetrix" ) ); | 101 | setCaption( tr("Tetrix" ) ); |
101 | 102 | ||
102 | QTime t = QTime::currentTime(); | 103 | QTime t = QTime::currentTime(); |
103 | TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/ | 104 | TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/ |
104 | (24+60+60) ); | 105 | (24+60+60) ); |
105 | 106 | ||
106 | QWidget *gameArea = new QWidget( this ); | 107 | QWidget *gameArea = new QWidget( this ); |
107 | setCentralWidget( gameArea ); | 108 | setCentralWidget( gameArea ); |
108 | 109 | ||
109 | QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 ); | 110 | QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 ); |
111 | gl->setColStretch( 1, 5 ); | ||
112 | gl->setColStretch( 2, 10 ); | ||
110 | 113 | ||
111 | QLabel *l; | 114 | QLabel *l; |
112 | l = new QLabel( tr("Next"), gameArea ); | 115 | l = new QLabel( tr("Next"), gameArea ); |
113 | gl->addWidget( l, 0, 0 ); | 116 | gl->addWidget( l, 0, 0 ); |
114 | showNext = new ShowNextPiece(gameArea); | 117 | showNext = new ShowNextPiece(gameArea); |
115 | showNext->setBackgroundColor(QColor(0,0,0)); | 118 | showNext->setBackgroundColor(QColor(0,0,0)); |
116 | gl->addWidget( showNext, 0, 1 ); | 119 | gl->addWidget( showNext, 0, 1 ); |
117 | 120 | ||
118 | l = new QLabel( tr("Score"), gameArea ); | 121 | l = new QLabel( tr("Score"), gameArea ); |
119 | gl->addWidget( l, 1, 0 ); | 122 | gl->addWidget( l, 1, 0 ); |
120 | showScore = new QLabel(gameArea); | 123 | showScore = new QLabel(gameArea); |
121 | gl->addWidget( showScore, 1, 1 ); | 124 | gl->addWidget( showScore, 1, 1 ); |
122 | l = new QLabel( tr("Level"), gameArea ); | 125 | l = new QLabel( tr("Level"), gameArea ); |
123 | gl->addWidget( l, 2, 0 ); | 126 | gl->addWidget( l, 2, 0 ); |
124 | showLevel = new QLabel(gameArea); | 127 | showLevel = new QLabel(gameArea); |
125 | gl->addWidget( showLevel, 2, 1 ); | 128 | gl->addWidget( showLevel, 2, 1 ); |
126 | l = new QLabel( tr("Removed"), gameArea ); | 129 | l = new QLabel( tr("Removed"), gameArea ); |
127 | gl->addWidget( l, 3, 0 ); | 130 | gl->addWidget( l, 3, 0 ); |
128 | showLines = new QLabel(gameArea); | 131 | showLines = new QLabel(gameArea); |
129 | gl->addWidget( showLines, 3, 1 ); | 132 | gl->addWidget( showLines, 3, 1 ); |
130 | 133 | ||
131 | board = new QTetrixBoard(gameArea); | 134 | board = new QTetrixBoard(gameArea); |
132 | board->setBackgroundColor(QColor(0,0,0)); | 135 | board->setBackgroundColor(QColor(0,0,0)); |
133 | board->setFixedWidth( 124 ); | ||
134 | gl->addMultiCellWidget( board, 0, 4, 2, 2 ); | 136 | gl->addMultiCellWidget( board, 0, 4, 2, 2 ); |
135 | gl->addColSpacing( 2, 100 ); | ||
136 | gl->addColSpacing( 1, 35 ); | ||
137 | gl->addRowSpacing( 0, 35 ); | ||
138 | 137 | ||
139 | QPushButton *pb = new QPushButton( tr("Start"), gameArea ); | 138 | QPushButton *pb = new QPushButton( tr("Start"), gameArea ); |
140 | pb->setFocusPolicy( NoFocus ); | 139 | pb->setFocusPolicy( NoFocus ); |
141 | connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) ); | 140 | connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) ); |
142 | gl->addMultiCellWidget( pb, 4, 4, 0, 1 ); | 141 | gl->addMultiCellWidget( pb, 4, 4, 0, 1 ); |
143 | 142 | ||
144 | connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) ); | 143 | connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) ); |
145 | connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), showNext, | 144 | connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), this, |
146 | SLOT(drawNextSquare(int,int,QColor*)) ); | 145 | SLOT(setNext(int,int,QColor*)) ); |
147 | connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) ); | 146 | connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) ); |
148 | connect( board, SIGNAL(updateScoreSignal(int)), showScore, | 147 | connect( board, SIGNAL(updateScoreSignal(int)), showScore, SLOT(setNum(int)) ); |
149 | SLOT(setNum(int)) ); | 148 | connect( board, SIGNAL(updateLevelSignal(int)), showLevel, SLOT(setNum(int)) ); |
150 | connect( board, SIGNAL(updateLevelSignal(int)), showLevel, | 149 | connect( board, SIGNAL(updateRemovedSignal(int)), showLines, SLOT(setNum(int)) ); |
151 | SLOT(setNum(int))); | ||
152 | connect( board, SIGNAL(updateRemovedSignal(int)), showLines, | ||
153 | SLOT(setNum(int))); | ||
154 | 150 | ||
155 | showScore->setNum( 0 ); | 151 | showScore->setNum( 0 ); |
156 | showLevel->setNum( 0 ); | 152 | showLevel->setNum( 0 ); |
157 | showLines->setNum( 0 ); | 153 | showLines->setNum( 0 ); |
158 | board->revealNextPiece(TRUE); | 154 | board->revealNextPiece(TRUE); |
159 | board->setFocusPolicy( StrongFocus ); | 155 | board->setFocusPolicy( StrongFocus ); |
156 | |||
157 | QTimer::singleShot( -1, this, SLOT(setup()) ); | ||
158 | } | ||
159 | |||
160 | void QTetrix::setup() | ||
161 | { | ||
162 | resizeEvent( 0x0 ); | ||
160 | } | 163 | } |
161 | 164 | ||
162 | void QTetrix::gameOver() | 165 | void QTetrix::gameOver() |
163 | { | 166 | { |
164 | OHighscore *hs = new OHighscore( showScore->text().toInt() , showLevel->text().toInt() ); | 167 | OHighscore *hs = new OHighscore( showScore->text().toInt() , showLevel->text().toInt() ); |
165 | if ( hs->isNewhighscore ) | 168 | if ( hs->isNewhighscore ) |
166 | hs->insertData( hs->getName(), showScore->text().toInt() , showLevel->text().toInt() ); | 169 | hs->insertData( hs->getName(), showScore->text().toInt() , showLevel->text().toInt() ); |
167 | OHighscoreDialog hscdlg( hs, this, "OHighscoreDialog", true ); | 170 | OHighscoreDialog hscdlg( hs, this, "OHighscoreDialog", true ); |
168 | hscdlg.exec(); | 171 | hscdlg.exec(); |
169 | } | 172 | } |
170 | 173 | ||
171 | void QTetrix::quit() | 174 | void QTetrix::quit() |
172 | { | 175 | { |
173 | close(); | 176 | close(); |
174 | } | 177 | } |
178 | |||
179 | void QTetrix::setNext( int x, int y, QColor *color ) | ||
180 | { | ||
181 | resizeEvent( 0x0 ); | ||
182 | showNext->drawNextSquare( x, y, color ); | ||
183 | } | ||
184 | |||
185 | void QTetrix::resizeEvent( QResizeEvent * ) | ||
186 | { | ||
187 | // Set size of board | ||
188 | int widthFactor = board->QFrame::width() / board->boardWidth(); | ||
189 | if ( widthFactor < 1 ) | ||
190 | widthFactor = 1; | ||
191 | int heightFactor = board->QFrame::height() / board->boardHeight(); | ||
192 | if ( heightFactor < 1 ) | ||
193 | heightFactor = 1; | ||
194 | widthFactor > heightFactor ? board->resize( heightFactor * board->boardWidth() + 2, | ||
195 | heightFactor * board->boardHeight() + 2 ) | ||
196 | : board->resize( widthFactor * board->boardWidth() + 2, | ||
197 | widthFactor * board->boardHeight() + 2 ); | ||
198 | |||
199 | // Set size of preview widget | ||
200 | widthFactor = showNext->width() / 5; | ||
201 | if ( widthFactor < 1 ) | ||
202 | widthFactor = 1; | ||
203 | heightFactor = showNext->height() / 6; | ||
204 | if ( heightFactor < 1 ) | ||
205 | heightFactor = 1; | ||
206 | widthFactor > heightFactor ? showNext->resize( heightFactor * 5 + 2, heightFactor * 6 + 2 ) | ||
207 | : showNext->resize( widthFactor * 5 + 2, widthFactor * 6 + 2 ); | ||
208 | } | ||
diff --git a/noncore/games/tetrix/qtetrix.h b/noncore/games/tetrix/qtetrix.h index c8959c5..8c44b77 100644 --- a/noncore/games/tetrix/qtetrix.h +++ b/noncore/games/tetrix/qtetrix.h | |||
@@ -1,77 +1,85 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | #ifndef QTETRIX_H | 20 | #ifndef QTETRIX_H |
21 | #define QTETRIX_H | 21 | #define QTETRIX_H |
22 | 22 | ||
23 | #include "qtetrixb.h" | 23 | #include "qtetrixb.h" |
24 | #include <qframe.h> | 24 | #include <qframe.h> |
25 | #include <qlcdnumber.h> | 25 | #include <qlcdnumber.h> |
26 | #include <qpushbutton.h> | 26 | #include <qpushbutton.h> |
27 | #include <qpainter.h> | 27 | #include <qpainter.h> |
28 | #include <qmainwindow.h> | 28 | #include <qmainwindow.h> |
29 | 29 | ||
30 | class QLabel; | 30 | class QLabel; |
31 | 31 | ||
32 | class ShowNextPiece : public QFrame | 32 | class ShowNextPiece : public QFrame |
33 | { | 33 | { |
34 | Q_OBJECT | 34 | Q_OBJECT |
35 | friend class QTetrix; | 35 | friend class QTetrix; |
36 | public: | 36 | public: |
37 | ShowNextPiece( QWidget *parent=0, const char *name=0 ); | 37 | ShowNextPiece( QWidget *parent=0, const char *name=0 ); |
38 | public slots: | 38 | public slots: |
39 | void drawNextSquare( int x, int y,QColor *color ); | 39 | void drawNextSquare( int x, int y,QColor *color ); |
40 | signals: | 40 | signals: |
41 | void update(); | 41 | void update(); |
42 | private: | 42 | private: |
43 | void paintEvent( QPaintEvent * ); | 43 | void paintEvent( QPaintEvent * ); |
44 | void resizeEvent( QResizeEvent * ); | 44 | void resizeEvent( QResizeEvent * ); |
45 | 45 | ||
46 | int blockWidth,blockHeight; | 46 | int blockWidth,blockHeight; |
47 | int xOffset,yOffset; | 47 | int xOffset,yOffset; |
48 | }; | 48 | }; |
49 | 49 | ||
50 | 50 | ||
51 | class QTetrix : public QMainWindow | 51 | class QTetrix : public QMainWindow |
52 | { | 52 | { |
53 | Q_OBJECT | 53 | Q_OBJECT |
54 | public: | 54 | public: |
55 | static QString appName() { return QString::fromLatin1("tetrix"); } | 55 | static QString appName() { return QString::fromLatin1("tetrix"); } |
56 | QTetrix( QWidget *parent=0, const char *name=0, WFlags f=0 ); | 56 | QTetrix( QWidget *parent=0, const char *name=0, WFlags f=0 ); |
57 | void startGame() { board->startGame(); } | 57 | void startGame() { board->startGame(); } |
58 | 58 | ||
59 | public slots: | 59 | public slots: |
60 | void setup(); | ||
60 | void gameOver(); | 61 | void gameOver(); |
61 | void quit(); | 62 | void quit(); |
63 | |||
64 | void setNext( int x, int y, QColor *color ); | ||
65 | // void setScore( int score ); | ||
66 | // void setLevel( int level ); | ||
67 | // void setLines( int lines ); | ||
68 | |||
62 | private: | 69 | private: |
63 | void keyPressEvent( QKeyEvent *e ) { board->keyPressEvent(e); } | 70 | void keyPressEvent( QKeyEvent *e ) { board->keyPressEvent(e); } |
71 | void resizeEvent( QResizeEvent * ); | ||
64 | 72 | ||
65 | QTetrixBoard *board; | 73 | QTetrixBoard *board; |
66 | ShowNextPiece *showNext; | 74 | ShowNextPiece *showNext; |
67 | QLabel *showScore; | 75 | QLabel *showScore; |
68 | QLabel *showLevel; | 76 | QLabel *showLevel; |
69 | QLabel *showLines; | 77 | QLabel *showLines; |
70 | }; | 78 | }; |
71 | 79 | ||
72 | 80 | ||
73 | void drawTetrixButton( QPainter *, int x, int y, int w, int h, | 81 | void drawTetrixButton( QPainter *, int x, int y, int w, int h, |
74 | const QColor *color ); | 82 | const QColor *color ); |
75 | 83 | ||
76 | 84 | ||
77 | #endif | 85 | #endif |
diff --git a/noncore/games/tetrix/qtetrixb.cpp b/noncore/games/tetrix/qtetrixb.cpp index 3c179df..8c41fb2 100644 --- a/noncore/games/tetrix/qtetrixb.cpp +++ b/noncore/games/tetrix/qtetrixb.cpp | |||
@@ -1,249 +1,253 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | 21 | ||
22 | #include "qtetrixb.h" | 22 | #include "qtetrixb.h" |
23 | #include "qtetrix.h" | 23 | #include "qtetrix.h" |
24 | #include <qtimer.h> | 24 | #include <qtimer.h> |
25 | 25 | ||
26 | const int waitAfterLineTime = 500; | 26 | const int waitAfterLineTime = 500; |
27 | 27 | ||
28 | QTetrixBoard::QTetrixBoard( QWidget *p, const char *name ) | 28 | QTetrixBoard::QTetrixBoard( QWidget *p, const char *name ) |
29 | : QFrame( p, name ) | 29 | : QFrame( p, name ) |
30 | { | 30 | { |
31 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); | 31 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); |
32 | paint = 0; | 32 | paint = 0; |
33 | timer = new QTimer(this); | 33 | timer = new QTimer(this); |
34 | connect( timer, SIGNAL(timeout()), SLOT(timeout()) ); | 34 | connect( timer, SIGNAL(timeout()), SLOT(timeout()) ); |
35 | 35 | ||
36 | colors[0].setRgb(200,100,100); | 36 | colors[0].setRgb(200,100,100); |
37 | colors[1].setRgb(100,200,100); | 37 | colors[1].setRgb(100,200,100); |
38 | colors[2].setRgb(100,100,200); | 38 | colors[2].setRgb(100,100,200); |
39 | colors[3].setRgb(200,200,100); | 39 | colors[3].setRgb(200,200,100); |
40 | colors[4].setRgb(200,100,200); | 40 | colors[4].setRgb(200,100,200); |
41 | colors[5].setRgb(100,200,200); | 41 | colors[5].setRgb(100,200,200); |
42 | colors[6].setRgb(218,170, 0); | 42 | colors[6].setRgb(218,170, 0); |
43 | 43 | ||
44 | xOffset = -1; // -1 until a resizeEvent is received. | 44 | xOffset = -1; // -1 until a resizeEvent is received. |
45 | blockWidth = 20; | 45 | blockWidth = 20; |
46 | yOffset = 30; | 46 | yOffset = 30; |
47 | blockHeight = 20; | 47 | blockHeight = 20; |
48 | noGame = TRUE; | 48 | noGame = TRUE; |
49 | isPaused = FALSE; | 49 | isPaused = FALSE; |
50 | waitingAfterLine = FALSE; | 50 | waitingAfterLine = FALSE; |
51 | updateTimeoutTime(); // Sets timeoutTime | 51 | updateTimeoutTime(); // Sets timeoutTime |
52 | } | 52 | } |
53 | 53 | ||
54 | void QTetrixBoard::startGame(int gameType,int fillRandomLines) | 54 | void QTetrixBoard::startGame(int gameType,int fillRandomLines) |
55 | { | 55 | { |
56 | if ( isPaused ) | 56 | if ( isPaused ) |
57 | return; // ignore if game is paused | 57 | return; // ignore if game is paused |
58 | noGame = FALSE; | 58 | noGame = FALSE; |
59 | |||
59 | GenericTetrix::startGame( gameType, fillRandomLines ); | 60 | GenericTetrix::startGame( gameType, fillRandomLines ); |
60 | // Note that the timer is started by updateLevel! | 61 | // Note that the timer is started by updateLevel! |
61 | } | 62 | } |
62 | 63 | ||
63 | 64 | ||
64 | void QTetrixBoard::pause() | 65 | void QTetrixBoard::pause() |
65 | { | 66 | { |
66 | if ( noGame ) // game not active | 67 | if ( noGame ) // game not active |
67 | return; | 68 | return; |
68 | isPaused = !isPaused; | 69 | isPaused = !isPaused; |
69 | if ( isPaused ) { | 70 | if ( isPaused ) { |
70 | timer->stop(); | 71 | timer->stop(); |
71 | hideBoard(); | 72 | hideBoard(); |
72 | } | 73 | } |
73 | else | 74 | else |
74 | timer->start(timeoutTime); | 75 | timer->start(timeoutTime); |
75 | update(); | 76 | update(); |
76 | } | 77 | } |
77 | 78 | ||
78 | 79 | ||
79 | void QTetrixBoard::drawSquare(int x,int y,int value) | 80 | void QTetrixBoard::drawSquare(int x,int y,int value) |
80 | { | 81 | { |
81 | if (xOffset == -1) // Before first resizeEvent? | 82 | if (xOffset == -1) // Before first resizeEvent? |
82 | return; | 83 | return; |
83 | 84 | ||
84 | const int X = xOffset + x*blockWidth; | 85 | const int X = xOffset + x*blockWidth; |
85 | const int Y = yOffset + (y - 1)*blockHeight; | 86 | const int Y = yOffset + (y - 1)*blockHeight; |
86 | 87 | ||
87 | bool localPainter = paint == 0; | 88 | bool localPainter = paint == 0; |
88 | QPainter *p; | 89 | QPainter *p; |
89 | if ( localPainter ) | 90 | if ( localPainter ) |
90 | p = new QPainter( this ); | 91 | p = new QPainter( this ); |
91 | else | 92 | else |
92 | p = paint; | 93 | p = paint; |
93 | drawTetrixButton( p, X, Y, blockWidth, blockHeight, | 94 | drawTetrixButton( p, X, Y, blockWidth, blockHeight, |
94 | value == 0 ? 0 : &colors[value-1] ); | 95 | value == 0 ? 0 : &colors[value-1] ); |
95 | /* | 96 | /* |
96 | if ( value != 0 ) { | 97 | if ( value != 0 ) { |
97 | QColor tc, bc; | 98 | QColor tc, bc; |
98 | tc = colors[value-1].light(); | 99 | tc = colors[value-1].light(); |
99 | bc = colors[value-1].dark(); | 100 | bc = colors[value-1].dark(); |
100 | p->drawShadePanel( X, Y, blockWidth, blockHeight, | 101 | p->drawShadePanel( X, Y, blockWidth, blockHeight, |
101 | tc, bc, 1, colors[value-1], TRUE ); | 102 | tc, bc, 1, colors[value-1], TRUE ); |
102 | } | 103 | } |
103 | else | 104 | else |
104 | p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() ); | 105 | p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() ); |
105 | */ | 106 | */ |
106 | if ( localPainter ) | 107 | if ( localPainter ) |
107 | delete p; | 108 | delete p; |
108 | } | 109 | } |
109 | 110 | ||
110 | void QTetrixBoard::drawNextSquare( int x, int y, int value ) | 111 | void QTetrixBoard::drawNextSquare( int x, int y, int value ) |
111 | { | 112 | { |
112 | if ( value == 0 ) | 113 | if ( value == 0 ) |
113 | emit drawNextSquareSignal (x, y, 0 ); | 114 | emit drawNextSquareSignal (x, y, 0 ); |
114 | else | 115 | else |
115 | emit drawNextSquareSignal( x, y, &colors[value-1] ); | 116 | emit drawNextSquareSignal( x, y, &colors[value-1] ); |
116 | } | 117 | } |
117 | 118 | ||
118 | void QTetrixBoard::updateRemoved( int noOfLines ) | 119 | void QTetrixBoard::updateRemoved( int noOfLines ) |
119 | { | 120 | { |
120 | if ( noOfLines > 0 ) { | 121 | if ( noOfLines > 0 ) { |
121 | timer->stop(); | 122 | timer->stop(); |
122 | timer->start( waitAfterLineTime ); | 123 | timer->start( waitAfterLineTime ); |
123 | waitingAfterLine = TRUE; | 124 | waitingAfterLine = TRUE; |
124 | } | 125 | } |
125 | emit updateRemovedSignal( noOfLines ); | 126 | emit updateRemovedSignal( noOfLines ); |
126 | } | 127 | } |
127 | 128 | ||
128 | void QTetrixBoard::updateScore( int newScore ) | 129 | void QTetrixBoard::updateScore( int newScore ) |
129 | { | 130 | { |
130 | emit updateScoreSignal( newScore ); | 131 | emit updateScoreSignal( newScore ); |
131 | } | 132 | } |
132 | 133 | ||
133 | void QTetrixBoard::updateLevel( int newLevel ) | 134 | void QTetrixBoard::updateLevel( int newLevel ) |
134 | { | 135 | { |
135 | timer->stop(); | 136 | timer->stop(); |
136 | updateTimeoutTime(); | 137 | updateTimeoutTime(); |
137 | timer->start( timeoutTime ); | 138 | timer->start( timeoutTime ); |
138 | emit updateLevelSignal( newLevel ); | 139 | emit updateLevelSignal( newLevel ); |
139 | } | 140 | } |
140 | 141 | ||
141 | void QTetrixBoard::pieceDropped(int) | 142 | void QTetrixBoard::pieceDropped(int) |
142 | { | 143 | { |
143 | if ( waitingAfterLine ) // give player a break if a line has been removed | 144 | if ( waitingAfterLine ) // give player a break if a line has been removed |
144 | return; | 145 | return; |
145 | newPiece(); | 146 | newPiece(); |
146 | } | 147 | } |
147 | 148 | ||
148 | void QTetrixBoard::gameOver() | 149 | void QTetrixBoard::gameOver() |
149 | { | 150 | { |
150 | timer->stop(); | 151 | timer->stop(); |
151 | noGame = TRUE; | 152 | noGame = TRUE; |
152 | emit gameOverSignal(); | 153 | emit gameOverSignal(); |
153 | } | 154 | } |
154 | 155 | ||
155 | void QTetrixBoard::timeout() | 156 | void QTetrixBoard::timeout() |
156 | { | 157 | { |
157 | if ( waitingAfterLine ) { | 158 | if ( waitingAfterLine ) { |
158 | timer->stop(); | 159 | timer->stop(); |
159 | waitingAfterLine = FALSE; | 160 | waitingAfterLine = FALSE; |
160 | newPiece(); | 161 | newPiece(); |
161 | timer->start( timeoutTime ); | 162 | timer->start( timeoutTime ); |
162 | } else { | 163 | } else { |
163 | oneLineDown(); | 164 | oneLineDown(); |
164 | } | 165 | } |
165 | } | 166 | } |
166 | 167 | ||
167 | void QTetrixBoard::drawContents( QPainter *p ) | 168 | void QTetrixBoard::drawContents( QPainter *p ) |
168 | { | 169 | { |
169 | const char *text = "Press \"Pause\""; | 170 | const char *text = "Press \"Pause\""; |
170 | QRect r = contentsRect(); | 171 | QRect r = contentsRect(); |
171 | paint = p; // set widget painter | 172 | paint = p; // set widget painter |
172 | if ( isPaused ) { | 173 | if ( isPaused ) { |
173 | p->drawText( r, AlignCenter | AlignVCenter, text ); | 174 | p->drawText( r, AlignCenter | AlignVCenter, text ); |
174 | return; | 175 | return; |
175 | } | 176 | } |
176 | int x1,y1,x2,y2; | 177 | int x1,y1,x2,y2; |
177 | x1 = (r.left() - xOffset) / blockWidth; | 178 | x1 = (r.left() - xOffset) / blockWidth; |
178 | if (x1 < 0) | 179 | if (x1 < 0) |
179 | x1 = 0; | 180 | x1 = 0; |
180 | if (x1 >= boardWidth()) | 181 | if (x1 >= boardWidth()) |
181 | x1 = boardWidth() - 1; | 182 | x1 = boardWidth() - 1; |
182 | 183 | ||
183 | x2 = (r.right() - xOffset) / blockWidth; | 184 | x2 = (r.right() - xOffset) / blockWidth; |
184 | if (x2 < 0) | 185 | if (x2 < 0) |
185 | x2 = 0; | 186 | x2 = 0; |
186 | if (x2 >= boardWidth()) | 187 | if (x2 >= boardWidth()) |
187 | x2 = boardWidth() - 1; | 188 | x2 = boardWidth() - 1; |
188 | 189 | ||
189 | y1 = (r.top() - yOffset) / blockHeight; | 190 | y1 = (r.top() - yOffset) / blockHeight; |
190 | if (y1 < 0) | 191 | if (y1 < 0) |
191 | y1 = 0; | 192 | y1 = 0; |
192 | if (y1 >= boardHeight()) | 193 | if (y1 >= boardHeight()) |
193 | y1 = boardHeight() - 1; | 194 | y1 = boardHeight() - 1; |
194 | 195 | ||
195 | y2 = (r.bottom() - yOffset) / blockHeight; | 196 | y2 = (r.bottom() - yOffset) / blockHeight; |
196 | if (y2 < 0) | 197 | if (y2 < 0) |
197 | y2 = 0; | 198 | y2 = 0; |
198 | if (y2 >= boardHeight()) | 199 | if (y2 >= boardHeight()) |
199 | y2 = boardHeight() - 1; | 200 | y2 = boardHeight() - 1; |
200 | 201 | ||
201 | updateBoard( x1, y1, x2, y2, TRUE ); | 202 | updateBoard( x1, y1, x2, y2, TRUE ); |
202 | paint = 0; // reset widget painter | 203 | paint = 0; // reset widget painter |
203 | return; | 204 | return; |
204 | } | 205 | } |
205 | 206 | ||
206 | void QTetrixBoard::resizeEvent(QResizeEvent *e) | 207 | void QTetrixBoard::resizeEvent(QResizeEvent *e) |
207 | { | 208 | { |
208 | QSize sz = e->size(); | 209 | QSize sz = e->size(); |
210 | |||
209 | blockWidth = (sz.width() - 2)/10; | 211 | blockWidth = (sz.width() - 2)/10; |
210 | blockHeight = (sz.height() - 2)/22; | 212 | blockHeight = (sz.height() - 2)/22; |
213 | /* blockWidth > blockHeight ? blockWidth = blockHeight | ||
214 | : blockHeight = blockWidth;*/ | ||
211 | xOffset = 1; | 215 | xOffset = 1; |
212 | //yOffset = 1; | 216 | //yOffset = 1; |
213 | yOffset = (sz.height() - 2) - (blockHeight *22); | 217 | yOffset = (sz.height() - 2) - (blockHeight *22); |
214 | } | 218 | } |
215 | 219 | ||
216 | void QTetrixBoard::keyPressEvent( QKeyEvent *e ) | 220 | void QTetrixBoard::keyPressEvent( QKeyEvent *e ) |
217 | { | 221 | { |
218 | if ( noGame || isPaused || waitingAfterLine ) | 222 | if ( noGame || isPaused || waitingAfterLine ) |
219 | return; | 223 | return; |
220 | switch( e->key() ) { | 224 | switch( e->key() ) { |
221 | case Key_Left : | 225 | case Key_Left : |
222 | moveLeft(); | 226 | moveLeft(); |
223 | break; | 227 | break; |
224 | case Key_Right : | 228 | case Key_Right : |
225 | moveRight(); | 229 | moveRight(); |
226 | break; | 230 | break; |
227 | case Key_Down : | 231 | case Key_Down : |
228 | // rotateRight(); | 232 | // rotateRight(); |
229 | dropDown(); | 233 | dropDown(); |
230 | break; | 234 | break; |
231 | case Key_Up : | 235 | case Key_Up : |
232 | rotateLeft(); | 236 | rotateLeft(); |
233 | break; | 237 | break; |
234 | case Key_Space : | 238 | case Key_Space : |
235 | dropDown(); | 239 | dropDown(); |
236 | break; | 240 | break; |
237 | case Key_D : | 241 | case Key_D : |
238 | oneLineDown(); | 242 | oneLineDown(); |
239 | break; | 243 | break; |
240 | default: | 244 | default: |
241 | return; | 245 | return; |
242 | } | 246 | } |
243 | e->accept(); | 247 | e->accept(); |
244 | } | 248 | } |
245 | 249 | ||
246 | void QTetrixBoard::updateTimeoutTime() | 250 | void QTetrixBoard::updateTimeoutTime() |
247 | { | 251 | { |
248 | timeoutTime = 1000/(1 + getLevel()); | 252 | timeoutTime = 1000/(1 + getLevel()); |
249 | } | 253 | } |