774539a9581a4ab0467047a43241e6da7c2e209b
[profile/ivi/qtdeclarative.git] / examples / particles / itemparticle / particleview.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 examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42 import QtQuick.Particles 2.0
43 import "content/script.js" as Script
44 import "content"
45
46 Item {
47     id: root
48     width: 640
49     height: 480
50     Rectangle {
51         anchors.fill: parent
52         color: "black"
53         z: -1
54     }
55     Item {
56         id: loading
57         Behavior on opacity {NumberAnimation {}}
58         anchors.fill: parent
59         Text {
60             anchors.centerIn: parent
61             text: "Loading"
62             color: "white"
63         }
64     }
65     ParticleSystem {
66         id: sys;
67         running: true
68     }
69     Emitter {
70         id: emitter
71         system: sys
72         height: parent.height - 132/2
73         x: -132/2
74         y: 132/2
75         speed: PointDirection { x: 32; xVariation: 8 }
76         emitRate: 0.5
77         lifeSpan: Emitter.InfiniteLife
78         group: "photos"
79     }
80     Age {
81         system: sys
82         x: parent.width + 132/2
83         height: parent.height
84         width: 1000
85     }
86     ImageParticle {
87         system: sys
88         groups: ["fireworks"]
89         source: "../trails/../images/star.png"
90         color: "lightsteelblue"
91         alpha: 0
92         colorVariation: 0
93         z: 1000
94     }
95     ItemParticle {
96         id: mp
97         z: 0
98         system: sys
99         fade: false
100         groups: ["photos"]
101     }
102     Component {
103         id: alertDelegate
104         Rectangle {
105             width: 132
106             height: 132
107             NumberAnimation on scale {
108                 running: true
109                 loops: 1
110                 from: 0.2
111                 to: 1
112             }
113             Image {
114                 source: "../asteroid/../images/rocket.png"
115                 anchors.centerIn: parent
116             }
117             Text {
118                 anchors.bottom: parent.bottom
119                 anchors.horizontalCenter: parent.horizontalCenter
120                 text: "A new ship has arrived!"
121             }
122         }
123     }
124     property Item alertItem;
125     function alert() {
126         //resetter.active = false
127         force.active = true;
128         alertItem = alertDelegate.createObject(root);
129         alertItem.x = root.width/2 - alertItem.width/2
130         alertItem.y = root.height/2 - alertItem.height/2
131         spawnFireworks.pulse(200);
132         stopAlert.start();
133     }
134     focus: true
135     Keys.onSpacePressed: alert();
136     Timer {
137         id: stopAlert
138         running: false
139         repeat: false
140         interval: 800
141         onTriggered: {
142             force.active = false
143             //resetter.active = true;
144             mp.take(alertItem, true);
145             centerEmitter.burst(1);
146         }
147     }
148     Attractor {
149         id: force
150         system: sys
151         pointX: root.width/2
152         pointY: root.height/2
153         strength: -10000
154         enabled: false
155         anchors.centerIn: parent
156         width: parent.width/2
157         height: parent.height/2
158         groups:["photos"]
159         affectedParameter: Attractor.Position
160     }
161     Emitter {
162         id: centerEmitter
163         speed: PointDirection { x: 32; xVariation: 8;}
164         emitRate: 0.5
165         lifeSpan: 12000 //TODO: A -1 or something which does 'infinite'? (but need disable fade first)
166         maximumEmitted: 20
167         group: "photos"
168         system: sys
169         anchors.centerIn: parent
170         enabled: false
171
172         //TODO: Zoom in effect
173     }
174     Emitter {
175         id: spawnFireworks
176         group: "fireworks"
177         system: sys
178         maximumEmitted: 400
179         emitRate: 400
180         lifeSpan: 2800
181         x: parent.width/2
182         y: parent.height/2 - 64
183         width: 8
184         height: 8
185         enabled: false
186         size: 32
187         endSize: 8
188         speed: AngleDirection { magnitude: 160; magnitudeVariation: 120; angleVariation: 90; angle: 270 }
189         acceleration: PointDirection { y: 160 }
190     }
191     Item { x: -1000; y: -1000 //offscreen
192         Repeater {//Load them here, add to system on completed
193             model: theModel
194             delegate: theDelegate
195         }
196     }
197     RssModel {id: theModel; tags:"particle,particles"}
198     Component {
199         id: theDelegate
200         Rectangle {
201             id: container
202             border.width: 2
203             property real myRand: Math.random();//'depth'
204             z: Math.floor(myRand * 100)
205             scale: (myRand + 1.0)/2;
206             //TODO: Darken based on 'depth'
207             width: 132
208             height: 132
209             //ItemParticle.onAttached: console.log("I'm in"  + x + "," + y + ":" + opacity);
210             ItemParticle.onDetached: mp.take(container);//respawns
211             function manage()
212             {
213                 if (state == "selected") {
214                     console.log("Taking " + index);
215                     mp.freeze(container);
216                 } else {
217                     console.log("Returning " +index);
218                     mp.unfreeze(container);
219                 }
220             }
221             Image {
222                 id: img
223                 anchors.centerIn: parent
224                 smooth: true; source: "http://" + Script.getImagePath(content); cache: true
225                 fillMode: Image.PreserveAspectFit; 
226                 width: parent.width-4; height: parent.height-4
227                 onStatusChanged: if (img.status == Image.Ready) {
228                     loading.opacity = 0;
229                     mp.take(container);
230                 }
231             }
232             Text {
233                 anchors.bottom: parent.bottom
234                 width: parent.width
235                 horizontalAlignment: Text.AlignHCenter
236                 elide: Text.ElideRight
237                 text: title
238                 color: "black"
239             }
240             MouseArea {
241                 anchors.fill: parent
242                 onClicked: container.state == "selected" ? container.state = "" : container.state = "selected"
243             }
244             states: State {
245                 name: "selected"
246                 ParentChange {
247                     target: container
248                     parent: root
249                     x: 0
250                     y: 0
251                 }
252                 PropertyChanges {
253                     target: container
254                     width: root.width
255                     height: root.height
256                     z: 101
257                     opacity: 1
258                     rotation: 0
259                 }
260             }
261             transitions: Transition {
262                 to: "selected"
263                 reversible: true
264                 SequentialAnimation {
265                     ScriptAction {script: container.manage();}
266                     ParallelAnimation {
267                         ParentAnimation {NumberAnimation { properties: "x,y" }}//Doesn't work, particles takes control of x,y instantly
268                         NumberAnimation { properties: "width, height, z, rotation" }
269                     }
270                 }
271             }
272         }
273     }
274 }