diff options
author | Qt Continuous Integration System <qt-info@nokia.com> | 2010-04-13 02:24:13 (GMT) |
---|---|---|
committer | Qt Continuous Integration System <qt-info@nokia.com> | 2010-04-13 02:24:13 (GMT) |
commit | 3e94b3a77affeef80544e48949e018ced4e0d96e (patch) | |
tree | 621251f4fd85ced80d80e15f8d120e90bc095585 /examples/declarative/tutorials | |
parent | e9a0067ef4b272f1893522959dc15561970590ea (diff) | |
parent | ff0020481398e7c109973949260a42711c4cdcdc (diff) | |
download | Qt-3e94b3a77affeef80544e48949e018ced4e0d96e.zip Qt-3e94b3a77affeef80544e48949e018ced4e0d96e.tar.gz Qt-3e94b3a77affeef80544e48949e018ced4e0d96e.tar.bz2 |
Merge branch '4.7' of scm.dev.nokia.troll.no:qt/qt-qml into 4.7-integration
* '4.7' of scm.dev.nokia.troll.no:qt/qt-qml: (147 commits)
doc fixes
Don't test XmlListModel examples on platforms without QtXmlPatterns
More error output for QML_IMPORT_TRACE=1
Fix declarative examples autotest, avoid using native separators
Doc: update Rectangle smooth painting screenshot.
Add some TextInput properties and methods
Remove GraphicsObjectContainer from the documentation.
import Qt 4.6 -> import Qt 4.7
Move documentation code to snippet.
Test fix.
Fix test
Fix test on QWS.
Fix PathView crash.
Make bindings dump more useful
Improve font value type documentation.
coding conventions
Doc fixes
Warning
Read Maemo orientation at startup
Exclude gestures from examples autotest
...
Diffstat (limited to 'examples/declarative/tutorials')
22 files changed, 321 insertions, 355 deletions
diff --git a/examples/declarative/tutorials/helloworld/Cell.qml b/examples/declarative/tutorials/helloworld/Cell.qml index 9249ffe..1e52a67 100644 --- a/examples/declarative/tutorials/helloworld/Cell.qml +++ b/examples/declarative/tutorials/helloworld/Cell.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 //![1] Item { diff --git a/examples/declarative/tutorials/helloworld/tutorial1.qml b/examples/declarative/tutorials/helloworld/tutorial1.qml index 93d3c34..5e27b45 100644 --- a/examples/declarative/tutorials/helloworld/tutorial1.qml +++ b/examples/declarative/tutorials/helloworld/tutorial1.qml @@ -1,6 +1,6 @@ //![0] //![3] -import Qt 4.6 +import Qt 4.7 //![3] //![1] diff --git a/examples/declarative/tutorials/helloworld/tutorial2.qml b/examples/declarative/tutorials/helloworld/tutorial2.qml index 38447e2..085efa4 100644 --- a/examples/declarative/tutorials/helloworld/tutorial2.qml +++ b/examples/declarative/tutorials/helloworld/tutorial2.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Rectangle { id: page diff --git a/examples/declarative/tutorials/helloworld/tutorial3.qml b/examples/declarative/tutorials/helloworld/tutorial3.qml index d851c49..4bf4970 100644 --- a/examples/declarative/tutorials/helloworld/tutorial3.qml +++ b/examples/declarative/tutorials/helloworld/tutorial3.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Rectangle { id: page diff --git a/examples/declarative/tutorials/samegame/samegame1/Block.qml b/examples/declarative/tutorials/samegame/samegame1/Block.qml index f133b17..7cf819b 100644 --- a/examples/declarative/tutorials/samegame/samegame1/Block.qml +++ b/examples/declarative/tutorials/samegame/samegame1/Block.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Item { id:block diff --git a/examples/declarative/tutorials/samegame/samegame1/Button.qml b/examples/declarative/tutorials/samegame/samegame1/Button.qml index 5e28da7..8ad7c0f 100644 --- a/examples/declarative/tutorials/samegame/samegame1/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame1/Button.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Rectangle { id: container @@ -8,20 +8,21 @@ Rectangle { property string text: "Button" color: activePalette.button; smooth: true - width: txtItem.width + 20; height: txtItem.height + 6 + width: buttonLabel.width + 20; height: buttonLabel.height + 6 border.width: 1; border.color: Qt.darker(activePalette.button); radius: 8; gradient: Gradient { GradientStop { - id: topGrad; position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + position: 0.0 + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } 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 } } //![0] diff --git a/examples/declarative/tutorials/samegame/samegame1/samegame.qml b/examples/declarative/tutorials/samegame/samegame1/samegame.qml index 006b926..ae881ba 100644 --- a/examples/declarative/tutorials/samegame/samegame1/samegame.qml +++ b/examples/declarative/tutorials/samegame/samegame1/samegame.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Rectangle { id: screen @@ -24,7 +24,7 @@ Rectangle { anchors.bottom: screen.bottom Button { - id: btnA; text: "New Game"; onClicked: console.log("Starting a new game..."); + text: "New Game"; onClicked: console.log("Starting a new game..."); anchors.left: parent.left; anchors.leftMargin: 3 anchors.verticalCenter: parent.verticalCenter } diff --git a/examples/declarative/tutorials/samegame/samegame2/Block.qml b/examples/declarative/tutorials/samegame/samegame2/Block.qml index e4b3354..44ff5d7 100644 --- a/examples/declarative/tutorials/samegame/samegame2/Block.qml +++ b/examples/declarative/tutorials/samegame/samegame2/Block.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 Item { id:block diff --git a/examples/declarative/tutorials/samegame/samegame2/Button.qml b/examples/declarative/tutorials/samegame/samegame2/Button.qml index a7853d4..cf4c61b 100644 --- a/examples/declarative/tutorials/samegame/samegame2/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame2/Button.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 Rectangle { id: container @@ -7,19 +7,20 @@ Rectangle { property string text: "Button" color: activePalette.button; smooth: true - width: txtItem.width + 20; height: txtItem.height + 6 + width: buttonLabel.width + 20; height: buttonLabel.height + 6 border.width: 1; border.color: Qt.darker(activePalette.button); radius: 8; gradient: Gradient { GradientStop { - id: topGrad; position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + position: 0.0 + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } 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/examples/declarative/tutorials/samegame/samegame2/samegame.js b/examples/declarative/tutorials/samegame/samegame2/samegame.js index 0ec3a8b..9809c1d 100644 --- a/examples/declarative/tutorials/samegame/samegame2/samegame.js +++ b/examples/declarative/tutorials/samegame/samegame2/samegame.js @@ -1,63 +1,59 @@ //![0] -//Note that X/Y referred to here are in game coordinates -var maxX = 10;//Nums are for tileSize 40 -var maxY = 15; -var tileSize = 40; -var maxIndex = maxX*maxY; +var blockSize = 40; +var maxColumn = 10; +var maxRow = 15; +var maxIndex = maxColumn * maxRow; var board = new Array(maxIndex); -var tileSrc = "Block.qml"; var component; //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 initBoard() -{ - //Delete old blocks - for(var i = 0; i<maxIndex; i++){ - if(board[i] != null) +function startNewGame() { + //Delete blocks from previous game + for (var i = 0; i < maxIndex; i++) { + if (board[i] != null) board[i].destroy(); } //Calculate board size - maxX = Math.floor(background.width/tileSize); - maxY = Math.floor(background.height/tileSize); - maxIndex = maxY*maxX; + maxColumn = Math.floor(background.width / blockSize); + maxRow = Math.floor(background.height / blockSize); + maxIndex = maxRow * maxColumn; //Initialize Board board = new Array(maxIndex); - 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); } } } -function createBlock(xIdx,yIdx){ - if(component==null) - component = createComponent(tileSrc); +function createBlock(column, row) { + if (component == null) + component = createComponent("Block.qml"); - // 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 - // component you could use if you want to wait to load remote files. - if(component.isReady){ + // Note that if Block.qml was not a local file, component.isReady would be + // false and we should wait for the component's statusChanged() signal to + // know when the file is downloaded and fully loaded before calling createObject(). + if (component.isReady) { var dynamicObject = component.createObject(); - if(dynamicObject == null){ + if (dynamicObject == null) { print("error creating block"); print(component.errorsString()); return false; } dynamicObject.parent = background; - dynamicObject.x = xIdx*tileSize; - dynamicObject.y = yIdx*tileSize; - dynamicObject.width = tileSize; - dynamicObject.height = tileSize; - board[index(xIdx,yIdx)] = dynamicObject; - }else{//isError or isLoading + dynamicObject.x = column * blockSize; + dynamicObject.y = row * blockSize; + dynamicObject.width = blockSize; + dynamicObject.height = blockSize; + board[index(column, row)] = dynamicObject; + } else { print("error loading block component"); print(component.errorsString()); return false; @@ -65,3 +61,4 @@ function createBlock(xIdx,yIdx){ return true; } //![0] + diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.qml b/examples/declarative/tutorials/samegame/samegame2/samegame.qml index 89d8035..e0706c2 100644 --- a/examples/declarative/tutorials/samegame/samegame2/samegame.qml +++ b/examples/declarative/tutorials/samegame/samegame2/samegame.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 //![2] import "samegame.js" as SameGame //![2] @@ -27,7 +27,7 @@ Rectangle { //![1] Button { - id: btnA; text: "New Game"; onClicked: SameGame.initBoard(); + text: "New Game"; onClicked: SameGame.startNewGame(); anchors.left: parent.left; anchors.leftMargin: 3 anchors.verticalCenter: parent.verticalCenter } diff --git a/examples/declarative/tutorials/samegame/samegame3/Block.qml b/examples/declarative/tutorials/samegame/samegame3/Block.qml index 7620104..bb48ac8 100644 --- a/examples/declarative/tutorials/samegame/samegame3/Block.qml +++ b/examples/declarative/tutorials/samegame/samegame3/Block.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Item { id:block diff --git a/examples/declarative/tutorials/samegame/samegame3/Button.qml b/examples/declarative/tutorials/samegame/samegame3/Button.qml index a7853d4..cf4c61b 100644 --- a/examples/declarative/tutorials/samegame/samegame3/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame3/Button.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 Rectangle { id: container @@ -7,19 +7,20 @@ Rectangle { property string text: "Button" color: activePalette.button; smooth: true - width: txtItem.width + 20; height: txtItem.height + 6 + width: buttonLabel.width + 20; height: buttonLabel.height + 6 border.width: 1; border.color: Qt.darker(activePalette.button); radius: 8; gradient: Gradient { GradientStop { - id: topGrad; position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + position: 0.0 + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } 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/examples/declarative/tutorials/samegame/samegame3/Dialog.qml b/examples/declarative/tutorials/samegame/samegame3/Dialog.qml index 966f85a..a76b517 100644 --- a/examples/declarative/tutorials/samegame/samegame3/Dialog.qml +++ b/examples/declarative/tutorials/samegame/samegame3/Dialog.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 Rectangle { id: page @@ -8,16 +8,16 @@ Rectangle { page.opacity = 0; } function show(txt) { - myText.text = txt; + dialogText.text = txt; page.opacity = 1; } signal closed(); - color: "white"; border.width: 1; width: myText.width + 20; height: 60; + color: "white"; border.width: 1; width: dialogText.width + 20; height: dialogText.height + 20; opacity: 0 Behavior on opacity { NumberAnimation { duration: 1000 } } - Text { id: myText; anchors.centerIn: parent; text: "Hello World!" } - MouseArea { id: mouseArea; anchors.fill: parent; onClicked: forceClose(); } + Text { id: dialogText; anchors.centerIn: parent; text: "Hello World!" } + MouseArea { anchors.fill: parent; onClicked: forceClose(); } } //![0] diff --git a/examples/declarative/tutorials/samegame/samegame3/samegame.js b/examples/declarative/tutorials/samegame/samegame3/samegame.js index 33449fa..c12def7 100644 --- a/examples/declarative/tutorials/samegame/samegame3/samegame.js +++ b/examples/declarative/tutorials/samegame/samegame3/samegame.js @@ -1,23 +1,20 @@ /* This script file handles the game logic */ -//Note that X/Y referred to here are in game coordinates -var maxX = 10;//Nums are for 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 = "Block.qml"; var component; //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 initBoard() -{ +function startNewGame() { //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 dialog.forceClose(); @@ -25,37 +22,36 @@ function initBoard() //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); } } } -function createBlock(xIdx,yIdx){ - if(component==null) - component = createComponent(tileSrc); +function createBlock(column, row) { + if (component == null) + component = createComponent("Block.qml"); - // 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 - // component you could use if you want to wait to load remote files. - if(component.isReady){ + // Note that if Block.qml was not a local file, component.isReady would be + // false and we should wait for the component's statusChanged() signal to + // know when the file is downloaded and fully loaded before calling createObject(). + if (component.isReady) { var dynamicObject = component.createObject(); - if(dynamicObject == null){ + if (dynamicObject == null) { print("error creating block"); print(component.errorsString()); return false; } dynamicObject.type = Math.floor(Math.random() * 3); dynamicObject.parent = gameCanvas; - dynamicObject.x = xIdx*gameCanvas.tileSize; - dynamicObject.y = yIdx*gameCanvas.tileSize; - dynamicObject.width = gameCanvas.tileSize; - dynamicObject.height = gameCanvas.tileSize; - board[index(xIdx,yIdx)] = dynamicObject; - }else{//isError or isLoading + dynamicObject.x = column * gameCanvas.blockSize; + dynamicObject.y = row * gameCanvas.blockSize; + dynamicObject.width = gameCanvas.blockSize; + dynamicObject.height = gameCanvas.blockSize; + board[index(column, row)] = dynamicObject; + } else { print("error loading block component"); print(component.errorsString()); return false; @@ -63,21 +59,21 @@ function createBlock(xIdx,yIdx){ return true; } -var fillFound;//Set after a floodFill call to the number of tiles 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 +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 //![1] -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) +function handleClick(xPos, yPos) { + var column = Math.floor(xPos / gameCanvas.blockSize); + var row = Math.floor(yPos / 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(fillFound <= 0) + //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); shuffleDown(); @@ -85,67 +81,65 @@ function handleClick(x,y) } //![1] -function floodFill(xIdx,yIdx,type) -{ - if(board[index(xIdx, yIdx)] == null) +function floodFill(column, row, type) { + if (board[index(column, row)] == null) return; var first = false; - if(type == -1){ + 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); - if(first==true && fillFound == 0) - return;//Can't remove single tiles - board[index(xIdx,yIdx)].opacity = 0; - board[index(xIdx,yIdx)] = null; + 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 blocks + board[index(column, row)].opacity = 0; + board[index(column, row)] = null; fillFound += 1; } -function shuffleDown() -{ +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.y += fallDist * gameCanvas.tileSize; - board[index(xIdx,yIdx+fallDist)] = obj; - board[index(xIdx,yIdx)] = null; + } else { + if (fallDist > 0) { + var obj = board[index(column, row)]; + obj.y += fallDist * gameCanvas.blockSize; + board[index(column, row + fallDist)] = obj; + board[index(column, row)] = null; } } } } //Fall to the left var fallDist = 0; - for(var xIdx=0; xIdx<maxX; xIdx++){ - if(board[index(xIdx, maxY - 1)] == null){ + for (var column = 0; column < maxColumn; column++) { + if (board[index(column, maxRow - 1)] == null) { fallDist += 1; - }else{ - if(fallDist > 0){ - for(var yIdx=0; yIdx<maxY; yIdx++){ - var obj = board[index(xIdx,yIdx)]; - if(obj == null) + } else { + if (fallDist > 0) { + for (var row = 0; row < maxRow; row++) { + var obj = board[index(column, row)]; + if (obj == null) continue; - obj.x -= fallDist * gameCanvas.tileSize; - board[index(xIdx-fallDist,yIdx)] = obj; - board[index(xIdx,yIdx)] = null; + obj.x -= fallDist * gameCanvas.blockSize; + board[index(column - fallDist, row)] = obj; + board[index(column, row)] = null; } } } @@ -153,31 +147,30 @@ function shuffleDown() } //![2] -function victoryCheck() -{ - //awards bonuses for no tiles left +function victoryCheck() { + //Award bonus points if no blocks left var deservesBonus = true; - for(var xIdx=maxX-1; xIdx>=0; xIdx--) - if(board[index(xIdx, maxY - 1)] != null) - deservesBonus = false; - if(deservesBonus) + 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))) + + //Check whether game has finished + if (deservesBonus || !(floodMoveCheck(0, maxRow - 1, -1))) dialog.show("Game Over. Your score is " + gameCanvas.score); } //![2] -//only floods up and right, to see if it can find adjacent same-typed tiles -function floodMoveCheck(xIdx, yIdx, type) -{ - if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) +//only floods up and right, to see if it can find adjacent same-typed blocks +function floodMoveCheck(column, row, type) { + 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; - if(type == myType) + 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); } + diff --git a/examples/declarative/tutorials/samegame/samegame3/samegame.qml b/examples/declarative/tutorials/samegame/samegame3/samegame.qml index db25e24..cdf99d7 100644 --- a/examples/declarative/tutorials/samegame/samegame3/samegame.qml +++ b/examples/declarative/tutorials/samegame/samegame3/samegame.qml @@ -1,5 +1,5 @@ //![0] -import Qt 4.6 +import Qt 4.7 import "samegame.js" as SameGame Rectangle { @@ -21,11 +21,11 @@ Rectangle { 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 { anchors.fill: parent; onClicked: SameGame.handleClick(mouse.x,mouse.y); @@ -45,7 +45,7 @@ Rectangle { anchors.bottom: screen.bottom Button { - id: btnA; text: "New Game"; onClicked: SameGame.initBoard(); + text: "New Game"; onClicked: SameGame.startNewGame(); anchors.left: parent.left; anchors.leftMargin: 3 anchors.verticalCenter: parent.verticalCenter } diff --git a/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml b/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml index e50aae0..243df75 100644 --- a/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml +++ b/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 import Qt.labs.particles 1.0 Item { id:block @@ -9,8 +9,8 @@ Item { id:block property int targetX: 0 property int targetY: 0 - SpringFollow on x { source: targetX; spring: 2; damping: 0.2; enabled: spawned } - SpringFollow on y { source: targetY; spring: 2; damping: 0.2 } + SpringFollow on x { to: targetX; spring: 2; damping: 0.2; enabled: spawned } + SpringFollow on y { to: targetY; spring: 2; damping: 0.2 } //![1] //![2] diff --git a/examples/declarative/tutorials/samegame/samegame4/content/Button.qml b/examples/declarative/tutorials/samegame/samegame4/content/Button.qml index a7853d4..cf4c61b 100644 --- a/examples/declarative/tutorials/samegame/samegame4/content/Button.qml +++ b/examples/declarative/tutorials/samegame/samegame4/content/Button.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 Rectangle { id: container @@ -7,19 +7,20 @@ Rectangle { property string text: "Button" color: activePalette.button; smooth: true - width: txtItem.width + 20; height: txtItem.height + 6 + width: buttonLabel.width + 20; height: buttonLabel.height + 6 border.width: 1; border.color: Qt.darker(activePalette.button); radius: 8; gradient: Gradient { GradientStop { - id: topGrad; position: 0.0 - color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } } + position: 0.0 + color: if (mouseArea.pressed) { activePalette.dark } else { activePalette.light } + } GradientStop { position: 1.0; color: activePalette.button } } 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/examples/declarative/tutorials/samegame/samegame4/content/Dialog.qml b/examples/declarative/tutorials/samegame/samegame4/content/Dialog.qml index fc83e39..15f5b19 100644 --- a/examples/declarative/tutorials/samegame/samegame4/content/Dialog.qml +++ b/examples/declarative/tutorials/samegame/samegame4/content/Dialog.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 Rectangle { id: page @@ -7,15 +7,15 @@ Rectangle { page.opacity = 0; } function show(txt) { - myText.text = txt; + dialogText.text = txt; page.opacity = 1; } signal closed(); - color: "white"; border.width: 1; width: myText.width + 20; height: 60; + color: "white"; border.width: 1; width: dialogText.width + 20; height: dialogText.height + 20; opacity: 0 Behavior on opacity { NumberAnimation { duration: 1000 } } - Text { id: myText; anchors.centerIn: parent; text: "Hello World!" } - MouseArea { id: mouseArea; anchors.fill: parent; onClicked: forceClose(); } + Text { id: dialogText; anchors.centerIn: parent; text: "Hello World!" } + MouseArea { anchors.fill: parent; onClicked: forceClose(); } } diff --git a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js index 2a0d718..47985de 100755 --- a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js +++ b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js @@ -1,245 +1,219 @@ /* 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 = "content/BoomBlock.qml"; -//var scoresURL = "http://qtfx-nokia.trolltech.com.au/samegame/scores.php"; +var component; var scoresURL = ""; -var timer; -var component = createComponent(tileSrc); +var gameDuration; //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) { - var secs = Math.floor(msecs/1000); - var m = Math.floor(secs/60); - var ret = "" + m + "m " + (secs%60) + "s"; - return ret; -} - -function getTileSize() -{ - return tileSize; -} - -function initBoard() -{ - for(var i = 0; i<maxIndex; i++){ - //Delete old blocks - if(board[i] != null) +function startNewGame() { + for (var i = 0; i < maxIndex; i++) { + //Delete blocks from previous game + 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(); +} + +function createBlock(column, row) { + if (component == null) + component = createComponent("content/BoomBlock.qml"); + + // Note that if Block.qml was not a local file, component.isReady would be + // false and we should wait for the component's statusChanged() signal to + // know when the file is downloaded and fully loaded before calling createObject(). + if (component.isReady) { + var dynamicObject = component.createObject(); + if (dynamicObject == null) { + print("error creating block"); + print(component.errorsString()); + return false; + } + dynamicObject.type = Math.floor(Math.random() * 3); + dynamicObject.parent = gameCanvas; + 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(column, row)] = dynamicObject; + } else { + print("error loading block component"); + print(component.errorsString()); + return false; + } + return true; } -var fillFound;//Set after a floodFill call to the number of tiles 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 fillFound; +//Set after a floodFill call to the number of blocks found +var floodBoard; +//Set to 1 if the floodFill reaches off that node +function handleClick(xPos, yPos) { + var column = Math.floor(xPos / gameCanvas.blockSize); + var row = Math.floor(yPos / 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(fillFound <= 0) + //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); shuffleDown(); victoryCheck(); } -function floodFill(xIdx,yIdx,type) -{ - if(board[index(xIdx, yIdx)] == null) +function floodFill(column, row, type) { + if (board[index(column, row)] == null) return; var first = false; - if(type == -1){ + 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); - if(first==true && fillFound == 0) - return;//Can't remove single tiles - board[index(xIdx,yIdx)].dying = true; - board[index(xIdx,yIdx)] = null; + 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 blocks + board[index(column, row)].dying = true; + board[index(column, row)] = null; fillFound += 1; } -function shuffleDown() -{ +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; + } else { + if (fallDist > 0) { + 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)]; - if(obj == null) + } else { + if (fallDist > 0) { + 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; } } } } } -function victoryCheck() -{ - //awards bonuses for no tiles left +function victoryCheck() { + //Award bonus points if no blocks left var deservesBonus = true; - for(var xIdx=maxX-1; xIdx>=0; xIdx--) - if(board[index(xIdx, maxY - 1)] != null) - deservesBonus = false; - if(deservesBonus) + 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: "); - //dialog.show("Game Over. Your score is " + gameCanvas.score); + + //Check whether game has finished + if (deservesBonus || !(floodMoveCheck(0, maxRow - 1, -1))) { + gameDuration = new Date() - gameDuration; + nameInputDialog.show("You won! Please enter your name: "); } } -//only floods up and right, to see if it can find adjacent same-typed tiles -function floodMoveCheck(xIdx, yIdx, type) -{ - if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) +//only floods up and right, to see if it can find adjacent same-typed blocks +function floodMoveCheck(column, row, type) { + 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; - if(type == myType) + 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); -} - -function createBlock(xIdx,yIdx){ - // 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 - // component you could use if you want to wait to load remote files. - if(component.isReady){ - var dynamicObject = component.createObject(); - if(dynamicObject == null){ - print("error creating block"); - print(component.errorsString()); - return false; - } - 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.spawned = true; - board[index(xIdx,yIdx)] = dynamicObject; - }else{//isError or isLoading - print("error loading block component"); - print(component.errorsString()); - return false; - } - return true; + return floodMoveCheck(column + 1, row, myType) || floodMoveCheck(column, row - 1, board[index(column, row)].type); } //![2] function saveHighScore(name) { - if(scoresURL!="") + if (scoresURL != "") sendHighScore(name); - //OfflineStorage - var db = openDatabaseSync("SameGameScores", "1.0", "Local SameGame High Scores",100); + + 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)]; - db.transaction( - function(tx) { - tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize TEXT, time NUMBER)'); - tx.executeSql(dataStr, data); - - var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "12x17" ORDER BY score desc LIMIT 10'); - var r = "\nHIGH SCORES for a standard sized grid\n\n" - for(var i = 0; i < rs.rows.length; i++){ - r += (i+1)+". " + rs.rows.item(i).name +' got ' - + rs.rows.item(i).score + ' points in ' - + rs.rows.item(i).time + ' seconds.\n'; - } - dialog.show(r); + 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); + + var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = "12x17" ORDER BY score desc LIMIT 10'); + var r = "\nHIGH SCORES for a standard sized grid\n\n" + for (var i = 0; i < rs.rows.length; i++) { + r += (i + 1) + ". " + rs.rows.item(i).name + ' got ' + rs.rows.item(i).score + ' points in ' + rs.rows.item(i).time + ' seconds.\n'; } - ); + dialog.show(r); + }); } //![2] //![1] function sendHighScore(name) { var postman = new XMLHttpRequest() - var postData = "name="+name+"&score="+gameCanvas.score - +"&gridSize="+maxX+"x"+maxY +"&time="+Math.floor(timer/1000); + var postData = "name=" + name + "&score=" + gameCanvas.score + "&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() { + postman.onreadystatechange = function() { if (postman.readyState == postman.DONE) { dialog.show("Your score has been uploaded."); } @@ -247,3 +221,4 @@ function sendHighScore(name) { postman.send(postData); } //![1] + diff --git a/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php b/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php index 3cceb2d..daf480e 100755 --- a/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php +++ b/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php @@ -8,10 +8,7 @@ $time = $_POST["time"]; if($name == "") $name = "Anonymous"; - //if($grid != "10x10"){ - //Need a standard, so as to reject others? - //} - $file = fopen("score_data.xml", "a"); #It's XML. Happy? + $file = fopen("score_data.xml", "a"); $ret = fwrite($file, "<record><score>". $score . "</score><name>" . $name . "</name><gridSize>" . $grid . "</gridSize><seconds>" . $time . "</seconds></record>\n"); diff --git a/examples/declarative/tutorials/samegame/samegame4/samegame.qml b/examples/declarative/tutorials/samegame/samegame4/samegame.qml index 090496d..5d5c81f 100644 --- a/examples/declarative/tutorials/samegame/samegame4/samegame.qml +++ b/examples/declarative/tutorials/samegame/samegame4/samegame.qml @@ -1,4 +1,4 @@ -import Qt 4.6 +import Qt 4.7 import "content" import "content/samegame.js" as SameGame @@ -20,11 +20,11 @@ Rectangle { 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 % getTileSize()); - height: parent.height - (parent.height % getTileSize()); + width: parent.width - (parent.width % blockSize); + height: parent.height - (parent.height % blockSize); MouseArea { anchors.fill: parent; onClicked: SameGame.handleClick(mouse.x,mouse.y); @@ -36,7 +36,7 @@ Rectangle { //![0] Dialog { - id: scoreName; anchors.centerIn: parent; z: 22; + id: nameInputDialog; anchors.centerIn: parent; z: 22; Text { id: spacer opacity: 0 @@ -45,9 +45,9 @@ Rectangle { TextInput { id: editor onAccepted: { - if(scoreName.opacity==1&&editor.text!="") - saveHighScore(editor.text); - scoreName.forceClose(); + if(nameInputDialog.opacity==1&&editor.text!="") + SameGame.saveHighScore(editor.text); + nameInputDialog.forceClose(); } anchors.verticalCenter: parent.verticalCenter width: 72; focus: true @@ -63,7 +63,7 @@ Rectangle { anchors.bottom: screen.bottom Button { - id: btnA; text: "New Game"; onClicked: {SameGame.initBoard();} + text: "New Game"; onClicked: SameGame.startNewGame(); anchors.left: parent.left; anchors.leftMargin: 3 anchors.verticalCenter: parent.verticalCenter } |