From b2e97817684f40ab9353fa279a4c02ef9c12e13d Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Tue, 19 Jun 2012 15:47:09 +1000 Subject: Remove the 'special' naming of examples. Just go back to the naming convention we had before. Change-Id: Iefecb7a5e0ab3aa4d22267b4f475a8e9c1c96058 Reviewed-by: Lorn Potter --- examples/sensors/maze/Congratulation.qml | 61 +++++ examples/sensors/maze/LabyrinthSquare.qml | 80 ++++++ examples/sensors/maze/Mouse.qml | 96 +++++++ .../sensors/maze/components/ApplicationWindow.qml | 51 ++++ examples/sensors/maze/components/Button.qml | 75 ++++++ .../images/button_background_disabled.png | Bin 0 -> 579 bytes .../components/images/button_background_normal.png | Bin 0 -> 901 bytes .../images/button_background_pressed.png | Bin 0 -> 334 bytes examples/sensors/maze/content/00.png | Bin 0 -> 186 bytes examples/sensors/maze/content/01.png | Bin 0 -> 708 bytes examples/sensors/maze/content/cheese.png | Bin 0 -> 317 bytes examples/sensors/maze/content/cheeseeating.gif | Bin 0 -> 650 bytes examples/sensors/maze/content/congratulations.gif | Bin 0 -> 10811 bytes examples/sensors/maze/content/mouse_down.gif | Bin 0 -> 974 bytes examples/sensors/maze/content/start.png | Bin 0 -> 677 bytes examples/sensors/maze/icon.png | Bin 0 -> 4693 bytes examples/sensors/maze/info.json | 15 ++ examples/sensors/maze/lib.js | 264 +++++++++++++++++++ examples/sensors/maze/maze.pro | 4 + examples/sensors/maze/maze.qml | 293 +++++++++++++++++++++ examples/sensors/maze/qml.pro | 17 ++ 21 files changed, 956 insertions(+) create mode 100644 examples/sensors/maze/Congratulation.qml create mode 100644 examples/sensors/maze/LabyrinthSquare.qml create mode 100644 examples/sensors/maze/Mouse.qml create mode 100644 examples/sensors/maze/components/ApplicationWindow.qml create mode 100644 examples/sensors/maze/components/Button.qml create mode 100644 examples/sensors/maze/components/images/button_background_disabled.png create mode 100644 examples/sensors/maze/components/images/button_background_normal.png create mode 100644 examples/sensors/maze/components/images/button_background_pressed.png create mode 100644 examples/sensors/maze/content/00.png create mode 100644 examples/sensors/maze/content/01.png create mode 100644 examples/sensors/maze/content/cheese.png create mode 100644 examples/sensors/maze/content/cheeseeating.gif create mode 100644 examples/sensors/maze/content/congratulations.gif create mode 100644 examples/sensors/maze/content/mouse_down.gif create mode 100644 examples/sensors/maze/content/start.png create mode 100644 examples/sensors/maze/icon.png create mode 100644 examples/sensors/maze/info.json create mode 100644 examples/sensors/maze/lib.js create mode 100644 examples/sensors/maze/maze.pro create mode 100644 examples/sensors/maze/maze.qml create mode 100644 examples/sensors/maze/qml.pro (limited to 'examples/sensors/maze') diff --git a/examples/sensors/maze/Congratulation.qml b/examples/sensors/maze/Congratulation.qml new file mode 100644 index 00000000..a8d6c41c --- /dev/null +++ b/examples/sensors/maze/Congratulation.qml @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//Import the declarative plugins +import QtQuick 2.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +//Implementation of the Congratulation control +Item { + x: Lib.cellDimension + y: Lib.cellDimension + width: Lib.cellDimension * (Lib.dimension - 2) + height: Lib.cellDimension * (Lib.dimension - 2) + + //Containing a animated gif image + AnimatedImage { + id: img + anchors.fill: parent + visible: true + source: "content/congratulations.gif" + } +} diff --git a/examples/sensors/maze/LabyrinthSquare.qml b/examples/sensors/maze/LabyrinthSquare.qml new file mode 100644 index 00000000..e5aacfcd --- /dev/null +++ b/examples/sensors/maze/LabyrinthSquare.qml @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//Import the declarative plugins +import QtQuick 2.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +//Implementation of the Labyrinth square control. +Rectangle { + id: sq + x: 0 + y: 0 + width: Lib.cellDimension + height: Lib.cellDimension + color: "white" + property int val: 0 + property AnimatedImage picture: img + + //Dependend of its position and the labyrinth value a square can be the start, cheese, empty or a wall + AnimatedImage { + id: img + anchors.fill: parent + source: "" + } + + function updateImage() + { + if (sq.val == -1) + sq.picture.source = "content/start.png"; + else if (sq.val == 3) + sq.picture.source = "content/cheese.png"; + else if (sq.val == 4){ + sq.picture.source = "content/cheeseeating.gif"; + sq.picture.playing = true; + } + else if (sq.val == 1) + sq.picture.source = "content/01.png"; + else + sq.picture.source = "content/00.png"; + } +} diff --git a/examples/sensors/maze/Mouse.qml b/examples/sensors/maze/Mouse.qml new file mode 100644 index 00000000..434acebb --- /dev/null +++ b/examples/sensors/maze/Mouse.qml @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//Import the declarative plugins +import QtQuick 2.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +//Implementation of the Mouse control. +Item { + id: mouse + x: 0 + y: 0 + width: Lib.cellDimension + height: Lib.cellDimension + property int angle + + AnimatedImage { + id: img + source: "content/mouse_down.gif" + anchors.fill: parent + visible: true + } + + function distance(origX, origY, newX, newY) { + return Math.sqrt((Math.pow((newX - origX),2)) + (Math.pow((newY - origY),2))) + } + + //Function for moving the mouse + function move(newx, newy) + { + if (mouse.x === newx && mouse.y === newy) + return + // somehow this actually works +//! [0] + var a = newy - mouse.y + var b = newx - mouse.x + var c = distance(mouse.x, mouse.y, newx, newy) + var radians_to_degrees = 57.2957795 + + if (a > 0) + angle = -Math.acos(a / b) * radians_to_degrees + else + angle = -Math.asin(b / c) * radians_to_degrees + if (b > 0) + angle = -Math.acos(a / c) * radians_to_degrees + else + angle = Math.acos(a / c) * radians_to_degrees + + if (angle < 0) + angle = 360 + angle + + img.rotation = angle + mouse.x = newx; + mouse.y = newy; +//! [0] + } +} diff --git a/examples/sensors/maze/components/ApplicationWindow.qml b/examples/sensors/maze/components/ApplicationWindow.qml new file mode 100644 index 00000000..cab5e94c --- /dev/null +++ b/examples/sensors/maze/components/ApplicationWindow.qml @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//Import the declarative plugins +import QtQuick 2.0 + +Rectangle { + id: appWnd + x: 0 + y: 0 + width: 320 + height: 480 + color: "#ececec" +} diff --git a/examples/sensors/maze/components/Button.qml b/examples/sensors/maze/components/Button.qml new file mode 100644 index 00000000..537a8bce --- /dev/null +++ b/examples/sensors/maze/components/Button.qml @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//Import the declarative plugins +import QtQuick 2.0 + +//Implementation of the Button control. +Item { + id: button + width: 30 + height: 100 + property alias text: innerText.text + signal clicked + + Image { + id: backgroundImage + anchors.fill: parent + source: (button.enabled ? "images/button_background_normal.png" : "images/button_background_disabled.png") + } + + Text { + id: innerText + anchors.centerIn: parent + color: "white" + font.bold: true + } + + //Mouse area to react on click events + MouseArea { + anchors.fill: button + onClicked: { button.clicked();} + onPressed: { + backgroundImage.source = "images/button_background_pressed.png" } + onReleased: { + backgroundImage.source = (button.enabled ? "images/button_background_normal.png" : "images/button_background_disabled.png") + } + } +} diff --git a/examples/sensors/maze/components/images/button_background_disabled.png b/examples/sensors/maze/components/images/button_background_disabled.png new file mode 100644 index 00000000..62a00b9a Binary files /dev/null and b/examples/sensors/maze/components/images/button_background_disabled.png differ diff --git a/examples/sensors/maze/components/images/button_background_normal.png b/examples/sensors/maze/components/images/button_background_normal.png new file mode 100644 index 00000000..1fecad5b Binary files /dev/null and b/examples/sensors/maze/components/images/button_background_normal.png differ diff --git a/examples/sensors/maze/components/images/button_background_pressed.png b/examples/sensors/maze/components/images/button_background_pressed.png new file mode 100644 index 00000000..149529e1 Binary files /dev/null and b/examples/sensors/maze/components/images/button_background_pressed.png differ diff --git a/examples/sensors/maze/content/00.png b/examples/sensors/maze/content/00.png new file mode 100644 index 00000000..ab29a622 Binary files /dev/null and b/examples/sensors/maze/content/00.png differ diff --git a/examples/sensors/maze/content/01.png b/examples/sensors/maze/content/01.png new file mode 100644 index 00000000..0e70d1b2 Binary files /dev/null and b/examples/sensors/maze/content/01.png differ diff --git a/examples/sensors/maze/content/cheese.png b/examples/sensors/maze/content/cheese.png new file mode 100644 index 00000000..ecef4503 Binary files /dev/null and b/examples/sensors/maze/content/cheese.png differ diff --git a/examples/sensors/maze/content/cheeseeating.gif b/examples/sensors/maze/content/cheeseeating.gif new file mode 100644 index 00000000..6b2cb47f Binary files /dev/null and b/examples/sensors/maze/content/cheeseeating.gif differ diff --git a/examples/sensors/maze/content/congratulations.gif b/examples/sensors/maze/content/congratulations.gif new file mode 100644 index 00000000..70204176 Binary files /dev/null and b/examples/sensors/maze/content/congratulations.gif differ diff --git a/examples/sensors/maze/content/mouse_down.gif b/examples/sensors/maze/content/mouse_down.gif new file mode 100644 index 00000000..5491c18f Binary files /dev/null and b/examples/sensors/maze/content/mouse_down.gif differ diff --git a/examples/sensors/maze/content/start.png b/examples/sensors/maze/content/start.png new file mode 100644 index 00000000..698c0b1c Binary files /dev/null and b/examples/sensors/maze/content/start.png differ diff --git a/examples/sensors/maze/icon.png b/examples/sensors/maze/icon.png new file mode 100644 index 00000000..1e7ff8d8 Binary files /dev/null and b/examples/sensors/maze/icon.png differ diff --git a/examples/sensors/maze/info.json b/examples/sensors/maze/info.json new file mode 100644 index 00000000..8e2ad1a8 --- /dev/null +++ b/examples/sensors/maze/info.json @@ -0,0 +1,15 @@ +{ + "info-version": "1.0", + "dict": { + "category": "application", + "runtime": "qml", + "displayName": { + "en_US": "Maze" + }, + "subcategory": "utility", + "main": "QtSensors_maze.qml", + "version": "1.0.0", + "identifier": "com.nokia.mt.maze", + "author": "Qt" + } +} diff --git a/examples/sensors/maze/lib.js b/examples/sensors/maze/lib.js new file mode 100644 index 00000000..3bae6a86 --- /dev/null +++ b/examples/sensors/maze/lib.js @@ -0,0 +1,264 @@ +//global variables +var labyrinth = null; +var dimension = 24; +var cellDimension = 13; +var won; +var objectArray = null; +var sec = 0.0 + +//Allocate labyrinth arrays and create labyrinth and way reflected in the labyrinth array +function createLabyrinth() +{ + won = false; + //create the labyrinth matrix + labyrinth = null; + labyrinth = new Array(dimension); + for (var x = 0; x < dimension; x++ ){ + labyrinth[x] = new Array(dimension); + for (var y = 0; y < dimension; y++ ){ + labyrinth[x][y] = 0; + } + } + createWay(); + createLab(); +} + +//Create a way where the mouse can reach the cheese +function createWay() +{ + //Create rnd way to have at least one solution + //A way square is marked as a 2 in the labyrinth array + var x = 0; + var y = 0; + var ox = x; + var oy = y; + labyrinth[0][0] = 2; + while (x < dimension && y < dimension){ + var rnd = Math.floor(Math.random()*5); + if (Math.floor(Math.random()*2) == 1){ + if (rnd == 0) x--; + if (rnd >= 1) x++; + if (x < 0) x++; + if (x >= dimension){ + x = ox; + break; + } + } + else { + if (rnd == 0) y--; + if (rnd >= 1) y++; + if (y < 0) y++; + if (y >= dimension){ + y = oy; + break; + } + } + + /*avoid to have [2]2| + |2|2|*/ + if (x < (dimension - 1) && y < (dimension - 1)){ + if (labyrinth[x + 1][y] == 2 + && labyrinth[x][y + 1] == 2 + && labyrinth[x + 1][y + 1] == 2){ + y = oy; + x = ox; + continue; + } + } + /*avoid to have |2[2] + |2|2|*/ + if (x > 0 && y < (dimension - 1)){ + if (labyrinth[x - 1][y] == 2 + && labyrinth[x][y + 1] == 2 + && labyrinth[x - 1][y + 1] == 2){ + y = oy; + x = ox; + continue; + } + } + /*avoid to have |2|2| + [2]2|*/ + if (x < (dimension - 1) && y > 0){ + if (labyrinth[x + 1][y] == 2 + && labyrinth[x][y - 1] == 2 + && labyrinth[x + 1][y - 1] == 2){ + y = oy; + x = ox; + continue; + } + } + /*avoid to have |2|2| + |2[2]*/ + if (x > 0 && y > 0){ + if (labyrinth[x - 1][y] == 2 + && labyrinth[x][y - 1] == 2 + && labyrinth[x - 1][y - 1] == 2){ + y = oy; + x = ox; + continue; + } + } + + labyrinth[x][y] = 2; + ox = x; + oy = y; + } + //finish way + while (x < (dimension - 1)){ + labyrinth[x][y] = 2; + x++; + } + while (y < (dimension - 1)){ + labyrinth[x][y] = 2; + y++; + } +} + +//Create the labyrinth with rnd values +function createLab() +{ + //A wall square is marked as a 1 in the labyrinth array + //Not a wall square is marked as a 0 in the labyrinth array + //The Cheese square is marked as a 3 in the labyrinth array + //The start is marked as a -1 in the labyrinth array + for (var x = 0; x < dimension; x++ ){ + var rnd = 0; + for (var y = 0; y < dimension; y++){ + //But don't overwrite the way + if (labyrinth[x][y] != 2){ + var rnd = Math.floor(Math.random()*2); + var xy = 0; + var xxy = 0; + var xyy = 0; + var xxyy = 0; + + if (x > 0 && y > 0){ + xy = labyrinth[x - 1][y - 1]; + if (xy == 2) + xy = 0; + + xyy = labyrinth[x - 1][y]; + if (xyy == 2) + xyy = 0; + + xxy = labyrinth[x][y - 1]; + if (xxy == 2) + xxy = 0; + + xxyy = rnd; + if (xxyy == 2) + xxyy = 0; + + //avoid to have to many |0|1| or |1|0| [xy ][xxy ] + // |1[0] |0[1] [xyy ][xxyy] + if (xyy == xxy && xy == xxyy && xy != xxy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + + //avoid to have to many |1|1| or |0|0| + // |1[1] |0[0] + if (xy == xxy && xxy == xxyy && xxyy == xyy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + } + else if (x == 0 && y > 0){ + xy = labyrinth[x][y - 1]; + if (xy == 2) + xy = 0; + + xyy = rnd; + if (xyy == 2) + xyy = 0; + + xxy = labyrinth[x + 1][y - 1]; + if (xxy == 2) + xxy = 0; + + xxyy = labyrinth[x + 1][y]; + if (xxyy == 2) + xxyy = 0; + + //avoid to have to many |1|1| or |0|0| + // |1[1] |0[0] + if (xy == xxy && xxy == xxyy && xxyy == xyy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + + //avoid to have to many |0|1| or |1|0| [xy ][xxy ] + // |1[0] |0[1] [xyy ][xxyy] + if (xyy == xxy && xy == xxyy && xy != xxy){ + if (rnd == 1) + rnd = 0; + else rnd = 1; + } + } + labyrinth[x][y] = rnd; + } + + } + } + //set start and end + labyrinth[0][0] = -1; + labyrinth[0][1] = 0; + labyrinth[1][0] = 0; + labyrinth[1][1] = 0; + + labyrinth[dimension - 2][dimension - 2] = 0; + labyrinth[dimension - 2][dimension - 1] = 0; + labyrinth[dimension - 1][dimension - 2] = 0; + labyrinth[dimension - 1][dimension - 1] = 3; +} + +//Function that checks if the mouse can be moved in x and y +function canMove(x, y) +{ + //Check if movement is allowed + var xcenter = x + (cellDimension / 2); + var ycenter = y + (cellDimension / 2); + //try to get the index + var idx = Math.floor(xcenter / cellDimension); + var idy = Math.floor(ycenter / cellDimension); + var dx = xcenter - (idx * cellDimension + ( cellDimension / 2 )); + var dy = ycenter - (idy * cellDimension + ( cellDimension / 2 )); + + if (dx > 0){ + if (labyrinth[idx][idy] == 1) + return false; + } + if (dx < 0){ + if (labyrinth[idx][idy] == 1) + return false; + } + if (dy > 0){ + if (labyrinth[idx][idy] == 1) + return false; + } + if (dy < 0){ + if (labyrinth[idx][idy] == 1) + return false; + } + //check if won + if (idx == (dimension - 1) && idy == (dimension - 1)) + won = true; + return true; +} + +//Function that prints out the labyrith array values in the console +function printLab() +{ + //for debug purposes print out lab n console + var iy = 0; + for (var y = 0; y < dimension; y++ ){ + var line = ""; + for (var x = 0; x < dimension; x++ ){ + line += labyrinth[x][y]; + } + console.log(line); + } +} diff --git a/examples/sensors/maze/maze.pro b/examples/sensors/maze/maze.pro new file mode 100644 index 00000000..a15e39ef --- /dev/null +++ b/examples/sensors/maze/maze.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS = qml.pro + diff --git a/examples/sensors/maze/maze.qml b/examples/sensors/maze/maze.qml new file mode 100644 index 00000000..40c1d1dd --- /dev/null +++ b/examples/sensors/maze/maze.qml @@ -0,0 +1,293 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* Layout + mainWnd + / +------------------------------/ gameRect +| / +|-----------------------------/ +||---------------------------| +||||M| || +||| \ || +||| mouseCtrl || +||| || +||| || +||| Labyrinth || +||| || +||| || +||| cheeseSquare || +||| \ || +||| |C||| +||---------------------------| +|----------------------------- +| +|----------------------------- +|| || | +|----------------------------- +| \ \ +| \ timePlayingLabel +| newGameButton +------------------------------ + +*/ +//Import the declarative plugins +import QtQuick 2.0 +import "components" + +//! [0] +import QtSensors 5.0 +//! [0] +import QtSystemInfo 5.0 + +//Import the javascript functions for this game +import "lib.js" as Lib + +ApplicationWindow { + id: mainWnd + + property Mouse mouseCtrl; + property LabyrinthSquare cheeseSquare; + property Congratulation congratulation; + ScreenSaver { screenSaverEnabled: !tiltTimer.running } + + Rectangle { + id: gameRect + x: (mainWnd.width - width) / 2 + y: 25 + width: Lib.dimension * Lib.cellDimension + height: Lib.dimension * Lib.cellDimension + color: "transparent" + border.width: 2 + + //timer for starting the labyrinth game + Timer { + id: startTimer + interval: 50; running: true; repeat: false + onTriggered: { + + //reset game time + timePlayingLabel.text = "--"; + Lib.sec = 0.0; + Lib.createLabyrinth(); + + //create labyrinth elements (only at the first time) + var needloadcomponent = false; + if (Lib.objectArray === null) { + needloadcomponent = true; + Lib.objectArray = new Array(Lib.dimension * Lib.dimension); + } + var idx = 0; + for (var y = 0; y < Lib.dimension; y++ ) { + for (var x = 0; x < Lib.dimension; x++ ) { + var component = null; + + //create labyrinth components (only at the first time) + if (needloadcomponent) { + component = Qt.createComponent("LabyrinthSquare.qml"); + if (component.status == Component.Ready) { + var square = component.createObject(parent); + square.x = x * square.width; + square.y = y * square.height; + square.val = Lib.labyrinth[x][y]; + square.updateImage(); + Lib.objectArray[idx] = square; + if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)) { + cheeseSquare = square; + var component1 = Qt.createComponent("Congratulation.qml"); + if (component1.status == Component.Ready) { + congratulation = component1.createObject(parent); + congratulation.visible = false; + } + } + } + } + else{ + Lib.objectArray[idx].val = Lib.labyrinth[x][y]; + Lib.objectArray[idx].updateImage(); + if (x == (Lib.dimension - 1) && y == (Lib.dimension - 1)) { + cheeseSquare = Lib.objectArray[idx]; + congratulation.visible = false; + } + } + idx++; + } + } + + //Lib.printLab(); //this is for debug. Labyrinth will be printed out in the console + + //Create the mouse control (only at the first time) + if (mouseCtrl === null) { + var component = Qt.createComponent("Mouse.qml"); + if (component.status == Component.Ready) { + mouseCtrl = component.createObject(parent); + } + } + mouseCtrl.x = 0; + mouseCtrl.y = 0; + newGameButton.enabled = true; + + //Start the Tilt reader timer + tiltTimer.running = true; + } + } + } + +//! [1] + TiltSensor{ + id: tiltSensor + unit: TiltSensor.Degrees + enabled: true + accuracy: 5.0 + } +//! [1] + + //Timer to read out the x and y rotation of the TiltSensor + Timer { + id: tiltTimer + interval: 50; running: false; repeat: true + +//! [2] + onTriggered: { + if (!tiltSensor.enabled) + tiltSensor.enabled = true; +//! [2] + + if (mouseCtrl === null) + return; + + //check if already solved + if (Lib.won !== true) { + Lib.sec += 0.05; + timePlayingLabel.text = Math.floor(Lib.sec) + " seconds"; + + //check if we can move the mouse + var xval = -1; + var yval = -1; + +//! [3] + var xstep = 0; + xstep = tiltSensor.yRotation * 0.1 //acceleration + + var ystep = 0; + ystep = tiltSensor.xRotation * 0.1 //acceleration +//! [3] +//! [4] + if (xstep < 1 && xstep > 0) + xstep = 0 + else if (xstep > -1 && xstep < 0) + xstep = 0 + + if (ystep < 1 && ystep > 0) + ystep = 0; + else if (ystep > -1 && ystep < 0) + ystep = 0; + + if ((xstep < 0 && mouseCtrl.x > 0 + && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y))) { + xval = mouseCtrl.x + xstep; + + } else if (xstep > 0 && mouseCtrl.x < (Lib.cellDimension * (Lib.dimension - 1)) + && Lib.canMove(mouseCtrl.x + xstep,mouseCtrl.y)) { + xval = mouseCtrl.x + xstep; + } else + xval = mouseCtrl.x; + + if (ystep < 0 && mouseCtrl.y > 0 + && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) { + yval = mouseCtrl.y + ystep; + } else if (ystep > 0 && (mouseCtrl.y < (Lib.cellDimension * (Lib.dimension - 1))) + && Lib.canMove(mouseCtrl.x, mouseCtrl.y + ystep)) { + yval = mouseCtrl.y + ystep; + } else + yval = mouseCtrl.y + + mouseCtrl.move(xval, yval); +//! [4] + + } else { + //game won, stop the tilt meter + mainWnd.cheeseSquare.val = 4; + mainWnd.cheeseSquare.updateImage(); + mainWnd.congratulation.visible = true; + newGameButton.enabled = true; + tiltTimer.running = false; + } + } + } + + + //Button to start a new Game + Button{ + id: newGameButton + anchors.left: gameRect.left + anchors.top: gameRect.bottom + anchors.topMargin: 5 + height: 30 + width: 100 + text: "new game" + enabled: false; + onClicked: { + newGameButton.enabled = false; + startTimer.start(); + } + } + Button{ + id: calibrateButton + anchors.left: gameRect.left + anchors.top: newGameButton.bottom + anchors.topMargin: 5 + height: 30 + width: 100 + text: "calibrate" + onClicked: { + tiltSensor.calibrate(); + } + } + + //Label to print out the game time + Text{ + id: timePlayingLabel + anchors.right: gameRect.right + anchors.top: gameRect.bottom + anchors.topMargin: 5 + } +} + diff --git a/examples/sensors/maze/qml.pro b/examples/sensors/maze/qml.pro new file mode 100644 index 00000000..95fbb65e --- /dev/null +++ b/examples/sensors/maze/qml.pro @@ -0,0 +1,17 @@ +TEMPLATE = aux + +app.files = \ + components \ + info.json \ + $$files(*.qml) \ + $$files(*.js) \ + icon.png \ + content + +MT_SYSROOT=$$(MT_SYSROOT) +!isEmpty(MT_SYSROOT):EXAMPLES_PREFIX=/opt/mt/applications +!isEmpty(EXAMPLES_PREFIX) { + app.path = $$EXAMPLES_PREFIX/com.nokia.mt.maze + INSTALLS = app +} + -- cgit v1.2.3