diff options
Diffstat (limited to 'demos')
-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 | 3 | ||||
-rw-r--r-- | demos/declarative/minehunt/minehunt.qml | 103 | ||||
-rw-r--r-- | demos/declarative/photoviewer/PhotoViewerCore/AlbumDelegate.qml | 4 | ||||
-rw-r--r-- | demos/declarative/samegame/SamegameCore/BoomBlock.qml | 21 | ||||
-rw-r--r-- | demos/declarative/samegame/SamegameCore/Button.qml | 25 | ||||
-rw-r--r-- | demos/declarative/samegame/SamegameCore/Dialog.qml | 25 | ||||
-rwxr-xr-x | demos/declarative/samegame/SamegameCore/samegame.js | 167 | ||||
-rw-r--r-- | demos/declarative/samegame/samegame.qml | 78 |
13 files changed, 302 insertions, 279 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..41640f5 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 @@ -26,7 +27,7 @@ symbian:{ load(data_caging_paths) TARGET.EPOCALLOWDLLDATA = 1 include($$QT_SOURCE_TREE/demos/symbianpkgrules.pri) - + TARGET.CAPABILITY = NetworkServices ReadUserData importFiles.sources = minehunt.dll \ MinehuntCore/Explosion.qml \ MinehuntCore/pics \ 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 } diff --git a/demos/declarative/photoviewer/PhotoViewerCore/AlbumDelegate.qml b/demos/declarative/photoviewer/PhotoViewerCore/AlbumDelegate.qml index b494651..cd9ecbc 100644 --- a/demos/declarative/photoviewer/PhotoViewerCore/AlbumDelegate.qml +++ b/demos/declarative/photoviewer/PhotoViewerCore/AlbumDelegate.qml @@ -79,9 +79,9 @@ Component { GridView.onAdd: NumberAnimation { target: albumWrapper; properties: "scale"; from: 0.0; to: 1.0 } GridView.onRemove: SequentialAnimation { - PropertyAction { target: albumWrapper.GridView; property: "delayRemove"; value: true } + PropertyAction { target: albumWrapper; property: "GridView.delayRemove"; value: true } NumberAnimation { target: albumWrapper; property: "scale"; from: 1.0; to: 0.0 } - PropertyAction { target: albumWrapper.GridView; property: "delayRemove"; value: false } + PropertyAction { target: albumWrapper; property: "GridView.delayRemove"; value: false } } transitions: [ diff --git a/demos/declarative/samegame/SamegameCore/BoomBlock.qml b/demos/declarative/samegame/SamegameCore/BoomBlock.qml index db43182..bad1bf4 100644 --- a/demos/declarative/samegame/SamegameCore/BoomBlock.qml +++ b/demos/declarative/samegame/SamegameCore/BoomBlock.qml @@ -26,12 +26,16 @@ Item { id:block anchors.fill: parent } - Particles { id: particles - width:1; height:1; anchors.centerIn: parent; - emissionRate: 0; - lifeSpan: 700; lifeSpanDeviation: 600; + Particles { + id: particles + + width: 1; height: 1 + anchors.centerIn: parent + + emissionRate: 0 + lifeSpan: 700; lifeSpanDeviation: 600 angle: 0; angleDeviation: 360; - velocity: 100; velocityDeviation:30; + velocity: 100; velocityDeviation: 30 source: { if(type == 0){ "pics/redStar.png"; @@ -44,10 +48,13 @@ Item { id:block } states: [ - State{ name: "AliveState"; when: spawned == true && dying == false + State { + name: "AliveState"; when: spawned == true && dying == false PropertyChanges { target: img; opacity: 1 } }, - State{ name: "DeathState"; when: dying == true + + State { + name: "DeathState"; when: dying == true StateChangeScript { script: particles.burst(50); } PropertyChanges { target: img; opacity: 0 } StateChangeScript { script: block.destroy(1000); } diff --git a/demos/declarative/samegame/SamegameCore/Button.qml b/demos/declarative/samegame/SamegameCore/Button.qml index 9c7986b..0faabc9 100644 --- a/demos/declarative/samegame/SamegameCore/Button.qml +++ b/demos/declarative/samegame/SamegameCore/Button.qml @@ -3,23 +3,32 @@ import Qt 4.7 Rectangle { id: container - signal clicked property string text: "Button" - color: activePalette.button; smooth: true - width: txtItem.width + 20; height: txtItem.height + 6 - border.width: 1; border.color: Qt.darker(activePalette.button); radius: 8; + signal clicked + + width: buttonLabel.width + 20; height: buttonLabel.height + 6 + smooth: true + border { width: 1; color: Qt.darker(activePalette.button) } + radius: 8 + color: activePalette.button gradient: Gradient { GradientStop { - id: topGrad; position: 0.0 - color: if (mr.pressed) { activePalette.dark } else { activePalette.light } } + position: 0.0 + color: { + if (mouseArea.pressed) + return activePalette.dark + else + return activePalette.light + } + } GradientStop { position: 1.0; color: activePalette.button } } - MouseArea { id: mr; anchors.fill: parent; onClicked: container.clicked() } + MouseArea { id: mouseArea; anchors.fill: parent; onClicked: container.clicked() } Text { - id: txtItem; text: container.text; anchors.centerIn: container; color: activePalette.buttonText + id: buttonLabel; text: container.text; anchors.centerIn: container; color: activePalette.buttonText } } diff --git a/demos/declarative/samegame/SamegameCore/Dialog.qml b/demos/declarative/samegame/SamegameCore/Dialog.qml index 7f1189e..8784348 100644 --- a/demos/declarative/samegame/SamegameCore/Dialog.qml +++ b/demos/declarative/samegame/SamegameCore/Dialog.qml @@ -2,21 +2,32 @@ import Qt 4.7 Rectangle { id: page + + property Item text: dialogText + + signal closed + function forceClose() { page.closed(); page.opacity = 0; } + function show(txt) { - myText.text = txt; + dialogText.text = txt; page.opacity = 1; } - signal closed(); - property Item text: myText - color: "white"; border.width: 1; width: myText.width + 20; height: myText.height + 40; + + width: dialogText.width + 20; height: dialogText.height + 20 + color: "white" + border.width: 1 opacity: 0 - Behavior on opacity { + + Behavior on opacity { NumberAnimation { duration: 1000 } } - Text { id: myText; anchors.centerIn: parent; text: "Hello World!" } - MouseArea { id: mr; anchors.fill: parent; onClicked: forceClose(); } + + Text { id: dialogText; anchors.centerIn: parent; text: "Hello World!" } + + MouseArea { anchors.fill: parent; onClicked: forceClose(); } } + diff --git a/demos/declarative/samegame/SamegameCore/samegame.js b/demos/declarative/samegame/SamegameCore/samegame.js index a119a88..3888381 100755 --- a/demos/declarative/samegame/SamegameCore/samegame.js +++ b/demos/declarative/samegame/SamegameCore/samegame.js @@ -1,18 +1,18 @@ /* This script file handles the game logic */ -//Note that X/Y referred to here are in game coordinates -var maxX = 10;//Nums are for gameCanvas.tileSize 40 -var maxY = 15; -var maxIndex = maxX*maxY; + +var maxColumn = 10; +var maxRow = 15; +var maxIndex = maxColumn*maxRow; var board = new Array(maxIndex); -var tileSrc = "SamegameCore/BoomBlock.qml"; +var blockSrc = "SamegameCore/BoomBlock.qml"; var scoresURL = "http://qtfx-nokia.trolltech.com.au/samegame/scores.php"; var scoresURL = ""; -var timer; -var component = createComponent(tileSrc); +var gameDuration; +var component = createComponent(blockSrc); //Index function used instead of a 2D array -function index(xIdx,yIdx) { - return xIdx + (yIdx * maxX); +function index(column,row) { + return column + (row * maxColumn); } function timeStr(msecs) { @@ -22,51 +22,48 @@ function timeStr(msecs) { return ret; } -function initBoard() +function startNewGame() { + //Delete blocks from previous game for(var i = 0; i<maxIndex; i++){ - //Delete old blocks if(board[i] != null) board[i].destroy(); } //Calculate board size - maxX = Math.floor(gameCanvas.width/gameCanvas.tileSize); - maxY = Math.floor(gameCanvas.height/gameCanvas.tileSize); - maxIndex = maxY*maxX; + maxColumn = Math.floor(gameCanvas.width/gameCanvas.blockSize); + maxRow = Math.floor(gameCanvas.height/gameCanvas.blockSize); + maxIndex = maxRow*maxColumn; //Close dialogs - scoreName.forceClose(); + nameInputDialog.forceClose(); dialog.forceClose(); - var a = new Date(); //Initialize Board board = new Array(maxIndex); gameCanvas.score = 0; - for(var xIdx=0; xIdx<maxX; xIdx++){ - for(var yIdx=0; yIdx<maxY; yIdx++){ - board[index(xIdx,yIdx)] = null; - createBlock(xIdx,yIdx); + for(var column=0; column<maxColumn; column++){ + for(var row=0; row<maxRow; row++){ + board[index(column,row)] = null; + createBlock(column,row); } } - timer = new Date(); - - //print(timer.valueOf() - a.valueOf()); + gameDuration = new Date(); } -var fillFound;//Set after a floodFill call to the number of tiles found +var fillFound;//Set after a floodFill call to the number of blocks found var floodBoard;//Set to 1 if the floodFill reaches off that node //NOTE: Be careful with vars named x,y, as the calling object's x,y are still in scope function handleClick(x,y) { - var xIdx = Math.floor(x/gameCanvas.tileSize); - var yIdx = Math.floor(y/gameCanvas.tileSize); - if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + var column = Math.floor(x/gameCanvas.blockSize); + var row = Math.floor(y/gameCanvas.blockSize); + if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(board[index(xIdx, yIdx)] == null) + if(board[index(column, row)] == null) return; - //If it's a valid tile, remove it and all connected (does nothing if it's not connected) - floodFill(xIdx,yIdx, -1); + //If it's a valid block, remove it and all connected (does nothing if it's not connected) + floodFill(column,row, -1); if(fillFound <= 0) return; gameCanvas.score += (fillFound - 1) * (fillFound - 1); @@ -74,67 +71,67 @@ function handleClick(x,y) victoryCheck(); } -function floodFill(xIdx,yIdx,type) +function floodFill(column,row,type) { - if(board[index(xIdx, yIdx)] == null) + if(board[index(column, row)] == null) return; var first = false; if(type == -1){ first = true; - type = board[index(xIdx,yIdx)].type; + type = board[index(column,row)].type; //Flood fill initialization fillFound = 0; floodBoard = new Array(maxIndex); } - if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) return; - if(floodBoard[index(xIdx, yIdx)] == 1 || (!first && type != board[index(xIdx,yIdx)].type)) + if(floodBoard[index(column, row)] == 1 || (!first && type != board[index(column,row)].type)) return; - floodBoard[index(xIdx, yIdx)] = 1; - floodFill(xIdx+1,yIdx,type); - floodFill(xIdx-1,yIdx,type); - floodFill(xIdx,yIdx+1,type); - floodFill(xIdx,yIdx-1,type); + floodBoard[index(column, row)] = 1; + floodFill(column+1,row,type); + floodFill(column-1,row,type); + floodFill(column,row+1,type); + floodFill(column,row-1,type); if(first==true && fillFound == 0) - return;//Can't remove single tiles - board[index(xIdx,yIdx)].dying = true; - board[index(xIdx,yIdx)] = null; + return;//Can't remove single blocks + board[index(column,row)].dying = true; + board[index(column,row)] = null; fillFound += 1; } function shuffleDown() { //Fall down - for(var xIdx=0; xIdx<maxX; xIdx++){ + for(var column=0; column<maxColumn; column++){ var fallDist = 0; - for(var yIdx=maxY-1; yIdx>=0; yIdx--){ - if(board[index(xIdx,yIdx)] == null){ + for(var row=maxRow-1; row>=0; row--){ + if(board[index(column,row)] == null){ fallDist += 1; }else{ if(fallDist > 0){ - var obj = board[index(xIdx,yIdx)]; - obj.targetY += fallDist * gameCanvas.tileSize; - board[index(xIdx,yIdx+fallDist)] = obj; - board[index(xIdx,yIdx)] = null; + var obj = board[index(column,row)]; + obj.targetY += fallDist * gameCanvas.blockSize; + board[index(column,row+fallDist)] = obj; + board[index(column,row)] = null; } } } } //Fall to the left fallDist = 0; - for(xIdx=0; xIdx<maxX; xIdx++){ - if(board[index(xIdx, maxY - 1)] == null){ + for(column=0; column<maxColumn; column++){ + if(board[index(column, maxRow - 1)] == null){ fallDist += 1; }else{ if(fallDist > 0){ - for(yIdx=0; yIdx<maxY; yIdx++){ - obj = board[index(xIdx,yIdx)]; + for(row=0; row<maxRow; row++){ + obj = board[index(column,row)]; if(obj == null) continue; - obj.targetX -= fallDist * gameCanvas.tileSize; - board[index(xIdx-fallDist,yIdx)] = obj; - board[index(xIdx,yIdx)] = null; + obj.targetX -= fallDist * gameCanvas.blockSize; + board[index(column-fallDist,row)] = obj; + board[index(column,row)] = null; } } } @@ -143,40 +140,38 @@ function shuffleDown() function victoryCheck() { - //awards bonuses for no tiles left + //awards bonuses for no blocks left var deservesBonus = true; - for(var xIdx=maxX-1; xIdx>=0; xIdx--) - if(board[index(xIdx, maxY - 1)] != null) + for(var column=maxColumn-1; column>=0; column--) + if(board[index(column, maxRow - 1)] != null) deservesBonus = false; if(deservesBonus) gameCanvas.score += 500; //Checks for game over - if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1))){ - timer = new Date() - timer; - //scoreName.show("You won! Please enter your name: "); - scoreName.show("You won! Please enter your name: "); - scoreName.initialWidth = scoreName.text.width + 20; - scoreName.width = scoreName.initialWidth; - scoreName.text.opacity = 0;//Just a spacer - //dialog.show("Game Over. Your score is " + gameCanvas.score); + if(deservesBonus || !(floodMoveCheck(0,maxRow-1, -1))){ + gameDuration = new Date() - gameDuration; + nameInputDialog.show("You won! Please enter your name: "); + nameInputDialog.initialWidth = nameInputDialog.text.width + 20; + nameInputDialog.width = nameInputDialog.initialWidth; + nameInputDialog.text.opacity = 0;//Just a spacer } } -//only floods up and right, to see if it can find adjacent same-typed tiles -function floodMoveCheck(xIdx, yIdx, type) +//only floods up and right, to see if it can find adjacent same-typed blocks +function floodMoveCheck(column, row, type) { - if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + if(column >= maxColumn || column < 0 || row >= maxRow || row < 0) return false; - if(board[index(xIdx, yIdx)] == null) + if(board[index(column, row)] == null) return false; - var myType = board[index(xIdx, yIdx)].type; + var myType = board[index(column, row)].type; if(type == myType) return true; - return floodMoveCheck(xIdx + 1, yIdx, myType) || - floodMoveCheck(xIdx, yIdx - 1, board[index(xIdx,yIdx)].type); + return floodMoveCheck(column + 1, row, myType) || + floodMoveCheck(column, row - 1, board[index(column,row)].type); } -function createBlock(xIdx,yIdx){ +function createBlock(column,row){ // Note that we don't wait for the component to become ready. This will // only work if the block QML is a local file. Otherwise the component will // not be ready immediately. There is a statusChanged signal on the @@ -190,14 +185,14 @@ function createBlock(xIdx,yIdx){ } dynamicObject.type = Math.floor(Math.random() * 3); dynamicObject.parent = gameCanvas; - dynamicObject.x = xIdx*gameCanvas.tileSize; - dynamicObject.targetX = xIdx*gameCanvas.tileSize; - dynamicObject.targetY = yIdx*gameCanvas.tileSize; - dynamicObject.width = gameCanvas.tileSize; - dynamicObject.height = gameCanvas.tileSize; + dynamicObject.x = column*gameCanvas.blockSize; + dynamicObject.targetX = column*gameCanvas.blockSize; + dynamicObject.targetY = row*gameCanvas.blockSize; + dynamicObject.width = gameCanvas.blockSize; + dynamicObject.height = gameCanvas.blockSize; dynamicObject.spawned = true; - board[index(xIdx,yIdx)] = dynamicObject; - }else{//isError or isLoading + board[index(column,row)] = dynamicObject; + }else{ print("error loading block component"); print(component.errorsString()); return false; @@ -211,14 +206,14 @@ function saveHighScore(name) { //OfflineStorage var db = openDatabaseSync("SameGameScores", "1.0", "Local SameGame High Scores",100); var dataStr = "INSERT INTO Scores VALUES(?, ?, ?, ?)"; - var data = [name, gameCanvas.score, maxX+"x"+maxY ,Math.floor(timer/1000)]; + var data = [name, gameCanvas.score, maxColumn+"x"+maxRow ,Math.floor(gameDuration/1000)]; db.transaction( function(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize TEXT, time NUMBER)'); tx.executeSql(dataStr, data); //Only show results for the current grid size - var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "'+maxX+"x"+maxY+'" ORDER BY score desc LIMIT 10'); + var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "'+maxColumn+"x"+maxRow+'" ORDER BY score desc LIMIT 10'); var r = "\nHIGH SCORES for this grid size\n\n" for(var i = 0; i < rs.rows.length; i++){ r += (i+1)+". " + rs.rows.item(i).name +' got ' @@ -233,7 +228,7 @@ function saveHighScore(name) { function sendHighScore(name) { var postman = new XMLHttpRequest() var postData = "name="+name+"&score="+gameCanvas.score - +"&gridSize="+maxX+"x"+maxY +"&time="+Math.floor(timer/1000); + +"&gridSize="+maxColumn+"x"+maxRow +"&time="+Math.floor(gameDuration/1000); postman.open("POST", scoresURL, true); postman.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); postman.onreadystatechange = function() { diff --git a/demos/declarative/samegame/samegame.qml b/demos/declarative/samegame/samegame.qml index 94f5c24..92201f5 100644 --- a/demos/declarative/samegame/samegame.qml +++ b/demos/declarative/samegame/samegame.qml @@ -9,85 +9,95 @@ Rectangle { SystemPalette { id: activePalette } Item { - width: parent.width; anchors.top: parent.top; anchors.bottom: toolBar.top + width: parent.width + anchors { top: parent.top; bottom: toolBar.top } Image { id: background - anchors.fill: parent; source: "SamegameCore/pics/background.png" + anchors.fill: parent + source: "SamegameCore/pics/background.png" fillMode: Image.PreserveAspectCrop - smooth: true } Item { id: gameCanvas property int score: 0 - property int tileSize: 40 + property int blockSize: 40 z: 20; anchors.centerIn: parent - width: parent.width - (parent.width % tileSize); - height: parent.height - (parent.height % tileSize); + width: parent.width - (parent.width % blockSize); + height: parent.height - (parent.height % blockSize); MouseArea { - id: gameMR anchors.fill: parent; onClicked: Logic.handleClick(mouse.x,mouse.y); } } } Dialog { id: dialog; anchors.centerIn: parent; z: 21 } + Dialog { - id: scoreName; anchors.centerIn: parent; z: 22; + id: nameInputDialog + property int initialWidth: 0 - Behavior on width {NumberAnimation{} enabled: initialWidth!=0} + + anchors.centerIn: parent + z: 22; + + Behavior on width { + NumberAnimation {} + enabled: initialWidth != 0 + } + Text { - id: spacer - anchors.left: scoreName.left - anchors.leftMargin: 20 - anchors.verticalCenter: parent.verticalCenter + id: dialogText + anchors { left: nameInputDialog.left; leftMargin: 20; verticalCenter: parent.verticalCenter } text: "You won! Please enter your name: " } + TextInput { - id: editor + id: nameInputText + anchors { verticalCenter: parent.verticalCenter; left: dialogText.right } + focus: true + onTextChanged: { - var newWidth = editor.width + spacer.width + 40; - if((newWidth > scoreName.width && newWidth < screen.width) - || (scoreName.width > scoreName.initialWidth)) - scoreName.width = newWidth; + var newWidth = nameInputText.width + dialogText.width + 40; + if ( (newWidth > nameInputDialog.width && newWidth < screen.width) + || (nameInputDialog.width > nameInputDialog.initialWidth) ) + nameInputDialog.width = newWidth; } onAccepted: { - if(scoreName.opacity==1&&editor.text!="") - Logic.saveHighScore(editor.text); - scoreName.forceClose(); + if (nameInputDialog.opacity == 1 && nameInputText.text != "") + Logic.saveHighScore(nameInputText.text); + nameInputDialog.forceClose(); } - anchors.verticalCenter: parent.verticalCenter - focus: true - anchors.left: spacer.right } } Rectangle { id: toolBar + width: parent.width; height: 32 color: activePalette.window - height: 32; width: parent.width anchors.bottom: screen.bottom Button { - id: btnA; text: "New Game"; onClicked: Logic.initBoard(); - anchors.left: parent.left; anchors.leftMargin: 3 - anchors.verticalCenter: parent.verticalCenter + id: newGameButton + anchors { left: parent.left; leftMargin: 3; verticalCenter: parent.verticalCenter } + text: "New Game" + onClicked: Logic.startNewGame() } Button { - id: btnB; text: "Quit"; onClicked: Qt.quit(); - anchors.left: btnA.right; anchors.leftMargin: 3 - anchors.verticalCenter: parent.verticalCenter + text: "Quit" + anchors { left: newGameButton.right; leftMargin: 3; verticalCenter: parent.verticalCenter } + onClicked: Qt.quit(); } Text { id: score - text: "Score: " + gameCanvas.score; font.bold: true - anchors.right: parent.right; anchors.rightMargin: 3 - anchors.verticalCenter: parent.verticalCenter + anchors { right: parent.right; rightMargin: 3; verticalCenter: parent.verticalCenter } + text: "Score: " + gameCanvas.score + font.bold: true color: activePalette.windowText } } |