1/* Copyright (C) 1997 Mathias Mueller <in5y158@public.uni-hamburg.de>
2 * Copyright (C) 2006 Mauricio Piacentini <mauricio@tabuleiro.com>
3 *
4 * Kmahjongg is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef BOARDWIDGET_H
19#define BOARDWIDGET_H
20
21
22#include "KmTypes.h"
23#include "kmahjonggtileset.h"
24#include "kmahjonggbackground.h"
25#include "kmahjongglayout.h"
26#include "GameData.h"
27#include "TileCoord.h"
28#include "TileSprite.h"
29
30#include <QHash>
31#include <QPoint>
32
33#include <qevent.h>
34
35#define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
36#include <libkdegamesprivate/kgamecanvas.h>
37
38#include <ktoolbar.h>
39#include <kstatusbar.h>
40#include <kfiledialog.h>
41
42
43#define ANIMSPEED 200
44#define ID_GAME_TIMER 999
45
46
47/**
48 * @short Where all the funn happens
49 *
50 * @see BoardLayout
51 * @author Mathias Mueller */
52class BoardWidget : public KGameCanvasWidget
53{
54 Q_OBJECT
55
56public:
57 /**
58 * Class Constructor
59 *
60 * @param *parent blah blah */
61 explicit BoardWidget(QWidget *parent = 0);
62
63 /**
64 * Class Destructor */
65 ~BoardWidget();
66
67 /**
68 * Method Description */
69 void calculateNewGame(int num = -1);
70
71 /**
72 * Method Description
73 *
74 * @return int blah blah
75 * @ref redoMove() */
76 int undoMove();
77
78 /**
79 * Method Description */
80 void redoMove();
81
82 /**
83 * Method Description */
84 void startDemoMode();
85
86 /**
87 * Method Description */
88 void stopDemoMode();
89
90 /**
91 * Method Description */
92 void pause();
93
94 /**
95 * Method Description */
96 void gameLoaded();
97
98 /**
99 * Method Description */
100 void animateMoveList();
101
102 /**
103 * Method Description
104 *
105 * @param show */
106 void setShowMatch(bool show);
107
108 /**
109 * Method Description
110 *
111 * @return long gameGenerationNum
112 * @see gameGenerationNum */
113 long getGameNum() {return gameGenerationNum;}
114
115 /**
116 * Method Description
117 *
118 * @return QString filename */
119 QString getBoardName() {return theBoardLayout.board()->getFilename();}
120
121 /**
122 * Method Description
123 *
124 * @return QString filename */
125 QString getLayoutName();
126
127 TileViewAngle m_angle;
128 GameData *Game;
129 KMahjonggTileset theTiles;
130 KMahjonggBackground theBackground;
131 KMahjonggLayout theBoardLayout;
132
133 QHash<TileCoord, TileSprite *> spriteMap;
134
135public slots:
136 /**
137 * Slot Description */
138 void loadSettings();
139
140 /**
141 * Slot Description */
142 void saveSettings();
143
144 /**
145 * Slot Description */
146 void resizeTileset(const QSize &wsize);
147
148 /**
149 * Slot Description */
150 void animatingMoveListForward();
151
152 /**
153 * Slot Description */
154 void animatingMoveListBackwards();
155
156 /**
157 * Slot Description */
158 void shuffle();
159
160 /**
161 * Slot Description */
162 void angleSwitchCW();
163
164 /**
165 * Slot Description */
166 void angleSwitchCCW();
167
168 /**
169 * Slot Description */
170 void helpMove();
171
172 /**
173 * Slot Description */
174 void helpMoveTimeout();
175
176 /**
177 * Slot Description */
178 void helpMoveStop();
179
180 /**
181 * Slot Description */
182 void demoMoveTimeout();
183
184 /**
185 * Slot Description */
186 void matchAnimationTimeout();
187
188 /**
189 * Slot Description */
190 void setDisplayedWidth();
191
192 /**
193 * Slot Description
194 *
195 * @return @c true if ...
196 * @return @c false if ... */
197 bool loadTileset ( const QString & );
198
199 /**
200 * Slot Description
201 *
202 * @return @c true if ...
203 * @return @c false if ... */
204 bool loadBoardLayout( const QString& );
205
206 /**
207 * Slot Description
208 *
209 * @return @c true if ...
210 * @return @c false if ... */
211 bool loadBoard();
212
213 /**
214 * Slot Description */
215 void drawBoard(bool showTiles = true);
216
217 /**
218 * Slot Description */
219 void updateSpriteMap();
220
221 /**
222 * Slot Description */
223 void populateSpriteMap();
224
225 /**
226 * Slot Description
227 *
228 * @return @c true if ...
229 * @return @c false if ... */
230 bool loadBackground(const QString&, bool bShowError = true);
231
232signals:
233 /**
234 * Signal Description */
235 void statusTextChanged(const QString&, long);
236
237 /**
238 * Signal Description */
239 void tileNumberChanged(int iMaximum, int iCurrent, int iLeft);
240
241 /**
242 * Signal Description
243 *
244 * @param bActive */
245 void demoModeChanged(bool bActive);
246
247 /**
248 * Signal Description */
249 void gameCalculated();
250
251 /**
252 * Signal Description */
253 void gameOver(unsigned short removed, unsigned short cheats);
254
255protected:
256 /**
257 * Overridden virtual method for wheel events. */
258 virtual void wheelEvent(QWheelEvent *event);
259
260 /**
261 * Override the itemAt method of the KGameCanvasWidget. We need this cause of corrections in the
262 * 3D simulated view with the stones, as the normal KGameCanvasWidget only supports 2D objects,
263 * and the thrown shadows should not be a part of the click event.
264 *
265 * @param point the point we click on the KGameCanvasWidget. */
266 KGameCanvasItem* itemAt(const QPoint &point) const;
267
268 /**
269 * Virtual Method Description */
270 virtual void resizeEvent(QResizeEvent *event);
271
272 /**
273 * Method Description */
274 void getFileOrDefault(const QString &filename, const QString &type, QString &res);
275
276 /**
277 * Method Description */
278 void mousePressEvent(QMouseEvent*);
279
280 /**
281 * Method Description */
282 void setStatusText(const QString&);
283
284 /**
285 * Method Description */
286 void cancelUserSelectedTiles();
287
288 /**
289 * Method Description */
290 void drawTileNumber();
291
292 /**
293 * Method Description */
294 void hilightTile(POSITION&, bool on = true, bool refresh = true);
295
296 /**
297 * Method Description */
298 void putTileInBoard(POSITION&, bool refresh = true);
299
300 /**
301 * Method Description */
302 void removeTile(POSITION&, bool refresh = true);
303
304 /**
305 * Transform window point to board position.
306 *
307 * @param point Input: Point in window coordinates
308 * @param MouseClickPos Output: Position in game board */
309 void transformPointToPosition(const QPoint &point, POSITION &MouseClickPos);
310
311 /**
312 * Method Description */
313 void stopEndAnimation();
314
315 /**
316 * Method Description */
317 void stopMatchAnimation();
318
319 /**
320 * Method Description
321 *
322 * @return int blah blah */
323 int requiredWidth();
324
325 /**
326 * Method Description
327 *
328 * @return int blah blah */
329 int requiredHeight();
330
331 /**
332 * Method Description
333 *
334 * @return int blah blah */
335 int requiredHorizontalCells();
336
337 /**
338 * Method Description
339 *
340 * @return int blah blah */
341 int requiredVerticalCells();
342
343 /**
344 * This method test if any valid moves are available.
345 *
346 * @return False if no valid move available, else true. */
347 bool validMovesAvailable();
348
349 int iTimerStep;
350
351 short matchCount;
352 bool showMatch;
353 bool showHelp;
354 bool gamePaused;
355
356 unsigned short cheatsUsed;
357 long gameGenerationNum;
358
359 POSITION MouseClickPos1;
360 POSITION MouseClickPos2;
361 POSITION TimerPos1;
362 POSITION TimerPos2;
363
364 /**
365 * @short Describe the enum */
366 enum STATES {
367 Stop,
368 Demo,
369 Help,
370 Animation,
371 Match
372 } TimerState;
373
374 QTimer *timer;
375 QTimer *animateForwardTimer;
376 QTimer *animateBackwardsTimer;
377
378private:
379 QString getRandomLayoutName() const;
380};
381
382
383#endif // BOARDWIDGET_H
384