54bbf61df8d3707e37e02cf26f5f8d7d60f0cf79
[profile/ivi/qtdeclarative.git] / examples / declarative / toys / dynamicscene / dynamicscene.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 import QtQuick 2.0
42 import QtQuick.Particles 2.0
43 import "content"
44
45 Item {
46     id: window
47
48     property int activeSuns: 0
49     property int centerOffset: 72
50
51     height: 480; width: 360
52
53
54     MouseArea {
55         anchors.fill: parent
56         onClicked: window.focus = false;
57     }
58
59     //This is the message box that pops up when there's an error
60     Rectangle {
61         id: dialog
62
63         opacity: 0
64         anchors.centerIn: parent
65         width: dialogText.width + 6; height: dialogText.height + 6
66         border.color: 'black'
67         color: 'lightsteelblue'
68         z: 65535 //Arbitrary number chosen to be above all the items, including the scaled perspective ones.
69
70         function show(str){
71             dialogText.text = str;
72             dialogAnim.start();
73         }
74
75         Text {
76             id: dialogText
77             x: 3; y: 3
78             font.pixelSize: 14
79         }
80
81         SequentialAnimation {
82             id: dialogAnim
83             NumberAnimation { target: dialog; property:"opacity"; to: 1; duration: 1000 }
84             PauseAnimation { duration: 5000 }
85             NumberAnimation { target: dialog; property:"opacity"; to: 0; duration: 1000 }
86         }
87     }
88
89     Item {
90         id: scene
91         anchors { top: sky.top; bottom: ground.bottom; left: parent.left; right: parent.right}
92         z: 10
93     }
94
95    // sky
96     Rectangle {
97         id: sky
98         anchors { left: parent.left; top: toolbox.bottom; right: parent.right;  bottomMargin: -centerOffset; bottom: parent.verticalCenter }
99         gradient: Gradient {
100             GradientStop { id: gradientStopA; position: 0.0; color: "#0E1533" }
101             GradientStop { id: gradientStopB; position: 1.0; color: "#437284" }
102         }
103     }
104
105     // stars (when there's no sun)
106     ParticleSystem {
107         id: particlesystem
108         anchors.fill: sky
109
110         ImageParticle {
111             id: stars
112             source: "content/images/star.png"
113             groups: ["stars"]
114             opacity: .5
115         }
116
117         Emitter {
118             id: starsemitter
119             anchors.fill: parent
120             emitRate: parent.width / 50
121             lifeSpan: 5000
122             group: "stars"
123         }
124     }
125
126     // ground
127     Rectangle {
128         id: ground
129         z: 2    // just above the sun so that the sun can set behind it
130         anchors { left: parent.left; top: parent.verticalCenter; topMargin: centerOffset; right: parent.right; bottom: parent.bottom }
131         gradient: Gradient {
132             GradientStop { position: 0.0; color: "ForestGreen" }
133             GradientStop { position: 1.0; color: "DarkGreen" }
134         }
135     }
136
137     SystemPalette { id: activePalette }
138
139     // right-hand panel
140     Rectangle {
141         id: toolbox
142
143         height: centerOffset * 2
144         color: activePalette.window
145         anchors { right: parent.right; top: parent.top; left: parent.left}
146
147         Column {
148             anchors.centerIn: parent
149             spacing: 8
150
151             Text { text: "Drag an item into the scene." }
152
153             Rectangle {
154                 width: palette.width + 10; height: palette.height + 10
155                 border.color: "black"
156
157                 Row {
158                     id: palette
159                     anchors.centerIn: parent
160                     spacing: 8
161
162                     PaletteItem {
163                         anchors.verticalCenter: parent.verticalCenter
164                         componentFile: "Sun.qml"
165                         source: "content/images/sun.png"
166                         image: "images/sun.png"
167                     }
168                     PaletteItem {
169                         anchors.verticalCenter: parent.verticalCenter
170                         componentFile: "GenericSceneItem.qml"
171                         source: "content/images/moon.png"
172                         image: "images/moon.png"
173                     }
174                     PaletteItem {
175                         anchors.verticalCenter: parent.verticalCenter
176                         componentFile: "PerspectiveItem.qml"
177                         source: "content/images/tree_s.png"
178                         image: "images/tree_s.png"
179                     }
180                     PaletteItem {
181                         anchors.verticalCenter: parent.verticalCenter
182                         componentFile: "PerspectiveItem.qml"
183                         source: "content/images/rabbit_brown.png"
184                         image: "images/rabbit_brown.png"
185                     }
186                     PaletteItem {
187                         anchors.verticalCenter: parent.verticalCenter
188                         componentFile: "PerspectiveItem.qml"
189                         source: "content/images/rabbit_bw.png"
190                         image: "images/rabbit_bw.png"
191                     }
192                 }
193             }
194
195             Text { text: "Active Suns: " + activeSuns }
196         }
197     }
198
199     //Popup toolbox down the bottom
200     Rectangle {
201         id: popupToolbox
202         z: 1000
203         width: parent.width
204         height: popupColumn.height + 16
205         color: activePalette.window
206
207         property bool poppedUp: false
208         property int downY: window.height - (createButton.height + 16)
209         property int upY: window.height - (popupColumn.height + 16)
210         y: poppedUp ? upY : downY
211         Behavior on y { NumberAnimation {}}
212
213         Column {
214             id: popupColumn
215             y: 8
216             anchors.centerIn: parent
217             spacing: 8
218
219             Row {
220                 height: createButton.height
221                 spacing: 8
222                 Text { text: "Custom QML:"; anchors.verticalCenter: parent.verticalCenter }
223                 Button {
224                     id: popupButton
225                     text: popupToolbox.poppedUp ? "Hide" : "Show"
226                     onClicked: popupToolbox.poppedUp = !popupToolbox.poppedUp
227                 }
228                 Button {
229                     id: createButton
230                     text: "Create"
231                     onClicked: {
232                         try {
233                             Qt.createQmlObject(qmlText.text, scene, 'CustomObject');
234                         } catch(err) {
235                             dialog.show('Error on line ' + err.qmlErrors[0].lineNumber + '\n' + err.qmlErrors[0].message);
236                         }
237                     }
238                 }
239
240             }
241
242             Rectangle {
243                 width: 360; height: 240
244
245                 TextEdit {
246                     id: qmlText
247                     anchors.fill: parent; anchors.margins: 5
248                     readOnly: false
249                     font.pixelSize: 14
250                     selectByMouse: true
251                     wrapMode: TextEdit.WordWrap
252
253                     text: "import QtQuick 2.0\nImage {\n    id: smile\n    x: 360 * Math.random()\n    y: 180 * Math.random() \n    source: 'content/images/face-smile.png'\n    NumberAnimation on opacity { \n        to: 0; duration: 1500\n    }\n    Component.onCompleted: smile.destroy(1500);\n}"
254                 }
255             }
256         }
257     }
258
259     //Day state, for when a sun is added to the scene
260     states: State {
261         name: "Day"
262         when: window.activeSuns > 0
263
264         PropertyChanges { target: gradientStopA; color: "DeepSkyBlue" }
265         PropertyChanges { target: gradientStopB; color: "SkyBlue" }
266         PropertyChanges { target: stars; opacity: 0 }
267     }
268
269     //! [top-level transitions]
270     transitions: Transition {
271         PropertyAnimation { duration: 3000 }
272         ColorAnimation { duration: 3000 }
273     }
274     //! [top-level transitions]
275 }