author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/games/tetrix/qtetrix.cpp | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'noncore/games/tetrix/qtetrix.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/games/tetrix/qtetrix.cpp | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/noncore/games/tetrix/qtetrix.cpp b/noncore/games/tetrix/qtetrix.cpp new file mode 100644 index 0000000..f649894 --- a/dev/null +++ b/noncore/games/tetrix/qtetrix.cpp | |||
@@ -0,0 +1,170 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
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 | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
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. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | |||
22 | #include "qtetrix.h" | ||
23 | |||
24 | #include <qpe/resource.h> | ||
25 | |||
26 | #include <qapplication.h> | ||
27 | #include <qlabel.h> | ||
28 | #include <qdatetime.h> | ||
29 | #include <qlayout.h> | ||
30 | |||
31 | |||
32 | |||
33 | void drawTetrixButton( QPainter *p, int x, int y, int w, int h, | ||
34 | const QColor *color ) | ||
35 | { | ||
36 | QColor fc; | ||
37 | if ( color ) { | ||
38 | QPointArray a; | ||
39 | a.setPoints( 3, x,y+h-1, x,y, x+w-1,y ); | ||
40 | p->setPen( color->light() ); | ||
41 | p->drawPolyline( a ); | ||
42 | 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->drawPolyline( a ); | ||
45 | x++; | ||
46 | y++; | ||
47 | w -= 2; | ||
48 | h -= 2; | ||
49 | fc = *color; | ||
50 | } | ||
51 | else | ||
52 | fc = p->backgroundColor(); | ||
53 | p->fillRect( x, y, w, h, fc ); | ||
54 | } | ||
55 | |||
56 | |||
57 | ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name ) | ||
58 | : QFrame( parent, name ) | ||
59 | { | ||
60 | setFrameStyle( QFrame::Panel | QFrame::Sunken ); | ||
61 | xOffset = -1; // -1 until first resizeEvent. | ||
62 | } | ||
63 | |||
64 | void ShowNextPiece::resizeEvent( QResizeEvent *e ) | ||
65 | { | ||
66 | QSize sz = e->size(); | ||
67 | blockWidth = (sz.width() - 3)/5; | ||
68 | blockHeight = (sz.height() - 3)/6; | ||
69 | xOffset = (sz.width() - 3)/5; | ||
70 | yOffset = (sz.height() - 3)/6; | ||
71 | } | ||
72 | |||
73 | |||
74 | void ShowNextPiece::paintEvent( QPaintEvent * ) | ||
75 | { | ||
76 | QPainter p( this ); | ||
77 | drawFrame( &p ); | ||
78 | p.end(); // explicit end() so any slots can paint too | ||
79 | emit update(); | ||
80 | } | ||
81 | |||
82 | |||
83 | void ShowNextPiece::drawNextSquare(int x, int y,QColor *color) | ||
84 | { | ||
85 | if (xOffset == -1) // Before first resizeEvent? | ||
86 | return; | ||
87 | |||
88 | QPainter paint; | ||
89 | paint.begin(this); | ||
90 | drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight, | ||
91 | blockWidth, blockHeight, color ); | ||
92 | paint.end(); | ||
93 | } | ||
94 | |||
95 | |||
96 | QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f ) | ||
97 | : QMainWindow( parent, name, f ) | ||
98 | { | ||
99 | setIcon( Resource::loadPixmap( "tetrix_icon" ) ); | ||
100 | setCaption( tr("Tetrix" ) ); | ||
101 | |||
102 | QTime t = QTime::currentTime(); | ||
103 | TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/ | ||
104 | (24+60+60) ); | ||
105 | |||
106 | QWidget *gameArea = new QWidget( this ); | ||
107 | setCentralWidget( gameArea ); | ||
108 | |||
109 | QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 ); | ||
110 | |||
111 | QLabel *l; | ||
112 | l = new QLabel( tr("Next"), gameArea ); | ||
113 | gl->addWidget( l, 0, 0 ); | ||
114 | showNext = new ShowNextPiece(gameArea); | ||
115 | showNext->setBackgroundColor(QColor(0,0,0)); | ||
116 | gl->addWidget( showNext, 0, 1 ); | ||
117 | |||
118 | l = new QLabel( tr("Score"), gameArea ); | ||
119 | gl->addWidget( l, 1, 0 ); | ||
120 | showScore = new QLabel(gameArea); | ||
121 | gl->addWidget( showScore, 1, 1 ); | ||
122 | l = new QLabel( tr("Level"), gameArea ); | ||
123 | gl->addWidget( l, 2, 0 ); | ||
124 | showLevel = new QLabel(gameArea); | ||
125 | gl->addWidget( showLevel, 2, 1 ); | ||
126 | l = new QLabel( tr("Removed"), gameArea ); | ||
127 | gl->addWidget( l, 3, 0 ); | ||
128 | showLines = new QLabel(gameArea); | ||
129 | gl->addWidget( showLines, 3, 1 ); | ||
130 | |||
131 | board = new QTetrixBoard(gameArea); | ||
132 | board->setBackgroundColor(QColor(0,0,0)); | ||
133 | board->setFixedWidth( 124 ); | ||
134 | gl->addMultiCellWidget( board, 0, 4, 2, 2 ); | ||
135 | gl->addColSpacing( 2, 100 ); | ||
136 | gl->addColSpacing( 1, 35 ); | ||
137 | gl->addRowSpacing( 0, 35 ); | ||
138 | |||
139 | QPushButton *pb = new QPushButton( tr("Start"), gameArea ); | ||
140 | pb->setFocusPolicy( NoFocus ); | ||
141 | connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) ); | ||
142 | gl->addMultiCellWidget( pb, 4, 4, 0, 1 ); | ||
143 | |||
144 | connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) ); | ||
145 | connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), showNext, | ||
146 | SLOT(drawNextSquare(int,int,QColor*)) ); | ||
147 | connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) ); | ||
148 | connect( board, SIGNAL(updateScoreSignal(int)), showScore, | ||
149 | SLOT(setNum(int)) ); | ||
150 | connect( board, SIGNAL(updateLevelSignal(int)), showLevel, | ||
151 | SLOT(setNum(int))); | ||
152 | connect( board, SIGNAL(updateRemovedSignal(int)), showLines, | ||
153 | SLOT(setNum(int))); | ||
154 | |||
155 | showScore->setNum( 0 ); | ||
156 | showLevel->setNum( 0 ); | ||
157 | showLines->setNum( 0 ); | ||
158 | board->revealNextPiece(TRUE); | ||
159 | board->setFocusPolicy( StrongFocus ); | ||
160 | } | ||
161 | |||
162 | void QTetrix::gameOver() | ||
163 | { | ||
164 | } | ||
165 | |||
166 | |||
167 | void QTetrix::quit() | ||
168 | { | ||
169 | close(); | ||
170 | } | ||