diff options
66 files changed, 1608 insertions, 62 deletions
diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js index e42b7cc..ba2346f 100755 --- a/demos/declarative/samegame/content/samegame.js +++ b/demos/declarative/samegame/content/samegame.js @@ -1,8 +1,7 @@ /* 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 maxX = 10;//Nums are for gameCanvas.tileSize 40 var maxY = 15; -var tileSize = 40; var maxIndex = maxX*maxY; var board = new Array(maxIndex); var tileSrc = "content/BoomBlock.qml"; @@ -30,8 +29,9 @@ function initBoard() board[i].destroy(); } - maxX = Math.floor(gameCanvas.width/tileSize); - maxY = Math.floor(gameCanvas.height/tileSize); + //Calculate board size + maxX = Math.floor(gameCanvas.width/gameCanvas.tileSize); + maxY = Math.floor(gameCanvas.height/gameCanvas.tileSize); maxIndex = maxY*maxX; //Close dialogs @@ -55,8 +55,8 @@ 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) { - xIdx = Math.floor(x/tileSize); - yIdx = Math.floor(y/tileSize); + xIdx = Math.floor(x/gameCanvas.tileSize); + yIdx = Math.floor(y/gameCanvas.tileSize); if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) return; if(board[index(xIdx, yIdx)] == null) @@ -110,7 +110,7 @@ function shuffleDown() }else{ if(fallDist > 0){ obj = board[index(xIdx,yIdx)]; - obj.targetY += fallDist * tileSize; + obj.targetY += fallDist * gameCanvas.tileSize; board[index(xIdx,yIdx+fallDist)] = obj; board[index(xIdx,yIdx)] = null; } @@ -128,7 +128,7 @@ function shuffleDown() obj = board[index(xIdx,yIdx)]; if(obj == null) continue; - obj.targetX -= fallDist * tileSize; + obj.targetX -= fallDist * gameCanvas.tileSize; board[index(xIdx-fallDist,yIdx)] = obj; board[index(xIdx,yIdx)] = null; } @@ -187,11 +187,11 @@ function createBlock(xIdx,yIdx){ } dynamicObject.type = Math.floor(Math.random() * 3); dynamicObject.parent = gameCanvas; - dynamicObject.x = xIdx*tileSize; - dynamicObject.targetX = xIdx*tileSize; - dynamicObject.targetY = yIdx*tileSize; - dynamicObject.width = tileSize; - dynamicObject.height = tileSize; + 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 diff --git a/demos/declarative/samegame/samegame.qml b/demos/declarative/samegame/samegame.qml index 92d85f3..709f4e9 100644 --- a/demos/declarative/samegame/samegame.qml +++ b/demos/declarative/samegame/samegame.qml @@ -21,6 +21,7 @@ Rectangle { Item { id: gameCanvas property int score: 0 + property int tileSize: 40 z: 20; anchors.centerIn: parent width: parent.width - (parent.width % tileSize); diff --git a/doc/src/declarative/advtutorial.qdoc b/doc/src/declarative/advtutorial.qdoc new file mode 100644 index 0000000..c8075de --- /dev/null +++ b/doc/src/declarative/advtutorial.qdoc @@ -0,0 +1,18 @@ +/*! +\page advtutorial.html +\title Advanced Tutorial + +This tutorial goes step-by-step through creating a full application using just QML. It is assumed that you already know basic QML (such as from doing the simple tutorial) and the focus is on showing how to turn that knowledge into a complete and functioning application. + +In this tutorial we recreate, step by step, the Same Game demo in $QTDIR/demos/declarative/samegame.qml. The results of the individual steps are in the $QTDIR/examples/declarative/tutorials/samegame directory. + +Tutorial chapters: + +\list +\o \l {advtutorial1}{Advanced Tutorial 1 - Basic Game Screen and Block} +\o \l {advtutorial2}{Advanced Tutorial 2 - Dynamically create the Blocks} +\o \l {advtutorial3}{Advanced Tutorial 3 - Implement the Game Logic} +\o \l {advtutorial4}{Advanced Tutorial 4 - Finishing Touches} +\endlist + +*/ diff --git a/doc/src/declarative/advtutorial1.qdoc b/doc/src/declarative/advtutorial1.qdoc new file mode 100644 index 0000000..b940986 --- /dev/null +++ b/doc/src/declarative/advtutorial1.qdoc @@ -0,0 +1,116 @@ +/*! +\page advtutorial1.html +\example declarative/tutorials/samegame/samegame1 +\title Advanced Tutorial 1 - Creating the Game canvas and block +\target advtutorial1 + +The first step is to create the items in your application. In Same Game we have a main game screen and the blocks that populate it. + +\image declarative-adv-tutorial1.png + +Here is the QML code for the basic elements. The game window: + +\code +import Qt 4.6 + +Rectangle { + id: Screen + width: 490; height: 720 + + SystemPalette { id: activePalette; colorGroup: Qt.Active } + + Item { + width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top + + Image { + id: background + anchors.fill: parent; source: "pics/background.png" + fillMode: "PreserveAspectCrop" + } + } + + Rectangle { + id: ToolBar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: Screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: print("Implement me!"); + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + id: Score + text: "Score: Who knows?"; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } +} +\endcode + +This gives you a basic game window, with room for the game canvas. A new game +button and room to display the score. The one thing you may not recognize here +is the SystemPalette item. This item provides access to the Qt system palette +and is used to make the button look more like a system button (for exact native +feel you would use a QPushButton). Since we want a fully QML button, and the Fx +primitives don't include a button, we had to write our own. Below is the code +which we wrote to do this: + +\code +import Qt 4.6 + +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: activePalette.darker(activePalette.button); radius: 8; + + gradient: Gradient { + GradientStop { + id: topGrad; position: 0.0 + color: if (mr.pressed) { activePalette.dark } else { activePalette.light } } + GradientStop { position: 1.0; color: activePalette.button } + } + + MouseRegion { id: mr; anchors.fill: parent; onClicked: Container.clicked() } + + Text { + id: txtItem; text: Container.text; anchors.centerIn: Container; color: activePalette.buttonText + } +} +\endcode +Note that this Button component was written to be fairly generic, in case we +want to use a similarly styled button later. + +And here is a simple block: +\code +import Qt 4.6 + +Item { + id:block + + Image { id: img + source: "pics/redStone.png"; + anchors.fill: parent + } +} +\endcode + +Since it doesn't do anything yet it's very simple, just an image. As the +tutorial progresses and the block starts doing things the file will become +more than just an image. Note that we've set the image to be the size of the itm. This will be used later, when we dynamically create and size the block items the image will be scaled automatically to the correct size. + +You should be familiar with all that goes on in these files so far. This is a +very basic start and doesn't move at all - next we will populate the game canvas +with some blocks. + +[\l {advtutorial.html}{Advanced Tutorial}] [Next: \l {advtutorial2}{Advanced Tutorial 2}] +*/ + diff --git a/doc/src/declarative/advtutorial2.qdoc b/doc/src/declarative/advtutorial2.qdoc new file mode 100644 index 0000000..c17f9c4 --- /dev/null +++ b/doc/src/declarative/advtutorial2.qdoc @@ -0,0 +1,113 @@ +/*! +\page advtutorial2.html +\title Advanced Tutorial 2 - Populating the Game Canvas +\target advtutorial2 + +Now that we've written some basic elements, let's start writing the game. The +first thing to do is to generate all of the blocks. Now we need to dynamically +generate all of these blocks, because you have a new, random set of blocks +every time. As they are dynamically generated every time the new game button is +clicked, as opposed to on startup, we will be dynamically generating the blocks +in the ECMA script, as opposed to using a Repeater. + +This adds enough script to justify a new file, samegame.js, the intial version +of which is shown below + +\code +//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 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 initBoard() +{ + //Calculate board size + maxX = Math.floor(background.width/tileSize); + maxY = Math.floor(background.height/tileSize); + maxIndex = maxY*maxX; + + //Initialize Board + board = new Array(maxIndex); + for(xIdx=0; xIdx<maxX; xIdx++){ + for(yIdx=0; yIdx<maxY; yIdx++){ + board[index(xIdx,yIdx)] = null; + createBlock(xIdx,yIdx); + } + } +} + +function createBlock(xIdx,yIdx){ + if(component==null) + component = createComponent(tileSrc); + + // 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){ + dynamicObject = component.createObject(); + 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 + print("error loading block component"); + print(component.errorsString()); + return false; + } + return true; +} + +\endcode + +The gist of this code is that we create the blocks dynamically, as many as will fit, and then store them in an array for future reference. The 'initBoard' function will be hooked up to the new game button soon, and should be fairly straight forward. + +The 'createBlock' function is a lot bigger, and I'll explain it block by block. +First we ensure that the component has been constructed. QML elements, including composite ones like the Block.qml that we've written, are never created directly in script. While there is a function to parse and create an arbitrary QML string, in the case where you are repeatedly creating the sme item you will want to use the createComponent function. createComponent is a built-in function in the declarative ECMAscript, and returns a component object. A component object prepares and stores a QML element (usually a composite element) for easy and efficient use. When the component is ready, you can create a new instance of the loaded QML with the createObject method. If the component is loaded remotely (over HTTP fro example) then you will have to wait for the component to finish loading before calling createObject. Since we don't wait here (the waiting is a syncronous, the component object has a signal to tell you when it's done) this code will only work if the block QML is a local file. + +As we aren't waiting for he component, the next block of code creates a game block with component.createObject. Since there could be an error in the QML file you are trying to load, success is not guaranteed. The first bit of error checkign code comes right after createObject(), to ensure that the object loaded correctly. If it did not load correctly the function returns false, but we don't have that hooked up to the main UI to indicate that something has gone wrong. Instead we print out error messages to the console, because an error here means an invalid QML file and should only happen while you are developing and testing the UI. + +Next we start to set up our dynamically created block. Because the Block.qml file is generic it needs to be placed in the main scene, and in the right place. This is why parent, x, y, width and height are set. We then store it in the board array for later use. + +Finally, we have some more error handling. You can only call createObject if the component has loaded. If it has not loaded, either it is still loading or there was an error loading (such as a missing file). Since we don't request remote files the problem is likely to be a missing or misplaced file. Again we print this to the console to aid debugging. + +You now have the code to create a field of blocks dynamically, like below: + +\image declarative-adv-tutorial2.png + +To hook this code up to the 'New Game' button, you alter it as below: + +\code +Button { + id: btnA; text: "New Game"; onClicked: initBoard() ; + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter +} +\endcode + +We have just replaced the 'onClicked: print("Implement me!")' with 'onClicked: initBoard()'. Note that in order to have the function available, you'll need to include the script in the main file, by adding a script element to it. +\code + Script { source: "samegame.js" } +\endcode + +With those two changes, and the script file, you are now dynamically creating a field of blocks you can play with. They don't do anything now though; the next chapter will add the game mechanics. + +[Previous: \l {advtutorial1}{Advanced Tutorial 1}] [\l {advtutorial.html}{Advanced Tutorial}] [Next: \l {advtutorial3}{Advanced Tutorial 3}] + +*/ diff --git a/doc/src/declarative/advtutorial3.qdoc b/doc/src/declarative/advtutorial3.qdoc new file mode 100644 index 0000000..1c50555 --- /dev/null +++ b/doc/src/declarative/advtutorial3.qdoc @@ -0,0 +1,197 @@ +/*! +\page advtutorial3.html +\title Advanced Tutorial 3 - Implementing the Game Logic +\target advtutorial3 + +To the initBoard function we added clearing the board before hand, so that clicking new game won't leave the previous game lying around in the background. To the createComponent function we have added setting the type of the block to a number between one and three - it's fundamental to the game logic that the blocks be different types if you want a fun game. + +The main change was adding the following game logic functions: +\list +\o function handleClick(x,y) +\o function floodFill(xIdx,yIdx,type) +\o function shuffleDown() +\o function victoryCheck() +\o function floodMoveCheck(xIdx, yIdx, type) +\endlist + +As this is a tutorial about QML, not game design, these functions will not be discussed in detail. The game logic here was written in script, but it could have been written in C++ and had these functions exposed just as well (in fact, probably faster). The interfacing between these funcions and QML is of interest though. Of these functions, only handleClick and victoryCheck interface closely with the QML. Those functions are shown below (the rest are still in the code for this tutorial located at $QTDIR/examples/declarative/tutorials/samegame). + +\code +function handleClick(x,y) +{ + xIdx = Math.floor(x/gameCanvas.tileSize); + yIdx = Math.floor(y/gameCanvas.tileSize); + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + return; + if(board[index(xIdx, yIdx)] == 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) + return; + gameCanvas.score += (fillFound - 1) * (fillFound - 1); + shuffleDown(); + victoryCheck(); +} + +function victoryCheck() +{ + //awards bonuses for no tiles left + deservesBonus = true; + for(xIdx=maxX-1; xIdx>=0; xIdx--) + if(board[index(xIdx, maxY - 1)] != null) + deservesBonus = false; + if(deservesBonus) + gameCanvas.score += 500; + //Checks for game over + if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1))) + dialog.show("Game Over. Your score is " + gameCanvas.score); +} +\endcode + +You'll notice them referring to the 'gameCanvas' item. This is an item that has been added to the QML for easy interfacing. It is placed next to the background image and replaces the background as the item to create the blocks in. Its code is shown below: +\code + Item { + id: gameCanvas + property int score: 0 + property int tileSize: 40 + + z: 20; anchors.centerIn: parent + width: parent.width - (parent.width % tileSize); + height: parent.height - (parent.height % tileSize); + + MouseRegion { + id: gameMR + anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y); + } + } +\endcode + +This item is the exact size of the board, contains a score property, and a mouse region for input. The blocks are now created as its children, and its size is used as the noe determining board size. Since it needs to bind its size to a multiple of tileSize, tileSize needs to be moved into a QML property and out of the script file. It can still be accessed from the script. + +The mouse region simply calls handleClick(), which deals with the input events.Should those events cause the player to score, gameCanvas.score is updated. The score display text item has also been changed to bind its text property to gamecanvas.score. Note that if score was a global variable in the samegame.js file yo ucould not bind to it. You can only bind to QML properties. + +victoryCheck() mostly just updates score. But it also pops up a dialog saying 'Game Over' when the game is over. In this example we wanted a pure-QML, animated dialog, and since the Fx primitives set doesn't contain one, we wrote our own. Below is the code for the Dialog element, note how it's designed so as to be quite usable imperatively from within the script file: +\code +import Qt 4.6 + +Rectangle { + id: page + function forceClose() { + page.closed(); + page.opacity = 0; + } + function show(txt) { + MyText.text = txt; + page.opacity = 1; + } + signal closed(); + color: "white"; border.width: 1; width: MyText.width + 20; height: 60; + opacity: 0 + opacity: Behavior { + NumberAnimation { duration: 1000 } + } + Text { id: MyText; anchors.centerIn: parent; text: "Hello World!" } + MouseRegion { id: mr; anchors.fill: parent; onClicked: forceClose(); } +} +\endcode +And this is how it's used in the main QML file: +\code + Dialog { id: dialog; anchors.centerIn: parent; z: 21 } +\endcode +Combined with the line of code in victoryCheck, this causes a dialog to appear when the game is over, informing the user of that fact. + +We now have a working game! The blocks can be clicked, the player can score, and the game can end (and then you start a new one). Below is a screenshot of what has been accomplished so far: + +\image declarative-adv-tutorial3.png + +Here is the QML code as it is now for the main file: + +\code +import Qt 4.6 + +Rectangle { + id: Screen + width: 490; height: 720 + + SystemPalette { id: activePalette; colorGroup: Qt.Active } + Script { source: "samegame.js" } + + Item { + width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top + + Image { + id: background + anchors.fill: parent; source: "pics/background.png" + fillMode: "PreserveAspectCrop" + } + + Item { + id: gameCanvas + property int score: 0 + property int tileSize: 40 + + z: 20; anchors.centerIn: parent + width: parent.width - (parent.width % tileSize); + height: parent.height - (parent.height % tileSize); + + MouseRegion { + id: gameMR + anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y); + } + } + } + + Dialog { id: dialog; anchors.centerIn: parent; z: 21 } + + Rectangle { + id: ToolBar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: Screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: initBoard(); + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + id: Score + text: "Score: " + gameCanvas.score; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } +} +\endcode + +And the code for the block: +\code +import Qt 4.6 + +Item { + id:block + property int type: 0 + + Image { id: img + source: { + if(type == 0){ + "pics/redStone.png"; + } else if(type == 1) { + "pics/blueStone.png"; + } else { + "pics/greenStone.png"; + } + } + anchors.fill: parent + } +} +\endcode + +The game works, but it's a little boring right now. Where's the smooth animated transitions? Where's the high scores? If you were a QML expert you could have written these in for the first iteration, but in this tutorial they've been saved until the next chapter - where your application becomes alive! + +[Previous: \l {advtutorial2}{Advanced Tutorial 2}] [\l {advtutorial.html}{Advanced Tutorial}] [Next: \l {advtutorial4}{Advanced Tutorial 4}] + +*/ + diff --git a/doc/src/declarative/advtutorial4.qdoc b/doc/src/declarative/advtutorial4.qdoc new file mode 100644 index 0000000..5ad1ec3 --- /dev/null +++ b/doc/src/declarative/advtutorial4.qdoc @@ -0,0 +1,120 @@ +/*! +\page advtutorial4.html +\title Advanced Tutorial 4 - Finishing Touches +\target advtutorial4 + +Now we're going to do two things to liven the game up. Animate the blocks and add a web-based high score system. + +If you compare the samegame3 directory with samegame4, you'll noticed that we've cleaned the directory structure up. We now have a lot of files, and so they've been split up into folders - the most notable one being a content folder which we've placed all the QML but the main file. + +\section2 Animated Blocks + +The most vital animations are that the blocks move fluidly around the board. QML has many tools for fluid behavior, and in this case we're going to use the Follow element. By having the script set targetX and targetY, instead of x and y directly, we can set the x and y of the block to a follow. SpringFollow is a property value source, which means that you can set a property to be one of these elements and it will automatically bind the property to the element's value. The SpringFollow's value follows another value over time, when the value it is tracking changes the SpringFollow's value will also change, but it will move smoothly there over time with a spring-like movement (based on the spring parameters specified). This is shown in the below snippet of code from Block.qml: +\code + property int targetX: 0 + property int targetY: 0 + + x: SpringFollow { source: targetX; spring: 2; damping: 0.2 } + y: SpringFollow { source: targetY; spring: 2; damping: 0.2 } +\endcode + +We also have to change the samegame.js code, so that wherever it was setting the x or y it now sets targetX and targetY (including when creating the block). This simple change is all you need to get spring moving blocks that no longer teleport around the board. If you try doing just this though, you'll notice that they now never jump from one point to another, even in the initialization! This gives an odd effect of having them all jump out of the corner (0,0) on start up. We'd rather that they fall down from the top in rows. To do this, we disable the x Follow (but not the y follow) and only enable it after we've set the x in the createBlock function. The above snippet now becomes: + +\code + property bool spawned: false + property int targetX: 0 + property int targetY: 0 + + x: SpringFollow { enabled: spawned; source: targetX; spring: 2; damping: 0.2 } + y: SpringFollow { source: targetY; spring: 2; damping: 0.2 } +\endcode + +The next-most vital animation is a smooth exit. For this animation, we'll use a Behavior element. A Behavior is also a property value source, and it is much like SpringFollow except that it doesn't model the behavior of a spring. You specify how a Behavior transitions using the standard animations. As we want the blocks to smoothly fade in and out we'll set a Behavior on the block image's opacity, like so: +\code + Image { id: img + source: { + if(type == 0){ + "pics/redStone.png"; + } else if(type == 1) { + "pics/blueStone.png"; + } else { + "pics/greenStone.png"; + } + } + opacity: 0 + opacity: Behavior { NumberAnimation { properties:"opacity"; duration: 200 } } + anchors.fill: parent + } +\endcode + +Note that the 'opacity: 0' makes it start out transparent. We could set the opacity in the script file when we create the blocks, but instead we use states (as this is useful for the next animation we'll implement). The below snippet is set on the root element of Block.qml: +\code + property bool dying: false + states: [ + State{ name: "AliveState"; when: spawned == true && dying == false + PropertyChanges { target: img; opacity: 1 } + }, State{ name: "DeathState"; when: dying == true + PropertyChanges { target: img; opacity: 0 } + } + ] +\endcode + +Now it will automatically fade in, as we set spawned to true already when implementing the block movement animations. To fade out, we set 'dying' to true instead of setting opacity to 0 when a block is destroyed (in the floodFill function). + +The least vital animations are a cool-looking particle effect when they get destroyed. First we create a Particles Element in the block, like so: +\code + Particles { id: particles + width:1; height:1; anchors.centerIn: parent; opacity: 0 + lifeSpan: 700; lifeSpanDeviation: 600; count:0; streamIn: false + angle: 0; angleDeviation: 360; velocity: 100; velocityDeviation:30 + source: { + if(type == 0){ + "pics/redStar.png"; + } else if (type == 1) { + "pics/blueStar.png"; + } else { + "pics/greenStar.png"; + } + } + } +\endcode +To fully understand this you'll want to look at the Particles element documentation, but it's important to note that count is set to zero. +We next extend the 'dying' state, which triggers the particles by setting the count to non-zero. The code for that state looks like this: +\code + State{ name: "DeathState"; when: dying == true + PropertyChanges { target: particles; count: 50 } + PropertyChanges { target: particles; opacity: 1 } + PropertyChanges { target: particles; emitting: false } // i.e. emit only once + PropertyChanges { target: img; opacity: 0 } + } +\endcode +And now the game should be beautifully animated and smooth, with a subtle (or not-so-subtle) animation added for all of the player's actions. + +\section2 Web-based High Scores + +Another extension we might want for the game is some way of storing and retriveing high scores. In this tutorial we'll show you how to integrate a web enabled high score storage into your QML application. The implementation we've done is very simple - the high score data is posted to a php script running on a server somewhere, and that server then stores it and displays it to visitors. You could request an XML or QML file from that same server, which contained and displayed the scores, but that's beyond the scope of this tutorial. + +For better high score data, we want the name and time of the player. The time is obtained in the script fairly simply, but we have to ask the player for their name. We thus re-use the dialog QML file to pop up a dialog asking for the player's name (and if they exit this dialog without entering it they have a way to opt out of posting their high score). When the dialog is closed, if the player entered their name we can send the data to the web service in the followign snippet out of the script file: +\code +function sendHighScore(name) { + var postman = new XMLHttpRequest() + var postData = "name="+name+"&score="+gameCanvas.score + +"&gridSize="+maxX+"x"+maxY +"&time="+Math.floor(timer/1000); + postman.open("POST", scoresURL, true); + postman.onreadystatechange = function() { + if (postman.readyState == postman.DONE) { + dialog.show("Your score has been uploaded."); + } + } + postman.send(postData); +} +\endcode + +This is the same XMLHttpRequest() as you'll find in browser javascript, and can be used in the same way to dynamically get XML or QML from the web service to display the high scores. We don't worry about the response here though, we just post the high score data to the web server. If it had returned a QML file (or a URL to a QML file) you could instantiate it in much the same way as you did the blocks. + +An alternate way to access and submit web-based data would be to use QML elements designed for this purpose - XmlListModel makes it very easy to fetch and display XML based data such as RSS in a QML application (see the Flickr demo for an example). + +By following this tutorial you've now ben shown how to write a fully functional application in QML, with the application logic written in a script file and with both many fluid animations and being web-enabled. Congratulations, you should now be skilled enough to write your own QML applications. + +[Previous: \l {advtutorial3}{Advanced Tutorial 3}] [\l {advtutorial.html}{Advanced Tutorial}] +*/ diff --git a/doc/src/declarative/qtdeclarative.qdoc b/doc/src/declarative/qtdeclarative.qdoc index 06cba15..f94b9f8 100644 --- a/doc/src/declarative/qtdeclarative.qdoc +++ b/doc/src/declarative/qtdeclarative.qdoc @@ -64,6 +64,7 @@ \list \o \l {qmlexamples}{Examples} \o \l {tutorial}{Tutorial: 'Hello World'} + \o \l {advtutorial.html}{Advanced Tutorial: 'Same Game'} \o \l {tutorials-declarative-contacts.html}{Tutorial: 'Introduction to QML'} \o \l {qmlforcpp}{QML For C++ Programmers} \endlist diff --git a/examples/declarative/tutorials/samegame/samegame1/Block.qml b/examples/declarative/tutorials/samegame/samegame1/Block.qml new file mode 100644 index 0000000..228ac4e --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame1/Block.qml @@ -0,0 +1,10 @@ +import Qt 4.6 + +Item { + id:block + + Image { id: img + source: "pics/redStone.png"; + anchors.fill: parent + } +} diff --git a/examples/declarative/tutorials/samegame/samegame1/Button.qml b/examples/declarative/tutorials/samegame/samegame1/Button.qml new file mode 100644 index 0000000..2354218 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame1/Button.qml @@ -0,0 +1,25 @@ +import Qt 4.6 + +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: activePalette.darker(activePalette.button); radius: 8; + + gradient: Gradient { + GradientStop { + id: topGrad; position: 0.0 + color: if (mr.pressed) { activePalette.dark } else { activePalette.light } } + GradientStop { position: 1.0; color: activePalette.button } + } + + MouseRegion { id: mr; anchors.fill: parent; onClicked: Container.clicked() } + + Text { + id: txtItem; text: Container.text; anchors.centerIn: Container; color: activePalette.buttonText + } +} diff --git a/examples/declarative/tutorials/samegame/samegame1/pics/background.png b/examples/declarative/tutorials/samegame/samegame1/pics/background.png Binary files differnew file mode 100644 index 0000000..25e885f --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame1/pics/background.png diff --git a/examples/declarative/tutorials/samegame/samegame1/pics/redStone.png b/examples/declarative/tutorials/samegame/samegame1/pics/redStone.png Binary files differnew file mode 100644 index 0000000..b099f60 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame1/pics/redStone.png diff --git a/examples/declarative/tutorials/samegame/samegame1/samegame.qml b/examples/declarative/tutorials/samegame/samegame1/samegame.qml new file mode 100644 index 0000000..d18718d --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame1/samegame.qml @@ -0,0 +1,38 @@ +import Qt 4.6 + +Rectangle { + id: Screen + width: 490; height: 720 + + SystemPalette { id: activePalette; colorGroup: Qt.Active } + + Item { + width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top + + Image { + id: background + anchors.fill: parent; source: "pics/background.png" + fillMode: "PreserveAspectCrop" + } + } + + Rectangle { + id: ToolBar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: Screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: print("Implement me!"); + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + id: Score + text: "Score: Who knows?"; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } +} diff --git a/examples/declarative/tutorials/samegame/samegame2/Block.qml b/examples/declarative/tutorials/samegame/samegame2/Block.qml new file mode 100644 index 0000000..228ac4e --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame2/Block.qml @@ -0,0 +1,10 @@ +import Qt 4.6 + +Item { + id:block + + Image { id: img + source: "pics/redStone.png"; + anchors.fill: parent + } +} diff --git a/examples/declarative/tutorials/samegame/samegame2/Button.qml b/examples/declarative/tutorials/samegame/samegame2/Button.qml new file mode 100644 index 0000000..2354218 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame2/Button.qml @@ -0,0 +1,25 @@ +import Qt 4.6 + +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: activePalette.darker(activePalette.button); radius: 8; + + gradient: Gradient { + GradientStop { + id: topGrad; position: 0.0 + color: if (mr.pressed) { activePalette.dark } else { activePalette.light } } + GradientStop { position: 1.0; color: activePalette.button } + } + + MouseRegion { id: mr; anchors.fill: parent; onClicked: Container.clicked() } + + Text { + id: txtItem; text: Container.text; anchors.centerIn: Container; color: activePalette.buttonText + } +} diff --git a/examples/declarative/tutorials/samegame/samegame2/pics/background.png b/examples/declarative/tutorials/samegame/samegame2/pics/background.png Binary files differnew file mode 100644 index 0000000..25e885f --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame2/pics/background.png diff --git a/examples/declarative/tutorials/samegame/samegame2/pics/redStone.png b/examples/declarative/tutorials/samegame/samegame2/pics/redStone.png Binary files differnew file mode 100644 index 0000000..b099f60 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame2/pics/redStone.png diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.js b/examples/declarative/tutorials/samegame/samegame2/samegame.js new file mode 100644 index 0000000..064d87e --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame2/samegame.js @@ -0,0 +1,59 @@ +//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 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 initBoard() +{ + //Calculate board size + maxX = Math.floor(background.width/tileSize); + maxY = Math.floor(background.height/tileSize); + maxIndex = maxY*maxX; + + //Initialize Board + board = new Array(maxIndex); + for(xIdx=0; xIdx<maxX; xIdx++){ + for(yIdx=0; yIdx<maxY; yIdx++){ + board[index(xIdx,yIdx)] = null; + createBlock(xIdx,yIdx); + } + } +} + +function createBlock(xIdx,yIdx){ + if(component==null) + component = createComponent(tileSrc); + + // 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){ + dynamicObject = component.createObject(); + 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 + print("error loading block component"); + print(component.errorsString()); + return false; + } + return true; +} diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.qml b/examples/declarative/tutorials/samegame/samegame2/samegame.qml new file mode 100644 index 0000000..e446fa4 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame2/samegame.qml @@ -0,0 +1,39 @@ +import Qt 4.6 + +Rectangle { + id: Screen + width: 490; height: 720 + + SystemPalette { id: activePalette; colorGroup: Qt.Active } + Script { source: "samegame.js" } + + Item { + width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top + + Image { + id: background + anchors.fill: parent; source: "pics/background.png" + fillMode: "PreserveAspectCrop" + } + } + + Rectangle { + id: ToolBar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: Screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: initBoard(); + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + id: Score + text: "Score: Who knows?"; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } +} diff --git a/examples/declarative/tutorials/samegame/samegame3/Block.qml b/examples/declarative/tutorials/samegame/samegame3/Block.qml new file mode 100644 index 0000000..2f28923 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/Block.qml @@ -0,0 +1,19 @@ +import Qt 4.6 + +Item { + id:block + property int type: 0 + + Image { id: img + source: { + if(type == 0){ + "pics/redStone.png"; + } else if(type == 1) { + "pics/blueStone.png"; + } else { + "pics/greenStone.png"; + } + } + anchors.fill: parent + } +} diff --git a/examples/declarative/tutorials/samegame/samegame3/Button.qml b/examples/declarative/tutorials/samegame/samegame3/Button.qml new file mode 100644 index 0000000..2354218 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/Button.qml @@ -0,0 +1,25 @@ +import Qt 4.6 + +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: activePalette.darker(activePalette.button); radius: 8; + + gradient: Gradient { + GradientStop { + id: topGrad; position: 0.0 + color: if (mr.pressed) { activePalette.dark } else { activePalette.light } } + GradientStop { position: 1.0; color: activePalette.button } + } + + MouseRegion { id: mr; anchors.fill: parent; onClicked: Container.clicked() } + + Text { + id: txtItem; 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 new file mode 100644 index 0000000..401d211 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/Dialog.qml @@ -0,0 +1,21 @@ +import Qt 4.6 + +Rectangle { + id: page + function forceClose() { + page.closed(); + page.opacity = 0; + } + function show(txt) { + MyText.text = txt; + page.opacity = 1; + } + signal closed(); + color: "white"; border.width: 1; width: MyText.width + 20; height: 60; + opacity: 0 + opacity: Behavior { + NumberAnimation { duration: 1000 } + } + Text { id: MyText; anchors.centerIn: parent; text: "Hello World!" } + MouseRegion { id: mr; anchors.fill: parent; onClicked: forceClose(); } +} diff --git a/examples/declarative/tutorials/samegame/samegame3/pics/background.png b/examples/declarative/tutorials/samegame/samegame3/pics/background.png Binary files differnew file mode 100644 index 0000000..25e885f --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/pics/background.png diff --git a/examples/declarative/tutorials/samegame/samegame3/pics/blueStone.png b/examples/declarative/tutorials/samegame/samegame3/pics/blueStone.png Binary files differnew file mode 100644 index 0000000..bf342e0 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/pics/blueStone.png diff --git a/examples/declarative/tutorials/samegame/samegame3/pics/greenStone.png b/examples/declarative/tutorials/samegame/samegame3/pics/greenStone.png Binary files differnew file mode 100644 index 0000000..5ac14a5 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/pics/greenStone.png diff --git a/examples/declarative/tutorials/samegame/samegame3/pics/redStone.png b/examples/declarative/tutorials/samegame/samegame3/pics/redStone.png Binary files differnew file mode 100644 index 0000000..b099f60 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/pics/redStone.png diff --git a/examples/declarative/tutorials/samegame/samegame3/samegame.js b/examples/declarative/tutorials/samegame/samegame3/samegame.js new file mode 100644 index 0000000..16b349d --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/samegame.js @@ -0,0 +1,185 @@ +/* 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 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 initBoard() +{ + for(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; + + //Close dialogs + dialog.forceClose(); + + //Initialize Board + board = new Array(maxIndex); + gameCanvas.score = 0; + for(xIdx=0; xIdx<maxX; xIdx++){ + for(yIdx=0; yIdx<maxY; yIdx++){ + board[index(xIdx,yIdx)] = null; + createBlock(xIdx,yIdx); + } + } +} + +function createBlock(xIdx,yIdx){ + if(component==null) + component = createComponent(tileSrc); + + // 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){ + 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.y = yIdx*gameCanvas.tileSize; + dynamicObject.width = gameCanvas.tileSize; + dynamicObject.height = gameCanvas.tileSize; + board[index(xIdx,yIdx)] = dynamicObject; + }else{//isError or isLoading + 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) +{ + xIdx = Math.floor(x/gameCanvas.tileSize); + yIdx = Math.floor(y/gameCanvas.tileSize); + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + return; + if(board[index(xIdx, yIdx)] == 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) + return; + gameCanvas.score += (fillFound - 1) * (fillFound - 1); + shuffleDown(); + victoryCheck(); +} + +function floodFill(xIdx,yIdx,type) +{ + if(board[index(xIdx, yIdx)] == null) + return; + var first = false; + if(type == -1){ + first = true; + type = board[index(xIdx,yIdx)].type; + + //Flood fill initialization + fillFound = 0; + floodBoard = new Array(maxIndex); + } + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + return; + if(floodBoard[index(xIdx, yIdx)] == 1 || (!first && type != board[index(xIdx,yIdx)].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; + fillFound += 1; +} + +function shuffleDown() +{ + //Fall down + for(xIdx=0; xIdx<maxX; xIdx++){ + fallDist = 0; + for(yIdx=maxY-1; yIdx>=0; yIdx--){ + if(board[index(xIdx,yIdx)] == null){ + fallDist += 1; + }else{ + if(fallDist > 0){ + obj = board[index(xIdx,yIdx)]; + obj.y += fallDist * gameCanvas.tileSize; + board[index(xIdx,yIdx+fallDist)] = obj; + board[index(xIdx,yIdx)] = null; + } + } + } + } + //Fall to the left + fallDist = 0; + for(xIdx=0; xIdx<maxX; xIdx++){ + if(board[index(xIdx, maxY - 1)] == null){ + fallDist += 1; + }else{ + if(fallDist > 0){ + for(yIdx=0; yIdx<maxY; yIdx++){ + obj = board[index(xIdx,yIdx)]; + if(obj == null) + continue; + obj.x -= fallDist * gameCanvas.tileSize; + board[index(xIdx-fallDist,yIdx)] = obj; + board[index(xIdx,yIdx)] = null; + } + } + } + } +} + +function victoryCheck() +{ + //awards bonuses for no tiles left + deservesBonus = true; + for(xIdx=maxX-1; xIdx>=0; xIdx--) + if(board[index(xIdx, maxY - 1)] != null) + deservesBonus = false; + if(deservesBonus) + gameCanvas.score += 500; + //Checks for game over + if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1))) + dialog.show("Game Over. Your score is " + gameCanvas.score); +} + +//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) + return false; + if(board[index(xIdx, yIdx)] == null) + return false; + myType = board[index(xIdx, yIdx)].type; + if(type == myType) + return true; + return floodMoveCheck(xIdx + 1, yIdx, myType) || + floodMoveCheck(xIdx, yIdx - 1, board[index(xIdx,yIdx)].type); +} diff --git a/examples/declarative/tutorials/samegame/samegame3/samegame.qml b/examples/declarative/tutorials/samegame/samegame3/samegame.qml new file mode 100644 index 0000000..ea43ab2 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame3/samegame.qml @@ -0,0 +1,56 @@ +import Qt 4.6 + +Rectangle { + id: Screen + width: 490; height: 720 + + SystemPalette { id: activePalette; colorGroup: Qt.Active } + Script { source: "samegame.js" } + + Item { + width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top + + Image { + id: background + anchors.fill: parent; source: "pics/background.png" + fillMode: "PreserveAspectCrop" + } + + Item { + id: gameCanvas + property int score: 0 + property int tileSize: 40 + + z: 20; anchors.centerIn: parent + width: parent.width - (parent.width % tileSize); + height: parent.height - (parent.height % tileSize); + + MouseRegion { + id: gameMR + anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y); + } + } + } + + Dialog { id: dialog; anchors.centerIn: parent; z: 21 } + + Rectangle { + id: ToolBar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: Screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: initBoard(); + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + id: Score + text: "Score: " + gameCanvas.score; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } +} diff --git a/examples/declarative/tutorials/samegame/samegame4/README b/examples/declarative/tutorials/samegame/samegame4/README new file mode 100644 index 0000000..244b205 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/README @@ -0,0 +1,10 @@ +This demo uses pictures from the KDE project (www.kde.org), +specifically the images from the KSame game. These images are + +background.png +blueStone.png +redStone.png +greenStone.png +yellowStone.png + +and are presumably under the same GPL2 license as the rest of kdegames diff --git a/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml b/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml new file mode 100644 index 0000000..a495cd0 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/BoomBlock.qml @@ -0,0 +1,54 @@ +import Qt 4.6 + +Item { id:block + property bool dying: false + property bool spawned: false + property int type: 0 + property int targetX: 0 + property int targetY: 0 + + x: SpringFollow { enabled: spawned; source: targetX; spring: 2; damping: 0.2 } + y: SpringFollow { source: targetY; spring: 2; damping: 0.2 } + + Image { id: img + source: { + if(type == 0){ + "pics/redStone.png"; + } else if(type == 1) { + "pics/blueStone.png"; + } else { + "pics/greenStone.png"; + } + } + opacity: 0 + opacity: Behavior { NumberAnimation { properties:"opacity"; duration: 200 } } + anchors.fill: parent + } + + Particles { id: particles + width:1; height:1; anchors.centerIn: parent; opacity: 0 + lifeSpan: 700; lifeSpanDeviation: 600; count:0; streamIn: false + angle: 0; angleDeviation: 360; velocity: 100; velocityDeviation:30 + source: { + if(type == 0){ + "pics/redStar.png"; + } else if (type == 1) { + "pics/blueStar.png"; + } else { + "pics/greenStar.png"; + } + } + } + + states: [ + State{ name: "AliveState"; when: spawned == true && dying == false + PropertyChanges { target: img; opacity: 1 } + }, + State{ name: "DeathState"; when: dying == true + PropertyChanges { target: particles; count: 50 } + PropertyChanges { target: particles; opacity: 1 } + PropertyChanges { target: particles; emitting: false } // i.e. emit only once + PropertyChanges { target: img; opacity: 0 } + } + ] +} diff --git a/examples/declarative/tutorials/samegame/samegame4/content/Button.qml b/examples/declarative/tutorials/samegame/samegame4/content/Button.qml new file mode 100644 index 0000000..2354218 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/Button.qml @@ -0,0 +1,25 @@ +import Qt 4.6 + +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: activePalette.darker(activePalette.button); radius: 8; + + gradient: Gradient { + GradientStop { + id: topGrad; position: 0.0 + color: if (mr.pressed) { activePalette.dark } else { activePalette.light } } + GradientStop { position: 1.0; color: activePalette.button } + } + + MouseRegion { id: mr; anchors.fill: parent; onClicked: Container.clicked() } + + Text { + id: txtItem; 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 new file mode 100644 index 0000000..401d211 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/Dialog.qml @@ -0,0 +1,21 @@ +import Qt 4.6 + +Rectangle { + id: page + function forceClose() { + page.closed(); + page.opacity = 0; + } + function show(txt) { + MyText.text = txt; + page.opacity = 1; + } + signal closed(); + color: "white"; border.width: 1; width: MyText.width + 20; height: 60; + opacity: 0 + opacity: Behavior { + NumberAnimation { duration: 1000 } + } + Text { id: MyText; anchors.centerIn: parent; text: "Hello World!" } + MouseRegion { id: mr; anchors.fill: parent; onClicked: forceClose(); } +} diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/background.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/background.png Binary files differnew file mode 100644 index 0000000..25e885f --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/background.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/blueStar.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/blueStar.png Binary files differnew file mode 100644 index 0000000..ff9588f --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/blueStar.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/blueStone.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/blueStone.png Binary files differnew file mode 100644 index 0000000..bf342e0 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/blueStone.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/greenStar.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/greenStar.png Binary files differnew file mode 100644 index 0000000..cd06854 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/greenStar.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/greenStone.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/greenStone.png Binary files differnew file mode 100644 index 0000000..5ac14a5 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/greenStone.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/redStar.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/redStar.png Binary files differnew file mode 100644 index 0000000..0a4dffe --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/redStar.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/redStone.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/redStone.png Binary files differnew file mode 100644 index 0000000..b099f60 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/redStone.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/star.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/star.png Binary files differnew file mode 100644 index 0000000..defbde5 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/star.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/pics/yellowStone.png b/examples/declarative/tutorials/samegame/samegame4/content/pics/yellowStone.png Binary files differnew file mode 100644 index 0000000..c56124a --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/pics/yellowStone.png diff --git a/examples/declarative/tutorials/samegame/samegame4/content/samegame.js b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js new file mode 100755 index 0000000..cf55b59 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/content/samegame.js @@ -0,0 +1,217 @@ +/* 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 tileSize = 40; +var maxIndex = maxX*maxY; +var board = new Array(maxIndex); +var tileSrc = "content/BoomBlock.qml"; +var scoresURL = "http://qtfx-nokia.trolltech.com.au/samegame/scores.php"; +var timer; +var component; + +//Index function used instead of a 2D array +function index(xIdx,yIdx) { + return xIdx + (yIdx * maxX); +} + +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 initBoard() +{ + for(i = 0; i<maxIndex; i++){ + //Delete old blocks + if(board[i] != null) + board[i].destroy(); + } + + //Calculate board size + maxX = Math.floor(gameCanvas.width/tileSize); + maxY = Math.floor(gameCanvas.height/tileSize); + maxIndex = maxY*maxX; + + //Close dialogs + scoreName.forceClose(); + dialog.forceClose(); + + //Initialize Board + board = new Array(maxIndex); + gameCanvas.score = 0; + for(xIdx=0; xIdx<maxX; xIdx++){ + for(yIdx=0; yIdx<maxY; yIdx++){ + board[index(xIdx,yIdx)] = null; + createBlock(xIdx,yIdx); + } + } + timer = new Date(); +} + +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) +{ + xIdx = Math.floor(x/tileSize); + yIdx = Math.floor(y/tileSize); + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + return; + if(board[index(xIdx, yIdx)] == 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) + return; + gameCanvas.score += (fillFound - 1) * (fillFound - 1); + shuffleDown(); + victoryCheck(); +} + +function floodFill(xIdx,yIdx,type) +{ + if(board[index(xIdx, yIdx)] == null) + return; + var first = false; + if(type == -1){ + first = true; + type = board[index(xIdx,yIdx)].type; + + //Flood fill initialization + fillFound = 0; + floodBoard = new Array(maxIndex); + } + if(xIdx >= maxX || xIdx < 0 || yIdx >= maxY || yIdx < 0) + return; + if(floodBoard[index(xIdx, yIdx)] == 1 || (!first && type != board[index(xIdx,yIdx)].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; + fillFound += 1; +} + +function shuffleDown() +{ + //Fall down + for(xIdx=0; xIdx<maxX; xIdx++){ + fallDist = 0; + for(yIdx=maxY-1; yIdx>=0; yIdx--){ + if(board[index(xIdx,yIdx)] == null){ + fallDist += 1; + }else{ + if(fallDist > 0){ + obj = board[index(xIdx,yIdx)]; + obj.targetY += fallDist * tileSize; + board[index(xIdx,yIdx+fallDist)] = obj; + board[index(xIdx,yIdx)] = null; + } + } + } + } + //Fall to the left + fallDist = 0; + for(xIdx=0; xIdx<maxX; xIdx++){ + if(board[index(xIdx, maxY - 1)] == null){ + fallDist += 1; + }else{ + if(fallDist > 0){ + for(yIdx=0; yIdx<maxY; yIdx++){ + obj = board[index(xIdx,yIdx)]; + if(obj == null) + continue; + obj.targetX -= fallDist * tileSize; + board[index(xIdx-fallDist,yIdx)] = obj; + board[index(xIdx,yIdx)] = null; + } + } + } + } +} + +function victoryCheck() +{ + //awards bonuses for no tiles left + deservesBonus = true; + for(xIdx=maxX-1; xIdx>=0; xIdx--) + if(board[index(xIdx, maxY - 1)] != null) + deservesBonus = false; + if(deservesBonus) + gameCanvas.score += 500; + //Checks for game over + if(deservesBonus || !(floodMoveCheck(0,maxY-1, -1))){ + timer = new Date() - timer; + if(scoresURL != "") + scoreName.show("You've won! Please enter your name: "); + else + dialog.show("Game Over. Your score is " + gameCanvas.score); + } +} + +//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) + return false; + if(board[index(xIdx, yIdx)] == null) + return false; + myType = board[index(xIdx, yIdx)].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){ + if(component==null) + component = createComponent(tileSrc); + + // 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){ + 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*tileSize; + dynamicObject.targetX = xIdx*tileSize; + dynamicObject.targetY = yIdx*tileSize; + dynamicObject.width = tileSize; + dynamicObject.height = 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; +} + +function sendHighScore(name) { + var postman = new XMLHttpRequest() + var postData = "name="+name+"&score="+gameCanvas.score + +"&gridSize="+maxX+"x"+maxY +"&time="+Math.floor(timer/1000); + postman.open("POST", scoresURL, true); + postman.onreadystatechange = function() { + if (postman.readyState == postman.DONE) { + dialog.show("Your score has been uploaded."); + } + } + postman.send(postData); +} diff --git a/examples/declarative/tutorials/samegame/samegame4/highscores/README b/examples/declarative/tutorials/samegame/samegame4/highscores/README new file mode 100644 index 0000000..eaa00fa --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/highscores/README @@ -0,0 +1 @@ +The SameGame example can interface with a simple PHP script to store XML high score data on a remote server. We do not have a publically accessible server available for this use, but if you have access to a PHP capable webserver you can copy the files (score_data.xml, score.php, score_style.xsl) to it and alter the highscore_server variable at the top of the samegame.js file to point to it. diff --git a/examples/declarative/tutorials/samegame/samegame4/highscores/score_data.xml b/examples/declarative/tutorials/samegame/samegame4/highscores/score_data.xml new file mode 100755 index 0000000..c3fd90d --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/highscores/score_data.xml @@ -0,0 +1,2 @@ +<record><score>1000000</score><name>Alan the Tester</name><gridSize>0x0</gridSize><seconds>0</seconds></record> +<record><score>6213</score><name>Alan</name><gridSize>12x17</gridSize><seconds>51</seconds></record> diff --git a/examples/declarative/tutorials/samegame/samegame4/highscores/score_style.xsl b/examples/declarative/tutorials/samegame/samegame4/highscores/score_style.xsl new file mode 100755 index 0000000..670354c --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/highscores/score_style.xsl @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> +<xsl:template match="/"> + <html> + <head><title>SameGame High Scores</title></head> + <body> + <h2>SameGame High Scores</h2> + <table border="1"> + <tr bgcolor="lightsteelblue"> + <th>Name</th> + <th>Score</th> + <th>Grid Size</th> + <th>Time, s</th> + </tr> + <xsl:for-each select="records/record"> + <xsl:sort select="score" data-type="number" order="descending"/> + <tr> + <td><xsl:value-of select="name"/></td> + <td><xsl:value-of select="score"/></td> + <td><xsl:value-of select="gridSize"/></td> + <td><xsl:value-of select="seconds"/></td> + </tr> + </xsl:for-each> + </table> + </body> + </html> +</xsl:template> +</xsl:stylesheet> diff --git a/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php b/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php new file mode 100755 index 0000000..3cceb2d --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/highscores/scores.php @@ -0,0 +1,34 @@ +<?php + $score = $_POST["score"]; + echo "<html>"; + echo "<head><title>SameGame High Scores</title></head><body>"; + if($score > 0){#Sending in a new high score + $name = $_POST["name"]; + $grid = $_POST["gridSize"]; + $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? + $ret = fwrite($file, "<record><score>". $score . "</score><name>" + . $name . "</name><gridSize>" . $grid . "</gridSize><seconds>" + . $time . "</seconds></record>\n"); + echo "Your score has been recorded. Thanks for playing!"; + if($ret == False) + echo "<br/> There was an error though, so don't expect to see that score again."; + }else{#Read high score list + #Now uses XSLT to display. So just print the file. With XML cruft added. + #Note that firefox at least won't apply the XSLT on a php file. So redirecting + $file = fopen("scores.xml", "w"); + $ret = fwrite($file, '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" + . '<?xml-stylesheet type="text/xsl" href="score_style.xsl"?>' . "\n" + . "<records>\n" . file_get_contents("score_data.xml") . "</records>\n"); + if($ret == False) + echo "There was an internal error. Sorry."; + else + echo '<script type="text/javascript">window.location.replace("scores.xml")</script>'; + } + echo "</body></html>"; +?> diff --git a/examples/declarative/tutorials/samegame/samegame4/samegame.qml b/examples/declarative/tutorials/samegame/samegame4/samegame.qml new file mode 100644 index 0000000..ede4362 --- /dev/null +++ b/examples/declarative/tutorials/samegame/samegame4/samegame.qml @@ -0,0 +1,71 @@ +import Qt 4.6 +import "content" + +Rectangle { + id: Screen + width: 490; height: 720 + + Script { source: "content/samegame.js" } + + SystemPalette { id: activePalette; colorGroup: Qt.Active } + + Item { + width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top + + Image { + id: background + anchors.fill: parent; source: "content/pics/background.png" + fillMode: "PreserveAspectCrop" + } + + Item { + id: gameCanvas + property int score: 0 + + z: 20; anchors.centerIn: parent + width: parent.width - (parent.width % tileSize); + height: parent.height - (parent.height % tileSize); + + MouseRegion { + id: gameMR + anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y); + } + } + } + + Dialog { id: dialog; anchors.centerIn: parent; z: 21 } + Dialog { + id: scoreName; anchors.centerIn: parent; z: 22; + TextInput { + id: Editor + onAccepted: { + if(scoreName.opacity==1&&Editor.text!="") + sendHighScore(Editor.text); + scoreName.forceClose(); + } + anchors.verticalCenter: parent.verticalCenter + width: 72; focus: true + anchors.right: scoreName.right + } + } + + Rectangle { + id: ToolBar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: Screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: {initBoard();} + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + id: Score + text: "Score: " + gameCanvas.score; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } +} diff --git a/tests/auto/declarative/qfxwebview/data/basic.html b/tests/auto/declarative/qfxwebview/data/basic.html index 254317c..c262f12 100644 --- a/tests/auto/declarative/qfxwebview/data/basic.html +++ b/tests/auto/declarative/qfxwebview/data/basic.html @@ -1,6 +1,6 @@ <html> <head><title>Basic</title> -<link rel="shortcut icon" type="image/x-icon" href="basic.ico"> +<link rel="icon" sizes="48x48" href="basic.png"> </head> <body leftmargin="0" marginwidth="0"> <table width="123"> diff --git a/tests/auto/declarative/qfxwebview/data/basic.png b/tests/auto/declarative/qfxwebview/data/basic.png Binary files differnew file mode 100644 index 0000000..35717cc --- /dev/null +++ b/tests/auto/declarative/qfxwebview/data/basic.png diff --git a/tests/auto/declarative/qfxwebview/tst_qfxwebview.cpp b/tests/auto/declarative/qfxwebview/tst_qfxwebview.cpp index 834c07f..dc124ce 100644 --- a/tests/auto/declarative/qfxwebview/tst_qfxwebview.cpp +++ b/tests/auto/declarative/qfxwebview/tst_qfxwebview.cpp @@ -72,8 +72,8 @@ void tst_qfxwebview::testBasicProperties() QVERIFY(wv != 0); QTRY_COMPARE(wv->progress(), 1.0); QCOMPARE(wv->title(),QString("Basic")); - wv->icon().save("test.png"); - //QCOMPARE(wv->icon(),QPixmap(SRCDIR "/data/basic.ico")); + QTRY_COMPARE(wv->icon().width(), 48); + QCOMPARE(wv->icon(),QPixmap(SRCDIR "/data/basic.png")); QCOMPARE(wv->statusText(),QString("")); QFile htmlfile(SRCDIR "/data/basic.html"); QVERIFY(htmlfile.open(QIODevice::ReadOnly)); @@ -83,6 +83,9 @@ void tst_qfxwebview::testBasicProperties() expectedhtml.replace(QRegExp("\\s+"),""); QCOMPARE(actualhtml____,expectedhtml); // same, ignoring whitespace QCOMPARE(wv->width(), 123.0); + QCOMPARE(wv->webPageWidth(), 0); + QCOMPARE(wv->preferredWidth(), 0); + QCOMPARE(wv->zoomFactor(), 1.0); QCOMPARE(wv->url(), QUrl::fromLocalFile(SRCDIR "/data/basic.html")); QCOMPARE(wv->status(), QFxWebView::Ready); QVERIFY(wv->reloadAction()); diff --git a/tests/auto/declarative/visual/bindinganimation/data/bindinganimation.qml b/tests/auto/declarative/visual/bindinganimation/data/bindinganimation.qml index 1d2f6da..2501797 100644 --- a/tests/auto/declarative/visual/bindinganimation/data/bindinganimation.qml +++ b/tests/auto/declarative/visual/bindinganimation/data/bindinganimation.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/bindinganimation/data/bindinganimation.0.png" + image: "bindinganimation.0.png" } Frame { msec: 976 @@ -498,7 +498,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/bindinganimation/data/bindinganimation.1.png" + image: "bindinganimation.1.png" } Frame { msec: 1936 diff --git a/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.qml b/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.qml index 8f7f068..6654021 100644 --- a/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.qml +++ b/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.qml @@ -258,7 +258,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.0.png" + image: "colorAnimation.0.png" } Frame { msec: 976 @@ -498,7 +498,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.1.png" + image: "colorAnimation.1.png" } Frame { msec: 1936 @@ -738,7 +738,7 @@ VisualTest { } Frame { msec: 2880 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/colorAnimation/data/colorAnimation.2.png" + image: "colorAnimation.2.png" } Frame { msec: 2896 diff --git a/tests/auto/declarative/visual/easing/data/easing.qml b/tests/auto/declarative/visual/easing/data/easing.qml index d991596..c41d676 100644 --- a/tests/auto/declarative/visual/easing/data/easing.qml +++ b/tests/auto/declarative/visual/easing/data/easing.qml @@ -258,7 +258,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/easing/data/easing.0.png" + image: "easing.0.png" } Frame { msec: 976 @@ -514,7 +514,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/easing/data/easing.1.png" + image: "easing.1.png" } Frame { msec: 1936 @@ -770,7 +770,7 @@ VisualTest { } Frame { msec: 2880 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/easing/data/easing.2.png" + image: "easing.2.png" } Frame { msec: 2896 diff --git a/tests/auto/declarative/visual/flickable/data/flickable.qml b/tests/auto/declarative/visual/flickable/data/flickable.qml index 45bb989..730c128 100644 --- a/tests/auto/declarative/visual/flickable/data/flickable.qml +++ b/tests/auto/declarative/visual/flickable/data/flickable.qml @@ -298,7 +298,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/flickable/data/flickable.0.png" + image: "flickable.0.png" } Frame { msec: 976 @@ -538,7 +538,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/flickable/data/flickable.1.png" + image: "flickable.1.png" } Frame { msec: 1936 @@ -786,6 +786,6 @@ VisualTest { } Frame { msec: 2880 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/flickable/data/flickable.2.png" + image: "flickable.2.png" } } diff --git a/tests/auto/declarative/visual/focusscope/data/test.qml b/tests/auto/declarative/visual/focusscope/data/test.qml index 95e23fc..d86c034 100644 --- a/tests/auto/declarative/visual/focusscope/data/test.qml +++ b/tests/auto/declarative/visual/focusscope/data/test.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test.0.png" + image: "test.0.png" } Frame { msec: 976 @@ -506,7 +506,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test.1.png" + image: "test.1.png" } Frame { msec: 1936 @@ -770,7 +770,7 @@ VisualTest { } Frame { msec: 2880 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test.2.png" + image: "test.2.png" } Frame { msec: 2896 @@ -1042,7 +1042,7 @@ VisualTest { } Frame { msec: 3840 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test.3.png" + image: "test.3.png" } Frame { msec: 3856 @@ -1306,7 +1306,7 @@ VisualTest { } Frame { msec: 4800 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test.4.png" + image: "test.4.png" } Frame { msec: 4816 @@ -1554,7 +1554,7 @@ VisualTest { } Frame { msec: 5760 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test.5.png" + image: "test.5.png" } Frame { msec: 5776 diff --git a/tests/auto/declarative/visual/focusscope/data/test2.qml b/tests/auto/declarative/visual/focusscope/data/test2.qml index 4ad4b89..fedc96a 100644 --- a/tests/auto/declarative/visual/focusscope/data/test2.qml +++ b/tests/auto/declarative/visual/focusscope/data/test2.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test2.0.png" + image: "test2.0.png" } Frame { msec: 976 @@ -482,7 +482,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test2.1.png" + image: "test2.1.png" } Frame { msec: 1936 diff --git a/tests/auto/declarative/visual/focusscope/data/test3.qml b/tests/auto/declarative/visual/focusscope/data/test3.qml index 86025a6..8ce7944 100644 --- a/tests/auto/declarative/visual/focusscope/data/test3.qml +++ b/tests/auto/declarative/visual/focusscope/data/test3.qml @@ -258,7 +258,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.0.png" + image: "test3.0.png" } Frame { msec: 976 @@ -530,7 +530,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.1.png" + image: "test3.1.png" } Frame { msec: 1936 @@ -802,7 +802,7 @@ VisualTest { } Frame { msec: 2880 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.2.png" + image: "test3.2.png" } Frame { msec: 2896 @@ -1074,7 +1074,7 @@ VisualTest { } Frame { msec: 3840 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.3.png" + image: "test3.3.png" } Frame { msec: 3856 @@ -1330,7 +1330,7 @@ VisualTest { } Frame { msec: 4800 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.4.png" + image: "test3.4.png" } Frame { msec: 4816 @@ -1594,7 +1594,7 @@ VisualTest { } Frame { msec: 5760 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.5.png" + image: "test3.5.png" } Frame { msec: 5776 @@ -1858,7 +1858,7 @@ VisualTest { } Frame { msec: 6720 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.6.png" + image: "test3.6.png" } Frame { msec: 6736 @@ -2130,7 +2130,7 @@ VisualTest { } Frame { msec: 7680 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.7.png" + image: "test3.7.png" } Frame { msec: 7696 @@ -2402,7 +2402,7 @@ VisualTest { } Frame { msec: 8640 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.8.png" + image: "test3.8.png" } Frame { msec: 8656 @@ -2658,7 +2658,7 @@ VisualTest { } Frame { msec: 9600 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/focusscope/data/test3.9.png" + image: "test3.9.png" } Frame { msec: 9616 diff --git a/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.qml b/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.qml index 5d42d03..73c6542 100644 --- a/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.qml +++ b/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.0.png" + image: "pauseAnimation.0.png" } Frame { msec: 976 @@ -482,7 +482,7 @@ VisualTest { } Frame { msec: 1920 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.1.png" + image: "pauseAnimation.1.png" } Frame { msec: 1936 @@ -722,7 +722,7 @@ VisualTest { } Frame { msec: 2880 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.2.png" + image: "pauseAnimation.2.png" } Frame { msec: 2896 @@ -962,7 +962,7 @@ VisualTest { } Frame { msec: 3840 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.3.png" + image: "pauseAnimation.3.png" } Frame { msec: 3856 @@ -1202,7 +1202,7 @@ VisualTest { } Frame { msec: 4800 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.4.png" + image: "pauseAnimation.4.png" } Frame { msec: 4816 @@ -1442,7 +1442,7 @@ VisualTest { } Frame { msec: 5760 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/pauseAnimation/data/pauseAnimation.5.png" + image: "pauseAnimation.5.png" } Frame { msec: 5776 diff --git a/tests/auto/declarative/visual/qfxtext/elide/data/elide.qml b/tests/auto/declarative/visual/qfxtext/elide/data/elide.qml index e62c1b7..59f17f7 100644 --- a/tests/auto/declarative/visual/qfxtext/elide/data/elide.qml +++ b/tests/auto/declarative/visual/qfxtext/elide/data/elide.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/qfxtext/elide/data/elide.0.png" + image: "elide.0.png" } Frame { msec: 976 diff --git a/tests/auto/declarative/visual/repeater/data/basic1.qml b/tests/auto/declarative/visual/repeater/data/basic1.qml index ad7e911..9535a2c 100644 --- a/tests/auto/declarative/visual/repeater/data/basic1.qml +++ b/tests/auto/declarative/visual/repeater/data/basic1.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/repeater/data/basic1.0.png" + image: "basic1.0.png" } Frame { msec: 976 diff --git a/tests/auto/declarative/visual/repeater/data/basic2.qml b/tests/auto/declarative/visual/repeater/data/basic2.qml index 082f349..81bc1f7 100644 --- a/tests/auto/declarative/visual/repeater/data/basic2.qml +++ b/tests/auto/declarative/visual/repeater/data/basic2.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/repeater/data/basic2.0.png" + image: "basic2.0.png" } Frame { msec: 976 diff --git a/tests/auto/declarative/visual/repeater/data/basic3.qml b/tests/auto/declarative/visual/repeater/data/basic3.qml index e76bf6f..417eaab 100644 --- a/tests/auto/declarative/visual/repeater/data/basic3.qml +++ b/tests/auto/declarative/visual/repeater/data/basic3.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/repeater/data/basic3.0.png" + image: "basic3.0.png" } Frame { msec: 976 diff --git a/tests/auto/declarative/visual/repeater/data/basic4.qml b/tests/auto/declarative/visual/repeater/data/basic4.qml index cdff83f..264d825 100644 --- a/tests/auto/declarative/visual/repeater/data/basic4.qml +++ b/tests/auto/declarative/visual/repeater/data/basic4.qml @@ -242,7 +242,7 @@ VisualTest { } Frame { msec: 960 - image: "/Users/akennedy/Qt/kinetic/tests/auto/declarative/visual/repeater/data/basic4.0.png" + image: "basic4.0.png" } Frame { msec: 976 diff --git a/tests/auto/declarative/visual/tst_visual.cpp b/tests/auto/declarative/visual/tst_visual.cpp index 458b96e..d95fa5b 100644 --- a/tests/auto/declarative/visual/tst_visual.cpp +++ b/tests/auto/declarative/visual/tst_visual.cpp @@ -78,6 +78,8 @@ void tst_visual::visual() QProcess p; p.start(qmlviewer, arguments); QVERIFY(p.waitForFinished()); + if (p.exitCode() != 0) + qDebug() << p.readAllStandardError(); QCOMPARE(p.exitStatus(), QProcess::NormalExit); QCOMPARE(p.exitCode(), 0); } diff --git a/tools/qmlviewer/qfxtester.cpp b/tools/qmlviewer/qfxtester.cpp index 7903fa9..a6a46eb 100644 --- a/tools/qmlviewer/qfxtester.cpp +++ b/tools/qmlviewer/qfxtester.cpp @@ -285,9 +285,9 @@ void QFxTester::updateCurrentTime(int msec) } if (options & QmlViewer::TestImages && !frame->image().isEmpty()) { - QImage goodImage(frame->image()); + QImage goodImage(frame->image().toLocalFile()); if (goodImage != img) { - QString reject(frame->image() + ".reject.png"); + QString reject(frame->image().toLocalFile() + ".reject.png"); qWarning() << "QFxTester: Image mismatch. Reject saved to:" << reject; img.save(reject); diff --git a/tools/qmlviewer/qfxtester.h b/tools/qmlviewer/qfxtester.h index bb027f6..52987db 100644 --- a/tools/qmlviewer/qfxtester.h +++ b/tools/qmlviewer/qfxtester.h @@ -44,7 +44,7 @@ class QFxVisualTestFrame : public QObject Q_OBJECT Q_PROPERTY(int msec READ msec WRITE setMsec) Q_PROPERTY(QString hash READ hash WRITE setHash) - Q_PROPERTY(QString image READ image WRITE setImage) + Q_PROPERTY(QUrl image READ image WRITE setImage) public: QFxVisualTestFrame() : m_msec(-1) {} @@ -54,13 +54,13 @@ public: QString hash() const { return m_hash; } void setHash(const QString &hash) { m_hash = hash; } - QString image() const { return m_image; } - void setImage(const QString &image) { m_image = image; } + QUrl image() const { return m_image; } + void setImage(const QUrl &image) { m_image = image; } private: int m_msec; QString m_hash; - QString m_image; + QUrl m_image; }; QML_DECLARE_TYPE(QFxVisualTestFrame) |