Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / examples / declarative / snake / snake.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 import QtQuick 2.0
43 import "content" as Content
44 import "content/snake.js" as Logic
45
46 Rectangle {
47     id: screen;
48     SystemPalette { id: activePalette }
49     color: activePalette.window
50     property bool activeGame: false
51
52     property int gridSize : 34
53     property int margin: 4
54     property int numRowsAvailable: Math.floor((height-32-2*margin)/gridSize)
55     property int numColumnsAvailable: Math.floor((width-2*margin)/gridSize)
56
57     property int lastScore : 0
58
59     property int score: 0;
60     property int heartbeatInterval: 200
61     property int halfbeatInterval: 160
62
63     width: 480
64     height: 750
65
66     property int direction
67     property int headDirection
68
69     property variant head;
70
71     Content.HighScoreModel {
72         id: highScores
73         game: "Snake"
74     }
75
76     Timer {
77         id: heartbeat;
78         interval: heartbeatInterval;
79         running: activeGame
80         repeat: true
81         onTriggered: { Logic.move() }
82     }
83     Timer {
84         id: halfbeat;
85         interval: halfbeatInterval;
86         repeat: true
87         running: heartbeat.running
88         onTriggered: { Logic.moveSkull() }
89     }
90     Timer {
91         id: startNewGameTimer;
92         interval: 700;
93         onTriggered: { Logic.startNewGame(); }
94     }
95
96     Timer {
97         id: startHeartbeatTimer;
98         interval: 1000 ;
99         onTriggered: { state = "running"; activeGame = true; }
100     }
101
102     Image{
103         id: pauseDialog
104         z: 1
105         source: "content/pics/pause.png"
106         anchors.centerIn: parent;
107         //opacity is deliberately not animated
108         opacity: 0 //Was !Qt.application.active && activeGame, but application doesn't work (QTBUG-23331)
109     }
110
111     Image {
112
113         Image {
114             id: title
115             source: "content/pics/snake.jpg"
116             fillMode: Image.PreserveAspectCrop
117             anchors.fill: parent
118             anchors.horizontalCenter: parent.horizontalCenter
119             anchors.verticalCenter: parent.verticalCenter
120
121             Column {
122                 spacing: 140
123                 anchors.verticalCenter: parent.verticalCenter;
124                 anchors.left:  parent.left;
125                 anchors.right:  parent.right;
126
127                 Text {
128                     color: "white"
129                     font.pointSize: 48
130                     font.italic: true;
131                     font.bold: true;
132                     text: "Snake"
133                     anchors.horizontalCenter: parent.horizontalCenter;
134                 }
135
136                 Text {
137                     color: "white"
138                     font.pointSize: 24
139                     anchors.horizontalCenter: parent.horizontalCenter;
140                     //horizontalAlignment: Text.AlignHCenter
141                     text: "Last Score:\t" + lastScore + "\nHighscore:\t" + highScores.topScore;
142                 }
143             }
144         }
145
146         source: "content/pics/background.png"
147         fillMode: Image.PreserveAspectCrop
148
149         anchors.left: parent.left
150         anchors.right: parent.right
151         anchors.top: parent.top
152         anchors.bottom: toolbar.top
153
154         Rectangle {
155             id: playfield
156             border.width: 1
157             border.color: "white"
158             color: "transparent"
159             anchors.horizontalCenter: parent.horizontalCenter
160             y: (screen.height - 32 - height)/2;
161             width: numColumnsAvailable * gridSize + 2*margin
162             height: numRowsAvailable * gridSize + 2*margin
163
164
165             Content.Skull {
166                 id: skull
167             }
168
169             MouseArea {
170                 anchors.fill: parent
171                 onPressed: {
172                     if (screen.state == "") {
173                         Logic.startNewGame();
174                         return;
175                     }
176                     if (direction == 0 || direction == 2)
177                         Logic.scheduleDirection((mouseX > (head.x + head.width/2)) ? 1 : 3);
178                     else
179                         Logic.scheduleDirection((mouseY > (head.y + head.height/2)) ? 2 : 0);
180                 }
181             }
182         }
183
184     }
185
186     Rectangle {
187         id: progressBar
188         opacity: 0
189         Behavior on opacity { NumberAnimation { duration: 200 } }
190         color: "transparent"
191         border.width: 2
192         border.color: "#221edd"
193         x: 50
194         y: 50
195         width: 200
196         height: 30
197         anchors.horizontalCenter: parent.horizontalCenter
198         anchors.verticalCenter: parent.verticalCenter
199         anchors.verticalCenterOffset: 40
200
201         Rectangle {
202             id: progressIndicator
203             color: "#221edd";
204             width: 0;
205             height: 30;
206         }
207     }
208
209     Rectangle {
210         id: toolbar
211         color: activePalette.window
212         height: 32; width: parent.width
213         anchors.bottom: screen.bottom
214
215         Content.Button {
216             id: btnA; text: "New Game"; onClicked: Logic.startNewGame();
217             anchors.left: parent.left; anchors.leftMargin: 3
218             anchors.verticalCenter: parent.verticalCenter
219         }
220
221         Content.Button {
222             text: "Quit"
223             anchors { left: btnA.right; leftMargin: 3; verticalCenter: parent.verticalCenter }
224             onClicked: Qt.quit();
225         }
226
227         Text {
228             color: activePalette.text
229             text: "Score: " + score; font.bold: true
230             anchors.right: parent.right; anchors.rightMargin: 3
231             anchors.verticalCenter: parent.verticalCenter
232         }
233     }
234
235     focus: true
236     Keys.onSpacePressed: Logic.startNewGame();
237     Keys.onLeftPressed: if (state == "starting" || direction != 1) Logic.scheduleDirection(3);
238     Keys.onRightPressed: if (state == "starting" || direction != 3) Logic.scheduleDirection(1);
239     Keys.onUpPressed: if (state == "starting" || direction != 2) Logic.scheduleDirection(0);
240     Keys.onDownPressed: if (state == "starting" || direction != 0) Logic.scheduleDirection(2);
241
242     states: [
243         State {
244             name: "starting"
245             PropertyChanges {target: progressIndicator; width: 200}
246             PropertyChanges {target: title; opacity: 0}
247             PropertyChanges {target: progressBar; opacity: 1}
248         },
249         State {
250             name: "running"
251             PropertyChanges {target: progressIndicator; width: 200}
252             PropertyChanges {target: title; opacity: 0}
253             PropertyChanges {target: skull; row: 0; column: 0; }
254             PropertyChanges {target: skull; spawned: 1}
255         }
256     ]
257
258     transitions: [
259         Transition {
260             from: "*"
261             to: "starting"
262             NumberAnimation { target: progressIndicator; property: "width"; duration: 1000 }
263             NumberAnimation { property: "opacity"; duration: 200 }
264         },
265         Transition {
266             to: "starting"
267             NumberAnimation { target: progressIndicator; property: "width"; duration: 1000 }
268             NumberAnimation { property: "opacity"; duration: 200 }
269         }
270     ]
271
272 }