scene.h 2.86 KB
Newer Older
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
/*******************************************************************
 *
 * Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
 *
 * This file is part of the KDE project "KLines"
 *
 * KLines 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.
 *
 * KLines 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 KAtomic; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 ********************************************************************/
#ifndef KL_SCENE_H
#define KL_SCENE_H

#include <QGraphicsScene>
#include <QGraphicsView>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
28
29
#include <KRandomSequence>

30
31
32
33
34
#include "commondefs.h"

static const int FIELD_SIZE=9;

class KLinesRenderer;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
35
class KLinesAnimator;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
36
class BallItem;
37
38
39

class KLinesScene : public QGraphicsScene
{
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
40
    Q_OBJECT
41
42
43
44
45
public:
    KLinesScene( QObject *parent );
    ~KLinesScene();

    void resizeScene( int width, int height );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
46
47
48
49
50
    /**
     *  Brings in next three balls to scene
     */
    void nextThreeBalls();
    /**
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
51
52
     *  Returns ballitem in field position pos or 0 if there
     *  is no item there
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
53
     */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
54
    BallItem* ballAt( const FieldPos& pos ) { return m_field[pos.x][pos.y]; }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
55
56
57
    /**
     *  Field coords to pixel coords
     */
58
59
    inline QPointF fieldToPix(const FieldPos& fpos) const {
        return QPointF( fpos.x*32+2, fpos.y*32+2 );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
60
    }
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
61
62
63
    /**
     *  Pixel coords to field coords
     */
64
65
66
    inline FieldPos pixToField( const QPointF& p ) const { 
        return FieldPos(static_cast<int>(p.x()/32), static_cast<int>(p.y()/32)); }

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
67
68
69
70
71
72
73
74
private slots:
    void moveAnimFinished();
private:
    /**
     *  Creates a ball of random color and places it in random free cell
     */
    void placeRandomBall();

75
    virtual void drawBackground( QPainter*, const QRectF& );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
76
    virtual void mousePressEvent( QGraphicsSceneMouseEvent* );
77

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
78
79
80
81
82
83
    /**
     *  This array represents the play field.
     *  Each cell holds the pointer to BallItem
     *  or 0 if there's no ball in that cell
     */
    BallItem* m_field[FIELD_SIZE][FIELD_SIZE];
84
    KLinesRenderer* m_renderer;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
85
    KLinesAnimator* m_animator;
86

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
87
    KRandomSequence m_randomSeq;
88

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
89
    /**
90
     *  Position of selected ball (-1,-1) if none
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
91
     */
92
    FieldPos m_selPos;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
93
94
95
96
    /**
     *  Number of balls currently in field
     */
    int m_numBalls;
97
98
99
100
101
102
103
104
105
106
107
};

class KLinesView : public QGraphicsView
{
public:
    KLinesView( KLinesScene* scene, QWidget *parent );
private:
    void resizeEvent(QResizeEvent *);
};

#endif