|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "window.h" |
|
45 #include "movementtransition.h" |
|
46 |
|
47 //![0] |
|
48 Window::Window() |
|
49 { |
|
50 pX = 5; |
|
51 pY = 5; |
|
52 //![0] |
|
53 |
|
54 QFontDatabase database; |
|
55 QFont font; |
|
56 if (database.families().contains("Monospace")) |
|
57 font = QFont("Monospace", 12); |
|
58 else { |
|
59 foreach (QString family, database.families()) { |
|
60 if (database.isFixedPitch(family)) { |
|
61 font = QFont(family, 12); |
|
62 break; |
|
63 } |
|
64 } |
|
65 } |
|
66 setFont(font); |
|
67 |
|
68 //![1] |
|
69 setupMap(); |
|
70 buildMachine(); |
|
71 } |
|
72 //![1] |
|
73 |
|
74 void Window::setStatus(const QString &status) |
|
75 { |
|
76 myStatus = status; |
|
77 repaint(); |
|
78 } |
|
79 |
|
80 QString Window::status() const |
|
81 { |
|
82 return myStatus; |
|
83 } |
|
84 |
|
85 void Window::paintEvent(QPaintEvent * /* event */) |
|
86 { |
|
87 QFontMetrics metrics(font()); |
|
88 QPainter painter(this); |
|
89 int fontHeight = metrics.height(); |
|
90 int fontWidth = metrics.width('X'); |
|
91 int yPos = fontHeight; |
|
92 int xPos; |
|
93 |
|
94 painter.fillRect(rect(), Qt::black); |
|
95 painter.setPen(Qt::white); |
|
96 |
|
97 painter.drawText(QPoint(0, yPos), status()); |
|
98 |
|
99 for (int y = 0; y < HEIGHT; ++y) { |
|
100 yPos += fontHeight; |
|
101 xPos = 0; |
|
102 |
|
103 for (int x = 0; x < WIDTH; ++x) { |
|
104 if (y == pY && x == pX) { |
|
105 xPos += fontWidth; |
|
106 continue; |
|
107 } |
|
108 |
|
109 painter.drawText(QPoint(xPos, yPos), map[x][y]); |
|
110 xPos += fontWidth; |
|
111 } |
|
112 } |
|
113 painter.drawText(QPoint(pX * fontWidth, (pY + 2) * fontHeight), QChar('@')); |
|
114 } |
|
115 |
|
116 QSize Window::sizeHint() const |
|
117 { |
|
118 QFontMetrics metrics(font()); |
|
119 |
|
120 return QSize(metrics.width('X') * WIDTH, metrics.height() * (HEIGHT + 1)); |
|
121 } |
|
122 |
|
123 //![2] |
|
124 void Window::buildMachine() |
|
125 { |
|
126 machine = new QStateMachine; |
|
127 |
|
128 QState *inputState = new QState(machine); |
|
129 inputState->assignProperty(this, "status", "Move the rogue with 2, 4, 6, and 8"); |
|
130 |
|
131 MovementTransition *transition = new MovementTransition(this); |
|
132 inputState->addTransition(transition); |
|
133 //![2] |
|
134 |
|
135 //![3] |
|
136 QState *quitState = new QState(machine); |
|
137 quitState->assignProperty(this, "status", "Really quit(y/n)?"); |
|
138 |
|
139 QKeyEventTransition *yesTransition = new |
|
140 QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Y); |
|
141 yesTransition->setTargetState(new QFinalState(machine)); |
|
142 quitState->addTransition(yesTransition); |
|
143 |
|
144 QKeyEventTransition *noTransition = |
|
145 new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_N); |
|
146 noTransition->setTargetState(inputState); |
|
147 quitState->addTransition(noTransition); |
|
148 //![3] |
|
149 |
|
150 //![4] |
|
151 QKeyEventTransition *quitTransition = |
|
152 new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Q); |
|
153 quitTransition->setTargetState(quitState); |
|
154 inputState->addTransition(quitTransition); |
|
155 //![4] |
|
156 |
|
157 //![5] |
|
158 machine->setInitialState(inputState); |
|
159 |
|
160 connect(machine, SIGNAL(finished()), qApp, SLOT(quit())); |
|
161 |
|
162 machine->start(); |
|
163 } |
|
164 //![5] |
|
165 |
|
166 void Window::movePlayer(Direction direction) |
|
167 { |
|
168 switch (direction) { |
|
169 case Left: |
|
170 if (map[pX - 1][pY] != '#') |
|
171 --pX; |
|
172 break; |
|
173 case Right: |
|
174 if (map[pX + 1][pY] != '#') |
|
175 ++pX; |
|
176 break; |
|
177 case Up: |
|
178 if (map[pX][pY - 1] != '#') |
|
179 --pY; |
|
180 break; |
|
181 case Down: |
|
182 if (map[pX][pY + 1] != '#') |
|
183 ++pY; |
|
184 break; |
|
185 } |
|
186 repaint(); |
|
187 } |
|
188 |
|
189 void Window::setupMap() |
|
190 { |
|
191 qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
192 |
|
193 for (int x = 0; x < WIDTH; ++x) |
|
194 for (int y = 0; y < HEIGHT; ++y) { |
|
195 if (x == 0 || x == WIDTH - 1 || y == 0 || y == HEIGHT - 1 || qrand() % 40 == 0) |
|
196 map[x][y] = '#'; |
|
197 else |
|
198 map[x][y] = '.'; |
|
199 } |
|
200 } |
|
201 |