~ubuntu-branches/ubuntu/precise/kdegames/precise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*******************************************************************
 *
 * Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
 * Copyright 2010 Brian Croom <brian.s.croom@gmail.com>
 *
 * This file is part of the KDE project "KReversi"
 *
 * KReversi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * KReversi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KReversi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 ********************************************************************/
#ifndef KREVERSI_SCENE_H
#define KREVERSI_SCENE_H

#include <QGraphicsScene>
#include <QPixmap>
#include <KGameRenderedItem>
#include <KGameRenderer>

#include "commondefs.h"

class KReversiGame;
class KReversiChip;
class KGamePopupItem;
class QPainter;
class QTimer;

/**
 *  This class provides graphical representation of KReversiGame
 *  using QGraphicsScene for graphics display.
 *  It displays the reversi board in its current state,
 *  receives a mouse events, translates them so KReversiGame can understand,
 *  sends them to it,
 *  receives board-changed notifications, nicely animates them.
 *  It also drives the gameflow, i.e. it tells KReversiGame when to make
 *  the next move.
 */
class KReversiScene : public QGraphicsScene
{
    Q_OBJECT
public:
    /**
     * @param chipsPrefix prefix indicating what type of chips to render
     */
    KReversiScene( KReversiGame* game, const QString& chipsPrefix );

    /**
     *  Sets the game object which this scene will visualize/use.
     *  KReversiScene takes ownership of this object and will delete it when appropriate
     */
    void setGame( KReversiGame* game );
    /**
     *  Sets the chips pixmap to be one found in chipsPrefix
     */
    void setChipsPrefix( const QString& chipsPrefix );
    /**
     *  Sets whether to show board labels.
     *  You'll need to call
     *  QGraphicsView::resetCachedContent();
     *  QGraphicsView::update()
     *  for this to take effect
     */
    void setShowBoardLabels( bool show );
    /**
     *  Performs scene resize - rescales all corresponding svg's,
     *  recalcs playfield rect etc etc
     */
    void resizeScene( int width, int height );
    /**
     *  This function will tell you if the scene is currently performing
     *  some kind of "better-don't-interrupt-me" operation.
     *
     *  For example this call is used in KReversiMainWindow to decide
     *  whether it is feasible to perform undo action (which makes a little sence
     *  during, for example animation of computer move)
     */
    bool isBusy() const;
    /**
     *  @return whether the scene is in demo-mode
     */
    bool isDemoModeToggled() const { return m_demoMode; }
    /**
     *  Sets the animation speed (0 - slow, 1 - normal, 2 - fast)
     */
    void setAnimationSpeed(int speed);
public slots:
    /**
     *  Synchronizes graphical board with m_game's board
     */
    void updateBoard();
    /**
     *  This will make scene visually mark the last made move
     */
    void setShowLastMove( bool show );
    /**
     *  This will make scene visually mark squares with possible moves
     */
    void setShowLegalMoves( bool show );
    /**
     *  Shows hint for player
     */
    void slotHint();
    /**
     *  Sets Demo Mode.
     *  In this mode KReversiScene would not wait for user
     *  clicks to produce his turn. It will let computer
     *  play for user
     *  @see m_demoMode
     */
    void toggleDemoMode(bool toggle);
    /**
     *  @return whether game is in demo mode
     */
    bool isInDemoMode() const { return m_demoMode; }
private slots:
    void slotGameMoveFinished();
    void slotAnimationStep();
    void slotGameOver();
    void slotComputerCantMove();
    void slotPlayerCantMove();
signals:
    /**
     *  emitted when Scene finishes displaying last move
     */
    void moveFinished();
private:
    /**
     *  Draws a background with 8x8 cell matrix.
     *  Reimplemented from QGraphicsScene.
     */
    virtual void drawBackground( QPainter *p, const QRectF& rect );
    /**
     *  Mouse presses event handler
     */
    virtual void mousePressEvent( QGraphicsSceneMouseEvent* );
    /**
     *  Makes pending new game object a current one.
     *  @see m_pendingNewGame @see setGame
     */
    void setNewGameObject( KReversiGame* game );
    /**
     *  Visually displays last move and possible moves
     *  (if the scene is set up to show them)
     */
    void displayLastAndPossibleMoves();
    /**
     *  If scene is showing hint animation this function will stop it
     */
    void stopHintAnimation();
    /**
     *  Returns the center point of cell (row,col)
     */
    QPointF cellCenter( int row, int col ) const;
    /**
     *  Returns the top-left point of cell (row,col)
     */
    QPointF cellTopLeft( int row, int col ) const;
    /**
     * Caching SVG sprite renderer
     */
    KGameRenderer m_renderer;
    /**
     * Position of the board within the scene
     */
    QRectF m_boardRect;
    /**
     *  The Game object
     */
    KReversiGame *m_game;
    /**
     *  This pointer is meant for temporal storage of new game object being set.
     *  New game object is *actually* set after animation slot gets called by timer
     */
    KReversiGame *m_pendingNewGame;
    /**
     * The SVG element prefix for the current chip set
     */
    QString m_chipsPrefix;
    /**
     *  Current size of chip
     */
    qreal m_curCellSize;
    /**
     *  This list will hold a changed chips
     *  after each turn. It is received from the game object.
     *  Used to animate corresponding chips.
     */
    PosList m_changedChips;
    /**
     *  Animation timer
     */
    QTimer* m_animTimer;
    /**
     *  This is our "hint-chip" - used to show hints
     */
    KReversiChip* m_hintChip;
    /**
     *  This will hold a pointer to chip which has "last-move" mark
     */
    KReversiChip* m_lastMoveChip;
    /**
     *  Delay between animation frames
     */
    int m_timerDelay;
    /**
     *  Holds true if the scene is showing hint to the player
     */
    bool m_showingHint;
    /**
     *  Specifies whether computer should play human moves
     */
    bool m_demoMode;
    /**
     *  If true, then last made turn will be shown to the player
     */
    bool m_showLastMove;
    /**
     *  If true, then all possible moves will be shown to the player
     */
    bool m_showPossibleMoves;
    /**
     *  If true board labels will be rendered
     */
    bool m_showLabels;
    /**
     *  List of items which are used to show possible moves.
     *  This list behaves like this:
     *  it is created only once and grows only as needed.
     *  If number of items in it is sufficient to display all possible moves,
     *  than these items are reused, if not then it grows with new items until
     *  there are enough of them.
     *  Example: at start of the game there are 4 items (4 possible move to highlight)
     *  if after the next turn there will be 6 possible moves, this list
     *  will be grown by 2 rect items and previous 4 will be reused (their position
     *  can change though).
     *
     *  (isn't that too much words for this little thingie? ;) )
     */
    QList<KGameRenderedItem*> m_possibleMovesItems;
    /**
     * Item to show messages to user
     */
    KGamePopupItem* m_messageItem;
};
#endif