From 27d22c5f4201c7555b42e187252bee3fd88d3821 Mon Sep 17 00:00:00 2001 From: Topi Reinio Date: Thu, 13 Jun 2013 15:13:32 +0200 Subject: Doc: Move Qt Quick 1 demos and their documentation When installing the examples, we currently end up with a QT_INSTALL_EXAMPLES/demos directory with nothing but documentation files. This change moves the docs and also the demos themselves into examples/declarative/demos. Also fixes the links in demo .qdoc files. Task-number: QTBUG-31722 Change-Id: Ied15526be1db8437e14be786ea69eae98af8dd3f Reviewed-by: Jerome Pasion --- .../demos/snake/qml/snake/content/Button.qml | 66 +++++ .../demos/snake/qml/snake/content/Cookie.qml | 93 ++++++ .../snake/qml/snake/content/HighScoreModel.qml | 139 +++++++++ .../demos/snake/qml/snake/content/Link.qml | 128 +++++++++ .../demos/snake/qml/snake/content/Skull.qml | 62 ++++ .../demos/snake/qml/snake/content/pics/README | 1 + .../snake/qml/snake/content/pics/background.png | Bin 0 -> 144508 bytes .../snake/qml/snake/content/pics/blueStar.png | Bin 0 -> 272 bytes .../snake/qml/snake/content/pics/blueStone.png | Bin 0 -> 3139 bytes .../demos/snake/qml/snake/content/pics/cookie.png | Bin 0 -> 2729 bytes .../demos/snake/qml/snake/content/pics/eyes.svg | 118 ++++++++ .../demos/snake/qml/snake/content/pics/head.png | Bin 0 -> 3473 bytes .../demos/snake/qml/snake/content/pics/head.svg | 134 +++++++++ .../demos/snake/qml/snake/content/pics/pause.png | Bin 0 -> 4327 bytes .../demos/snake/qml/snake/content/pics/redStar.png | Bin 0 -> 273 bytes .../snake/qml/snake/content/pics/redStone.png | Bin 0 -> 3456 bytes .../demos/snake/qml/snake/content/pics/skull.png | Bin 0 -> 2543 bytes .../demos/snake/qml/snake/content/pics/snake.jpg | Bin 0 -> 663901 bytes .../demos/snake/qml/snake/content/pics/star.png | Bin 0 -> 262 bytes .../snake/qml/snake/content/pics/stoneShadow.png | Bin 0 -> 1699 bytes .../snake/qml/snake/content/pics/yellowStar.png | Bin 0 -> 276 bytes .../snake/qml/snake/content/pics/yellowStone.png | Bin 0 -> 2667 bytes .../demos/snake/qml/snake/content/snake.js | 316 +++++++++++++++++++++ 23 files changed, 1057 insertions(+) create mode 100644 examples/declarative/demos/snake/qml/snake/content/Button.qml create mode 100644 examples/declarative/demos/snake/qml/snake/content/Cookie.qml create mode 100644 examples/declarative/demos/snake/qml/snake/content/HighScoreModel.qml create mode 100644 examples/declarative/demos/snake/qml/snake/content/Link.qml create mode 100644 examples/declarative/demos/snake/qml/snake/content/Skull.qml create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/README create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/background.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/blueStar.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/blueStone.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/cookie.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/eyes.svg create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/head.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/head.svg create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/pause.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/redStar.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/redStone.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/skull.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/snake.jpg create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/star.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/stoneShadow.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/yellowStar.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/pics/yellowStone.png create mode 100644 examples/declarative/demos/snake/qml/snake/content/snake.js (limited to 'examples/declarative/demos/snake/qml/snake/content') diff --git a/examples/declarative/demos/snake/qml/snake/content/Button.qml b/examples/declarative/demos/snake/qml/snake/content/Button.qml new file mode 100644 index 00000000..322431d3 --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/Button.qml @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 1.0 + +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 } + } + + MouseArea { 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/demos/snake/qml/snake/content/Cookie.qml b/examples/declarative/demos/snake/qml/snake/content/Cookie.qml new file mode 100644 index 00000000..d101445a --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/Cookie.qml @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 1.0 +import Qt.labs.particles 1.0 + +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 + 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 } + } + ] + transitions: [ + Transition { + NumberAnimation { target: img; property: "opacity"; duration: 100 } + } + ] +} diff --git a/examples/declarative/demos/snake/qml/snake/content/HighScoreModel.qml b/examples/declarative/demos/snake/qml/snake/content/HighScoreModel.qml new file mode 100644 index 00000000..c9a81831 --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/HighScoreModel.qml @@ -0,0 +1,139 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 1.0 + +// 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 + + 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 <= ?", + [game, 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/examples/declarative/demos/snake/qml/snake/content/Link.qml b/examples/declarative/demos/snake/qml/snake/content/Link.qml new file mode 100644 index 00000000..f5658e95 --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/Link.qml @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 1.0 +import Qt.labs.particles 1.0 + +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 + Behavior on x { NumberAnimation { duration: spawned ? heartbeatInterval : 0} } + Behavior on y { 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 + Behavior on angle { + RotationAnimation{ + direction: RotationAnimation.Shortest + duration: spawned ? 200 : 0 + } + } + } + } + + Image { + source: "pics/stoneShadow.png" + } + + opacity: 0 + } + + + 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 } + } + ] + + transitions: [ + Transition { + NumberAnimation { target: img; property: "opacity"; duration: 200 } + } + ] + +} diff --git a/examples/declarative/demos/snake/qml/snake/content/Skull.qml b/examples/declarative/demos/snake/qml/snake/content/Skull.qml new file mode 100644 index 00000000..4242b708 --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/Skull.qml @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 1.0 + +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 + Behavior on x { NumberAnimation { duration: spawned ? halfbeatInterval : 0} } + Behavior on y { NumberAnimation { duration: spawned ? halfbeatInterval : 0 } } + + opacity: spawned ? 1 : 0 + Behavior on opacity { NumberAnimation { duration: 200 } } + + source: "pics/skull.png" + width: 24 + height: 40 +} diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/README b/examples/declarative/demos/snake/qml/snake/content/pics/README new file mode 100644 index 00000000..0215132c --- /dev/null +++ b/examples/declarative/demos/snake/qml/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/examples/declarative/demos/snake/qml/snake/content/pics/background.png b/examples/declarative/demos/snake/qml/snake/content/pics/background.png new file mode 100644 index 00000000..72dffaa8 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/background.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/blueStar.png b/examples/declarative/demos/snake/qml/snake/content/pics/blueStar.png new file mode 100644 index 00000000..ba7acabd Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/blueStar.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/blueStone.png b/examples/declarative/demos/snake/qml/snake/content/pics/blueStone.png new file mode 100644 index 00000000..356affdd Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/blueStone.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/cookie.png b/examples/declarative/demos/snake/qml/snake/content/pics/cookie.png new file mode 100644 index 00000000..aec2957f Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/cookie.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/eyes.svg b/examples/declarative/demos/snake/qml/snake/content/pics/eyes.svg new file mode 100644 index 00000000..10786927 --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/pics/eyes.svg @@ -0,0 +1,118 @@ + + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/head.png b/examples/declarative/demos/snake/qml/snake/content/pics/head.png new file mode 100644 index 00000000..550e0026 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/head.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/head.svg b/examples/declarative/demos/snake/qml/snake/content/pics/head.svg new file mode 100644 index 00000000..3bf0bd2f --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/pics/head.svg @@ -0,0 +1,134 @@ + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/pause.png b/examples/declarative/demos/snake/qml/snake/content/pics/pause.png new file mode 100644 index 00000000..056d97dd Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/pause.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/redStar.png b/examples/declarative/demos/snake/qml/snake/content/pics/redStar.png new file mode 100644 index 00000000..cd068547 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/redStar.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/redStone.png b/examples/declarative/demos/snake/qml/snake/content/pics/redStone.png new file mode 100644 index 00000000..9bb7fe42 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/redStone.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/skull.png b/examples/declarative/demos/snake/qml/snake/content/pics/skull.png new file mode 100644 index 00000000..63186167 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/skull.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/snake.jpg b/examples/declarative/demos/snake/qml/snake/content/pics/snake.jpg new file mode 100644 index 00000000..e91a784f Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/snake.jpg differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/star.png b/examples/declarative/demos/snake/qml/snake/content/pics/star.png new file mode 100644 index 00000000..defbde53 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/star.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/stoneShadow.png b/examples/declarative/demos/snake/qml/snake/content/pics/stoneShadow.png new file mode 100644 index 00000000..1bd56afd Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/stoneShadow.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/yellowStar.png b/examples/declarative/demos/snake/qml/snake/content/pics/yellowStar.png new file mode 100644 index 00000000..52fb9c4b Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/yellowStar.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/pics/yellowStone.png b/examples/declarative/demos/snake/qml/snake/content/pics/yellowStone.png new file mode 100644 index 00000000..c56124a8 Binary files /dev/null and b/examples/declarative/demos/snake/qml/snake/content/pics/yellowStone.png differ diff --git a/examples/declarative/demos/snake/qml/snake/content/snake.js b/examples/declarative/demos/snake/qml/snake/content/snake.js new file mode 100644 index 00000000..837b82ad --- /dev/null +++ b/examples/declarative/demos/snake/qml/snake/content/snake.js @@ -0,0 +1,316 @@ + +var snake = new Array; +var board = new Array; +var links = new Array; +var scheduledDirections = new Array; +var numRows = 1; +var numColumns = 1; +var linkComponent = Qt.createComponent("content/Link.qml"); // XXX should resolve relative to script, not component +var cookieComponent = Qt.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) +{ + if (state == "starting") { + direction = dir; + headDirection = direction; + head.rotation = headDirection; + } else if (state == "running"){ + direction = dir; + if(scheduledDirections[scheduledDirections.length-1]!=direction) + scheduledDirections.push(direction); + } +} + +function startNewGame() +{ + if (state == "starting") { + return; + } + + if (activeGame) { + endGame(); + startNewGameTimer.running = true; + return; + } + + state = "starting"; + + 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.status != Component.Ready) { + if(linkComponent.status == Component.Error) + console.log(linkComponent.errorString()); + else + console.log("Still loading linkComponent"); + continue;//TODO: Better error handling? + } + var link = linkComponent.createObject(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; +} + +function endGame() +{ + activeGame = false; + for(var i in snake) + snake[i].dying = true; + if (cookie) { + cookie.dying = true; + cookie = 0; + } + lastScore = score; + highScores.saveScore(lastScore); + state = ""; +} + +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.status != Component.Ready) { + if(cookieComponent.status == Component.Error) + console.log(cookieComponent.errorString()); + else + console.log("Still loading cookieComponent"); + return;//TODO: Better error handling? + } + cookie = cookieComponent.createObject(head.parent); + cookie.value = value; + cookie.row = row; + cookie.column = column; +} -- cgit v1.2.3