-rw-r--r-- | noncore/games/solitaire/canvascardgame.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/noncore/games/solitaire/canvascardgame.cpp b/noncore/games/solitaire/canvascardgame.cpp index 32635a0..ef35287 100644 --- a/noncore/games/solitaire/canvascardgame.cpp +++ b/noncore/games/solitaire/canvascardgame.cpp | |||
@@ -1,232 +1,237 @@ | |||
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 | #include "cardgame.h" | 21 | #include "cardgame.h" |
22 | #include "canvasshapes.h" | 22 | #include "canvasshapes.h" |
23 | #include "canvascard.h" | 23 | #include "canvascard.h" |
24 | #include "canvascardgame.h" | 24 | #include "canvascardgame.h" |
25 | 25 | ||
26 | #include <qpe/resource.h> | 26 | #include <qpe/resource.h> |
27 | #include <qpe/config.h> | 27 | #include <qpe/config.h> |
28 | 28 | ||
29 | #include <qmainwindow.h> | 29 | #include <qmainwindow.h> |
30 | #include <qpe/qpemenubar.h> | 30 | #include <qpe/qpemenubar.h> |
31 | #include <qpainter.h> | 31 | #include <qpainter.h> |
32 | #include <qgfx_qws.h> | ||
32 | 33 | ||
33 | #include <stdlib.h> | 34 | #include <stdlib.h> |
34 | #include <limits.h> | 35 | #include <limits.h> |
35 | #include <time.h> | 36 | #include <time.h> |
36 | #include <math.h> | 37 | #include <math.h> |
37 | 38 | ||
38 | 39 | ||
39 | extern int highestZ; | 40 | extern int highestZ; |
40 | 41 | ||
41 | 42 | ||
42 | class CanvasCardPile : public QCanvasRectangle | 43 | class CanvasCardPile : public QCanvasRectangle |
43 | { | 44 | { |
44 | public: | 45 | public: |
45 | CanvasCardPile( CanvasCardGame *ccg, QCanvas *canvas ) : QCanvasRectangle( canvas ), parent( ccg ) { | 46 | CanvasCardPile( CanvasCardGame *ccg, QCanvas *canvas ) : QCanvasRectangle( canvas ), parent( ccg ) { |
46 | pile = new QPixmap( 0, 0 ); | 47 | pile = new QPixmap( 0, 0 ); |
47 | pileHeight = 0; | 48 | pileHeight = 0; |
48 | firstCard = NULL; | 49 | firstCard = NULL; |
49 | } | 50 | } |
50 | 51 | ||
51 | void addCard( CanvasCard *card ); | 52 | void addCard( CanvasCard *card ); |
52 | void advance(int stage); | 53 | void advance(int stage); |
53 | void animatedMove() { animatedMove(savedX, savedY); } | 54 | void animatedMove() { animatedMove(savedX, savedY); } |
54 | void savePos(void) { savedX = (int)x(); savedY = (int)y(); } | 55 | void savePos(void) { savedX = (int)x(); savedY = (int)y(); } |
55 | void animatedMove(int x2, int y2, int steps = 7 ); | 56 | void animatedMove(int x2, int y2, int steps = 7 ); |
56 | 57 | ||
57 | protected: | 58 | protected: |
58 | virtual void draw( QPainter& p ); | 59 | virtual void draw( QPainter& p ); |
59 | 60 | ||
60 | private: | 61 | private: |
61 | CanvasCardGame *parent; | 62 | CanvasCardGame *parent; |
62 | QPixmap *pile; | 63 | QPixmap *pile; |
63 | QImage tempImage32; | 64 | QImage tempImage32; |
64 | CanvasCard *firstCard; | 65 | CanvasCard *firstCard; |
65 | int pileHeight; | 66 | int pileHeight; |
66 | int destX, destY; | 67 | int destX, destY; |
67 | int savedX, savedY; | 68 | int savedX, savedY; |
68 | int animSteps; | 69 | int animSteps; |
69 | }; | 70 | }; |
70 | 71 | ||
71 | 72 | ||
72 | void CanvasCardPile::addCard( CanvasCard *card ) | 73 | void CanvasCardPile::addCard( CanvasCard *card ) |
73 | { | 74 | { |
75 | int offsetDown = ( qt_screen->deviceWidth() < 200 ) ? 9 : 13; | ||
76 | int cardHeight = ( qt_screen->deviceWidth() < 200 ) ? 27 : 36; | ||
77 | int cardWidth = ( qt_screen->deviceWidth() < 200 ) ? 20 : 23; | ||
78 | |||
74 | if ( !firstCard ) | 79 | if ( !firstCard ) |
75 | firstCard = card; | 80 | firstCard = card; |
76 | 81 | ||
77 | int height = 36 + pileHeight * 13; | 82 | int height = cardHeight + pileHeight * offsetDown; |
78 | setSize( 23, height ); | 83 | setSize( cardWidth, height ); |
79 | pile->resize( 23, height ); | 84 | pile->resize( cardWidth, height ); |
80 | QPainter p( pile ); | 85 | QPainter p( pile ); |
81 | p.translate( -card->x(), -card->y() + pileHeight * 13 ); | 86 | p.translate( -card->x(), -card->y() + pileHeight * offsetDown ); |
82 | card->draw( p ); | 87 | card->draw( p ); |
83 | pileHeight++; | 88 | pileHeight++; |
84 | 89 | ||
85 | QImage tempImage; | 90 | QImage tempImage; |
86 | tempImage = *pile; | 91 | tempImage = *pile; |
87 | tempImage32 = tempImage.convertDepth( 32 ); | 92 | tempImage32 = tempImage.convertDepth( 32 ); |
88 | tempImage32.setAlphaBuffer( TRUE ); | 93 | tempImage32.setAlphaBuffer( TRUE ); |
89 | for ( int i = 0; i < tempImage32.width(); i++ ) | 94 | for ( int i = 0; i < tempImage32.width(); i++ ) |
90 | for ( int j = 0; j < tempImage32.height(); j++ ) { | 95 | for ( int j = 0; j < tempImage32.height(); j++ ) { |
91 | QRgb col = tempImage32.pixel( i, j ); | 96 | QRgb col = tempImage32.pixel( i, j ); |
92 | int a = 255-j*220/tempImage32.height(); | 97 | int a = 255-j*220/tempImage32.height(); |
93 | QRgb alpha = qRgba( qRed( col ), qGreen( col ), qBlue( col ), a ); | 98 | QRgb alpha = qRgba( qRed( col ), qGreen( col ), qBlue( col ), a ); |
94 | tempImage32.setPixel( i, j, alpha ); | 99 | tempImage32.setPixel( i, j, alpha ); |
95 | } | 100 | } |
96 | 101 | ||
97 | QRgb alpha = qRgba( 0, 0, 0, 0 ); | 102 | QRgb alpha = qRgba( 0, 0, 0, 0 ); |
98 | tempImage32.setPixel( 1, 0, alpha ); | 103 | tempImage32.setPixel( 1, 0, alpha ); |
99 | tempImage32.setPixel( 0, 0, alpha ); | 104 | tempImage32.setPixel( 0, 0, alpha ); |
100 | tempImage32.setPixel( 0, 1, alpha ); | 105 | tempImage32.setPixel( 0, 1, alpha ); |
101 | 106 | ||
102 | tempImage32.setPixel( 21, 0, alpha ); | 107 | tempImage32.setPixel( cardWidth - 2, 0, alpha ); |
103 | tempImage32.setPixel( 22, 0, alpha ); | 108 | tempImage32.setPixel( cardWidth - 1, 0, alpha ); |
104 | tempImage32.setPixel( 22, 1, alpha ); | 109 | tempImage32.setPixel( cardWidth - 1, 1, alpha ); |
105 | height--; | 110 | height--; |
106 | tempImage32.setPixel( 1, height, alpha ); | 111 | tempImage32.setPixel( 1, height, alpha ); |
107 | tempImage32.setPixel( 0, height - 1, alpha ); | 112 | tempImage32.setPixel( 0, height - 1, alpha ); |
108 | tempImage32.setPixel( 0, height, alpha ); | 113 | tempImage32.setPixel( 0, height, alpha ); |
109 | 114 | ||
110 | tempImage32.setPixel( 21, height, alpha ); | 115 | tempImage32.setPixel( cardWidth - 2, height, alpha ); |
111 | tempImage32.setPixel( 22, height, alpha ); | 116 | tempImage32.setPixel( cardWidth - 1, height, alpha ); |
112 | tempImage32.setPixel( 22, height - 1, alpha ); | 117 | tempImage32.setPixel( cardWidth - 1, height - 1, alpha ); |
113 | } | 118 | } |
114 | 119 | ||
115 | 120 | ||
116 | void CanvasCardPile::advance(int stage) | 121 | void CanvasCardPile::advance(int stage) |
117 | { | 122 | { |
118 | if ( stage==1 ) { | 123 | if ( stage==1 ) { |
119 | if ( animSteps-- <= 0 ) { | 124 | if ( animSteps-- <= 0 ) { |
120 | CanvasCard *item = firstCard; | 125 | CanvasCard *item = firstCard; |
121 | while (item) { | 126 | while (item) { |
122 | item->show(); | 127 | item->show(); |
123 | item = (CanvasCard *)item->getCardPile()->cardInfront(item); | 128 | item = (CanvasCard *)item->getCardPile()->cardInfront(item); |
124 | } | 129 | } |
125 | setVelocity(0,0); | 130 | setVelocity(0,0); |
126 | setAnimated(FALSE); | 131 | setAnimated(FALSE); |
127 | parent->cancelMoving(); | 132 | parent->cancelMoving(); |
128 | hide(); | 133 | hide(); |
129 | move(destX,destY); // exact | 134 | move(destX,destY); // exact |
130 | } | 135 | } |
131 | } | 136 | } |
132 | QCanvasRectangle::advance(stage); | 137 | QCanvasRectangle::advance(stage); |
133 | } | 138 | } |
134 | 139 | ||
135 | 140 | ||
136 | void CanvasCardPile::animatedMove(int x2, int y2, int steps = 7 ) | 141 | void CanvasCardPile::animatedMove(int x2, int y2, int steps ) |
137 | { | 142 | { |
138 | destX = x2; | 143 | destX = x2; |
139 | destY = y2; | 144 | destY = y2; |
140 | 145 | ||
141 | double x1 = x(), y1 = y(), dx = x2 - x1, dy = y2 - y1; | 146 | double x1 = x(), y1 = y(), dx = x2 - x1, dy = y2 - y1; |
142 | 147 | ||
143 | // Ensure a good speed | 148 | // Ensure a good speed |
144 | while ( fabs(dx/steps)+fabs(dy/steps) < 5.0 && steps > 4 ) | 149 | while ( fabs(dx/steps)+fabs(dy/steps) < 5.0 && steps > 4 ) |
145 | steps--; | 150 | steps--; |
146 | 151 | ||
147 | setAnimated(TRUE); | 152 | setAnimated(TRUE); |
148 | setVelocity(dx/steps, dy/steps); | 153 | setVelocity(dx/steps, dy/steps); |
149 | 154 | ||
150 | animSteps = steps; | 155 | animSteps = steps; |
151 | } | 156 | } |
152 | 157 | ||
153 | 158 | ||
154 | void CanvasCardPile::draw( QPainter& p ) | 159 | void CanvasCardPile::draw( QPainter& p ) |
155 | { | 160 | { |
156 | int ix = (int)x(), iy = (int)y(); | 161 | int ix = (int)x(), iy = (int)y(); |
157 | p.drawImage( ix, iy, tempImage32 ); | 162 | p.drawImage( ix, iy, tempImage32 ); |
158 | } | 163 | } |
159 | 164 | ||
160 | 165 | ||
161 | CanvasCardGame::~CanvasCardGame() { | 166 | CanvasCardGame::~CanvasCardGame() { |
162 | // the deletion stuff should be fixed now and only deletes | 167 | // the deletion stuff should be fixed now and only deletes |
163 | // items created by this CardGame. I haven't verified there are zero | 168 | // items created by this CardGame. I haven't verified there are zero |
164 | // memory leaks yet | 169 | // memory leaks yet |
165 | if ( alphaCardPile ) | 170 | if ( alphaCardPile ) |
166 | delete alphaCardPile; | 171 | delete alphaCardPile; |
167 | } | 172 | } |
168 | 173 | ||
169 | 174 | ||
170 | void CanvasCardGame::gameWon() { | 175 | void CanvasCardGame::gameWon() { |
171 | 176 | ||
172 | srand(time(NULL)); | 177 | srand(time(NULL)); |
173 | 178 | ||
174 | QCanvasItemList list = canvas()->allItems(); | 179 | QCanvasItemList list = canvas()->allItems(); |
175 | QCanvasItemList::Iterator it = list.begin(); | 180 | QCanvasItemList::Iterator it = list.begin(); |
176 | 181 | ||
177 | for (; it != list.end(); ++it) { | 182 | for (; it != list.end(); ++it) { |
178 | if ( (*it)->rtti() == canvasCardId ) { | 183 | if ( (*it)->rtti() == canvasCardId ) { |
179 | // disperse the cards everywhere | 184 | // disperse the cards everywhere |
180 | int x = 300 - rand() % 1000; | 185 | int x = 300 - rand() % 1000; |
181 | int y = 300 + rand() % 200; | 186 | int y = 300 + rand() % 200; |
182 | ((CanvasCard *)*it)->animatedMove( x, y, 50 ); | 187 | ((CanvasCard *)*it)->animatedMove( x, y, 50 ); |
183 | } | 188 | } |
184 | } | 189 | } |
185 | } | 190 | } |
186 | 191 | ||
187 | 192 | ||
188 | void CanvasCardGame::contentsMousePressEvent(QMouseEvent *e) { | 193 | void CanvasCardGame::contentsMousePressEvent(QMouseEvent *e) { |
189 | 194 | ||
190 | if ( moving ) | 195 | if ( moving ) |
191 | return; | 196 | return; |
192 | 197 | ||
193 | QCanvasItemList l = canvas()->collisions( e->pos() ); | 198 | QCanvasItemList l = canvas()->collisions( e->pos() ); |
194 | 199 | ||
195 | for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) { | 200 | for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it) { |
196 | 201 | ||
197 | if ( (*it)->rtti() == canvasCardId ) { | 202 | if ( (*it)->rtti() == canvasCardId ) { |
198 | 203 | ||
199 | moving = (CanvasCard *)*it; | 204 | moving = (CanvasCard *)*it; |
200 | 205 | ||
201 | if ( moving->animated() ) | 206 | if ( moving->animated() ) |
202 | return; | 207 | return; |
203 | 208 | ||
204 | cardXOff = (int)(e->pos().x() - moving->x()); | 209 | cardXOff = (int)(e->pos().x() - moving->x()); |
205 | cardYOff = (int)(e->pos().y() - moving->y()); | 210 | cardYOff = (int)(e->pos().y() - moving->y()); |
206 | 211 | ||
207 | if ( !mousePressCard( moving, e->pos() ) ) { | 212 | if ( !mousePressCard( moving, e->pos() ) ) { |
208 | CanvasCard *card = moving; | 213 | CanvasCard *card = moving; |
209 | 214 | ||
210 | if ( alphaCardPile ) | 215 | if ( alphaCardPile ) |
211 | delete alphaCardPile; | 216 | delete alphaCardPile; |
212 | 217 | ||
213 | alphaCardPile = new CanvasCardPile( this, canvas() ); | 218 | alphaCardPile = new CanvasCardPile( this, canvas() ); |
214 | alphaCardPile->move( card->x(), card->y() ); | 219 | alphaCardPile->move( card->x(), card->y() ); |
215 | alphaCardPile->savePos(); | 220 | alphaCardPile->savePos(); |
216 | alphaCardPile->show(); | 221 | alphaCardPile->show(); |
217 | 222 | ||
218 | while (card) { | 223 | while (card) { |
219 | alphaCardPile->addCard( card ); | 224 | alphaCardPile->addCard( card ); |
220 | card->hide(); | 225 | card->hide(); |
221 | card = (CanvasCard *)card->getCardPile()->cardInfront(card); | 226 | card = (CanvasCard *)card->getCardPile()->cardInfront(card); |
222 | } | 227 | } |
223 | 228 | ||
224 | alphaCardPile->setZ( INT_MAX ); | 229 | alphaCardPile->setZ( INT_MAX ); |
225 | 230 | ||
226 | moved = TRUE; | 231 | moved = TRUE; |
227 | } else { | 232 | } else { |
228 | if ( alphaCardPile ) | 233 | if ( alphaCardPile ) |
229 | alphaCardPile->hide(); | 234 | alphaCardPile->hide(); |
230 | } | 235 | } |
231 | return; | 236 | return; |
232 | } | 237 | } |