examples/QtQuick/qmlsensorex/qml/qmlsensorex/main.qml

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation
00006 **
00007 **
00008 ** $QT_BEGIN_LICENSE:BSD$
00009 ** You may use this file under the terms of the BSD license as follows:
00010 **
00011 ** "Redistribution and use in source and binary forms, with or without
00012 ** modification, are permitted provided that the following conditions are
00013 ** met:
00014 **   * Redistributions of source code must retain the above copyright
00015 **     notice, this list of conditions and the following disclaimer.
00016 **   * Redistributions in binary form must reproduce the above copyright
00017 **     notice, this list of conditions and the following disclaimer in
00018 **     the documentation and/or other materials provided with the
00019 **     distribution.
00020 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00021 **     the names of its contributors may be used to endorse or promote
00022 **     products derived from this software without specific prior written
00023 **     permission.
00024 **
00025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00036 ** $QT_END_LICENSE$
00037 **
00038 ****************************************************************************/
00039 
00040 import QtQuick 1.0
00041 import com.nokia.symbian 1.0
00042 import QtMobility.sensors 1.1
00043 
00044 Window {
00045     id:win
00046     width: 360
00047     height: 640
00048 
00049     // Score made by the user.
00050     property int score: 0
00051 
00052     // Count the accelerometer readings received, take one reading in every 8.
00053     property int count: 0
00054 
00055     // Reading of the Ambient light sensor.
00056     property string lightReading: ""
00057 
00058     // Reading of the rotation sesnor.
00059     property string rotationReading: ""
00060 
00061     // Reading of the proximity sensor.
00062     property string proximityReading: "far"
00063 
00064     // Position of the smiley.
00065     property alias xposition: smiley.x
00066     property alias yposition: smiley.y
00067 
00068     // This function is used for game logic to disapper objects and count points.
00069     function countPoints()
00070     {
00071 
00072         bomb1.countPoints();
00073         bomb2.countPoints();
00074         bomb3.countPoints();
00075         bomb4.countPoints();
00076         bomb5.countPoints();
00077         bomb6.countPoints();
00078 
00079         heart1.countPoints();
00080         heart2.countPoints();
00081         heart3.countPoints();
00082         heart4.countPoints();
00083         heart5.countPoints();
00084         heart6.countPoints();
00085     }
00086 
00087     // Accelerometer sensor element.
00088     Accelerometer{
00089         id: myAcceleroSensor
00090         onReadingChanged: {
00091             count++;
00092 
00093             // Take one reading in every 8 readings received.
00094             if( (count)%8 == 0 ){
00095             smiley.x = (reading.x)*100;
00096             smiley.y = (reading.y)*100;
00097 
00098             // Boundary conditions for the smiley.
00099             if( smiley.y > 310){
00100             smiley.y = 310;
00101             }
00102             if( smiley.x < 0){
00103             smiley.x = 0;
00104             }
00105             if( smiley.x > 330){
00106             smiley.x = 330;
00107             }
00108             countPoints();
00109             }
00110         }
00111     }
00112 
00113     // Proximity Sensor element.
00114     ProximitySensor{
00115         id: myProxiSensor
00116         onReadingChanged: {
00117             if( reading.close == true){
00118                 proximityReading = "close";
00119             }
00120             else{
00121                 proximityReading = "far";
00122             }
00123         }
00124     }
00125 
00126     // Light sensor reading.
00127     function lightCondition()
00128     {
00129         if( myLightSensor.reading.lightLevel == AmbientLightReading.Bright){
00130             lightReading = "Bright"
00131         }
00132         else if( myLightSensor.reading.lightLevel == AmbientLightReading.Dark){
00133             lightReading = "Dark"
00134         }
00135         else if( myLightSensor.reading.lightLevel == AmbientLightReading.Light){
00136             lightReading = "Light"
00137         }
00138         else if( myLightSensor.reading.lightLevel == AmbientLightReading.Sunny){
00139             lightReading = "Sunny"
00140         }
00141         else if( myLightSensor.reading.lightLevel == AmbientLightReading.Twilight){
00142             lightReading = "Twilight"
00143         }
00144         else{
00145             lightReading = "Undefined"
00146         }
00147     }
00148 
00149     // Light sensor element.
00150     AmbientLightSensor{
00151         id: myLightSensor
00152         onReadingChanged: {
00153             lightCondition();
00154         }
00155 
00156     }
00157 
00158     // Rotation sensor element.
00159     RotationSensor{
00160         id: myRotationSensor
00161         property int x: 0
00162         property int y: 0
00163         property int z: 0
00164         onReadingChanged: {
00165             x = reading.x
00166             y = reading.y
00167             z = reading.z
00168             rotationReading = "x: " + x + "  y: " + y + "  z: " + z
00169         }
00170 
00171     }
00172 
00173     // Exit button, to exit the application.
00174     Image{
00175         id:exitButton
00176         anchors.right: parent.right
00177         anchors.top: parent.top
00178         source: "exit.png"
00179         MouseArea {
00180             anchors.fill: parent
00181             onPressed:  { Qt.quit() }
00182         }
00183     }
00184 
00185     // Heart and Bomb elements.
00186     Bomb{ id:bomb1; visible: false }
00187     Bomb{ id:bomb2; visible: false }
00188     Bomb{ id:bomb3; visible: false }
00189     Bomb{ id:bomb4; visible: false }
00190     Bomb{ id:bomb5; visible: false }
00191     Bomb{ id:bomb6; visible: false }
00192 
00193     Heart{ id:heart1; visible: false }
00194     Heart{ id:heart2; visible: false }
00195     Heart{ id:heart3; visible: false }
00196     Heart{ id:heart4; visible: false }
00197     Heart{ id:heart5; visible: false }
00198     Heart{ id:heart6; visible: false }
00199 
00200 
00201     // Smiley element
00202     Image {id: smiley; source: "smile.png"; x: 5; y: 310; width: 30; height: 30 }
00203 
00204     // Function called to arrange the objects on the window.
00205     function randomArrangement()
00206     {
00207         bomb1.x = 5; bomb1.y = 5; bomb1.visible = true;
00208         heart1.x = 75; heart1.y = 5; heart1.visible = true
00209         heart2.x = 150; heart2.y = 5; heart2.visible = true
00210 
00211         heart3.x = 225; heart3.y = 5; heart3.visible = true
00212         bomb2.x = 5; bomb2.y = 75; bomb2.visible = true
00213         bomb3.x = 75; bomb3.y = 75; bomb3.visible = true
00214 
00215         heart4.x = 150; heart4.y = 75; heart4.visible = true
00216         bomb4.x = 225; bomb4.y = 75; bomb4.visible = true
00217 
00218         bomb5.x = 5; bomb5.y = 150; bomb5.visible = true
00219 
00220         heart5.x = 75; heart5.y = 150; heart5.visible = true
00221         heart6.x = 150; heart6.y = 150; heart6.visible = true
00222 
00223         bomb6.x = 225; bomb6.y = 150; bomb6.visible = true
00224     }
00225 
00226     // Start the game, by clicking this button.
00227     Button{
00228         id:startButton
00229         text:"Start Game"
00230         width: 300
00231         height: 50
00232         anchors.centerIn: parent
00233 
00234         onClicked:{
00235 
00236             // Connect the sensors to backend and start them.
00237             myAcceleroSensor.connectToBackend();
00238             myProxiSensor.connectToBackend();
00239             myLightSensor.connectToBackend();
00240             myRotationSensor.connectToBackend();
00241 
00242             myLightSensor.active = true;
00243             myRotationSensor.active = true;
00244             myProxiSensor.active = true;
00245             myAcceleroSensor.active = true;
00246 
00247             startButton.visible = false;
00248             resetButton.visible = true;
00249             lightCondition();
00250             randomArrangement();
00251         }
00252     }
00253 
00254     // Reset the game, and play again.
00255     Button{
00256         id:resetButton
00257         text:"Reset"
00258         width: 150
00259         height: 50
00260         anchors.left: scorecard.right
00261         anchors.bottom:parent.bottom
00262         anchors.bottomMargin: 10
00263         visible: false;
00264         onClicked:{
00265             score = 0;
00266             randomArrangement();
00267         }
00268     }
00269 
00270     // Information about, how to play the game.
00271     Text{
00272         id: helpText
00273         anchors.bottom: scorecard.bottom
00274         anchors.bottomMargin: 15
00275         anchors.left:  parent.left
00276         anchors.leftMargin:  15
00277         width: 300
00278         height: 250
00279         wrapMode: Text.WrapAnywhere
00280         color: "white"
00281         font.italic: true
00282         font.pointSize: 6
00283         text: {
00284             "Note: Supported in portrait mode only." + "\n" + "\n" +
00285             "Smiley moves, with the help of accelerometer reading." + "\n" +
00286             "You collect 100 points for touching heart." + "\n" +
00287             "You loose 100 points for touching bomb." + "\n" + "\n" +
00288             "Proximity sensor reading :- " + proximityReading + "\n" +
00289             "Light sensor reading :- " + lightReading + "\n" +
00290             "Rotation sensor reading:- " + rotationReading
00291         }
00292     }
00293 
00294     // Displays the score card.
00295     Text{
00296         id:scorecard
00297         text: "Score : " + score
00298         color: "white"
00299         width: 200
00300         height: 50
00301         anchors.bottom: parent.bottom
00302         anchors.bottomMargin: 10
00303         anchors.left:  parent.left
00304         anchors.leftMargin:  15
00305     }
00306 
00307 }

Generated by  doxygen 1.6.2