diff options
author | Alexis Menard <alexis.menard@nokia.com> | 2009-04-17 14:06:06 (GMT) |
---|---|---|
committer | Alexis Menard <alexis.menard@nokia.com> | 2009-04-17 14:06:06 (GMT) |
commit | f15b8a83e2e51955776a3f07cb85ebfc342dd8ef (patch) | |
tree | c5dc684986051654898db11ce73e03b9fec8db99 /examples/script/qstetrix | |
download | Qt-f15b8a83e2e51955776a3f07cb85ebfc342dd8ef.zip Qt-f15b8a83e2e51955776a3f07cb85ebfc342dd8ef.tar.gz Qt-f15b8a83e2e51955776a3f07cb85ebfc342dd8ef.tar.bz2 |
Initial import of statemachine branch from the old kinetic repository
Diffstat (limited to 'examples/script/qstetrix')
-rw-r--r-- | examples/script/qstetrix/main.cpp | 142 | ||||
-rw-r--r-- | examples/script/qstetrix/qstetrix.pro | 17 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrix.qrc | 8 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrixboard.cpp | 139 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrixboard.h | 102 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrixboard.js | 261 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrixpiece.js | 131 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrixwindow.js | 16 | ||||
-rw-r--r-- | examples/script/qstetrix/tetrixwindow.ui | 175 |
9 files changed, 991 insertions, 0 deletions
diff --git a/examples/script/qstetrix/main.cpp b/examples/script/qstetrix/main.cpp new file mode 100644 index 0000000..6d7af45 --- /dev/null +++ b/examples/script/qstetrix/main.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "tetrixboard.h" + +#include <QtGui> +#include <QtScript> +#include <QUiLoader> + +#ifndef QT_NO_SCRIPTTOOLS +#include <QtScriptTools> +#endif + +struct QtMetaObject : private QObject +{ +public: + static const QMetaObject *get() + { return &static_cast<QtMetaObject*>(0)->staticQtMetaObject; } +}; + +//! [0] +class TetrixUiLoader : public QUiLoader +{ +public: + TetrixUiLoader(QObject *parent = 0) + : QUiLoader(parent) + { } + virtual QWidget *createWidget(const QString &className, QWidget *parent = 0, + const QString &name = QString()) + { + if (className == QLatin1String("TetrixBoard")) { + QWidget *board = new TetrixBoard(parent); + board->setObjectName(name); + return board; + } + return QUiLoader::createWidget(className, parent, name); + } +}; +//! [0] + +static QScriptValue evaluateFile(QScriptEngine &engine, const QString &fileName) +{ + QFile file(fileName); + file.open(QIODevice::ReadOnly); + return engine.evaluate(file.readAll(), fileName); +} + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(tetrix); + +//! [1] + QApplication app(argc, argv); + QScriptEngine engine; + + QScriptValue Qt = engine.newQMetaObject(QtMetaObject::get()); + Qt.setProperty("App", engine.newQObject(&app)); + engine.globalObject().setProperty("Qt", Qt); +//! [1] + +#ifndef QT_NO_SCRIPTTOOLS + QScriptEngineDebugger debugger; + debugger.attachTo(&engine); + QMainWindow *debugWindow = debugger.standardWindow(); + debugWindow->resize(1024, 640); +#endif + +//! [2] + evaluateFile(engine, ":/tetrixpiece.js"); + evaluateFile(engine, ":/tetrixboard.js"); + evaluateFile(engine, ":/tetrixwindow.js"); +//! [2] + +//! [3] + TetrixUiLoader loader; + QFile uiFile(":/tetrixwindow.ui"); + uiFile.open(QIODevice::ReadOnly); + QWidget *ui = loader.load(&uiFile); + uiFile.close(); + + QScriptValue ctor = engine.evaluate("TetrixWindow"); + QScriptValue scriptUi = engine.newQObject(ui, QScriptEngine::ScriptOwnership); + QScriptValue tetrix = ctor.construct(QScriptValueList() << scriptUi); +//! [3] + + QPushButton *debugButton = qFindChild<QPushButton*>(ui, "debugButton"); +#ifndef QT_NO_SCRIPTTOOLS + QObject::connect(debugButton, SIGNAL(clicked()), + debugger.action(QScriptEngineDebugger::InterruptAction), + SIGNAL(triggered())); + QObject::connect(debugButton, SIGNAL(clicked()), + debugWindow, SLOT(show())); +#else + debugButton->hide(); +#endif + +//! [4] + ui->resize(550, 370); + ui->show(); + + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + return app.exec(); +//! [4] +} diff --git a/examples/script/qstetrix/qstetrix.pro b/examples/script/qstetrix/qstetrix.pro new file mode 100644 index 0000000..3db66ab --- /dev/null +++ b/examples/script/qstetrix/qstetrix.pro @@ -0,0 +1,17 @@ +QT += script +CONFIG += uitools + +HEADERS = tetrixboard.h +SOURCES = main.cpp \ + tetrixboard.cpp + +FORMS = tetrixwindow.ui +RESOURCES = tetrix.qrc + +contains(QT_CONFIG, scripttools): QT += scripttools + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/script/qstetrix +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS qstetrix.pro *.js +sources.path = $$[QT_INSTALL_EXAMPLES]/script/qstetrix +INSTALLS += target sources diff --git a/examples/script/qstetrix/tetrix.qrc b/examples/script/qstetrix/tetrix.qrc new file mode 100644 index 0000000..58d085a --- /dev/null +++ b/examples/script/qstetrix/tetrix.qrc @@ -0,0 +1,8 @@ +<RCC> + <qresource prefix="/" > + <file>tetrixwindow.js</file> + <file>tetrixwindow.ui</file> + <file>tetrixboard.js</file> + <file>tetrixpiece.js</file> + </qresource> +</RCC> diff --git a/examples/script/qstetrix/tetrixboard.cpp b/examples/script/qstetrix/tetrixboard.cpp new file mode 100644 index 0000000..55c4d3c --- /dev/null +++ b/examples/script/qstetrix/tetrixboard.cpp @@ -0,0 +1,139 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "tetrixboard.h" + +#include <QtGui> + +Q_DECLARE_METATYPE(QPainter*) + +TetrixBoard::TetrixBoard(QWidget *parent) + : QFrame(parent) +{ + timer = new QTimer(this); + qMetaTypeId<QPainter*>(); +} + +void TetrixBoard::setNextPieceLabel(QWidget *label) +{ + nextPieceLabel = qobject_cast<QLabel*>(label); +} + +QObject *TetrixBoard::getTimer() +{ + return timer; +} + +QSize TetrixBoard::minimumSizeHint() const +{ + return QSize(BoardWidth * 5 + frameWidth() * 2, + BoardHeight * 5 + frameWidth() * 2); +} + +void TetrixBoard::paintEvent(QPaintEvent *event) +{ + QFrame::paintEvent(event); + QPainter painter(this); + painter.drawImage(0, 0, image); +} + +void TetrixBoard::keyPressEvent(QKeyEvent *event) +{ + emit keyPressed(event->key()); +} + +void TetrixBoard::showNextPiece(int width, int height) +{ + if (!nextPieceLabel) + return; + + QPixmap pixmap(width * squareWidth(), height * squareHeight()); + QPainter painter(&pixmap); + painter.fillRect(pixmap.rect(), nextPieceLabel->palette().background()); + + emit paintNextPieceRequested(&painter); + + nextPieceLabel->setPixmap(pixmap); +} + +void TetrixBoard::drawPauseScreen(QPainter *painter) +{ + painter->drawText(contentsRect(), Qt::AlignCenter, tr("Pause")); +} + +void TetrixBoard::drawSquare(QPainter *painter, int x, int y, int shape) +{ + static const QRgb colorTable[8] = { + 0x000000, 0xCC6666, 0x66CC66, 0x6666CC, + 0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00 + }; + + x = x*squareWidth(); + y = y*squareHeight(); + + QColor color = colorTable[shape]; + painter->fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2, + color); + + painter->setPen(color.light()); + painter->drawLine(x, y + squareHeight() - 1, x, y); + painter->drawLine(x, y, x + squareWidth() - 1, y); + + painter->setPen(color.dark()); + painter->drawLine(x + 1, y + squareHeight() - 1, + x + squareWidth() - 1, y + squareHeight() - 1); + painter->drawLine(x + squareWidth() - 1, y + squareHeight() - 1, + x + squareWidth() - 1, y + 1); +} + +void TetrixBoard::update() +{ + QRect rect = contentsRect(); + if (image.size() != rect.size()) + image = QImage(rect.size(), QImage::Format_ARGB32_Premultiplied); + image.fill(qRgba(0,0,0,0)); + QPainter painter; + painter.begin(&image); + int boardTop = rect.bottom() - BoardHeight*squareHeight(); + painter.translate(rect.left(), boardTop); + emit paintRequested(&painter); + QFrame::update(); +} diff --git a/examples/script/qstetrix/tetrixboard.h b/examples/script/qstetrix/tetrixboard.h new file mode 100644 index 0000000..e8c1016 --- /dev/null +++ b/examples/script/qstetrix/tetrixboard.h @@ -0,0 +1,102 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TETRIXBOARD_H +#define TETRIXBOARD_H + +#include <QTimer> +#include <QFrame> +#include <QPointer> + +QT_BEGIN_NAMESPACE +class QLabel; +QT_END_NAMESPACE + +class TetrixBoard : public QFrame +{ + Q_OBJECT + Q_PROPERTY(QObject* timer READ getTimer) + Q_PROPERTY(QWidget* nextPieceLabel WRITE setNextPieceLabel) + +public: + TetrixBoard(QWidget *parent = 0); + + void setNextPieceLabel(QWidget *label); + void setBoardWidth(int width); + void setBoardHeight(int height); + QSize minimumSizeHint() const; + + QObject *getTimer(); + +signals: + void scoreChanged(int score); + void levelChanged(int level); + void linesRemovedChanged(int numLines); + +#if !defined(Q_MOC_RUN) +private: // can only be emitted by TetrixBoard +#endif + void keyPressed(int key); + void paintRequested(QPainter *painter); + void paintNextPieceRequested(QPainter *painter); + +protected: + void paintEvent(QPaintEvent *event); + void keyPressEvent(QKeyEvent *event); + +protected slots: + void showNextPiece(int width, int height); + void drawPauseScreen(QPainter *painter); + void drawSquare(QPainter *painter, int x, int y, int shape); + void update(); + +private: + enum { BoardWidth = 10, BoardHeight = 22 }; + + int squareWidth() { return contentsRect().width() / BoardWidth; } + int squareHeight() { return contentsRect().height() / BoardHeight; } + + QTimer *timer; + QPointer<QLabel> nextPieceLabel; + QImage image; +}; + +#endif diff --git a/examples/script/qstetrix/tetrixboard.js b/examples/script/qstetrix/tetrixboard.js new file mode 100644 index 0000000..f198397 --- /dev/null +++ b/examples/script/qstetrix/tetrixboard.js @@ -0,0 +1,261 @@ +function TetrixBoard(ui) +{ + this.ui = ui; + + this.isStarted = false; + this.isPaused = false; + this.inKeyPress = false; + + this._board = new Array(TetrixBoard.BoardWidth * TetrixBoard.BoardHeight); + this.clearBoard(); + + this.curPiece = new TetrixPiece(); + this.nextPiece = new TetrixPiece(); + this.nextPiece.setRandomShape(); + + ui.timer.singleShot = true; + ui.timer.timeout.connect(this, this.onTimer); + ui.keyPressed.connect(this, this.onKeyPress); + ui.paintRequested.connect(this, this.onPaint); + ui.paintNextPieceRequested.connect(this, this.onPaintNextPiece); +} + +TetrixBoard.BoardWidth = 10; +TetrixBoard.BoardHeight = 22; + +TetrixBoard.prototype.start = function() { + if (this.isPaused) + return; + + this.isStarted = true; + this.isWaitingAfterLine = false; + this.numLinesRemoved = 0; + this.numPiecesDropped = 0; + this.score = 0; + this.level = 1; + this.clearBoard(); + + this.ui.linesRemovedChanged(this.numLinesRemoved); + this.ui.scoreChanged(this.score); + this.ui.levelChanged(this.level); + + this.newPiece(); + this.ui.timer.start(this.timeoutTime()); +} + +TetrixBoard.prototype.pause = function() { + if (!this.isStarted) + return; + + this.isPaused = !this.isPaused; + if (this.isPaused) { + this.ui.timer.stop(); + } else { + this.ui.timer.start(this.timeoutTime()); + } + this.ui.update(); +} + +TetrixBoard.prototype.getShapeAt = function(x, y) { + return this._board[(y * TetrixBoard.BoardWidth) + x]; +} + +TetrixBoard.prototype.setShapeAt = function(x, y, newShape) { + this._board[(y * TetrixBoard.BoardWidth) + x] = newShape; +} + +TetrixBoard.prototype.clearBoard = function() { + for (var i = 0; i < TetrixBoard.BoardHeight * TetrixBoard.BoardWidth; ++i) + this._board[i] = TetrixShape.NoShape; +} + +TetrixBoard.prototype.dropDown = function() { + var dropHeight = 0; + var newY = this.curY; + while (newY > 0) { + if (!this.tryMove(this.curPiece, this.curX, newY - 1)) + break; + --newY; + ++dropHeight; + } + this.pieceDropped(dropHeight); +} + +TetrixBoard.prototype.oneLineDown = function() { + if (!this.tryMove(this.curPiece, this.curX, this.curY - 1)) + this.pieceDropped(0); +} + +TetrixBoard.prototype.pieceDropped = function(dropHeight) { + for (var i = 0; i < 4; ++i) { + var x = this.curX + this.curPiece.getX(i); + var y = this.curY - this.curPiece.getY(i); + this.setShapeAt(x, y, this.curPiece.shape); + } + + ++this.numPiecesDropped; + if ((this.numPiecesDropped % 25) == 0) { + ++this.level; + this.ui.timer.start(this.timeoutTime()); + this.ui.levelChanged(this.level); + } + + this.score += dropHeight + 7; + this.ui.scoreChanged(this.score); + this.removeFullLines(); + + if (!this.isWaitingAfterLine) + this.newPiece(); + + if (this.isStarted && !this.ui.timer.active) + this.ui.timer.start(this.timeoutTime()); +} + +TetrixBoard.prototype.removeFullLines = function() { + var numFullLines = 0; + + for (var i = TetrixBoard.BoardHeight - 1; i >= 0; --i) { + var lineIsFull = true; + + for (var j = 0; j < TetrixBoard.BoardWidth; ++j) { + if (this.getShapeAt(j, i) == TetrixShape.NoShape) { + lineIsFull = false; + break; + } + } + + if (lineIsFull) { + ++numFullLines; + for (var k = i; k < TetrixBoard.BoardHeight - 1; ++k) { + for (var j = 0; j < TetrixBoard.BoardWidth; ++j) + this.setShapeAt(j, k, this.getShapeAt(j, k + 1)); + } + for (var j = 0; j < TetrixBoard.BoardWidth; ++j) + this.setShapeAt(j, TetrixBoard.BoardHeight - 1, TetrixShape.NoShape); + } + } + + if (numFullLines > 0) { + this.numLinesRemoved += numFullLines; + this.score += 10 * numFullLines; + this.ui.linesRemovedChanged(this.numLinesRemoved); + this.ui.scoreChanged(this.score); + + this.ui.timer.start(500); + this.isWaitingAfterLine = true; + this.curPiece.shape = TetrixShape.NoShape; + this.ui.update(); + } +} + +TetrixBoard.prototype.newPiece = function() { + this.curPiece = this.nextPiece; + this.nextPiece = new TetrixPiece(); + this.nextPiece.setRandomShape(); + this.ui.showNextPiece(this.nextPiece.maxX - this.nextPiece.minX + 1, + this.nextPiece.maxY - this.nextPiece.minY + 1); + this.curX = TetrixBoard.BoardWidth / 2 + 1; + this.curY = TetrixBoard.BoardHeight - 1 + this.curPiece.minY; + + if (!this.tryMove(this.curPiece, this.curX, this.curY)) { + this.curPiece.shape = TetrixShape.NoShape; + this.ui.timer.stop(); + this.isStarted = false; + } +} + +TetrixBoard.prototype.tryMove = function(newPiece, newX, newY) { + for (var i = 0; i < 4; ++i) { + var x = newX + newPiece.getX(i); + var y = newY - newPiece.getY(i); + if ((x < 0) || (x >= TetrixBoard.BoardWidth) || (y < 0) || (y >= TetrixBoard.BoardHeight)) + return false; + if (this.getShapeAt(x, y) != TetrixShape.NoShape) + return false; + } + + this.curPiece = newPiece; + this.curX = newX; + this.curY = newY; + this.ui.update(); + return true; +} + +TetrixBoard.prototype.onPaint = function(painter) { + if (this.isPaused) { + this.ui.drawPauseScreen(painter); + return; + } + + for (var i = 0; i < TetrixBoard.BoardHeight; ++i) { + for (var j = 0; j < TetrixBoard.BoardWidth; ++j) { + var shape = this.getShapeAt(j, TetrixBoard.BoardHeight - i - 1); + if (shape != TetrixShape.NoShape) + this.ui.drawSquare(painter, j, i, shape); + } + } + + if (this.curPiece.shape != TetrixShape.NoShape) { + for (var i = 0; i < 4; ++i) { + var x = this.curX + this.curPiece.getX(i); + var y = this.curY - this.curPiece.getY(i); + this.ui.drawSquare(painter, x, TetrixBoard.BoardHeight - y - 1, + this.curPiece.shape); + } + } +} + +TetrixBoard.prototype.onPaintNextPiece = function(painter) { + for (var i = 0; i < 4; ++i) { + var x = this.nextPiece.getX(i) - this.nextPiece.minX; + var y = this.nextPiece.getY(i) - this.nextPiece.minY; + this.ui.drawSquare(painter, x, y, this.nextPiece.shape); + } +} + +TetrixBoard.prototype.onKeyPress = function(key) { + if (!this.isStarted || this.isPaused || (this.curPiece.shape == TetrixShape.NoShape)) + return; + this.inKeyPress = true; + switch (key) { + case Qt.Key_Left: + this.tryMove(this.curPiece, this.curX - 1, this.curY); + break; + case Qt.Key_Right: + this.tryMove(this.curPiece, this.curX + 1, this.curY); + break; + case Qt.Key_Down: + this.tryMove(this.curPiece.rotatedRight(), this.curX, this.curY); + break; + case Qt.Key_Up: + this.tryMove(this.curPiece.rotatedLeft(), this.curX, this.curY); + break; + case Qt.Key_Space: + this.dropDown(); + break; + case Qt.Key_D: + this.oneLineDown(); + break; + } + this.inKeyPress = false; + if (this.isStarted && !this.ui.timer.active) + this.ui.timer.start(this.timeoutTime()); +} + +TetrixBoard.prototype.onTimer = function() { + if (this.isWaitingAfterLine) { + this.isWaitingAfterLine = false; + this.newPiece(); + this.ui.timer.start(this.timeoutTime()); + } else { + if (!this.inKeyPress) { + this.oneLineDown(); + if (this.isStarted && !this.ui.timer.active) + this.ui.timer.start(this.timeoutTime()); + } + } +} + +TetrixBoard.prototype.timeoutTime = function() { + return 1000 / (1 + this.level); +} diff --git a/examples/script/qstetrix/tetrixpiece.js b/examples/script/qstetrix/tetrixpiece.js new file mode 100644 index 0000000..e99fa8a --- /dev/null +++ b/examples/script/qstetrix/tetrixpiece.js @@ -0,0 +1,131 @@ +TetrixShape = { + NoShape:0, + ZShape:1, + SShape:2, + LineShape:3, + TShape:4, + SquareShape:5, + LShape:6, + MirroredLShape:7 +} + +TetrixCoordsTable = [ + [ [ 0, 0 ], [ 0, 0 ], [ 0, 0 ], [ 0, 0 ] ], + [ [ 0, -1 ], [ 0, 0 ], [ -1, 0 ], [ -1, 1 ] ], + [ [ 0, -1 ], [ 0, 0 ], [ 1, 0 ], [ 1, 1 ] ], + [ [ 0, -1 ], [ 0, 0 ], [ 0, 1 ], [ 0, 2 ] ], + [ [ -1, 0 ], [ 0, 0 ], [ 1, 0 ], [ 0, 1 ] ], + [ [ 0, 0 ], [ 1, 0 ], [ 0, 1 ], [ 1, 1 ] ], + [ [ -1, -1 ], [ 0, -1 ], [ 0, 0 ], [ 0, 1 ] ], + [ [ 1, -1 ], [ 0, -1 ], [ 0, 0 ], [ 0, 1 ] ] +] + +function TetrixPiece() +{ + this.shape = TetrixShape.NoShape; +} + +TetrixPiece.prototype.__defineGetter__( + "shape", + function() { + return this._shape; + } +); + +TetrixPiece.prototype.__defineSetter__( + "shape", + function(shape) { + this._shape = shape; + this._coords = new Array(4); + for (var i = 0; i < 4; ++i) + this._coords[i] = TetrixCoordsTable[shape][i].slice(); + } +); + +TetrixPiece.prototype.setRandomShape = function() { + this.shape = Math.floor(((Math.random() * 100000) % 7) + 1); +} + +TetrixPiece.prototype.getX = function(index) { + return this._coords[index][0]; +} + +TetrixPiece.prototype.getY = function(index) { + return this._coords[index][1]; +} + +TetrixPiece.prototype._setX = function(index, x) { + this._coords[index][0] = x; +} + +TetrixPiece.prototype._setY = function(index, y) { + this._coords[index][1] = y; +} + +TetrixPiece.prototype.__defineGetter__( + "minX", + function() { + var min = this._coords[0][0]; + for (var i = 1; i < 4; ++i) + min = Math.min(min, this._coords[i][0]); + return min; + } +); + +TetrixPiece.prototype.__defineGetter__( + "maxX", + function() { + var max = this._coords[0][0]; + for (var i = 1; i < 4; ++i) + max = Math.max(max, this._coords[i][0]); + return max; + } +); + +TetrixPiece.prototype.__defineGetter__( + "minY", + function() { + var min = this._coords[0][1]; + for (var i = 1; i < 4; ++i) + min = Math.min(min, this._coords[i][1]); + return min; + } +); + +TetrixPiece.prototype.__defineGetter__( + "maxY", + function() { + var max = this._coords[0][1]; + for (var i = 1; i < 4; ++i) + max = Math.max(max, this._coords[i][1]); + return max; + } +); + +TetrixPiece.prototype.rotatedLeft = function() { + var result = new TetrixPiece(); + if (this._shape == TetrixShape.SquareShape) { + result.shape = this._shape; + return result; + } + result._shape = this._shape; + for (var i = 0; i < 4; ++i) { + result._setX(i, this.getY(i)); + result._setY(i, -this.getX(i)); + } + return result; +} + +TetrixPiece.prototype.rotatedRight = function() { + var result = new TetrixPiece(); + if (this._shape == TetrixShape.SquareShape) { + result.shape = this._shape; + return result; + } + result._shape = this._shape; + for (var i = 0; i < 4; ++i) { + result._setX(i, -this.getY(i)); + result._setY(i, this.getX(i)); + } + return result; +} diff --git a/examples/script/qstetrix/tetrixwindow.js b/examples/script/qstetrix/tetrixwindow.js new file mode 100644 index 0000000..6157a8c --- /dev/null +++ b/examples/script/qstetrix/tetrixwindow.js @@ -0,0 +1,16 @@ +function TetrixWindow(ui) +{ + this.ui = ui; + + var boardUi = ui.findChild("board"); + boardUi.nextPieceLabel = ui.findChild("nextPieceLabel"); + + this.board = new TetrixBoard(boardUi); + + ui.findChild("startButton").clicked.connect(this.board, this.board.start); + ui.findChild("quitButton").clicked.connect(Qt.App.quit); + ui.findChild("pauseButton").clicked.connect(this.board, this.board.pause); + boardUi.scoreChanged.connect(ui.findChild("scoreLcd")["display(int)"]); + boardUi.levelChanged.connect(ui.findChild("levelLcd")["display(int)"]); + boardUi.linesRemovedChanged.connect(ui.findChild("linesLcd")["display(int)"]); +} diff --git a/examples/script/qstetrix/tetrixwindow.ui b/examples/script/qstetrix/tetrixwindow.ui new file mode 100644 index 0000000..a53e94f --- /dev/null +++ b/examples/script/qstetrix/tetrixwindow.ui @@ -0,0 +1,175 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>TetrixWindow</class> + <widget class="QWidget" name="TetrixWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>537</width> + <height>475</height> + </rect> + </property> + <property name="windowTitle"> + <string>Tetrix</string> + </property> + <layout class="QVBoxLayout"> + <property name="spacing"> + <number>6</number> + </property> + <property name="margin"> + <number>9</number> + </property> + <item> + <layout class="QGridLayout"> + <property name="margin"> + <number>0</number> + </property> + <property name="spacing"> + <number>6</number> + </property> + <item row="4" column="0"> + <widget class="QPushButton" name="startButton"> + <property name="focusPolicy"> + <enum>Qt::NoFocus</enum> + </property> + <property name="text"> + <string>&Start</string> + </property> + </widget> + </item> + <item row="3" column="2"> + <widget class="QLCDNumber" name="linesLcd"> + <property name="segmentStyle"> + <enum>QLCDNumber::Filled</enum> + </property> + </widget> + </item> + <item row="2" column="2"> + <widget class="QLabel" name="linesRemovedLabel"> + <property name="text"> + <string>LINES REMOVED</string> + </property> + <property name="alignment"> + <set>Qt::AlignBottom|Qt::AlignHCenter</set> + </property> + </widget> + </item> + <item row="1" column="2"> + <widget class="QLCDNumber" name="scoreLcd"> + <property name="segmentStyle"> + <enum>QLCDNumber::Filled</enum> + </property> + </widget> + </item> + <item row="0" column="1" rowspan="6"> + <widget class="TetrixBoard" name="board"> + <property name="focusPolicy"> + <enum>Qt::StrongFocus</enum> + </property> + <property name="frameShape"> + <enum>QFrame::Panel</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Sunken</enum> + </property> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="levelLabel"> + <property name="text"> + <string>LEVEL</string> + </property> + <property name="alignment"> + <set>Qt::AlignBottom|Qt::AlignHCenter</set> + </property> + </widget> + </item> + <item row="0" column="0"> + <widget class="QLabel" name="nextLabel"> + <property name="text"> + <string>NEXT</string> + </property> + <property name="alignment"> + <set>Qt::AlignBottom|Qt::AlignHCenter</set> + </property> + </widget> + </item> + <item row="3" column="0"> + <widget class="QLCDNumber" name="levelLcd"> + <property name="segmentStyle"> + <enum>QLCDNumber::Filled</enum> + </property> + </widget> + </item> + <item row="0" column="2"> + <widget class="QLabel" name="scoreLabel"> + <property name="text"> + <string>SCORE</string> + </property> + <property name="alignment"> + <set>Qt::AlignBottom|Qt::AlignHCenter</set> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="nextPieceLabel"> + <property name="frameShape"> + <enum>QFrame::Box</enum> + </property> + <property name="frameShadow"> + <enum>QFrame::Raised</enum> + </property> + <property name="text"> + <string/> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + </widget> + </item> + <item row="5" column="0"> + <widget class="QPushButton" name="quitButton"> + <property name="focusPolicy"> + <enum>Qt::NoFocus</enum> + </property> + <property name="text"> + <string>&Quit</string> + </property> + </widget> + </item> + <item row="4" column="2"> + <widget class="QPushButton" name="pauseButton"> + <property name="focusPolicy"> + <enum>Qt::NoFocus</enum> + </property> + <property name="text"> + <string>&Pause</string> + </property> + </widget> + </item> + <item row="5" column="2"> + <widget class="QPushButton" name="debugButton"> + <property name="focusPolicy"> + <enum>Qt::NoFocus</enum> + </property> + <property name="text"> + <string>&Debug</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + <customwidgets> + <customwidget> + <class>TetrixBoard</class> + <extends>QFrame</extends> + <header>tetrixboard.h</header> + <container>1</container> + </customwidget> + </customwidgets> + <resources/> + <connections/> +</ui> |