1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 1999-2000, Robert Cimrman * |
8 | * cimrman3@students.zcu.cz * |
9 | * * |
10 | * Copyright (c) 2007, Nicolas Roffet * |
11 | * nicolas-kde@roffet.com * |
12 | * * |
13 | * * |
14 | * This program is free software; you can redistribute it and/or modify * |
15 | * it under the terms of the GNU General Public License as published by * |
16 | * the Free Software Foundation; either version 2 of the License, or * |
17 | * (at your option) any later version. * |
18 | * * |
19 | * This program is distributed in the hope that it will be useful, * |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 | * GNU General Public License for more details. * |
23 | * * |
24 | * You should have received a copy of the GNU General Public License * |
25 | * along with this program; if not, write to the * |
26 | * Free Software Foundation, Inc., * |
27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
28 | ***************************************************************************/ |
29 | |
30 | |
31 | |
32 | #ifndef KBBSCALABLEGRAPHICWIDGET_H |
33 | #define KBBSCALABLEGRAPHICWIDGET_H |
34 | |
35 | |
36 | class QAction; |
37 | class QGraphicsScene; |
38 | #include <QGraphicsView> |
39 | class QLCDNumber; |
40 | class QResizeEvent; |
41 | |
42 | |
43 | class ; |
44 | class KPushButton; |
45 | |
46 | |
47 | class KBBBallsOnBoard; |
48 | class KBBGameDoc; |
49 | class KBBGraphicsItem; |
50 | class KBBGraphicsItemBallRepository; |
51 | class KBBGraphicsItemBlackBox; |
52 | class KBBGraphicsItemCursor; |
53 | class KBBGraphicsItemRay; |
54 | class KBBGraphicsItemSet; |
55 | class KBBThemeManager; |
56 | |
57 | |
58 | |
59 | /** |
60 | * @brief Scalable graphic central widget for KBlackBox |
61 | */ |
62 | class KBBScalableGraphicWidget : public QGraphicsView |
63 | { |
64 | Q_OBJECT |
65 | |
66 | public: |
67 | /** |
68 | * @brief Distance between the black box and the widget border |
69 | * |
70 | * Note: The widget is scalable, so it's just an arbitrary default compared with other ranges. |
71 | * It's the minimal distance as the width/height ratio is constant and the widget may have another width/height ratio. |
72 | * @see RATIO |
73 | */ |
74 | static int const BORDER_SIZE = 50; |
75 | |
76 | /** |
77 | * @brief Width and height of a single square on the black box |
78 | * |
79 | * Note: The widget is scalable, so it's just an arbitrary default compared with other ranges. |
80 | */ |
81 | static int const RATIO = 25; |
82 | |
83 | /** |
84 | * @brief Every graphic items |
85 | * |
86 | * Values are used to define the relative heigths between the displayed graphic items. |
87 | * @see KBBThemeManager::zValue(const KBBScalableGraphicWidget::itemType itemType); |
88 | */ |
89 | enum itemType { |
90 | background=0, |
91 | informationBackground=1, |
92 | blackbox=2, |
93 | blackboxGrid=3, |
94 | tutorialMarker=4, |
95 | markerNothing=5, |
96 | solutionRay=6, |
97 | playerRay=7, |
98 | resultBackground=8, |
99 | resultBackgroundHighlight=9, |
100 | resultReflection=10, |
101 | resultHit=11, |
102 | resultText=12, |
103 | solutionBall=13, |
104 | playerBall=14, |
105 | unsureBall=15, |
106 | wrongPlayerBall=16, |
107 | rightPlayerBall=17, |
108 | interactionInfoDeflection=18, |
109 | interactionInfoHit=19, |
110 | interactionInfoNothing=20, |
111 | interactionInfoReflection=21, |
112 | interactionInfoReflectionSym=22, |
113 | laser0=23, |
114 | laser90=24, |
115 | laser180=25, |
116 | laser270=26, |
117 | cursor=27 |
118 | }; |
119 | |
120 | |
121 | /** |
122 | * @brief Constructor |
123 | */ |
124 | explicit KBBScalableGraphicWidget(KBBGameDoc* gameDoc, KBBThemeManager* themeManager, QAction* done); |
125 | ~KBBScalableGraphicWidget(); |
126 | |
127 | |
128 | void addBall(int boxPosition); |
129 | void addBall(int boxPosition, int outsidePosition); |
130 | void addBallUnsure(const int boxPosition); |
131 | void addMarkerNothing(const int boxPosition); |
132 | void drawRay(const int borderPosition); |
133 | void mouseBorderClick(const int borderPosition); |
134 | void mouseBoxClick(const Qt::MouseButton button, int boxPosition); |
135 | int moveBall(const int boxPositionFrom, const int boxPositionTo); |
136 | int moveMarkerNothing(const int boxPositionFrom, const int boxPositionTo); |
137 | void newGame(int columns, int rows, int ballNumber); |
138 | |
139 | /** |
140 | * @brief Message to display |
141 | * |
142 | * @param text Message. Attention: Message should not be too wide. |
143 | * @param time Time (in ms) the message remains displayed. 0 = forever. |
144 | */ |
145 | void (const QString& text, int time = 5000); |
146 | |
147 | int positionAfterMovingBall(const int boxPositionFrom, const int boxPositionTo) const; |
148 | |
149 | void setPause(bool state); |
150 | void removeAllBalls(); |
151 | void removeBall(const int boxPosition); |
152 | void removeRay(); |
153 | void resizeEvent(QResizeEvent*); |
154 | QGraphicsScene* scene(); |
155 | void setScore(int score); |
156 | |
157 | /** |
158 | * @brief display the solution |
159 | * |
160 | * Used at the end of the game and for the sandbox mode. |
161 | * @param continueGame Sould the game continue after displaying the solution? (Yes for sandbox mode, no for normal game end). |
162 | */ |
163 | void solve(const bool continueGame); |
164 | |
165 | |
166 | public slots: |
167 | void cursorAtNewPosition(int borderPosition); |
168 | void keyboardEnter(); |
169 | void keyboardMoveDown(); |
170 | void keyboardMoveLeft(); |
171 | void keyboardMoveRight(); |
172 | void keyboardMoveUp(); |
173 | void keyboardSpace(); |
174 | |
175 | |
176 | protected: |
177 | void drawBackground(QPainter* painter, const QRectF&); |
178 | |
179 | |
180 | private: |
181 | /** |
182 | * @brief Minimum width and height |
183 | * |
184 | * Minimum width and minimum height of the widget. The widget needs a minimal site: it is ugly if it is too small. |
185 | */ |
186 | static int const MINIMUM_SIZE = 250; |
187 | |
188 | static int const OFFSET_DONE_BUTTON = 12; |
189 | |
190 | void fillBallsOutside(); |
191 | void removeMarkerNothing(const int boxPosition); |
192 | void setBallUnsure(const int boxPosition, const bool unsure); |
193 | void setInputAccepted(bool inputAccepted); |
194 | void switchBall(); |
195 | void switchMarker(); |
196 | void updateDoneButton(); |
197 | void useLaser(const int incomingPosition); |
198 | |
199 | |
200 | // Graphics items |
201 | KBBGraphicsItemBlackBox* m_blackbox; |
202 | KBBGraphicsItemSet* m_balls; |
203 | KBBGraphicsItemSet* m_ballsSolution; |
204 | KBBGraphicsItemSet* m_ballsUnsure; |
205 | KBBGraphicsItemCursor* m_cursor; |
206 | KBBGraphicsItemBallRepository* m_ballRepository; |
207 | KBBGraphicsItemSet* m_lasers; |
208 | KBBGraphicsItemSet* m_markersNothing; |
209 | KBBGraphicsItemSet* m_rayResults; |
210 | KBBGraphicsItemRay* m_playerRay; |
211 | KBBGraphicsItemRay* m_solutionRay; |
212 | |
213 | |
214 | /** |
215 | * @brief Position and size of the background in scene coordinates |
216 | * |
217 | * Used for drwing the background. |
218 | * @see drawBackground(QPainter* painter, const QRectF&) |
219 | */ |
220 | QRectF m_rectBackground; |
221 | |
222 | // Various member variables |
223 | int m_ballNumber; |
224 | KBBBallsOnBoard* m_boardBalls; |
225 | KBBBallsOnBoard* m_boardBallsPlaced; |
226 | int m_columns; |
227 | QAction* m_doneAction; |
228 | KPushButton* m_doneButton; |
229 | KBBGameDoc* m_gameDoc; |
230 | KGamePopupItem* m_infoScore; |
231 | bool m_inputAccepted; |
232 | bool m_pause; |
233 | int m_rayNumber; |
234 | |
235 | int m_rows; |
236 | QGraphicsScene* m_scene; //TODO: Remove it because scene() already gives it back. |
237 | QLCDNumber* m_score; |
238 | KBBThemeManager* m_themeManager; |
239 | }; |
240 | |
241 | #endif // KBBSCALABLEGRAPHICWIDGET_H |
242 | |