1 /*******************************************************************
3 * Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
5 * This file is part of the KDE project "KReversi"
7 * KReversi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * KReversi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with KReversi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
22 ********************************************************************/
23 #ifndef KREVERSI_SCENE_H
24 #define KREVERSI_SCENE_H
26 #include <QGraphicsScene>
29 #include "commondefs.h"
32 class KReversiChipFrameSet
;
39 * This class provides graphical representation of KReversiGame
40 * using QGraphicsScene for graphics display.
41 * It displays the reversi board in its current state,
42 * receives a mouse events, translates them so KReversiGame can understand,
44 * receives board-changed notifications, nicely animates them.
45 * It also drives the gameflow, i.e. it tells KReversiGame when to make
48 class KReversiScene
: public QGraphicsScene
53 * @param chipsPath path to the svg pixmap with animation frames
55 KReversiScene( KReversiGame
* game
, const QString
& chipsPath
);
59 * Sets the game object which this scene will visualize/use.
60 * KReversiScene takes ownership of this object and will delete it when appropriate
62 void setGame( KReversiGame
* game
);
64 * Sets the chips pixmap to be one found in chipsPrefix
65 * @see KReversiChipFrameSet
67 void setChipsPrefix( const QString
& chipsPrefix
);
69 * Sets whether to show board labels.
71 * QGraphicsView::resetCachedContent();
72 * QGraphicsView::update()
73 * for this to take effect
75 void setShowBoardLabels( bool show
);
77 * Performs scene resize - rescales all corresponding svg's,
78 * recalcs playfield rect etc etc
80 void resizeScene( int width
, int height
);
82 * This function will tell you if the scene is currently performing
83 * some kind of "better-don't-interrupt-me" operation.
85 * For example this call is used in KReversiMainWindow to decide
86 * whether it is feasible to perform undo action (which makes a little sence
87 * during, for example animation of computer move)
91 * @return whether the scene is in demo-mode
93 bool isDemoModeToggled() const { return m_demoMode
; }
95 * Sets the animation speed (0 - slow, 1 - normal, 2 - fast)
97 void setAnimationSpeed(int speed
);
100 * Synchronizes graphical board with m_game's board
104 * This will make scene visually mark the last made move
106 void setShowLastMove( bool show
);
108 * This will make scene visually mark squares with possible moves
110 void setShowLegalMoves( bool show
);
112 * Shows hint for player
117 * In this mode KReversiScene would not wait for user
118 * clicks to produce his turn. It will let computer
122 void toggleDemoMode(bool toggle
);
124 * @return whether game is in demo mode
126 bool isInDemoMode() const { return m_demoMode
; }
128 void slotGameMoveFinished();
129 void slotAnimationStep();
131 void slotComputerCantMove();
132 void slotPlayerCantMove();
135 * emitted when Scene finishes displaying last move
140 * Draws a background with 8x8 cell matrix.
141 * Reimplemented from QGraphicsScene.
143 virtual void drawBackground( QPainter
*p
, const QRectF
& rect
);
145 * Mouse presses event handler
147 virtual void mousePressEvent( QGraphicsSceneMouseEvent
* );
149 * Makes pending new game object a current one.
150 * @see m_pendingNewGame @see setGame
152 void setNewGameObject( KReversiGame
* game
);
154 * Visually displays last move and possible moves
155 * (if the scene is set up to show them)
157 void displayLastAndPossibleMoves();
159 * If scene is showing hint animation this function will stop it
161 void stopHintAnimation();
163 * Returns the center point of cell (row,col)
165 QPointF
cellCenter( int row
, int col
) const;
167 * Returns the top-left point of cell (row,col)
169 QPointF
cellTopLeft( int row
, int col
) const;
171 * Position of the board within the scene
177 KReversiGame
*m_game
;
179 * This pointer is meant for temporal storage of new game object being set.
180 * New game object is *actually* set after animation slot gets called by timer
182 KReversiGame
*m_pendingNewGame
;
184 * This will hold pixmap which is rendered by m_possMovesRenderer.
185 * It will be rerendered on resizes
187 QPixmap m_possMovePix
;
189 * Animation frameset for chips
191 KReversiChipFrameSet
*m_frameSet
;
193 * Current size of chip
197 * This list will hold a changed chips
198 * after each turn. It is received from the game object.
199 * Used to animate corresponding chips.
201 PosList m_changedChips
;
207 * This is our "hint-chip" - used to show hints
209 KReversiChip
* m_hintChip
;
211 * This will hold a pointer to chip which has "last-move" mark
213 KReversiChip
* m_lastMoveChip
;
215 * Delay between animation frames
219 * Holds true if the scene is showing hint to the player
223 * Specifies whether computer should play human moves
227 * If true, then last made turn will be shown to the player
231 * If true, then all possible moves will be shown to the player
233 bool m_showPossibleMoves
;
235 * If true board labels will be rendered
239 * List of items which are used to show possible moves.
240 * This list behaves like this:
241 * it is created only once and grows only as needed.
242 * If number of items in it is sufficient to display all possible moves,
243 * than these items are reused, if not then it grows with new items until
244 * there are enough of them.
245 * Example: at start of the game there are 4 items (4 possible move to highlight)
246 * if after the next turn there will be 6 possible moves, this list
247 * will be grown by 2 rect items and previous 4 will be reused (their position
248 * can change though).
250 * (isn't that too much words for this little thingie? ;) )
252 QList
<QGraphicsPixmapItem
*> m_possibleMovesItems
;
254 * Item to show messages to user
256 KGamePopupItem
* m_messageItem
;