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