summaryrefslogtreecommitdiffstats
path: root/libksirtet/base/piece.h
blob: 4c0486a85b852948d0bf56184057c40c159b2705 (plain)
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
#ifndef BASE_PIECE_H
#define BASE_PIECE_H

#include <qcanvas.h>
#include <qptrvector.h>

#include <kgrid2d.h>


class KRandomSequence;

//-----------------------------------------------------------------------------
class GPieceInfo
{
 public:
    GPieceInfo();
    virtual ~GPieceInfo() {}

    virtual uint nbBlocks() const = 0; // nb of blocks in a piece
    virtual uint nbTypes() const  = 0; // nb of combin. of types in piece
    virtual uint nbForms() const  = 0; // nb of geometrical form of piece

    virtual const int *i(uint form, uint rotation) const = 0;
    virtual const int *j(uint form, uint rotation) const = 0;
    virtual uint value(uint type, uint n) const          = 0;
    virtual uint form(uint type) const                   = 0;
    virtual uint nbConfigurations(uint type) const       = 0;
    uint generateType(KRandomSequence *) const;

    KGrid2D::Coord maxSize() const;

    QPixmap *pixmap(uint blockSize, uint blockType, uint blockMode,
                    bool lighted) const;

    virtual uint nbNormalBlockTypes() const  = 0;
    virtual uint nbGarbageBlockTypes() const = 0;
    virtual uint nbBlockModes() const        = 0; // nb of modes per block
    bool isGarbage(uint type) const { return type>=nbNormalBlockTypes(); }
    uint generateGarbageBlockType(KRandomSequence *) const;

    virtual uint nbColors() const = 0;
    virtual QString colorLabel(uint i) const = 0;
    QCString colorKey(uint i) const;
    virtual QColor defaultColor(uint i) const = 0;
    void loadColors();

 protected:
    QColor color(uint i) const { return _colors[i]; }

    virtual void draw(QPixmap *, uint blockType, uint blockMode,
                      bool lighted) const = 0;
    virtual void setMask(QPixmap *, uint /*blockMode*/) const {}

 private:
    QValueVector<QColor> _colors;
};

class SequenceArray : public QMemArray<QCanvasPixmapArray *>
{
 public:
	SequenceArray();
	~SequenceArray();

	void setBlockSize(uint size);
    uint blockSize() const { return _size; }

 private:
	uint _size;
};

//-----------------------------------------------------------------------------
class BlockInfo : public QCanvas
{
 public:
    BlockInfo(const SequenceArray &);
    const SequenceArray &sequences() const { return _sequences; }

    QPoint toPoint(const KGrid2D::Coord &) const;

 private:
    const SequenceArray &_sequences;
};

//-----------------------------------------------------------------------------
class Block
{
 public:
    Block(uint value = 0);
    ~Block();

    void setValue(uint, BlockInfo *);
    uint value() const { return _value; }
    bool isGarbage() const;
    void toggleLight();
    QCanvasSprite *sprite() const { return _sprite; }

 private:
    uint           _value;
    QCanvasSprite *_sprite;

    Block(const Block &);             // disabled
    Block &operator =(const Block &); // disabled
};

//-----------------------------------------------------------------------------
class Piece
{
 public:
    Piece();

    void copy(const Piece *);
    void setBlockInfo(BlockInfo *bi)         { _binfo = bi; }
    static void setPieceInfo(GPieceInfo *pi) { _info = pi; }
    static GPieceInfo &info()                { return *_info; }

    uint type() const             { return _type; }
    uint nbBlocks() const         { return _blocks.size(); }
    uint nbConfigurations() const { return _info->nbConfigurations(_type); }

    int value(uint k) const       { return _blocks[k]->value(); }
    KGrid2D::Coord pos(uint k, const KGrid2D::Coord &) const;

    KGrid2D::Coord min() const;
    KGrid2D::Coord max() const;
    KGrid2D::Coord size() const { return max() - min() + KGrid2D::Coord(1, 1); }

    void generateNext(int type = -1);
    void rotate(bool left, const QPoint &);
    void move(const QPoint &);
    void moveCenter();
    void show(bool show);

    void setRandomSequence(KRandomSequence *random) { _random = random; }

    Block *garbageBlock() const;
    Block *takeBlock(uint k);

 private:
    QPtrVector<Block>  _blocks;
    uint               _type;
    KRandomSequence   *_random;
    static GPieceInfo *_info;
    BlockInfo         *_binfo;
    uint               _rotation;
    int const         *_i;
    int const         *_j;

    Piece(const Piece &);             // disabled
    Piece &operator =(const Piece &); // disabled

    KGrid2D::Coord coord(uint k) const { return KGrid2D::Coord(_i[k], _j[k]); }
    void moveBlock(uint k, const QPoint &);
};

#endif