9009857e2151e050a9cd14ea375e99fcf31bc520
[colorflood] / colorflood / src / field.hpp
1 /*
2   Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; version 2 of the License.
7
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12 */
13
14 #ifndef _FIELD_HPP
15 #define _FIELD_HPP
16
17 #include <QWidget>
18 #include <QVector>
19 #include <QBrush>
20
21 class QPaintEvent;
22 class QMouseEvent;
23
24 class Field : public QWidget
25 {
26     Q_OBJECT;
27
28 public:
29     typedef enum
30     {
31         SIZE_SMALL = 0,
32         SIZE_NORMAL,
33         SIZE_LARGE,
34         NUM_SIZES
35     }FieldSize;
36
37     typedef struct
38     {
39         quint8 brush;
40         bool   flood;
41     }FieldRect;
42
43     typedef QVector<Field::FieldRect> RectVector;
44
45     Field (QWidget *parent, int *turns);
46     ~Field ();
47
48     FieldSize getSize () const;
49
50     static int getNumRectsOfSize (FieldSize size);
51     static int getNumTurnsOfSize (FieldSize size);
52
53 private:
54     static const int numRects[NUM_SIZES];
55     static const int numTurns[NUM_SIZES];
56
57     void randomize ();
58     static int getRectSize (FieldSize size);
59     void tryFloodRecurse (quint8 brush, int x, int y);
60     void floodNeighbours (quint8 brush, int x, int y);
61
62     FieldSize  size;
63     RectVector data;
64     int        turns;
65     bool       finished;
66
67 protected:
68     void mousePressEvent (QMouseEvent *event);
69     void paintEvent (QPaintEvent *event);
70
71 signals:
72     void turnsChanged (int turns);
73
74 public slots:
75     void flood (int colorIndex);
76 };
77
78 #endif // !_FIELD_HPP