Unity 8
OrientationChangeAnimation.qml
1 /*
2  * Copyright 2015 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.4
18 
19 QtObject {
20  id: root
21 
22  // to be set from outside
23  property Item spreadDelegate
24  property Item background
25  property Item window
26  property Item screenshot
27 
28  function start() {
29  if (window.orientationAngle === 0) {
30  if (spreadDelegate.shellOrientationAngle === 90) {
31  chosenAnimation = simple90Animation;
32  } else if (spreadDelegate.shellOrientationAngle === 180) {
33  chosenAnimation = halfLoopAnimation;
34  } else if (spreadDelegate.shellOrientationAngle === 270) {
35  chosenAnimation = moving90Animation;
36  } else {
37  chosenAnimation = null;
38  }
39  } else if (window.orientationAngle === 90) {
40  if (spreadDelegate.shellOrientationAngle === 0) {
41  chosenAnimation = simple90Animation;
42  } else if (spreadDelegate.shellOrientationAngle === 180) {
43  chosenAnimation = moving90Animation;
44  } else if (spreadDelegate.shellOrientationAngle === 270) {
45  chosenAnimation = halfLoopAnimation;
46  } else {
47  chosenAnimation = null;
48  }
49  } else if (window.orientationAngle === 180) {
50  if (spreadDelegate.shellOrientationAngle === 0) {
51  chosenAnimation = halfLoopAnimation;
52  } else if (spreadDelegate.shellOrientationAngle === 90) {
53  chosenAnimation = moving90Animation;
54  } else if (spreadDelegate.shellOrientationAngle === 270) {
55  chosenAnimation = simple90Animation;
56  } else {
57  chosenAnimation = null;
58  }
59  } else if (window.orientationAngle === 270) {
60  if (spreadDelegate.shellOrientationAngle === 0) {
61  chosenAnimation = moving90Animation;
62  } else if (spreadDelegate.shellOrientationAngle === 90) {
63  chosenAnimation = halfLoopAnimation;
64  } else if (spreadDelegate.shellOrientationAngle === 180) {
65  chosenAnimation = simple90Animation;
66  } else {
67  chosenAnimation = null;
68  }
69  }
70 
71  if (chosenAnimation)
72  chosenAnimation.start();
73  }
74 
75  // to be read from outside
76  property bool running: chosenAnimation ? chosenAnimation.running : false
77 
78  property int duration: 450
79  property int easingType: Easing.InOutCubic
80 
81  property int shortestDimension: spreadDelegate.width < spreadDelegate.height
82  ? spreadDelegate.width : spreadDelegate.height
83  property int longestDimension: spreadDelegate.width > spreadDelegate.height
84  ? spreadDelegate.width : spreadDelegate.height
85  property string longestAxis: spreadDelegate.width > spreadDelegate.height ? "x" : "y"
86 
87  property QtObject chosenAnimation
88 
89  function setup90Animation() {
90  background.visible = true;
91 
92  screenshot.width = window.width;
93  screenshot.height = window.height;
94  screenshot.window.anchors.topMargin = window.window.anchors.topMargin;
95  screenshot.take();
96  screenshot.transformOriginX = root.shortestDimension / 2;
97  screenshot.transformOriginY = root.shortestDimension / 2;
98  screenshot.visible = true;
99 
100  window.rotation = 0;
101  window.width = spreadDelegate.width;
102  window.height = spreadDelegate.height;
103  window.transformOriginX = root.shortestDimension / 2;
104  window.transformOriginY = root.shortestDimension / 2;
105  }
106 
107  function tearDown90Animation() {
108  window.orientationAngle = spreadDelegate.shellOrientationAngle;
109  screenshot.discard();
110  screenshot.visible = false;
111  background.visible = false;
112  }
113 
114  property QtObject simple90Animation: SequentialAnimation {
115  id: simple90Animation
116 
117  ScriptAction { script: setup90Animation() }
118  ParallelAnimation {
119  RotationAnimation {
120  target: root.window
121  duration: root.duration
122  easing.type: root.easingType
123  from: window.orientationAngle - spreadDelegate.shellOrientationAngle
124  to: 0
125  property: "transformRotationAngle"
126  }
127  RotationAnimation {
128  target: root.screenshot
129  duration: root.duration
130  easing.type: root.easingType
131  from: window.orientationAngle - spreadDelegate.shellOrientationAngle
132  to: 0
133  property: "transformRotationAngle"
134  }
135  NumberAnimation {
136  target: root.screenshot
137  duration: root.duration
138  easing.type: root.easingType
139  property: "opacity"
140  from: 1.0
141  to: 0.0
142  }
143  NumberAnimation {
144  target: root.window
145  duration: root.duration
146  easing.type: root.easingType
147  property: "opacity"
148  from: 0.0
149  to: 1.0
150  }
151  }
152  ScriptAction { script: tearDown90Animation() }
153  }
154 
155  property QtObject moving90Animation: SequentialAnimation {
156  id: moving90Animation
157 
158  ScriptAction { script: setup90Animation() }
159  ParallelAnimation {
160  RotationAnimation {
161  target: root.window
162  duration: root.duration
163  easing.type: root.easingType
164  direction: RotationAnimation.Shortest
165  from: window.orientationAngle - spreadDelegate.shellOrientationAngle
166  to: 0
167  property: "transformRotationAngle"
168  }
169  RotationAnimation {
170  target: root.screenshot
171  duration: root.duration
172  easing.type: root.easingType
173  direction: RotationAnimation.Shortest
174  from: window.orientationAngle - spreadDelegate.shellOrientationAngle
175  to: 0
176  property: "transformRotationAngle"
177  }
178  NumberAnimation {
179  target: root.screenshot
180  duration: root.duration
181  easing.type: root.easingType
182  property: "opacity"
183  from: 1.0
184  to: 0.0
185  }
186  NumberAnimation {
187  target: root.window
188  duration: root.duration
189  easing.type: root.easingType
190  property: "opacity"
191  from: 0.0
192  to: 1.0
193  }
194  NumberAnimation {
195  target: root.window
196  duration: root.duration
197  easing.type: root.easingType
198  property: root.longestAxis
199  from: root.longestDimension - root.shortestDimension
200  to: 0
201  }
202  NumberAnimation {
203  target: root.screenshot
204  duration: root.duration
205  easing.type: root.easingType
206  property: root.longestAxis
207  from: root.longestDimension - root.shortestDimension
208  to: 0
209  }
210  }
211  ScriptAction { script: tearDown90Animation() }
212  }
213 
214  property QtObject halfLoopAnimation: SequentialAnimation {
215  id: halfLoopAnimation
216 
217  ScriptAction { script: {
218  background.visible = true;
219 
220  window.rotation = 0;
221  window.width = spreadDelegate.width;
222  window.height = spreadDelegate.height;
223  window.transformOriginX = window.width / 2
224  window.transformOriginY = window.height / 2
225  } }
226  ParallelAnimation {
227  RotationAnimation {
228  target: root.window
229  duration: root.duration
230  easing.type: root.easingType
231  from: window.orientationAngle - spreadDelegate.shellOrientationAngle
232  to: 0
233  property: "transformRotationAngle"
234  }
235  }
236  ScriptAction { script: {
237  window.orientationAngle = spreadDelegate.shellOrientationAngle;
238  background.visible = false;
239  } }
240  }
241 }