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