C# First Screen demo
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / firstscreen / EdenEffect.cs
1 using Dali;
2 using System;
3 using System.Collections.Generic;
4 using System.Collections.Specialized;
5
6 namespace FirstScreen
7 {
8     public class EdenEffect : IEdenEffect
9     {
10         private float _frameThickness;
11         private EdenData[] _edenFocusData; // Each EdenData is used for one key frame animation (total 6 key frame animations needed for EddenEffect)
12         private Animation _animation;      // Animation used to apply all six key frame animations
13
14         public EdenEffect()
15         {
16             _frameThickness = 10.0f;
17             float _bottomFrameTime = 0.6f; // complete the halo/bottom animation 60% of the way through
18             float _sideFrameTime = 0.8f; // Start the side frame  animation after the bottom animation and complete at 80% of the way through
19             float _topFrameTime = 1.0f; // start the top frame animation after the side frame animation and complete at 100% way through
20
21             // Six key frame animations (EdenData objects) needed for EddenEffect
22             // Two key frame animations for top horizontal effect
23             // Two key frame animations for bottom horizontal effect
24             // Two key frame animations for vertical horizontal effect
25             _edenFocusData = new EdenData[6];
26
27             EdenData edenData = new EdenData("halo", "halo.png", EdenData.Direction.Horizontal, NDalic.ParentOriginTopCenter,
28                                              new Vector3(50,20,0),  new Vector3(0.0f, 100.0f , 0.0f), 0.0f, _bottomFrameTime);
29             _edenFocusData[0] = edenData;
30
31             edenData = new EdenData("bottom", "horizontalFrame.png", EdenData.Direction.Horizontal, NDalic.ParentOriginTopCenter,
32                                     new Vector3(0.0f, 0.0f, 0.0f),  new Vector3(0.0f, _frameThickness, 0.0f), 0.0f, _bottomFrameTime);
33             _edenFocusData[1] = edenData;
34
35             edenData = new EdenData("left", "verticalFrame.png", EdenData.Direction.Vertical, NDalic.ParentOriginBottomLeft,
36                                     new Vector3(0.0f, 0.0f, 0.0f),  new Vector3(_frameThickness, 0.0f, 0.0f), _bottomFrameTime, _sideFrameTime);
37             _edenFocusData[2] = edenData;
38
39             edenData = new EdenData("right", "verticalFrame.png", EdenData.Direction.Vertical, NDalic.ParentOriginBottomRight,
40                                     new Vector3(0.0f, 0.0f, 0.0f),  new Vector3(_frameThickness, 0.0f, 0.0f), _bottomFrameTime, _sideFrameTime);
41             _edenFocusData[3] = edenData;
42
43             edenData = new EdenData("top-left", "horizontalFrame.png", EdenData.Direction.Horizontal, NDalic.ParentOriginBottomLeft,
44                                     new Vector3(0.0f, 0.0f, 0.0f),  new Vector3(0.0f ,_frameThickness, 0.0f), _sideFrameTime, _topFrameTime);
45             _edenFocusData[4] = edenData;
46
47             edenData = new EdenData("top-right", "horizontalFrame.png", EdenData.Direction.Horizontal, NDalic.ParentOriginBottomRight,
48                                     new Vector3(0.0f, 0.0f, 0.0f),  new Vector3(0.0f, _frameThickness, 0.0f), _sideFrameTime, _topFrameTime);
49             _edenFocusData[5] = edenData;
50         }
51
52         public void FocusAnimation(View parentItem, Vector3 itemSize, float duration, EdenEffectDirection direction)
53         {
54             var itemWidth = itemSize.x + _frameThickness / 2;
55             var itemHeight = itemSize.y + _frameThickness / 3;
56
57             if (_animation)
58             {
59                 _animation.Clear();
60                 _animation.Reset();
61             }
62             _animation = new Animation(duration);
63
64             if (direction == EdenEffectDirection.BottomToTop)
65             {
66                 _edenFocusData[0].ParentOrigin = NDalic.ParentOriginBottomCenter;
67                 _edenFocusData[1].ParentOrigin = NDalic.ParentOriginBottomCenter;
68                 _edenFocusData[2].ParentOrigin = NDalic.ParentOriginBottomLeft;
69                 _edenFocusData[3].ParentOrigin = NDalic.ParentOriginBottomRight;
70                 _edenFocusData[4].ParentOrigin = NDalic.ParentOriginTopLeft;
71                 _edenFocusData[5].ParentOrigin = NDalic.ParentOriginTopRight;
72             }
73             else
74             {
75                 _edenFocusData[0].ParentOrigin = NDalic.ParentOriginTopCenter;
76                 _edenFocusData[1].ParentOrigin = NDalic.ParentOriginTopCenter;
77                 _edenFocusData[2].ParentOrigin = NDalic.ParentOriginBottomLeft;
78                 _edenFocusData[3].ParentOrigin = NDalic.ParentOriginBottomRight;
79                 _edenFocusData[4].ParentOrigin = NDalic.ParentOriginBottomLeft;
80                 _edenFocusData[5].ParentOrigin = NDalic.ParentOriginBottomRight;
81             }
82
83             foreach (EdenData edenData in _edenFocusData)
84             {
85                 var currentParent =  edenData.ImageItem.GetParent();
86
87                 // first parent the controls
88                 if (parentItem != currentParent)
89                 {
90                     parentItem.Add(edenData.ImageItem);
91                 }
92
93                 edenData.ImageItem.Size = new Vector3(100.0f,100.0f, 0.0f);
94                 parentItem.Add(edenData.ImageItem);
95
96                 Vector3 targetSize = edenData.TargetSize;
97                 Vector3 initSize = edenData.InitSize;
98
99                 if (edenData.EdenDirection == EdenData.Direction.Horizontal)
100                 {
101                     // adjust the width to match the parent
102                     targetSize.x = itemWidth;
103                 }
104                 else // vertical frame
105                 {
106                     // adjust the height to match the parent
107                     targetSize.y = itemHeight;
108                 }
109
110                 // half the size for the top frame as we come out from both left / right sides
111                 if (edenData.Name == "top-right" ||  edenData.Name == "top-left")
112                 {
113                     targetSize.x = itemWidth - _frameThickness;
114                 }
115
116                 KeyFrames keyFrames = new KeyFrames();
117
118                 keyFrames.Add(0.0f, new Property.Value(initSize));
119                 keyFrames.Add(edenData.KeyFrameStart, new Property.Value(initSize));
120                 keyFrames.Add(edenData.KeyFrameEnd, new Property.Value(targetSize));
121
122                 // for halo add an extra keyframe to shrink it ( in 20% of time after it has finished)
123                 if (edenData.Name =="halo")
124                 {
125                     keyFrames.Add(edenData.KeyFrameEnd + 0.2f, new Property.Value(initSize));
126                 }
127
128                 _animation.AnimateBetween(new Property(edenData.ImageItem, Actor.Property.SIZE), keyFrames,
129                                           new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
130
131                 // Simulate the vertical frame growing from the top.
132                 // Vertical items are anchored to the bottom of the parent... so when they grow
133                 // we need to move them to the middle of the parent ( otherwise they stick out the bottom)
134                 if (edenData.EdenDirection == EdenData.Direction.Vertical)
135                 {
136                     //animate position as well so it looks like animation is coming from bottom
137                     KeyFrames keyFramesV = new KeyFrames();
138
139                     if (direction == EdenEffectDirection.BottomToTop)
140                     {
141                         keyFramesV.Add(0.0f, new Property.Value(0.0f));
142                         keyFramesV.Add(edenData.KeyFrameStart, new Property.Value(0.0f));
143                     }
144                     else
145                     {
146                         keyFramesV.Add(0.0f, new Property.Value(-itemHeight));
147                         keyFramesV.Add(edenData.KeyFrameStart, new Property.Value(-itemHeight));
148                     }
149
150                     keyFramesV.Add(edenData.KeyFrameEnd, new Property.Value(-itemHeight / 2)); // animate to halfway up the control
151
152                     _animation.AnimateBetween(new Property(edenData.ImageItem, Actor.Property.POSITION_Y), keyFramesV,
153                                               new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
154                 }
155
156                 // Simulate the top frame growing from the sides.
157                 if (edenData.Name == "top-left")
158                 {
159                     KeyFrames keyFramesTL = new KeyFrames();
160
161                     keyFramesTL.Add(0.0f, new Property.Value(0.0f));
162                     keyFramesTL.Add(edenData.KeyFrameStart, new Property.Value(0.0f));
163                     keyFramesTL.Add(edenData.KeyFrameEnd, new Property.Value(itemWidth / 2)); // animate to halfway up the control
164
165                     // grow these from the left or right
166                     _animation.AnimateBetween(new Property(edenData.ImageItem, Actor.Property.POSITION_X), keyFramesTL,
167                                               new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
168                 }
169
170                 if (edenData.Name == "top-right")
171                 {
172                     KeyFrames keyFramesTR = new KeyFrames();
173
174                     keyFramesTR.Add(0.0f, new Property.Value(0.0f));
175                     keyFramesTR.Add(edenData.KeyFrameStart, new Property.Value(0.0f));
176                     keyFramesTR.Add(edenData.KeyFrameEnd, new Property.Value(-itemWidth / 2)); // animate to halfway up the control
177
178                     // grow these from the left or right
179                     _animation.AnimateBetween(new Property(edenData.ImageItem, Actor.Property.POSITION_X), keyFramesTR,
180                                               new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
181                 }
182
183                 _animation.Finished += OnAnimationFinished;
184
185                 _animation.Play();
186             }
187         }
188
189         private void OnAnimationFinished(object source, Animation.FinishedEventArgs e)
190         {
191             foreach (EdenData edenData in _edenFocusData)
192             {
193                 var currentParent =  edenData.ImageItem.GetParent();
194
195                 if (currentParent)
196                 {
197                     currentParent.Remove(edenData.ImageItem);
198                 }
199             }
200         }
201     }
202 }
203