From 1f4d824286329887a65c777b59ae0289152c52a3 Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 11:36:30 +1000 Subject: Compile after change b9d7f819b7bf9f7c39735377ae2e3e4d4afa1cf7 --- tests/auto/declarative/pathview/tst_pathview.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/auto/declarative/pathview/tst_pathview.cpp b/tests/auto/declarative/pathview/tst_pathview.cpp index 57ae8c0..360228b 100644 --- a/tests/auto/declarative/pathview/tst_pathview.cpp +++ b/tests/auto/declarative/pathview/tst_pathview.cpp @@ -111,6 +111,14 @@ public: return returnHash; } + QVariant data(int index, int role) const { + if (role == 0) + return list.at(index).first; + if (role == 1) + return list.at(index).second; + return QVariant(); + } + void addItem(const QString &name, const QString &number) { list.append(QPair(name, number)); emit itemsInserted(list.count()-1, 1); -- cgit v0.12 From 71c73f80050fda96111122cc75676df60b51b0f5 Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 11:48:44 +1000 Subject: Follow naming conventions (pass examples test) --- demos/declarative/snake/Button.qml | 25 -- demos/declarative/snake/Cookie.qml | 48 ---- demos/declarative/snake/HighScoreModel.qml | 100 ------- demos/declarative/snake/Link.qml | 75 ----- demos/declarative/snake/Skull.qml | 21 -- demos/declarative/snake/Snake.qml | 189 ------------- demos/declarative/snake/content/Button.qml | 25 ++ demos/declarative/snake/content/Cookie.qml | 48 ++++ demos/declarative/snake/content/HighScoreModel.qml | 100 +++++++ demos/declarative/snake/content/Link.qml | 75 +++++ demos/declarative/snake/content/Skull.qml | 21 ++ demos/declarative/snake/content/pics/README | 1 + .../declarative/snake/content/pics/background.png | Bin 0 -> 144508 bytes demos/declarative/snake/content/pics/blueStar.png | Bin 0 -> 272 bytes demos/declarative/snake/content/pics/blueStone.png | Bin 0 -> 3139 bytes demos/declarative/snake/content/pics/cookie.png | Bin 0 -> 2729 bytes demos/declarative/snake/content/pics/eyes.svg | 118 ++++++++ demos/declarative/snake/content/pics/head.png | Bin 0 -> 3473 bytes demos/declarative/snake/content/pics/head.svg | 134 +++++++++ demos/declarative/snake/content/pics/redStar.png | Bin 0 -> 273 bytes demos/declarative/snake/content/pics/redStone.png | Bin 0 -> 3456 bytes demos/declarative/snake/content/pics/skull.png | Bin 0 -> 2543 bytes demos/declarative/snake/content/pics/snake.jpg | Bin 0 -> 663901 bytes demos/declarative/snake/content/pics/star.png | Bin 0 -> 262 bytes .../declarative/snake/content/pics/stoneShadow.png | Bin 0 -> 1699 bytes .../declarative/snake/content/pics/yellowStar.png | Bin 0 -> 276 bytes .../declarative/snake/content/pics/yellowStone.png | Bin 0 -> 2667 bytes demos/declarative/snake/content/snake.js | 308 +++++++++++++++++++++ demos/declarative/snake/pics/README | 1 - demos/declarative/snake/pics/background.png | Bin 144508 -> 0 bytes demos/declarative/snake/pics/blueStar.png | Bin 272 -> 0 bytes demos/declarative/snake/pics/blueStone.png | Bin 3139 -> 0 bytes demos/declarative/snake/pics/cookie.png | Bin 2729 -> 0 bytes demos/declarative/snake/pics/eyes.svg | 118 -------- demos/declarative/snake/pics/head.png | Bin 3473 -> 0 bytes demos/declarative/snake/pics/head.svg | 134 --------- demos/declarative/snake/pics/redStar.png | Bin 273 -> 0 bytes demos/declarative/snake/pics/redStone.png | Bin 3456 -> 0 bytes demos/declarative/snake/pics/skull.png | Bin 2543 -> 0 bytes demos/declarative/snake/pics/snake.jpg | Bin 663901 -> 0 bytes demos/declarative/snake/pics/star.png | Bin 262 -> 0 bytes demos/declarative/snake/pics/stoneShadow.png | Bin 1699 -> 0 bytes demos/declarative/snake/pics/yellowStar.png | Bin 276 -> 0 bytes demos/declarative/snake/pics/yellowStone.png | Bin 2667 -> 0 bytes demos/declarative/snake/snake.js | 308 --------------------- demos/declarative/snake/snake.qml | 190 +++++++++++++ 46 files changed, 1020 insertions(+), 1019 deletions(-) delete mode 100644 demos/declarative/snake/Button.qml delete mode 100644 demos/declarative/snake/Cookie.qml delete mode 100644 demos/declarative/snake/HighScoreModel.qml delete mode 100644 demos/declarative/snake/Link.qml delete mode 100644 demos/declarative/snake/Skull.qml delete mode 100644 demos/declarative/snake/Snake.qml create mode 100644 demos/declarative/snake/content/Button.qml create mode 100644 demos/declarative/snake/content/Cookie.qml create mode 100644 demos/declarative/snake/content/HighScoreModel.qml create mode 100644 demos/declarative/snake/content/Link.qml create mode 100644 demos/declarative/snake/content/Skull.qml create mode 100644 demos/declarative/snake/content/pics/README create mode 100644 demos/declarative/snake/content/pics/background.png create mode 100644 demos/declarative/snake/content/pics/blueStar.png create mode 100644 demos/declarative/snake/content/pics/blueStone.png create mode 100644 demos/declarative/snake/content/pics/cookie.png create mode 100644 demos/declarative/snake/content/pics/eyes.svg create mode 100644 demos/declarative/snake/content/pics/head.png create mode 100644 demos/declarative/snake/content/pics/head.svg create mode 100644 demos/declarative/snake/content/pics/redStar.png create mode 100644 demos/declarative/snake/content/pics/redStone.png create mode 100644 demos/declarative/snake/content/pics/skull.png create mode 100644 demos/declarative/snake/content/pics/snake.jpg create mode 100644 demos/declarative/snake/content/pics/star.png create mode 100644 demos/declarative/snake/content/pics/stoneShadow.png create mode 100644 demos/declarative/snake/content/pics/yellowStar.png create mode 100644 demos/declarative/snake/content/pics/yellowStone.png create mode 100644 demos/declarative/snake/content/snake.js delete mode 100644 demos/declarative/snake/pics/README delete mode 100644 demos/declarative/snake/pics/background.png delete mode 100644 demos/declarative/snake/pics/blueStar.png delete mode 100644 demos/declarative/snake/pics/blueStone.png delete mode 100644 demos/declarative/snake/pics/cookie.png delete mode 100644 demos/declarative/snake/pics/eyes.svg delete mode 100644 demos/declarative/snake/pics/head.png delete mode 100644 demos/declarative/snake/pics/head.svg delete mode 100644 demos/declarative/snake/pics/redStar.png delete mode 100644 demos/declarative/snake/pics/redStone.png delete mode 100644 demos/declarative/snake/pics/skull.png delete mode 100644 demos/declarative/snake/pics/snake.jpg delete mode 100644 demos/declarative/snake/pics/star.png delete mode 100644 demos/declarative/snake/pics/stoneShadow.png delete mode 100644 demos/declarative/snake/pics/yellowStar.png delete mode 100644 demos/declarative/snake/pics/yellowStone.png delete mode 100644 demos/declarative/snake/snake.js create mode 100644 demos/declarative/snake/snake.qml diff --git a/demos/declarative/snake/Button.qml b/demos/declarative/snake/Button.qml deleted file mode 100644 index 63cd555..0000000 --- a/demos/declarative/snake/Button.qml +++ /dev/null @@ -1,25 +0,0 @@ -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: Qt.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/demos/declarative/snake/Cookie.qml b/demos/declarative/snake/Cookie.qml deleted file mode 100644 index 7f0aadf..0000000 --- a/demos/declarative/snake/Cookie.qml +++ /dev/null @@ -1,48 +0,0 @@ -import Qt 4.6 - -Item { - id: root - property bool dying: false - property int row; - property int column; - x: margin + column * gridSize - y: margin + row * gridSize - - width: gridSize - height: gridSize - property int value : 1; - - Image { - id: img - anchors.fill: parent - source: "pics/cookie.png" - opacity: 0 - opacity: Behavior { NumberAnimation { duration: 100 } } - Text { - font.bold: true - anchors.verticalCenter: parent.verticalCenter - anchors.horizontalCenter: parent.horizontalCenter - text: value - } - } - - - Particles { id: particles - width:1; height:1; anchors.centerIn: parent; - emissionRate: 0; - lifeSpan: 700; lifeSpanDeviation: 600; - angle: 0; angleDeviation: 360; - velocity: 100; velocityDeviation:30; - source: "pics/yellowStar.png"; - } - - states: [ - State{ name: "AliveState"; when: dying == false - PropertyChanges { target: img; opacity: 1 } - }, - State{ name: "DeathState"; when: dying == true - StateChangeScript { script: particles.burst(50); } - PropertyChanges { target: img; opacity: 0 } - } - ] -} diff --git a/demos/declarative/snake/HighScoreModel.qml b/demos/declarative/snake/HighScoreModel.qml deleted file mode 100644 index f585ce8..0000000 --- a/demos/declarative/snake/HighScoreModel.qml +++ /dev/null @@ -1,100 +0,0 @@ -import Qt 4.6 - -// Models a high score table. -// -// Use this component like this: -// -// HighScoreModel { -// id: highScores -// game: "MyCoolGame" -// } -// -// Then use either use the top-score properties: -// -// Text { text: "HI: " + highScores.topScore } -// -// or, use the model in a view: -// -// ListView { -// model: highScore -// delegate: Component { -// ... player ... score ... -// } -// } -// -// Add new scores via: -// -// saveScore(newScore) -// -// or: -// -// savePlayerScore(playerName,newScore) -// -// The best maxScore scores added by this method will be retained in an SQL database, -// and presented in the model and in the topScore/topPlayer properties. -// - -ListModel { - id: model - property string game: "" - property int topScore: 0 - property string topPlayer: "" - property int maxScores: 10 - - Script { - function db() - { - return openDatabaseSync("HighScoreModel", "1.0", "Generic High Score Functionality for QML", 1000000); - } - function ensureTables(tx) - { - tx.executeSql('CREATE TABLE IF NOT EXISTS HighScores(game TEXT, score INT, player TEXT)', []); - } - } - - function fillModel() { - db().transaction( - function(tx) { - ensureTables(tx); - var rs = tx.executeSql("SELECT score,player FROM HighScores WHERE game=? ORDER BY score DESC", [game]); - model.clear(); - if (rs.rows.length > 0) { - topScore = rs.rows.item(0).score - topPlayer = rs.rows.item(0).player - for (var i=0; i maxScores) - tx.executeSql("DELETE FROM HighScores WHERE game=? AND score <= ?", - [rs.rows.item(maxScores).score]); - } - } - ) - } - - function savePlayerScore(player,score) { - db().transaction( - function(tx) { - ensureTables(tx); - tx.executeSql("INSERT INTO HighScores VALUES(?,?,?)", [game,score,player]); - fillModel(); - } - ) - } - - function saveScore(score) { - savePlayerScore("player",score); - } - - function clearScores() { - db().transaction( - function(tx) { - tx.executeSql("DELETE FROM HighScores WHERE game=?", [game]); - fillModel(); - } - ) - } - - Component.onCompleted: { fillModel() } -} diff --git a/demos/declarative/snake/Link.qml b/demos/declarative/snake/Link.qml deleted file mode 100644 index 1b3f7bf..0000000 --- a/demos/declarative/snake/Link.qml +++ /dev/null @@ -1,75 +0,0 @@ -import Qt 4.6 - -Item { id:link - property bool dying: false - property bool spawned: false - property int type: 0 - property int row: 0 - property int column: 0 - property int rotation; - - width: 40; - height: 40 - - x: margin - 3 + gridSize * column - y: margin - 3 + gridSize * row - x: Behavior { NumberAnimation { duration: spawned ? heartbeatInterval : 0} } - y: Behavior { NumberAnimation { duration: spawned ? heartbeatInterval : 0 } } - - - Item { - id: img - anchors.fill: parent - Image { - source: { - if(type == 1) { - "pics/blueStone.png"; - } else if (type == 2) { - "pics/head.png"; - } else { - "pics/redStone.png"; - } - } - - transform: Rotation { - id: actualImageRotation - origin.x: width/2; origin.y: height/2; - angle: rotation * 90 - angle: Behavior{ NumberAnimation { duration: spawned ? 200 : 0} } - } - } - - Image { - source: "pics/stoneShadow.png" - } - - opacity: 0 - opacity: Behavior { NumberAnimation { duration: 200 } } - } - - - Particles { id: particles - width:1; height:1; anchors.centerIn: parent; - emissionRate: 0; - lifeSpan: 700; lifeSpanDeviation: 600; - angle: 0; angleDeviation: 360; - velocity: 100; velocityDeviation:30; - source: { - if(type == 1){ - "pics/blueStar.png"; - } else { - "pics/redStar.png"; - } - } - } - - states: [ - State{ name: "AliveState"; when: spawned == true && dying == false - PropertyChanges { target: img; opacity: 1 } - }, - State{ name: "DeathState"; when: dying == true - StateChangeScript { script: particles.burst(50); } - PropertyChanges { target: img; opacity: 0 } - } - ] -} diff --git a/demos/declarative/snake/Skull.qml b/demos/declarative/snake/Skull.qml deleted file mode 100644 index 585e7d3..0000000 --- a/demos/declarative/snake/Skull.qml +++ /dev/null @@ -1,21 +0,0 @@ -import Qt 4.6 - -Image { - property bool spawned: false - property int row; - property int column; - property int verticalMovement; - property int horizontalMovement; - - x: margin + column * gridSize + 2 - y: margin + row * gridSize - 3 - x: Behavior { NumberAnimation { duration: spawned ? halfbeatInterval : 0} } - y: Behavior { NumberAnimation { duration: spawned ? halfbeatInterval : 0 } } - - opacity: spawned ? 1 : 0 - opacity: Behavior { NumberAnimation { duration: 200 } } - - source: "pics/skull.png" - width: 24 - height: 40 -} diff --git a/demos/declarative/snake/Snake.qml b/demos/declarative/snake/Snake.qml deleted file mode 100644 index a9835d4..0000000 --- a/demos/declarative/snake/Snake.qml +++ /dev/null @@ -1,189 +0,0 @@ -import Qt 4.6 - -Rectangle { - id: screen; - SystemPalette { id: activePalette } - color: activePalette.window - - Script { source: "snake.js" } - - property int gridSize : 34 - property int margin: 4 - property int numRowsAvailable: Math.floor((height-32-2*margin)/gridSize) - property int numColumnsAvailable: Math.floor((width-2*margin)/gridSize) - - property int lastScore : 0 - - property int score: 0; - property int heartbeatInterval: 200 - property int halfbeatInterval: 160 - - width: 480 - height: 750 - - property int direction - property int headDirection - - property var head; - - HighScoreModel { - id: highScores - game: "Snake" - } - - Timer { - id: heartbeat; - interval: heartbeatInterval; - repeat: true - onTriggered: { move() } - } - Timer { - id: halfbeat; - interval: halfbeatInterval; - repeat: true - running: heartbeat.running - onTriggered: { moveSkull() } - } - Timer { - id: startNewGameTimer; - interval: 700; - onTriggered: {startNewGame(); } - } - - Timer { - id: startHeartbeatTimer; - interval: 1000 ; - } - - - Image { - Image { - id: title - source: "pics/snake.jpg" - fillMode: "PreserveAspectCrop" - anchors.fill: parent - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - opacity: Behavior { NumberAnimation { duration: 500 } } - - Text { - color: "white" - font.pointSize: 24 - horizontalAlignment: "AlignHCenter" - text: "Last Score:\t" + lastScore + "\nHighscore:\t" + highScores.topScore; - anchors.horizontalCenter: parent.horizontalCenter - anchors.bottom: parent.bottom - anchors.bottomMargin: gridSize - } - } - - source: "pics/background.png" - fillMode: "PreserveAspectCrop" - - anchors.left: parent.left - anchors.right: parent.right - anchors.top: parent.top - anchors.bottom: toolbar.top - - Rectangle { - id: playfield - border.width: 1 - border.color: "white" - color: "transparent" - anchors.horizontalCenter: parent.horizontalCenter - y: (screen.height - 32 - height)/2; - width: numColumnsAvailable * gridSize + 2*margin - height: numRowsAvailable * gridSize + 2*margin - - - Skull { - id: skull - } - - MouseRegion { - anchors.fill: parent - onPressed: { - if (!head || !heartbeat.running) { - startNewGame(); - return; - } - if (direction == 0 || direction == 2) - scheduleDirection((mouseX > (head.x + head.width/2)) ? 1 : 3); - else - scheduleDirection((mouseY > (head.y + head.height/2)) ? 2 : 0); - } - } - } - - } - - Rectangle { - id: progressBar - opacity: 0 - opacity: Behavior { NumberAnimation { duration: 200 } } - color: "transparent" - border.width: 2 - border.color: "#221edd" - x: 50 - y: 50 - width: 200 - height: 30 - anchors.horizontalCenter: parent.horizontalCenter - anchors.verticalCenter: parent.verticalCenter - anchors.verticalCenterOffset: 40 - - Rectangle { - id: progressIndicator - color: "#221edd"; - width: 0; - width: Behavior { NumberAnimation { duration: startHeartbeatTimer.running ? 1000 : 0}} - height: 30; - } - } - - Rectangle { - id: toolbar - color: activePalette.window - height: 32; width: parent.width - anchors.bottom: screen.bottom - - Button { - id: btnA; text: "New Game"; onClicked: {startNewGame();} - anchors.left: parent.left; anchors.leftMargin: 3 - anchors.verticalCenter: parent.verticalCenter - } - - Text { - color: activePalette.text - text: "Score: " + score; font.bold: true - anchors.right: parent.right; anchors.rightMargin: 3 - anchors.verticalCenter: parent.verticalCenter - } - } - - focus: true - Keys.onSpacePressed: startNewGame(); - Keys.onLeftPressed: if (state == "starting" || direction != 1) scheduleDirection(3); - Keys.onRightPressed: if (state == "starting" || direction != 3) scheduleDirection(1); - Keys.onUpPressed: if (state == "starting" || direction != 2) scheduleDirection(0); - Keys.onDownPressed: if (state == "starting" || direction != 0) scheduleDirection(2); - - states: [ - State { - name: "starting" - when: startHeartbeatTimer.running; - PropertyChanges {target: progressIndicator; width: 200} - PropertyChanges {target: title; opacity: 0} - PropertyChanges {target: progressBar; opacity: 1} - }, - State { - name: "running" - when: heartbeat.running - PropertyChanges {target: progressIndicator; width: 200} - PropertyChanges {target: title; opacity: 0} - PropertyChanges {target: skull; row: 0; column: 0; } - PropertyChanges {target: skull; spawned: 1} - } - ] - -} diff --git a/demos/declarative/snake/content/Button.qml b/demos/declarative/snake/content/Button.qml new file mode 100644 index 0000000..63cd555 --- /dev/null +++ b/demos/declarative/snake/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: Qt.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/demos/declarative/snake/content/Cookie.qml b/demos/declarative/snake/content/Cookie.qml new file mode 100644 index 0000000..7f0aadf --- /dev/null +++ b/demos/declarative/snake/content/Cookie.qml @@ -0,0 +1,48 @@ +import Qt 4.6 + +Item { + id: root + property bool dying: false + property int row; + property int column; + x: margin + column * gridSize + y: margin + row * gridSize + + width: gridSize + height: gridSize + property int value : 1; + + Image { + id: img + anchors.fill: parent + source: "pics/cookie.png" + opacity: 0 + opacity: Behavior { NumberAnimation { duration: 100 } } + Text { + font.bold: true + anchors.verticalCenter: parent.verticalCenter + anchors.horizontalCenter: parent.horizontalCenter + text: value + } + } + + + Particles { id: particles + width:1; height:1; anchors.centerIn: parent; + emissionRate: 0; + lifeSpan: 700; lifeSpanDeviation: 600; + angle: 0; angleDeviation: 360; + velocity: 100; velocityDeviation:30; + source: "pics/yellowStar.png"; + } + + states: [ + State{ name: "AliveState"; when: dying == false + PropertyChanges { target: img; opacity: 1 } + }, + State{ name: "DeathState"; when: dying == true + StateChangeScript { script: particles.burst(50); } + PropertyChanges { target: img; opacity: 0 } + } + ] +} diff --git a/demos/declarative/snake/content/HighScoreModel.qml b/demos/declarative/snake/content/HighScoreModel.qml new file mode 100644 index 0000000..f585ce8 --- /dev/null +++ b/demos/declarative/snake/content/HighScoreModel.qml @@ -0,0 +1,100 @@ +import Qt 4.6 + +// Models a high score table. +// +// Use this component like this: +// +// HighScoreModel { +// id: highScores +// game: "MyCoolGame" +// } +// +// Then use either use the top-score properties: +// +// Text { text: "HI: " + highScores.topScore } +// +// or, use the model in a view: +// +// ListView { +// model: highScore +// delegate: Component { +// ... player ... score ... +// } +// } +// +// Add new scores via: +// +// saveScore(newScore) +// +// or: +// +// savePlayerScore(playerName,newScore) +// +// The best maxScore scores added by this method will be retained in an SQL database, +// and presented in the model and in the topScore/topPlayer properties. +// + +ListModel { + id: model + property string game: "" + property int topScore: 0 + property string topPlayer: "" + property int maxScores: 10 + + Script { + function db() + { + return openDatabaseSync("HighScoreModel", "1.0", "Generic High Score Functionality for QML", 1000000); + } + function ensureTables(tx) + { + tx.executeSql('CREATE TABLE IF NOT EXISTS HighScores(game TEXT, score INT, player TEXT)', []); + } + } + + function fillModel() { + db().transaction( + function(tx) { + ensureTables(tx); + var rs = tx.executeSql("SELECT score,player FROM HighScores WHERE game=? ORDER BY score DESC", [game]); + model.clear(); + if (rs.rows.length > 0) { + topScore = rs.rows.item(0).score + topPlayer = rs.rows.item(0).player + for (var i=0; i maxScores) + tx.executeSql("DELETE FROM HighScores WHERE game=? AND score <= ?", + [rs.rows.item(maxScores).score]); + } + } + ) + } + + function savePlayerScore(player,score) { + db().transaction( + function(tx) { + ensureTables(tx); + tx.executeSql("INSERT INTO HighScores VALUES(?,?,?)", [game,score,player]); + fillModel(); + } + ) + } + + function saveScore(score) { + savePlayerScore("player",score); + } + + function clearScores() { + db().transaction( + function(tx) { + tx.executeSql("DELETE FROM HighScores WHERE game=?", [game]); + fillModel(); + } + ) + } + + Component.onCompleted: { fillModel() } +} diff --git a/demos/declarative/snake/content/Link.qml b/demos/declarative/snake/content/Link.qml new file mode 100644 index 0000000..1b3f7bf --- /dev/null +++ b/demos/declarative/snake/content/Link.qml @@ -0,0 +1,75 @@ +import Qt 4.6 + +Item { id:link + property bool dying: false + property bool spawned: false + property int type: 0 + property int row: 0 + property int column: 0 + property int rotation; + + width: 40; + height: 40 + + x: margin - 3 + gridSize * column + y: margin - 3 + gridSize * row + x: Behavior { NumberAnimation { duration: spawned ? heartbeatInterval : 0} } + y: Behavior { NumberAnimation { duration: spawned ? heartbeatInterval : 0 } } + + + Item { + id: img + anchors.fill: parent + Image { + source: { + if(type == 1) { + "pics/blueStone.png"; + } else if (type == 2) { + "pics/head.png"; + } else { + "pics/redStone.png"; + } + } + + transform: Rotation { + id: actualImageRotation + origin.x: width/2; origin.y: height/2; + angle: rotation * 90 + angle: Behavior{ NumberAnimation { duration: spawned ? 200 : 0} } + } + } + + Image { + source: "pics/stoneShadow.png" + } + + opacity: 0 + opacity: Behavior { NumberAnimation { duration: 200 } } + } + + + Particles { id: particles + width:1; height:1; anchors.centerIn: parent; + emissionRate: 0; + lifeSpan: 700; lifeSpanDeviation: 600; + angle: 0; angleDeviation: 360; + velocity: 100; velocityDeviation:30; + source: { + if(type == 1){ + "pics/blueStar.png"; + } else { + "pics/redStar.png"; + } + } + } + + states: [ + State{ name: "AliveState"; when: spawned == true && dying == false + PropertyChanges { target: img; opacity: 1 } + }, + State{ name: "DeathState"; when: dying == true + StateChangeScript { script: particles.burst(50); } + PropertyChanges { target: img; opacity: 0 } + } + ] +} diff --git a/demos/declarative/snake/content/Skull.qml b/demos/declarative/snake/content/Skull.qml new file mode 100644 index 0000000..585e7d3 --- /dev/null +++ b/demos/declarative/snake/content/Skull.qml @@ -0,0 +1,21 @@ +import Qt 4.6 + +Image { + property bool spawned: false + property int row; + property int column; + property int verticalMovement; + property int horizontalMovement; + + x: margin + column * gridSize + 2 + y: margin + row * gridSize - 3 + x: Behavior { NumberAnimation { duration: spawned ? halfbeatInterval : 0} } + y: Behavior { NumberAnimation { duration: spawned ? halfbeatInterval : 0 } } + + opacity: spawned ? 1 : 0 + opacity: Behavior { NumberAnimation { duration: 200 } } + + source: "pics/skull.png" + width: 24 + height: 40 +} diff --git a/demos/declarative/snake/content/pics/README b/demos/declarative/snake/content/pics/README new file mode 100644 index 0000000..0215132 --- /dev/null +++ b/demos/declarative/snake/content/pics/README @@ -0,0 +1 @@ +snake.jpg: This image is based on the picture "Eastern Green Mamba.jpg" from the free media databse Wikimedia Commons and is published under the terms of the GNU Free Documentation License. The original picture was taken by Danleo. diff --git a/demos/declarative/snake/content/pics/background.png b/demos/declarative/snake/content/pics/background.png new file mode 100644 index 0000000..72dffaa Binary files /dev/null and b/demos/declarative/snake/content/pics/background.png differ diff --git a/demos/declarative/snake/content/pics/blueStar.png b/demos/declarative/snake/content/pics/blueStar.png new file mode 100644 index 0000000..ba7acab Binary files /dev/null and b/demos/declarative/snake/content/pics/blueStar.png differ diff --git a/demos/declarative/snake/content/pics/blueStone.png b/demos/declarative/snake/content/pics/blueStone.png new file mode 100644 index 0000000..356affd Binary files /dev/null and b/demos/declarative/snake/content/pics/blueStone.png differ diff --git a/demos/declarative/snake/content/pics/cookie.png b/demos/declarative/snake/content/pics/cookie.png new file mode 100644 index 0000000..aec2957 Binary files /dev/null and b/demos/declarative/snake/content/pics/cookie.png differ diff --git a/demos/declarative/snake/content/pics/eyes.svg b/demos/declarative/snake/content/pics/eyes.svg new file mode 100644 index 0000000..1078692 --- /dev/null +++ b/demos/declarative/snake/content/pics/eyes.svg @@ -0,0 +1,118 @@ + + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/demos/declarative/snake/content/pics/head.png b/demos/declarative/snake/content/pics/head.png new file mode 100644 index 0000000..550e002 Binary files /dev/null and b/demos/declarative/snake/content/pics/head.png differ diff --git a/demos/declarative/snake/content/pics/head.svg b/demos/declarative/snake/content/pics/head.svg new file mode 100644 index 0000000..3bf0bd2 --- /dev/null +++ b/demos/declarative/snake/content/pics/head.svg @@ -0,0 +1,134 @@ + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/demos/declarative/snake/content/pics/redStar.png b/demos/declarative/snake/content/pics/redStar.png new file mode 100644 index 0000000..cd06854 Binary files /dev/null and b/demos/declarative/snake/content/pics/redStar.png differ diff --git a/demos/declarative/snake/content/pics/redStone.png b/demos/declarative/snake/content/pics/redStone.png new file mode 100644 index 0000000..9bb7fe4 Binary files /dev/null and b/demos/declarative/snake/content/pics/redStone.png differ diff --git a/demos/declarative/snake/content/pics/skull.png b/demos/declarative/snake/content/pics/skull.png new file mode 100644 index 0000000..6318616 Binary files /dev/null and b/demos/declarative/snake/content/pics/skull.png differ diff --git a/demos/declarative/snake/content/pics/snake.jpg b/demos/declarative/snake/content/pics/snake.jpg new file mode 100644 index 0000000..e91a784 Binary files /dev/null and b/demos/declarative/snake/content/pics/snake.jpg differ diff --git a/demos/declarative/snake/content/pics/star.png b/demos/declarative/snake/content/pics/star.png new file mode 100644 index 0000000..defbde5 Binary files /dev/null and b/demos/declarative/snake/content/pics/star.png differ diff --git a/demos/declarative/snake/content/pics/stoneShadow.png b/demos/declarative/snake/content/pics/stoneShadow.png new file mode 100644 index 0000000..1bd56af Binary files /dev/null and b/demos/declarative/snake/content/pics/stoneShadow.png differ diff --git a/demos/declarative/snake/content/pics/yellowStar.png b/demos/declarative/snake/content/pics/yellowStar.png new file mode 100644 index 0000000..52fb9c4 Binary files /dev/null and b/demos/declarative/snake/content/pics/yellowStar.png differ diff --git a/demos/declarative/snake/content/pics/yellowStone.png b/demos/declarative/snake/content/pics/yellowStone.png new file mode 100644 index 0000000..c56124a Binary files /dev/null and b/demos/declarative/snake/content/pics/yellowStone.png differ diff --git a/demos/declarative/snake/content/snake.js b/demos/declarative/snake/content/snake.js new file mode 100644 index 0000000..a65aebc --- /dev/null +++ b/demos/declarative/snake/content/snake.js @@ -0,0 +1,308 @@ + +var snake = new Array; +var board = new Array; +var links = new Array; +var scheduledDirections = new Array; +var numRows = 1; +var numColumns = 1; +var linkComponent = createComponent("content/Link.qml"); // XXX should resolve relative to script, not component +var cookieComponent = createComponent("content/Cookie.qml"); +var cookie; +var linksToGrow = 0; +var linksToDie = 0; +var waitForCookie = 0; +var growType = 0; +var skullMovementsBeforeDirectionChange = 0; + + +function rand(n) +{ + return (Math.floor(Math.random() * n)); +} + +function scheduleDirection(dir) +{ + direction = dir; + if(scheduledDirections[scheduledDirections.length-1]!=direction) + scheduledDirections.push(direction); +} + +function startNewGame() +{ + if (state == "starting") + return; + + if (heartbeat.running) { + endGame(); + startNewGameTimer.running = true; + return; + } + numRows = numRowsAvailable; + numColumns = numColumnsAvailable; + board = new Array(numRows * numColumns); + snake = new Array; + scheduledDirections = new Array; + growType = 0; + + skull.z = numRows * numColumns + 1; + + for (var i = 0; i < numRows * numColumns; ++i) { + if (i < links.length) { + var link = links[i]; + link.spawned = false; + link.dying = false; + } else { + if(linkComponent.isReady == false){ + if(linkComponent.isError == true) + print(linkComponent.errorString()); + else + print("Still loading linkComponent"); + continue;//TODO: Better error handling? + } + var link = linkComponent.createObject(); + link.parent = playfield; + link.z = numRows * numColumns + 1 - i; + link.type = i == 0 ? 2 : 0; + link.spawned = false; + link.dying = false; + links.push(link); + } + } + + head = links[0]; + snake.push(head); + head.row = numRows/2 -1; + head.column = numColumns/2 -1; + head.spawned = true; + + linksToGrow = 5; + linksToDie = 0; + waitForCookie = 5; + score = 0; + startHeartbeatTimer.running = true; + heartbeat.running = true; +} + +function endGame() +{ + heartbeat.running = false; + for(var i in snake) + snake[i].dying = true; + if (cookie) { + cookie.dying = true; + cookie = 0; + } + lastScore = score; + highScores.saveScore(lastScore); +} + +function move() { + + if (!head) + return; + + var dir = direction; + + if (scheduledDirections.length) { + dir = scheduledDirections.shift(); + } + + if (state == "starting") { + var turn = (dir - headDirection); + head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn ); + headDirection = dir; + return; + } + + var row = head.row; + var column = head.column; + + if (dir == 0) { + row = row - 1; + } else if (dir == 1) { + column = column + 1 + } else if (dir == 2) { + row = row + 1; + } else if (dir == 3) { + column = column - 1; + } + + //validate the new position + if (row < 0 || row >= numRows + || column < 0 || column >= numColumns + || (row == skull.row && column == skull.column) + || !isFree(row, column)) { + var turn = (dir - headDirection); + head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn ); + headDirection = dir; + endGame(); + return; + } + + var newLink; + if (linksToGrow > 0) { + --linksToGrow; + newLink = links[snake.length]; + newLink.spawned = false; + newLink.rotation = snake[snake.length-1].rotation; + newLink.type = growType; + newLink.dying = false; + snake.push(newLink); + } else { + var lastLink = snake[snake.length-1]; + board[lastLink.row * numColumns + lastLink.column] = Undefined; + } + + if (waitForCookie > 0) { + if (--waitForCookie == 0) + createCookie(cookie? (cookie.value+1) : 1); + } + + for (var i = snake.length-1; i > 0; --i) { + snake[i].row = snake[i-1].row; + snake[i].column = snake[i-1].column; + snake[i].rotation = snake[i-1].rotation; + } + + if (newLink) { + newLink.spawned = true; + } + + // move the head + head.row = row; + head.column = column; + board[row * numColumns + column] = head; + + var turn = (dir - headDirection); + head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn ); + headDirection = dir; + + var value = testCookie(row, column); + if (value > 0) { + linksToGrow += value; + score += value; + } +} + +function isFree(row, column) +{ + return board[row * numColumns + column] == Undefined; +} + +function isHead(row, column) +{ + return head.column == column && head.row == row; +} + +function testCookie(row, column) +{ + if (cookie && !cookie.dying && cookie.row == row && cookie.column == column) { + var value = cookie.value; + waitForCookie = value; + growType = snake[snake.length-1].type == 1 ? 0 : 1; + cookie.dying = true; + cookie.z = numRows * numColumns + 2; + return value; + } + return 0; +} + +function moveSkull() +{ + + if (linksToDie > 0) { + --linksToDie; + var link = snake.pop(); + link.dying = true; + board[link.row * numColumns + link.column] = Undefined; + if (score > 0) + --score; + if (snake.length == 0) { + endGame(); + return; + } + } + + var row = skull.row; + var column = skull.column; + if (isHead(row, column)) { + endGame(); + return; + } + row += skull.verticalMovement; + column += skull.horizontalMovement; + + var attempts = 4; + + while (skullMovementsBeforeDirectionChange == 0 || row < 0 || row >= numRows + || column < 0 || column >= numColumns + || (!isFree(row, column) && !isHead(row, column))) { + var d = rand(8); + skull.verticalMovement = 0; + skull.horizontalMovement = 0; + skullMovementsBeforeDirectionChange = rand(20)+1; + if (d == 0) { + skull.verticalMovement = -1 + } else if (d == 1) { + skull.horizontalMovement = -1; + } else if (d == 2) { + skull.verticalMovement = 1 + } else if (d == 3){ + skull.horizontalMovement = 1; + } else if (cookie) { + var rd = cookie.row - skull.row; + var rc = cookie.column - skull.column; + if (Math.abs(rd) > Math.abs(rc)) { + skull.verticalMovement = rd > 0 ? 1 : -1; + skullMovementsBeforeDirectionChange = Math.abs(rd); + } else { + skull.horizontalMovement= rc > 0 ? 1 : -1; + skullMovementsBeforeDirectionChange = Math.abs(rc); + } + } + row = skull.row + skull.verticalMovement; + column = skull.column + skull.horizontalMovement; + if (--attempts == 0) + return; + } + + skull.row = row; + skull.column = column; + --skullMovementsBeforeDirectionChange; + var value = testCookie(row, column); + if (value > 0) + linksToDie += value/2; + + if (isHead(row, column)) + endGame(); +} + +function createCookie(value) { + if (numRows * numColumns - snake.length < 10) + return; + + var column = rand(numColumns); + var row = rand(numRows); + while (!isFree(row, column)) { + column++; + if (column == numColumns) { + column = 0; + row++; + if (row == numRows) + row = 0; + } + } + + if(cookieComponent.isReady == false){ + if(cookieComponent.isError == true) + print(cookieComponent.errorString()); + else + print("Still loading cookieComponent"); + return;//TODO: Better error handling? + } + cookie = cookieComponent.createObject(); + cookie.parent = head.parent; + cookie.value = value; + cookie.row = row; + cookie.column = column; +} diff --git a/demos/declarative/snake/pics/README b/demos/declarative/snake/pics/README deleted file mode 100644 index 0215132..0000000 --- a/demos/declarative/snake/pics/README +++ /dev/null @@ -1 +0,0 @@ -snake.jpg: This image is based on the picture "Eastern Green Mamba.jpg" from the free media databse Wikimedia Commons and is published under the terms of the GNU Free Documentation License. The original picture was taken by Danleo. diff --git a/demos/declarative/snake/pics/background.png b/demos/declarative/snake/pics/background.png deleted file mode 100644 index 72dffaa..0000000 Binary files a/demos/declarative/snake/pics/background.png and /dev/null differ diff --git a/demos/declarative/snake/pics/blueStar.png b/demos/declarative/snake/pics/blueStar.png deleted file mode 100644 index ba7acab..0000000 Binary files a/demos/declarative/snake/pics/blueStar.png and /dev/null differ diff --git a/demos/declarative/snake/pics/blueStone.png b/demos/declarative/snake/pics/blueStone.png deleted file mode 100644 index 356affd..0000000 Binary files a/demos/declarative/snake/pics/blueStone.png and /dev/null differ diff --git a/demos/declarative/snake/pics/cookie.png b/demos/declarative/snake/pics/cookie.png deleted file mode 100644 index aec2957..0000000 Binary files a/demos/declarative/snake/pics/cookie.png and /dev/null differ diff --git a/demos/declarative/snake/pics/eyes.svg b/demos/declarative/snake/pics/eyes.svg deleted file mode 100644 index 1078692..0000000 --- a/demos/declarative/snake/pics/eyes.svg +++ /dev/null @@ -1,118 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - diff --git a/demos/declarative/snake/pics/head.png b/demos/declarative/snake/pics/head.png deleted file mode 100644 index 550e002..0000000 Binary files a/demos/declarative/snake/pics/head.png and /dev/null differ diff --git a/demos/declarative/snake/pics/head.svg b/demos/declarative/snake/pics/head.svg deleted file mode 100644 index 3bf0bd2..0000000 --- a/demos/declarative/snake/pics/head.svg +++ /dev/null @@ -1,134 +0,0 @@ - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - diff --git a/demos/declarative/snake/pics/redStar.png b/demos/declarative/snake/pics/redStar.png deleted file mode 100644 index cd06854..0000000 Binary files a/demos/declarative/snake/pics/redStar.png and /dev/null differ diff --git a/demos/declarative/snake/pics/redStone.png b/demos/declarative/snake/pics/redStone.png deleted file mode 100644 index 9bb7fe4..0000000 Binary files a/demos/declarative/snake/pics/redStone.png and /dev/null differ diff --git a/demos/declarative/snake/pics/skull.png b/demos/declarative/snake/pics/skull.png deleted file mode 100644 index 6318616..0000000 Binary files a/demos/declarative/snake/pics/skull.png and /dev/null differ diff --git a/demos/declarative/snake/pics/snake.jpg b/demos/declarative/snake/pics/snake.jpg deleted file mode 100644 index e91a784..0000000 Binary files a/demos/declarative/snake/pics/snake.jpg and /dev/null differ diff --git a/demos/declarative/snake/pics/star.png b/demos/declarative/snake/pics/star.png deleted file mode 100644 index defbde5..0000000 Binary files a/demos/declarative/snake/pics/star.png and /dev/null differ diff --git a/demos/declarative/snake/pics/stoneShadow.png b/demos/declarative/snake/pics/stoneShadow.png deleted file mode 100644 index 1bd56af..0000000 Binary files a/demos/declarative/snake/pics/stoneShadow.png and /dev/null differ diff --git a/demos/declarative/snake/pics/yellowStar.png b/demos/declarative/snake/pics/yellowStar.png deleted file mode 100644 index 52fb9c4..0000000 Binary files a/demos/declarative/snake/pics/yellowStar.png and /dev/null differ diff --git a/demos/declarative/snake/pics/yellowStone.png b/demos/declarative/snake/pics/yellowStone.png deleted file mode 100644 index c56124a..0000000 Binary files a/demos/declarative/snake/pics/yellowStone.png and /dev/null differ diff --git a/demos/declarative/snake/snake.js b/demos/declarative/snake/snake.js deleted file mode 100644 index 1cea8b7..0000000 --- a/demos/declarative/snake/snake.js +++ /dev/null @@ -1,308 +0,0 @@ - -var snake = new Array; -var board = new Array; -var links = new Array; -var scheduledDirections = new Array; -var numRows = 1; -var numColumns = 1; -var linkComponent = createComponent("Link.qml"); -var cookieComponent = createComponent("Cookie.qml"); -var cookie; -var linksToGrow = 0; -var linksToDie = 0; -var waitForCookie = 0; -var growType = 0; -var skullMovementsBeforeDirectionChange = 0; - - -function rand(n) -{ - return (Math.floor(Math.random() * n)); -} - -function scheduleDirection(dir) -{ - direction = dir; - if(scheduledDirections[scheduledDirections.length-1]!=direction) - scheduledDirections.push(direction); -} - -function startNewGame() -{ - if (state == "starting") - return; - - if (heartbeat.running) { - endGame(); - startNewGameTimer.running = true; - return; - } - numRows = numRowsAvailable; - numColumns = numColumnsAvailable; - board = new Array(numRows * numColumns); - snake = new Array; - scheduledDirections = new Array; - growType = 0; - - skull.z = numRows * numColumns + 1; - - for (var i = 0; i < numRows * numColumns; ++i) { - if (i < links.length) { - var link = links[i]; - link.spawned = false; - link.dying = false; - } else { - if(linkComponent.isReady == false){ - if(linkComponent.isError == true) - print(linkComponent.errorString()); - else - print("Still loading linkComponent"); - continue;//TODO: Better error handling? - } - var link = linkComponent.createObject(); - link.parent = playfield; - link.z = numRows * numColumns + 1 - i; - link.type = i == 0 ? 2 : 0; - link.spawned = false; - link.dying = false; - links.push(link); - } - } - - head = links[0]; - snake.push(head); - head.row = numRows/2 -1; - head.column = numColumns/2 -1; - head.spawned = true; - - linksToGrow = 5; - linksToDie = 0; - waitForCookie = 5; - score = 0; - startHeartbeatTimer.running = true; - heartbeat.running = true; -} - -function endGame() -{ - heartbeat.running = false; - for(var i in snake) - snake[i].dying = true; - if (cookie) { - cookie.dying = true; - cookie = 0; - } - lastScore = score; - highScores.saveScore(lastScore); -} - -function move() { - - if (!head) - return; - - var dir = direction; - - if (scheduledDirections.length) { - dir = scheduledDirections.shift(); - } - - if (state == "starting") { - var turn = (dir - headDirection); - head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn ); - headDirection = dir; - return; - } - - var row = head.row; - var column = head.column; - - if (dir == 0) { - row = row - 1; - } else if (dir == 1) { - column = column + 1 - } else if (dir == 2) { - row = row + 1; - } else if (dir == 3) { - column = column - 1; - } - - //validate the new position - if (row < 0 || row >= numRows - || column < 0 || column >= numColumns - || (row == skull.row && column == skull.column) - || !isFree(row, column)) { - var turn = (dir - headDirection); - head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn ); - headDirection = dir; - endGame(); - return; - } - - var newLink; - if (linksToGrow > 0) { - --linksToGrow; - newLink = links[snake.length]; - newLink.spawned = false; - newLink.rotation = snake[snake.length-1].rotation; - newLink.type = growType; - newLink.dying = false; - snake.push(newLink); - } else { - var lastLink = snake[snake.length-1]; - board[lastLink.row * numColumns + lastLink.column] = Undefined; - } - - if (waitForCookie > 0) { - if (--waitForCookie == 0) - createCookie(cookie? (cookie.value+1) : 1); - } - - for (var i = snake.length-1; i > 0; --i) { - snake[i].row = snake[i-1].row; - snake[i].column = snake[i-1].column; - snake[i].rotation = snake[i-1].rotation; - } - - if (newLink) { - newLink.spawned = true; - } - - // move the head - head.row = row; - head.column = column; - board[row * numColumns + column] = head; - - var turn = (dir - headDirection); - head.rotation += turn == -3 ? 1 : (turn == 3 ? -1 : turn ); - headDirection = dir; - - var value = testCookie(row, column); - if (value > 0) { - linksToGrow += value; - score += value; - } -} - -function isFree(row, column) -{ - return board[row * numColumns + column] == Undefined; -} - -function isHead(row, column) -{ - return head.column == column && head.row == row; -} - -function testCookie(row, column) -{ - if (cookie && !cookie.dying && cookie.row == row && cookie.column == column) { - var value = cookie.value; - waitForCookie = value; - growType = snake[snake.length-1].type == 1 ? 0 : 1; - cookie.dying = true; - cookie.z = numRows * numColumns + 2; - return value; - } - return 0; -} - -function moveSkull() -{ - - if (linksToDie > 0) { - --linksToDie; - var link = snake.pop(); - link.dying = true; - board[link.row * numColumns + link.column] = Undefined; - if (score > 0) - --score; - if (snake.length == 0) { - endGame(); - return; - } - } - - var row = skull.row; - var column = skull.column; - if (isHead(row, column)) { - endGame(); - return; - } - row += skull.verticalMovement; - column += skull.horizontalMovement; - - var attempts = 4; - - while (skullMovementsBeforeDirectionChange == 0 || row < 0 || row >= numRows - || column < 0 || column >= numColumns - || (!isFree(row, column) && !isHead(row, column))) { - var d = rand(8); - skull.verticalMovement = 0; - skull.horizontalMovement = 0; - skullMovementsBeforeDirectionChange = rand(20)+1; - if (d == 0) { - skull.verticalMovement = -1 - } else if (d == 1) { - skull.horizontalMovement = -1; - } else if (d == 2) { - skull.verticalMovement = 1 - } else if (d == 3){ - skull.horizontalMovement = 1; - } else if (cookie) { - var rd = cookie.row - skull.row; - var rc = cookie.column - skull.column; - if (Math.abs(rd) > Math.abs(rc)) { - skull.verticalMovement = rd > 0 ? 1 : -1; - skullMovementsBeforeDirectionChange = Math.abs(rd); - } else { - skull.horizontalMovement= rc > 0 ? 1 : -1; - skullMovementsBeforeDirectionChange = Math.abs(rc); - } - } - row = skull.row + skull.verticalMovement; - column = skull.column + skull.horizontalMovement; - if (--attempts == 0) - return; - } - - skull.row = row; - skull.column = column; - --skullMovementsBeforeDirectionChange; - var value = testCookie(row, column); - if (value > 0) - linksToDie += value/2; - - if (isHead(row, column)) - endGame(); -} - -function createCookie(value) { - if (numRows * numColumns - snake.length < 10) - return; - - var column = rand(numColumns); - var row = rand(numRows); - while (!isFree(row, column)) { - column++; - if (column == numColumns) { - column = 0; - row++; - if (row == numRows) - row = 0; - } - } - - if(cookieComponent.isReady == false){ - if(cookieComponent.isError == true) - print(cookieComponent.errorString()); - else - print("Still loading cookieComponent"); - return;//TODO: Better error handling? - } - cookie = cookieComponent.createObject(); - cookie.parent = head.parent; - cookie.value = value; - cookie.row = row; - cookie.column = column; -} diff --git a/demos/declarative/snake/snake.qml b/demos/declarative/snake/snake.qml new file mode 100644 index 0000000..58827a7 --- /dev/null +++ b/demos/declarative/snake/snake.qml @@ -0,0 +1,190 @@ +import Qt 4.6 +import "content" + +Rectangle { + id: screen; + SystemPalette { id: activePalette } + color: activePalette.window + + Script { source: "content/snake.js" } + + property int gridSize : 34 + property int margin: 4 + property int numRowsAvailable: Math.floor((height-32-2*margin)/gridSize) + property int numColumnsAvailable: Math.floor((width-2*margin)/gridSize) + + property int lastScore : 0 + + property int score: 0; + property int heartbeatInterval: 200 + property int halfbeatInterval: 160 + + width: 480 + height: 750 + + property int direction + property int headDirection + + property var head; + + HighScoreModel { + id: highScores + game: "Snake" + } + + Timer { + id: heartbeat; + interval: heartbeatInterval; + repeat: true + onTriggered: { move() } + } + Timer { + id: halfbeat; + interval: halfbeatInterval; + repeat: true + running: heartbeat.running + onTriggered: { moveSkull() } + } + Timer { + id: startNewGameTimer; + interval: 700; + onTriggered: {startNewGame(); } + } + + Timer { + id: startHeartbeatTimer; + interval: 1000 ; + } + + + Image { + Image { + id: title + source: "content/pics/snake.jpg" + fillMode: "PreserveAspectCrop" + anchors.fill: parent + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + opacity: Behavior { NumberAnimation { duration: 500 } } + + Text { + color: "white" + font.pointSize: 24 + horizontalAlignment: "AlignHCenter" + text: "Last Score:\t" + lastScore + "\nHighscore:\t" + highScores.topScore; + anchors.horizontalCenter: parent.horizontalCenter + anchors.bottom: parent.bottom + anchors.bottomMargin: gridSize + } + } + + source: "content/pics/background.png" + fillMode: "PreserveAspectCrop" + + anchors.left: parent.left + anchors.right: parent.right + anchors.top: parent.top + anchors.bottom: toolbar.top + + Rectangle { + id: playfield + border.width: 1 + border.color: "white" + color: "transparent" + anchors.horizontalCenter: parent.horizontalCenter + y: (screen.height - 32 - height)/2; + width: numColumnsAvailable * gridSize + 2*margin + height: numRowsAvailable * gridSize + 2*margin + + + Skull { + id: skull + } + + MouseRegion { + anchors.fill: parent + onPressed: { + if (!head || !heartbeat.running) { + startNewGame(); + return; + } + if (direction == 0 || direction == 2) + scheduleDirection((mouseX > (head.x + head.width/2)) ? 1 : 3); + else + scheduleDirection((mouseY > (head.y + head.height/2)) ? 2 : 0); + } + } + } + + } + + Rectangle { + id: progressBar + opacity: 0 + opacity: Behavior { NumberAnimation { duration: 200 } } + color: "transparent" + border.width: 2 + border.color: "#221edd" + x: 50 + y: 50 + width: 200 + height: 30 + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter + anchors.verticalCenterOffset: 40 + + Rectangle { + id: progressIndicator + color: "#221edd"; + width: 0; + width: Behavior { NumberAnimation { duration: startHeartbeatTimer.running ? 1000 : 0}} + height: 30; + } + } + + Rectangle { + id: toolbar + color: activePalette.window + height: 32; width: parent.width + anchors.bottom: screen.bottom + + Button { + id: btnA; text: "New Game"; onClicked: {startNewGame();} + anchors.left: parent.left; anchors.leftMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + + Text { + color: activePalette.text + text: "Score: " + score; font.bold: true + anchors.right: parent.right; anchors.rightMargin: 3 + anchors.verticalCenter: parent.verticalCenter + } + } + + focus: true + Keys.onSpacePressed: startNewGame(); + Keys.onLeftPressed: if (state == "starting" || direction != 1) scheduleDirection(3); + Keys.onRightPressed: if (state == "starting" || direction != 3) scheduleDirection(1); + Keys.onUpPressed: if (state == "starting" || direction != 2) scheduleDirection(0); + Keys.onDownPressed: if (state == "starting" || direction != 0) scheduleDirection(2); + + states: [ + State { + name: "starting" + when: startHeartbeatTimer.running; + PropertyChanges {target: progressIndicator; width: 200} + PropertyChanges {target: title; opacity: 0} + PropertyChanges {target: progressBar; opacity: 1} + }, + State { + name: "running" + when: heartbeat.running + PropertyChanges {target: progressIndicator; width: 200} + PropertyChanges {target: title; opacity: 0} + PropertyChanges {target: skull; row: 0; column: 0; } + PropertyChanges {target: skull; spawned: 1} + } + ] + +} -- cgit v0.12 From ba6816d194423d220ba34a5e0b57aa5b97af942c Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 11:49:48 +1000 Subject: Move choice of pipelining to the point request. (based on advice from Thiago) --- src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp | 1 + src/declarative/graphicsitems/qmlgraphicsimagebase.cpp | 2 -- src/declarative/graphicsitems/qmlgraphicsitem.cpp | 2 -- src/declarative/util/qmlfontloader.cpp | 1 + src/declarative/util/qmlpixmapcache.cpp | 1 + tools/qmlviewer/qmlviewer.cpp | 13 ------------- 6 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp b/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp index 670b38b..e01e569 100644 --- a/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp @@ -238,6 +238,7 @@ void QmlGraphicsAnimatedImage::setSource(const QUrl &url) #endif d->status = Loading; QNetworkRequest req(d->url); + req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); d->reply = qmlEngine(this)->networkAccessManager()->get(req); QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(movieRequestFinished())); diff --git a/src/declarative/graphicsitems/qmlgraphicsimagebase.cpp b/src/declarative/graphicsitems/qmlgraphicsimagebase.cpp index e3b98aa..08617ac 100644 --- a/src/declarative/graphicsitems/qmlgraphicsimagebase.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsimagebase.cpp @@ -45,8 +45,6 @@ #include #include -#include -#include #include QT_BEGIN_NAMESPACE diff --git a/src/declarative/graphicsitems/qmlgraphicsitem.cpp b/src/declarative/graphicsitems/qmlgraphicsitem.cpp index aa73444..dd685d4 100644 --- a/src/declarative/graphicsitems/qmlgraphicsitem.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsitem.cpp @@ -56,8 +56,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/src/declarative/util/qmlfontloader.cpp b/src/declarative/util/qmlfontloader.cpp index 8230b1a..42b34b5 100644 --- a/src/declarative/util/qmlfontloader.cpp +++ b/src/declarative/util/qmlfontloader.cpp @@ -134,6 +134,7 @@ void QmlFontLoader::setSource(const QUrl &url) #endif { QNetworkRequest req(d->url); + req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); d->reply = qmlEngine(this)->networkAccessManager()->get(req); QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(replyFinished())); } diff --git a/src/declarative/util/qmlpixmapcache.cpp b/src/declarative/util/qmlpixmapcache.cpp index 3307ff8..6f36cad 100644 --- a/src/declarative/util/qmlpixmapcache.cpp +++ b/src/declarative/util/qmlpixmapcache.cpp @@ -404,6 +404,7 @@ QmlPixmapReply *QmlPixmapCache::request(QmlEngine *engine, const QUrl &url) QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies.find(key); if (iter == qmlActivePixmapReplies.end()) { QNetworkRequest req(url); + req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); QmlPixmapReply *item = new QmlPixmapReply(key, engine->networkAccessManager()->get(req)); iter = qmlActivePixmapReplies.insert(key, item); } else { diff --git a/tools/qmlviewer/qmlviewer.cpp b/tools/qmlviewer/qmlviewer.cpp index 60208d2..21288c4 100644 --- a/tools/qmlviewer/qmlviewer.cpp +++ b/tools/qmlviewer/qmlviewer.cpp @@ -291,18 +291,6 @@ private: QString customargs; }; -class ConfiguredNetworkAccessManager : public QNetworkAccessManager { -public: - ConfiguredNetworkAccessManager(QObject *parent) : QNetworkAccessManager(parent) { } - - QNetworkReply *createRequest (Operation op, const QNetworkRequest &req, QIODevice * outgoingData) - { - QNetworkRequest request = req; - request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); - return QNetworkAccessManager::createRequest(op,request,outgoingData); - } -}; - class PersistentCookieJar : public QNetworkCookieJar { public: PersistentCookieJar(QObject *parent) : QNetworkCookieJar(parent) { load(); } @@ -383,7 +371,6 @@ QmlViewer::QmlViewer(QWidget *parent, Qt::WindowFlags flags) canvas->setAttribute(Qt::WA_OpaquePaintEvent); canvas->setAttribute(Qt::WA_NoSystemBackground); canvas->setContentResizable(!skin || !scaleSkin); - canvas->engine()->setNetworkAccessManager(new ConfiguredNetworkAccessManager(canvas->engine())); canvas->setFocus(); QObject::connect(canvas, SIGNAL(sceneResized(QSize)), this, SLOT(sceneResized(QSize))); -- cgit v0.12 From 1d64c1669bded1a917046a7fc3ff1c9d1b64e247 Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 12:16:42 +1000 Subject: Compile. --- .../declarative/qmlgraphicslistview/data/listview-sections.qml | 2 +- .../qmlgraphicslistview/tst_qmlgraphicslistview.cpp | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml b/tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml index 2edc0bd..4b5bea6 100644 --- a/tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml +++ b/tests/auto/declarative/qmlgraphicslistview/data/listview-sections.qml @@ -54,6 +54,6 @@ Rectangle { height: 320 model: testModel delegate: myDelegate - sectionExpression: "Math.floor(number/5)" + section.property: "number" } } diff --git a/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp b/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp index 7b5cfc0..87d4649 100644 --- a/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp +++ b/tests/auto/declarative/qmlgraphicslistview/tst_qmlgraphicslistview.cpp @@ -160,6 +160,14 @@ public: } } + QVariant data(int index, int role) const + { + if (role==0) + return list.at(index).first; + if (role==1) + return list.at(index).second; + return QVariant(); + } QHash data(int index, const QList &roles) const { QHash returnHash; @@ -892,8 +900,6 @@ void tst_QmlGraphicsListView::sections() QmlGraphicsItem *viewport = listview->viewport(); QVERIFY(viewport != 0); - QCOMPARE(listview->sectionExpression(), QLatin1String("Math.floor(number/5)")); - // Confirm items positioned correctly int itemCount = findItems(viewport, "wrapper").count(); for (int i = 0; i < model.count() && i < itemCount; ++i) { -- cgit v0.12 From 7b84d6ab53d2ba0790cd4605260b2bad96275349 Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 12:20:29 +1000 Subject: Compile outside src/declarative (tests) --- src/declarative/graphicsitems/qmlgraphicspositioners_p.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/declarative/graphicsitems/qmlgraphicspositioners_p.h b/src/declarative/graphicsitems/qmlgraphicspositioners_p.h index 0e6d134..d6711f6 100644 --- a/src/declarative/graphicsitems/qmlgraphicspositioners_p.h +++ b/src/declarative/graphicsitems/qmlgraphicspositioners_p.h @@ -45,7 +45,7 @@ #include "qmlgraphicsitem.h" #include "../util/qmlstate_p.h" -#include "qpodvector_p.h" +#include #include #include -- cgit v0.12 From 271f074962aef959962eb02d1af893c77a839dda Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 12:20:56 +1000 Subject: Remove unused signal. --- src/declarative/graphicsitems/qmlgraphicslistview_p.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/declarative/graphicsitems/qmlgraphicslistview_p.h b/src/declarative/graphicsitems/qmlgraphicslistview_p.h index 2a2ef8a..79d678a 100644 --- a/src/declarative/graphicsitems/qmlgraphicslistview_p.h +++ b/src/declarative/graphicsitems/qmlgraphicslistview_p.h @@ -202,7 +202,6 @@ Q_SIGNALS: void orientationChanged(); void currentIndexChanged(); void currentSectionChanged(); - void sectionExpressionChanged(); void highlightMoveSpeedChanged(); void highlightResizeSpeedChanged(); void highlightChanged(); -- cgit v0.12 From f4c5b030db674f9a76a0156d55c5140420c400dd Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Mon, 11 Jan 2010 12:35:51 +1000 Subject: Compile (will still fail) --- tests/auto/declarative/qmlinstruction/tst_qmlinstruction.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/auto/declarative/qmlinstruction/tst_qmlinstruction.cpp b/tests/auto/declarative/qmlinstruction/tst_qmlinstruction.cpp index bfffc43..43c64cd 100644 --- a/tests/auto/declarative/qmlinstruction/tst_qmlinstruction.cpp +++ b/tests/auto/declarative/qmlinstruction/tst_qmlinstruction.cpp @@ -388,7 +388,7 @@ void tst_qmlinstruction::dump() { QmlInstruction i; i.line = 34; - i.type = QmlInstruction::StoreObjPropBinding; + //i.type = QmlInstruction::StoreObjPropBinding; // removed! i.assignObjPropBinding.property = 28; i.assignObjPropBinding.contextIdx = 3; i.assignObjPropBinding.context = 7; -- cgit v0.12 From b6a7d7cc9c4eaab53213d0116e4f1d5cbd267004 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 11 Jan 2010 12:58:38 +1000 Subject: More text benchmarks. --- tests/benchmarks/declarative/text/tst_text.cpp | 75 +++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/tests/benchmarks/declarative/text/tst_text.cpp b/tests/benchmarks/declarative/text/tst_text.cpp index e69ecd8..c7ea884 100644 --- a/tests/benchmarks/declarative/text/tst_text.cpp +++ b/tests/benchmarks/declarative/text/tst_text.cpp @@ -62,6 +62,11 @@ private slots: void document(); void paintDocToPixmap(); void paintDocToPixmap_painterFill(); + void control(); + void paintControlToPixmap(); + void paintControlToPixmap_painterFill(); + void constructControl(); + void constructDocument(); private: QString m_text; @@ -144,6 +149,48 @@ void tst_text::paintLayoutToPixmap_painterFill() void tst_text::document() { + QTextDocument *doc = new QTextDocument; + + QBENCHMARK { + QTextDocument *doc = new QTextDocument; + doc->setHtml(m_text); + } +} + +void tst_text::paintDocToPixmap() +{ + QTextDocument *doc = new QTextDocument; + doc->setHtml(m_text); + doc->setTextWidth(300); + QSize size = doc->size().toSize(); + + QBENCHMARK { + QPixmap img(size); + img.fill(Qt::transparent); + QPainter p(&img); + doc->drawContents(&p/*, QRectF(QPointF(0, 0), QSizeF(size))*/); + } +} + +void tst_text::paintDocToPixmap_painterFill() +{ + QTextDocument *doc = new QTextDocument; + doc->setHtml(m_text); + doc->setTextWidth(300); + QSize size = doc->size().toSize(); + + QBENCHMARK { + QPixmap img(size); + QPainter p(&img); + p.setCompositionMode(QPainter::CompositionMode_Source); + p.fillRect(0, 0, img.width(), img.height(), Qt::transparent); + p.setCompositionMode(QPainter::CompositionMode_SourceOver); + doc->drawContents(&p/*, QRectF(QPointF(0, 0), QSizeF(size))*/); + } +} + +void tst_text::control() +{ QTextControl *control = new QTextControl(m_text); QBENCHMARK { @@ -153,11 +200,12 @@ void tst_text::document() } } -void tst_text::paintDocToPixmap() +void tst_text::paintControlToPixmap() { QTextControl *control = new QTextControl; QTextDocument *doc = control->document(); doc->setHtml(m_text); + doc->setTextWidth(300); QSize size = doc->size().toSize(); QBENCHMARK { @@ -168,11 +216,12 @@ void tst_text::paintDocToPixmap() } } -void tst_text::paintDocToPixmap_painterFill() +void tst_text::paintControlToPixmap_painterFill() { QTextControl *control = new QTextControl; QTextDocument *doc = control->document(); doc->setHtml(m_text); + doc->setTextWidth(300); QSize size = doc->size().toSize(); QBENCHMARK { @@ -185,5 +234,27 @@ void tst_text::paintDocToPixmap_painterFill() } } +void tst_text::constructControl() +{ + QTextControl *control = new QTextControl; + delete control; + + QBENCHMARK { + QTextControl *control = new QTextControl; + delete control; + } +} + +void tst_text::constructDocument() +{ + QTextDocument *doc = new QTextDocument; + delete doc; + + QBENCHMARK { + QTextDocument *doc = new QTextDocument; + delete doc; + } +} + QTEST_MAIN(tst_text) #include "tst_text.moc" -- cgit v0.12 From 8585ba96eddafaeefea8ca493c6a6d1bf9635cfc Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 11 Jan 2010 13:20:12 +1000 Subject: Compile with QT_NO_CURSOR. --- src/declarative/qml/qmlmetatype.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/declarative/qml/qmlmetatype.cpp b/src/declarative/qml/qmlmetatype.cpp index 0b79a21..d9c007d 100644 --- a/src/declarative/qml/qmlmetatype.cpp +++ b/src/declarative/qml/qmlmetatype.cpp @@ -1102,9 +1102,11 @@ bool QmlMetaType::copy(int type, void *data, const void *copy) case QMetaType::QBitmap: *static_cast(data) = *static_cast(copy); return true; +#ifndef QT_NO_CURSOR case QMetaType::QCursor: *static_cast(data) = *static_cast(copy); return true; +#endif case QMetaType::QSizePolicy: *static_cast(data) = *static_cast(copy); return true; @@ -1308,9 +1310,11 @@ bool QmlMetaType::copy(int type, void *data, const void *copy) case QMetaType::QBitmap: *static_cast(data) = NS(QBitmap)(); return true; +#ifndef QT_NO_CURSOR case QMetaType::QCursor: *static_cast(data) = NS(QCursor)(); return true; +#endif case QMetaType::QSizePolicy: *static_cast(data) = NS(QSizePolicy)(); return true; -- cgit v0.12 From 831729db10ffa21a036411de67b72caf732054de Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 11 Jan 2010 14:20:20 +1000 Subject: Remove extra semicolons. --- src/declarative/util/qmlstate_p.h | 2 +- src/declarative/util/qmltransition_p.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/declarative/util/qmlstate_p.h b/src/declarative/util/qmlstate_p.h index 95a6fc5..fdac847 100644 --- a/src/declarative/util/qmlstate_p.h +++ b/src/declarative/util/qmlstate_p.h @@ -133,7 +133,7 @@ class Q_DECLARATIVE_EXPORT QmlState : public QObject Q_PROPERTY(QString extend READ extends WRITE setExtends) Q_PROPERTY(QmlList* changes READ changes) Q_CLASSINFO("DefaultProperty", "changes") - Q_CLASSINFO("DeferredPropertyNames", "changes"); + Q_CLASSINFO("DeferredPropertyNames", "changes") public: QmlState(QObject *parent=0); diff --git a/src/declarative/util/qmltransition_p.h b/src/declarative/util/qmltransition_p.h index 0a9b036..c1a6f66 100644 --- a/src/declarative/util/qmltransition_p.h +++ b/src/declarative/util/qmltransition_p.h @@ -67,7 +67,7 @@ class Q_DECLARATIVE_EXPORT QmlTransition : public QObject Q_PROPERTY(bool reversible READ reversible WRITE setReversible) Q_PROPERTY(QmlList* animations READ animations) Q_CLASSINFO("DefaultProperty", "animations") - Q_CLASSINFO("DeferredPropertyNames", "animations"); + Q_CLASSINFO("DeferredPropertyNames", "animations") public: QmlTransition(QObject *parent=0); -- cgit v0.12