diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2010-04-14 00:56:25 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2010-04-14 00:57:00 (GMT) |
commit | b16e6e70529b8ef86734324c6c6a0e151ddf4bc8 (patch) | |
tree | 18c82661106ce859833d4cff7fe9d9d74bf879ab /demos/declarative/minehunt | |
parent | 3469fe05e4b04567598202fecc16c1decfb3ab63 (diff) | |
download | Qt-b16e6e70529b8ef86734324c6c6a0e151ddf4bc8.zip Qt-b16e6e70529b8ef86734324c6c6a0e151ddf4bc8.tar.gz Qt-b16e6e70529b8ef86734324c6c6a0e151ddf4bc8.tar.bz2 |
Minehunt cleanup/optimization.
Diffstat (limited to 'demos/declarative/minehunt')
-rw-r--r-- | demos/declarative/minehunt/MinehuntCore/Explosion.qml | 8 | ||||
-rw-r--r-- | demos/declarative/minehunt/MinehuntCore/Tile.qml | 87 | ||||
-rw-r--r-- | demos/declarative/minehunt/MinehuntCore/qmldir | 1 | ||||
-rw-r--r-- | demos/declarative/minehunt/README | 2 | ||||
-rw-r--r-- | demos/declarative/minehunt/minehunt.cpp | 57 | ||||
-rw-r--r-- | demos/declarative/minehunt/minehunt.pro | 1 | ||||
-rw-r--r-- | demos/declarative/minehunt/minehunt.qml | 103 |
7 files changed, 125 insertions, 134 deletions
diff --git a/demos/declarative/minehunt/MinehuntCore/Explosion.qml b/demos/declarative/minehunt/MinehuntCore/Explosion.qml index 526cd34..73ada60 100644 --- a/demos/declarative/minehunt/MinehuntCore/Explosion.qml +++ b/demos/declarative/minehunt/MinehuntCore/Explosion.qml @@ -17,11 +17,9 @@ Item { velocity: 100 velocityDeviation: 20 z: 100 - opacity: 1 } - states: [ State { name: "exploding"; when: explode == true - StateChangeScript {script: particles.burst(200); } - } - ] + states: State { name: "exploding"; when: explode + StateChangeScript {script: particles.burst(200); } + } } diff --git a/demos/declarative/minehunt/MinehuntCore/Tile.qml b/demos/declarative/minehunt/MinehuntCore/Tile.qml new file mode 100644 index 0000000..f3620f4 --- /dev/null +++ b/demos/declarative/minehunt/MinehuntCore/Tile.qml @@ -0,0 +1,87 @@ +import Qt 4.7 + +Flipable { + id: flipable + property int angle: 0 + + width: 40; height: 40 + transform: Rotation { origin.x: 20; origin.y: 20; axis.x: 1; axis.z: 0; angle: flipable.angle } + + front: Image { + source: "pics/front.png"; width: 40; height: 40 + + Image { + anchors.centerIn: parent + source: "pics/flag.png"; opacity: modelData.hasFlag + + Behavior on opacity { NumberAnimation {} } + } + } + + back: Image { + source: "pics/back.png" + width: 40; height: 40 + + Text { + anchors.centerIn: parent + text: modelData.hint; color: "white"; font.bold: true + opacity: !modelData.hasMine && modelData.hint > 0 + } + + Image { + anchors.centerIn: parent + source: "pics/bomb.png"; opacity: modelData.hasMine + } + + Explosion { id: expl } + } + + states: State { + name: "back"; when: modelData.flipped + PropertyChanges { target: flipable; angle: 180 } + } + + property real pauseDur: 250 + transitions: Transition { + SequentialAnimation { + ScriptAction { + script: { + var ret = Math.abs(flipable.x - field.clickx) + + Math.abs(flipable.y - field.clicky); + if (modelData.hasMine && modelData.flipped) + pauseDur = ret * 3 + else + pauseDur = ret + } + } + PauseAnimation { + duration: pauseDur + } + RotationAnimation { easing.type: "InOutQuad" } + ScriptAction { script: if (modelData.hasMine && modelData.flipped) { expl.explode = true } } + } + } + + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton + onClicked: { + field.clickx = flipable.x + field.clicky = flipable.y + var row = Math.floor(index / 9) + var col = index - (Math.floor(index / 9) * 9) + if (mouse.button == undefined || mouse.button == Qt.RightButton) { + flag(row, col) + } else { + flip(row, col) + } + } + onPressAndHold: { + field.clickx = flipable.x + field.clicky = flipable.y + var row = Math.floor(index / 9) + var col = index - (Math.floor(index / 9) * 9) + flag(row, col) + } + } +} diff --git a/demos/declarative/minehunt/MinehuntCore/qmldir b/demos/declarative/minehunt/MinehuntCore/qmldir index 862c396..95bccc8 100644 --- a/demos/declarative/minehunt/MinehuntCore/qmldir +++ b/demos/declarative/minehunt/MinehuntCore/qmldir @@ -1,2 +1,3 @@ plugin minehunt Explosion 1.0 Explosion.qml +Tile 1.0 Tile.qml diff --git a/demos/declarative/minehunt/README b/demos/declarative/minehunt/README index 1b6cf81..b9f1d2a 100644 --- a/demos/declarative/minehunt/README +++ b/demos/declarative/minehunt/README @@ -1,5 +1,3 @@ To compile the C++ part, do 'qmake && make'. Minehunt will not run properly if the C++ plugin is not compiled. To run, simply load the minehunt.qml file with the qml runtime. - -Note that on X11, this demo has problems with the native graphicssystem. If you are using the X11 window system, please pass -graphicssystem raster to the qml binary. diff --git a/demos/declarative/minehunt/minehunt.cpp b/demos/declarative/minehunt/minehunt.cpp index a953c5a..d4b0039 100644 --- a/demos/declarative/minehunt/minehunt.cpp +++ b/demos/declarative/minehunt/minehunt.cpp @@ -48,11 +48,11 @@ #include <QTime> #include <QTimer> -class Tile : public QObject +class TileData : public QObject { Q_OBJECT public: - Tile() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {} + TileData() : _hasFlag(false), _hasMine(false), _hint(-1), _flipped(false) {} Q_PROPERTY(bool hasFlag READ hasFlag WRITE setHasFlag NOTIFY hasFlagChanged) bool hasFlag() const { return _hasFlag; } @@ -91,8 +91,8 @@ class MinehuntGame : public QObject public: MinehuntGame(); - Q_PROPERTY(QDeclarativeListProperty<Tile> tiles READ tiles CONSTANT) - QDeclarativeListProperty<Tile> tiles(); + Q_PROPERTY(QDeclarativeListProperty<TileData> tiles READ tiles CONSTANT) + QDeclarativeListProperty<TileData> tiles(); Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY isPlayingChanged) bool isPlaying() {return playing;} @@ -120,11 +120,11 @@ signals: private: bool onBoard( int r, int c ) const { return r >= 0 && r < numRows && c >= 0 && c < numCols; } - Tile *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; } + TileData *tile( int row, int col ) { return onBoard(row, col) ? _tiles[col+numRows*row] : 0; } int getHint(int row, int col); void setPlaying(bool b){if(b==playing) return; playing=b; emit isPlayingChanged();} - QList<Tile *> _tiles; + QList<TileData *> _tiles; int numCols; int numRows; bool playing; @@ -134,25 +134,25 @@ private: int nFlags; }; -void tilesPropAppend(QDeclarativeListProperty<Tile>* prop, Tile* value) +void tilesPropAppend(QDeclarativeListProperty<TileData>* prop, TileData* value) { Q_UNUSED(prop); Q_UNUSED(value); return; //Append not supported } -int tilesPropCount(QDeclarativeListProperty<Tile>* prop) +int tilesPropCount(QDeclarativeListProperty<TileData>* prop) { - return static_cast<QList<Tile*>*>(prop->data)->count(); + return static_cast<QList<TileData*>*>(prop->data)->count(); } -Tile* tilesPropAt(QDeclarativeListProperty<Tile>* prop, int index) +TileData* tilesPropAt(QDeclarativeListProperty<TileData>* prop, int index) { - return static_cast<QList<Tile*>*>(prop->data)->at(index); + return static_cast<QList<TileData*>*>(prop->data)->at(index); } -QDeclarativeListProperty<Tile> MinehuntGame::tiles(){ - return QDeclarativeListProperty<Tile>(this, &_tiles, &tilesPropAppend, +QDeclarativeListProperty<TileData> MinehuntGame::tiles(){ + return QDeclarativeListProperty<TileData>(this, &_tiles, &tilesPropAppend, &tilesPropCount, &tilesPropAt, 0); } @@ -164,7 +164,7 @@ MinehuntGame::MinehuntGame() //initialize array for(int ii = 0; ii < numRows * numCols; ++ii) { - _tiles << new Tile; + _tiles << new TileData; } reset(); @@ -172,7 +172,7 @@ MinehuntGame::MinehuntGame() void MinehuntGame::setBoard() { - foreach(Tile* t, _tiles){ + foreach(TileData* t, _tiles){ t->setHasMine(false); t->setHint(-1); } @@ -183,7 +183,7 @@ void MinehuntGame::setBoard() int col = int((double(rand()) / double(RAND_MAX)) * numCols); int row = int((double(rand()) / double(RAND_MAX)) * numRows); - Tile* t = tile( row, col ); + TileData* t = tile( row, col ); if (t && !t->hasMine()) { t->setHasMine( true ); @@ -194,7 +194,7 @@ void MinehuntGame::setBoard() //set hints for (int r = 0; r < numRows; r++) for (int c = 0; c < numCols; c++) { - Tile* t = tile(r, c); + TileData* t = tile(r, c); if (t && !t->hasMine()) { int hint = getHint(r,c); t->setHint(hint); @@ -206,7 +206,7 @@ void MinehuntGame::setBoard() void MinehuntGame::reset() { - foreach(Tile* t, _tiles){ + foreach(TileData* t, _tiles){ t->unflip(); t->setHasFlag(false); } @@ -221,7 +221,7 @@ int MinehuntGame::getHint(int row, int col) int hint = 0; for (int c = col-1; c <= col+1; c++) for (int r = row-1; r <= row+1; r++) { - Tile* t = tile(r, c); + TileData* t = tile(r, c); if (t && t->hasMine()) hint++; } @@ -233,7 +233,7 @@ bool MinehuntGame::flip(int row, int col) if(!playing) return false; - Tile *t = tile(row, col); + TileData *t = tile(row, col); if (!t || t->hasFlag()) return false; @@ -241,7 +241,7 @@ bool MinehuntGame::flip(int row, int col) int flags = 0; for (int c = col-1; c <= col+1; c++) for (int r = row-1; r <= row+1; r++) { - Tile *nearT = tile(r, c); + TileData *nearT = tile(r, c); if(!nearT || nearT == t) continue; if(nearT->hasFlag()) @@ -251,7 +251,7 @@ bool MinehuntGame::flip(int row, int col) return false; for (int c = col-1; c <= col+1; c++) for (int r = row-1; r <= row+1; r++) { - Tile *nearT = tile(r, c); + TileData *nearT = tile(r, c); if (nearT && !nearT->flipped() && !nearT->hasFlag()) { flip( r, c ); } @@ -264,7 +264,7 @@ bool MinehuntGame::flip(int row, int col) if (t->hint() == 0) { for (int c = col-1; c <= col+1; c++) for (int r = row-1; r <= row+1; r++) { - Tile* t = tile(r, c); + TileData* t = tile(r, c); if (t && !t->flipped()) { flip( r, c ); } @@ -274,7 +274,7 @@ bool MinehuntGame::flip(int row, int col) if(t->hasMine()){ for (int r = 0; r < numRows; r++)//Flip all other mines for (int c = 0; c < numCols; c++) { - Tile* t = tile(r, c); + TileData* t = tile(r, c); if (t && t->hasMine()) { flip(r, c); } @@ -295,7 +295,7 @@ bool MinehuntGame::flip(int row, int col) bool MinehuntGame::flag(int row, int col) { - Tile *t = tile(row, col); + TileData *t = tile(row, col); if(!t) return false; @@ -305,8 +305,7 @@ bool MinehuntGame::flag(int row, int col) return true; } -QML_DECLARE_TYPE(Tile); -QML_DECLARE_TYPE(MinehuntGame); +QML_DECLARE_TYPE(TileData); class MinehuntExtensionPlugin : public QDeclarativeExtensionPlugin { @@ -314,8 +313,8 @@ class MinehuntExtensionPlugin : public QDeclarativeExtensionPlugin public: void registerTypes(const char *uri) { - qmlRegisterType<Tile>(uri, 0, 1, "Tile"); - qmlRegisterType<MinehuntGame>(uri, 0, 1, "Game"); + Q_UNUSED(uri); + qmlRegisterType<TileData>(); } void initializeEngine(QDeclarativeEngine *engine, const char *uri) { diff --git a/demos/declarative/minehunt/minehunt.pro b/demos/declarative/minehunt/minehunt.pro index 8a5667d..43f68c3 100644 --- a/demos/declarative/minehunt/minehunt.pro +++ b/demos/declarative/minehunt/minehunt.pro @@ -16,6 +16,7 @@ target.path = $$[QT_INSTALL_DEMOS]/declarative/minehunt/MinehuntCore MinehuntCore_sources.files = \ MinehuntCore/Explosion.qml \ + MinehuntCore/Tile.qml \ MinehuntCore/pics \ MinehuntCore/qmldir MinehuntCore_sources.path = $$[QT_INSTALL_DEMOS]/declarative/minehunt/MinehuntCore diff --git a/demos/declarative/minehunt/minehunt.qml b/demos/declarative/minehunt/minehunt.qml index 98955e2..5ed78fb 100644 --- a/demos/declarative/minehunt/minehunt.qml +++ b/demos/declarative/minehunt/minehunt.qml @@ -8,101 +8,6 @@ Item { width: 450; height: 450 - Component { - id: tile - - Flipable { - id: flipable - property int angle: 0 - - width: 40; height: 40 - transform: Rotation { origin.x: 20; origin.y: 20; axis.x: 1; axis.z: 0; angle: flipable.angle } - - front: Image { - source: "MinehuntCore/pics/front.png"; width: 40; height: 40 - - Image { - anchors.horizontalCenter: parent.horizontalCenter; anchors.verticalCenter: parent.verticalCenter - source: "MinehuntCore/pics/flag.png"; opacity: modelData.hasFlag - - Behavior on opacity { NumberAnimation { property: "opacity"; duration: 250 } } - } - } - - back: Image { - source: "MinehuntCore/pics/back.png" - width: 40; height: 40 - - Text { - anchors.horizontalCenter: parent.horizontalCenter; anchors.verticalCenter: parent.verticalCenter - text: modelData.hint; color: "white"; font.bold: true - opacity: !modelData.hasMine && modelData.hint > 0 - } - - Image { - anchors.horizontalCenter: parent.horizontalCenter; anchors.verticalCenter: parent.verticalCenter - source: "MinehuntCore/pics/bomb.png"; opacity: modelData.hasMine - } - - Explosion { id: expl } - } - - states: State { - name: "back"; when: modelData.flipped - PropertyChanges { target: flipable; angle: 180 } - } - - transitions: Transition { - SequentialAnimation { - PauseAnimation { - duration: { - var ret - if (flipable.parent != null) - ret = Math.abs(flipable.parent.x - field.clickx) - + Math.abs(flipable.parent.y - field.clicky) - else - ret = 0 - if (ret > 0) { - if (modelData.hasMine && modelData.flipped) { - ret * 3 - } else { - ret - } - } else { - 0 - } - } - } - NumberAnimation { easing.type: "InOutQuad"; properties: "angle" } - ScriptAction { script: if (modelData.hasMine && modelData.flipped) { expl.explode = true } } - } - } - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton - onClicked: { - field.clickx = flipable.parent.x - field.clicky = flipable.parent.y - var row = Math.floor(index / 9) - var col = index - (Math.floor(index / 9) * 9) - if (mouse.button == undefined || mouse.button == Qt.RightButton) { - flag(row, col) - } else { - flip(row, col) - } - } - onPressAndHold: { - field.clickx = flipable.parent.x - field.clicky = flipable.parent.y - var row = Math.floor(index / 9) - var col = index - (Math.floor(index / 9) * 9) - flag(row, col) - } - } - } - } - Image { source: "MinehuntCore/pics/No-Ones-Laughing-3.jpg"; anchors.fill: parent; fillMode: Image.Tile } Grid { @@ -112,7 +17,7 @@ Item { Repeater { id: repeater model: tiles - Component { Loader { sourceComponent: tile } } + delegate: Tile {} } } @@ -143,8 +48,10 @@ Item { MouseArea { anchors.fill: parent; onPressed: reset() } } Text { - anchors.fill: parent; wrapMode: Text.WordWrap - text: "Minehunt will not run properly if the C++ plugin is not compiled.\nPlease see README." + anchors.centerIn: parent; width: parent.width - 20 + horizontalAlignment: Text.AlignHCenter + wrapMode: Text.WordWrap + text: "Minehunt will not run properly if the C++ plugin is not compiled.\n\nPlease see README." color: "white"; font.bold: true; font.pixelSize: 14 visible: tiles == undefined } |