[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 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 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[556] | 11 | **
|
---|
[846] | 12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
| 13 | ** modification, are permitted provided that the following conditions are
|
---|
| 14 | ** met:
|
---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
---|
| 19 | ** the documentation and/or other materials provided with the
|
---|
| 20 | ** distribution.
|
---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
---|
| 23 | ** products derived from this software without specific prior written
|
---|
| 24 | ** permission.
|
---|
[556] | 25 | **
|
---|
[846] | 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
[556] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include <QtGui>
|
---|
| 42 |
|
---|
| 43 | #include "window.h"
|
---|
| 44 | #include "movementtransition.h"
|
---|
| 45 |
|
---|
| 46 | //![0]
|
---|
| 47 | Window::Window()
|
---|
| 48 | {
|
---|
| 49 | pX = 5;
|
---|
| 50 | pY = 5;
|
---|
| 51 | //![0]
|
---|
| 52 |
|
---|
| 53 | QFontDatabase database;
|
---|
| 54 | QFont font;
|
---|
| 55 | if (database.families().contains("Monospace"))
|
---|
| 56 | font = QFont("Monospace", 12);
|
---|
| 57 | else {
|
---|
| 58 | foreach (QString family, database.families()) {
|
---|
| 59 | if (database.isFixedPitch(family)) {
|
---|
| 60 | font = QFont(family, 12);
|
---|
| 61 | break;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | setFont(font);
|
---|
| 66 |
|
---|
| 67 | //![1]
|
---|
| 68 | setupMap();
|
---|
| 69 | buildMachine();
|
---|
| 70 | }
|
---|
| 71 | //![1]
|
---|
| 72 |
|
---|
| 73 | void Window::setStatus(const QString &status)
|
---|
| 74 | {
|
---|
| 75 | myStatus = status;
|
---|
| 76 | repaint();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | QString Window::status() const
|
---|
| 80 | {
|
---|
| 81 | return myStatus;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void Window::paintEvent(QPaintEvent * /* event */)
|
---|
| 85 | {
|
---|
| 86 | QFontMetrics metrics(font());
|
---|
| 87 | QPainter painter(this);
|
---|
| 88 | int fontHeight = metrics.height();
|
---|
| 89 | int fontWidth = metrics.width('X');
|
---|
| 90 | int yPos = fontHeight;
|
---|
| 91 | int xPos;
|
---|
| 92 |
|
---|
| 93 | painter.fillRect(rect(), Qt::black);
|
---|
| 94 | painter.setPen(Qt::white);
|
---|
| 95 |
|
---|
| 96 | painter.drawText(QPoint(0, yPos), status());
|
---|
| 97 |
|
---|
| 98 | for (int y = 0; y < HEIGHT; ++y) {
|
---|
| 99 | yPos += fontHeight;
|
---|
| 100 | xPos = 0;
|
---|
| 101 |
|
---|
| 102 | for (int x = 0; x < WIDTH; ++x) {
|
---|
| 103 | if (y == pY && x == pX) {
|
---|
| 104 | xPos += fontWidth;
|
---|
| 105 | continue;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | painter.drawText(QPoint(xPos, yPos), map[x][y]);
|
---|
| 109 | xPos += fontWidth;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | painter.drawText(QPoint(pX * fontWidth, (pY + 2) * fontHeight), QChar('@'));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | QSize Window::sizeHint() const
|
---|
| 116 | {
|
---|
| 117 | QFontMetrics metrics(font());
|
---|
| 118 |
|
---|
| 119 | return QSize(metrics.width('X') * WIDTH, metrics.height() * (HEIGHT + 1));
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | //![2]
|
---|
| 123 | void Window::buildMachine()
|
---|
| 124 | {
|
---|
| 125 | machine = new QStateMachine;
|
---|
| 126 |
|
---|
| 127 | QState *inputState = new QState(machine);
|
---|
| 128 | inputState->assignProperty(this, "status", "Move the rogue with 2, 4, 6, and 8");
|
---|
| 129 |
|
---|
| 130 | MovementTransition *transition = new MovementTransition(this);
|
---|
| 131 | inputState->addTransition(transition);
|
---|
| 132 | //![2]
|
---|
| 133 |
|
---|
| 134 | //![3]
|
---|
| 135 | QState *quitState = new QState(machine);
|
---|
| 136 | quitState->assignProperty(this, "status", "Really quit(y/n)?");
|
---|
| 137 |
|
---|
| 138 | QKeyEventTransition *yesTransition = new
|
---|
| 139 | QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Y);
|
---|
| 140 | yesTransition->setTargetState(new QFinalState(machine));
|
---|
| 141 | quitState->addTransition(yesTransition);
|
---|
| 142 |
|
---|
| 143 | QKeyEventTransition *noTransition =
|
---|
| 144 | new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_N);
|
---|
| 145 | noTransition->setTargetState(inputState);
|
---|
| 146 | quitState->addTransition(noTransition);
|
---|
| 147 | //![3]
|
---|
| 148 |
|
---|
| 149 | //![4]
|
---|
| 150 | QKeyEventTransition *quitTransition =
|
---|
| 151 | new QKeyEventTransition(this, QEvent::KeyPress, Qt::Key_Q);
|
---|
| 152 | quitTransition->setTargetState(quitState);
|
---|
| 153 | inputState->addTransition(quitTransition);
|
---|
| 154 | //![4]
|
---|
| 155 |
|
---|
| 156 | //![5]
|
---|
| 157 | machine->setInitialState(inputState);
|
---|
| 158 |
|
---|
| 159 | connect(machine, SIGNAL(finished()), qApp, SLOT(quit()));
|
---|
| 160 |
|
---|
| 161 | machine->start();
|
---|
| 162 | }
|
---|
| 163 | //![5]
|
---|
| 164 |
|
---|
| 165 | void Window::movePlayer(Direction direction)
|
---|
| 166 | {
|
---|
| 167 | switch (direction) {
|
---|
| 168 | case Left:
|
---|
| 169 | if (map[pX - 1][pY] != '#')
|
---|
| 170 | --pX;
|
---|
| 171 | break;
|
---|
| 172 | case Right:
|
---|
| 173 | if (map[pX + 1][pY] != '#')
|
---|
| 174 | ++pX;
|
---|
| 175 | break;
|
---|
| 176 | case Up:
|
---|
| 177 | if (map[pX][pY - 1] != '#')
|
---|
| 178 | --pY;
|
---|
| 179 | break;
|
---|
| 180 | case Down:
|
---|
| 181 | if (map[pX][pY + 1] != '#')
|
---|
| 182 | ++pY;
|
---|
| 183 | break;
|
---|
| 184 | }
|
---|
| 185 | repaint();
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | void Window::setupMap()
|
---|
| 189 | {
|
---|
| 190 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
---|
| 191 |
|
---|
| 192 | for (int x = 0; x < WIDTH; ++x)
|
---|
| 193 | for (int y = 0; y < HEIGHT; ++y) {
|
---|
| 194 | if (x == 0 || x == WIDTH - 1 || y == 0 || y == HEIGHT - 1 || qrand() % 40 == 0)
|
---|
| 195 | map[x][y] = '#';
|
---|
| 196 | else
|
---|
| 197 | map[x][y] = '.';
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|