1
/*******************************************************************
3
* Copyright 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
5
* This file is part of the KDE project "KAtomic"
7
* KAtomic 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
* KAtomic 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 KAtomic; 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
********************************************************************/
25
#include <QGraphicsScene>
30
#define MIN_ELEM_SIZE 30
36
class MoleculePreviewItem;
41
* KAtomic level playfield
43
class PlayField : public QGraphicsScene
47
enum Direction { Up=0, Down, Left, Right };
51
explicit PlayField( QObject *parent );
57
* Resizes playfield to width,height
59
void resize( int width, int height );
61
* Loads level from config file
63
void loadLevel(const KConfigGroup& config);
65
* Sets animation speed (0-slow, 1-normal, 2-fast)
67
void setAnimationSpeed(int speed);
69
* Animates currently selected atom movement in direction dir
70
* @param numCells used on undos/redos
72
void moveSelectedAtom( Direction dir, int numCells=0 );
74
* Saves the current game to config object
76
void saveGame(KConfigGroup& config) const;
78
* Loads game from config object
80
void loadGame(const KConfigGroup& config);
82
* Returns whether level is finished already
84
bool isLevelFinished() const { return m_levelFinished; }
86
* Displays a passive popup message at the bottom of the scene
88
void showMessage( const QString& message );
90
* Name of the current molecule
92
QString moleculeName();
100
* Selects previous atom
104
* Undoes one movement
108
* Redoes one movement
112
* Undoes all movements
116
* Redoes all movements
120
void gameOver(int numMoves);
121
void updateMoves(int);
122
void enableUndo(bool);
123
void enableRedo(bool);
125
void atomAnimFrameChanged(int frame);
127
virtual void drawBackground( QPainter*, const QRectF& );
128
virtual void drawForeground( QPainter*, const QRectF& );
129
virtual void mousePressEvent( QGraphicsSceneMouseEvent* ev );
132
* Checks if molecule is finished
134
bool checkDone() const;
136
* Re-renders atoms&arrows Pixmaps, updates their positions
138
void updateFieldItems();
140
* Updates arrows around selected atom
142
void updateArrows(bool justHide=false);
144
* Returns true if Field cell (x,y) is empty, i.e. it isn't a wall and has no atom
146
bool cellIsEmpty(int x, int y) const;
148
* Returns true if atom animation is running
150
bool isAnimating() const;
152
inline int toPixX( int fieldX ) const { return fieldX*m_elemSize; }
153
inline int toPixY( int fieldY ) const { return fieldY*m_elemSize; }
154
inline int toFieldX( int pixX ) const { return pixX/m_elemSize; }
155
inline int toFieldY( int pixY ) const { return pixY/m_elemSize; }
156
inline int fieldCenterX() const { return toPixX(0) + m_elemSize*FIELD_SIZE/2; }
157
inline int fieldCenterY() const { return toPixY(0) + m_elemSize*FIELD_SIZE/2; }
160
* Molecule to be done
164
* Number of moves made for current level
169
* True means there's a wall, false means no wall
171
bool m_field[FIELD_SIZE][FIELD_SIZE];
173
* Element (i.e. atom, wall, arrow) size
177
* List of atom QGraphicsItems
179
QList<AtomFieldItem*> m_atoms;
183
ArrowFieldItem *m_upArrow, *m_leftArrow, *m_downArrow, *m_rightArrow;
185
* Item used to show messages to user
187
KGamePopupItem *m_messageItem;
189
* Index of currently selected atom
193
* Direction in which current atom animation moves
197
* Animation speed. Atom will move at 1cell in m_animSpeed msec speed
201
* Timeline object to control atom movement animation
203
QTimeLine *m_atomTimeLine;
205
* True if current level is finished and thus all player input should be disabled
207
bool m_levelFinished;
211
int atomIdx; // atom index in m_atoms
214
AtomMove( int idx=-1, Direction d=Up, int nc=0 )
215
: atomIdx(idx), dir(d), numCells(nc) { }
217
QStack<AtomMove> m_undoStack;
218
QStack<AtomMove> m_redoStack;
220
MoleculePreviewItem *m_previewItem;