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

« back to all changes in this revision

Viewing changes to katomic/playfield.h

  • Committer: Bazaar Package Importer
  • Author(s): Sune Vuorela
  • Date: 2009-04-06 01:33:00 UTC
  • mfrom: (1.1.3 upstream)
  • mto: (2.2.2 squeeze)
  • mto: This revision was merged to the branch mainline in revision 51.
  • Revision ID: james.westby@ubuntu.com-20090406013300-5d62yspscjsv5zd6
Tags: 4:4.2.2-1
* New upstream release:
  - Bugfixes in kpat. (Closes: #376420, #444050, #291007, #422437, #448641)
  - kmines, it is not longer possible cheat the timer. (Closes: #409310)
  - knetwalk, game type is shown after restart. (Closes: #417837)
* Bump build-deps.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
 *
 
3
 * Copyright 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
 
4
 *
 
5
 * This file is part of the KDE project "KAtomic"
 
6
 *
 
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)
 
10
 * any later version.
 
11
 *
 
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.
 
16
 *
 
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.
 
21
 *
 
22
 ********************************************************************/
 
23
#ifndef PLAYFIELD_H
 
24
#define PLAYFIELD_H
 
25
#include <QGraphicsScene>
 
26
#include <QList>
 
27
#include <QStack>
 
28
 
 
29
#define FIELD_SIZE 15
 
30
#define MIN_ELEM_SIZE 30
 
31
 
 
32
class KConfigGroup;
 
33
class Molecule;
 
34
class AtomFieldItem;
 
35
class ArrowFieldItem;
 
36
class MoleculePreviewItem;
 
37
class QTimeLine;
 
38
class KGamePopupItem;
 
39
 
 
40
/**
 
41
 *  KAtomic level playfield
 
42
 */
 
43
class PlayField : public QGraphicsScene
 
44
{
 
45
    Q_OBJECT
 
46
public:
 
47
    enum Direction { Up=0, Down, Left, Right };
 
48
    /**
 
49
     *  Constructor
 
50
     */
 
51
    explicit PlayField( QObject *parent );
 
52
    /**
 
53
     *  Destructor
 
54
     */
 
55
    virtual ~PlayField();
 
56
    /**
 
57
     *  Resizes playfield to width,height
 
58
     */
 
59
    void resize( int width, int height );
 
60
    /**
 
61
     *  Loads level from config file
 
62
     */
 
63
    void loadLevel(const KConfigGroup& config);
 
64
    /**
 
65
     *  Sets animation speed (0-slow, 1-normal, 2-fast)
 
66
     */
 
67
    void setAnimationSpeed(int speed);
 
68
    /**
 
69
     *  Animates currently selected atom movement in direction dir
 
70
     *  @param numCells used on undos/redos
 
71
     */
 
72
    void moveSelectedAtom( Direction dir, int numCells=0 );
 
73
    /**
 
74
     *  Saves the current game to config object
 
75
     */
 
76
    void saveGame(KConfigGroup& config) const;
 
77
    /**
 
78
     *  Loads game from config object
 
79
     */
 
80
    void loadGame(const KConfigGroup& config);
 
81
    /**
 
82
     *  Returns whether level is finished already
 
83
     */
 
84
    bool isLevelFinished() const { return m_levelFinished; }
 
85
    /**
 
86
     * Displays a passive popup message at the bottom of the scene
 
87
     */
 
88
    void showMessage( const QString& message );
 
89
    /**
 
90
     * Name of the current molecule
 
91
     */
 
92
    QString moleculeName();
 
93
 
 
94
public slots:
 
95
    /**
 
96
     *  Selects next atom
 
97
     */
 
98
    void nextAtom();
 
99
    /**
 
100
     *  Selects previous atom
 
101
     */
 
102
    void previousAtom();
 
103
    /**
 
104
     *  Undoes one movement
 
105
     */
 
106
    void undo();
 
107
    /**
 
108
     *  Redoes one movement
 
109
     */
 
110
    void redo();
 
111
      /**
 
112
     *  Undoes all movements
 
113
     */
 
114
    void undoAll();
 
115
    /**
 
116
     *  Redoes all movements
 
117
     */
 
118
    void redoAll();
 
119
signals:
 
120
    void gameOver(int numMoves);
 
121
    void updateMoves(int);
 
122
    void enableUndo(bool);
 
123
    void enableRedo(bool);
 
124
private slots:
 
125
    void atomAnimFrameChanged(int frame);
 
126
private:
 
127
    virtual void drawBackground( QPainter*, const QRectF& );
 
128
    virtual void drawForeground( QPainter*, const QRectF& );
 
129
    virtual void mousePressEvent( QGraphicsSceneMouseEvent* ev );
 
130
 
 
131
    /**
 
132
     *  Checks if molecule is finished
 
133
     */
 
134
    bool checkDone() const;
 
135
    /**
 
136
     *  Re-renders atoms&arrows Pixmaps, updates their positions
 
137
     */
 
138
    void updateFieldItems();
 
139
    /**
 
140
     *  Updates arrows around selected atom
 
141
     */
 
142
    void updateArrows(bool justHide=false);
 
143
    /**
 
144
     *  Returns true if Field cell (x,y) is empty, i.e. it isn't a wall and has no atom
 
145
     */
 
146
    bool cellIsEmpty(int x, int y) const;
 
147
    /**
 
148
     *  Returns true if atom animation is running
 
149
     */
 
150
    bool isAnimating() const;
 
151
 
 
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; }
 
158
 
 
159
    /**
 
160
     *  Molecule to be done
 
161
     */
 
162
    Molecule *m_mol;
 
163
    /**
 
164
     *  Number of moves made for current level
 
165
     */
 
166
    int m_numMoves;
 
167
    /**
 
168
     *  Represents level.
 
169
     *  True means there's a wall, false means no wall
 
170
     */
 
171
    bool m_field[FIELD_SIZE][FIELD_SIZE];
 
172
    /**
 
173
     *  Element (i.e. atom, wall, arrow) size
 
174
     */
 
175
    int m_elemSize;
 
176
    /**
 
177
     *  List of atom QGraphicsItems
 
178
     */
 
179
    QList<AtomFieldItem*> m_atoms;
 
180
    /**
 
181
     *  Arrow items
 
182
     */
 
183
    ArrowFieldItem *m_upArrow, *m_leftArrow, *m_downArrow, *m_rightArrow;
 
184
    /**
 
185
     * Item used to show messages to user
 
186
     */
 
187
    KGamePopupItem *m_messageItem;
 
188
    /**
 
189
     *  Index of currently selected atom
 
190
     */
 
191
    int m_selIdx;
 
192
    /**
 
193
     *  Direction in which current atom animation moves
 
194
     */
 
195
    Direction m_dir;
 
196
    /**
 
197
     *  Animation speed. Atom will move at 1cell in m_animSpeed msec speed
 
198
     */
 
199
    int m_animSpeed;
 
200
    /**
 
201
     *  Timeline object to control atom movement animation
 
202
     */
 
203
    QTimeLine *m_atomTimeLine;
 
204
    /**
 
205
     *  True if current level is finished and thus all player input should be disabled
 
206
     */
 
207
    bool m_levelFinished;
 
208
 
 
209
    struct AtomMove
 
210
    {
 
211
        int atomIdx; // atom index in m_atoms
 
212
        Direction dir;
 
213
        int numCells;
 
214
        AtomMove( int idx=-1, Direction d=Up, int nc=0 )
 
215
            : atomIdx(idx), dir(d), numCells(nc) { }
 
216
    };
 
217
    QStack<AtomMove> m_undoStack;
 
218
    QStack<AtomMove> m_redoStack;
 
219
 
 
220
    MoleculePreviewItem *m_previewItem;
 
221
};
 
222
 
 
223
#endif