Merge "DALi Version 1.2.17" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / firstscreen / App.cs
1 using Dali;
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Collections.Generic;
5
6 namespace FirstScreen
7 {
8     public class FirstScreenApp
9     {
10         private Application _application;                  // Reference to Dali Application.
11         private Stage _stage;                              // Reference to Dali stage.
12         private Vector2 _stageSize;                        // Reference to Dali stage size.
13
14         private View _topContainer;                        // Top Container added to the Stage will contain Poster ScrollContainers.
15         private View _bottomContainer;                     // Bottom Container added to the Stage will contain Menu ScrollContainer.
16         private int _currentPostersContainerID;            // Current Poster Container ID visible on the Top Container / Stage.
17         private int _totalPostersContainers;               // Number of Poster ScrollContainers to be added on Top Container.
18         private List<ScrollContainer> _postersContainer;   // List collection of Poster ScrollContainers used on the Top Container in this demo application.
19         private ScrollContainer _menuContainer;            // Menu ScrollContainer used on the Bottom Container in this demo application.
20         private Layer _bottomClipLayer;                    // Clip layer (Dali Clip Layer instance) used for Bottom Container.
21         private Layer _topClipLayer;                       // Clip layer (Dali Clip Layer instance) used for Top Container.
22
23         private FocusEffect _focusEffect;                  // FocusEffect is used to apply Focus animation effect on any supplied item/image.
24         private string _imagePath;                         // Contains the physical location of all images used in the demo application.
25
26         private ImageView _keyboardFocusIndicator;         // Reference to the ImageView (Keyboard Focus indicator) applied to the focused item on ScrollContainer.
27         private ImageView _launcherSeparator;              // Reference to the ImageView used for launcher separation (Launcher consists of three image icons on left of Menu ScrollContainer).
28         private ImageView[] launcherIcon;                  // ImageViews used for launcher Icons.
29         private Animation _showBottomContainerAnimation;   // Animation used to show/unhide Bottom Container (Menu ScrollContainer) when it is focused.
30         private Animation _hideBottomContainerAnimation;   // Animation used to hide Bottom Container (Menu ScrollContainer) when it is focused.
31         private Animation _showAnimation;                  // Animation used to move Poster scrollContainer from bottom to top and make it non-transparent.
32         private Animation _hideAnimation;                  // Animation used to make the unused specified Poster scrollContainer transparent.
33         private ScrollContainer _hideScrollContainer;      // The unused Poster scrollContainer which needs to be transparent.
34         KeyboardFocusManager _keyboardFocusManager;        // Reference to Dali KeyboardFocusManager.
35
36         private FirstScreenApp(Application application)
37         {
38             _application = application;
39             _application.Initialized += OnInitialize;
40         }
41
42         public static void Run()
43         {
44             FirstScreenApp tVApp = new FirstScreenApp(Application.NewApplication());
45             tVApp.MainLoop();
46         }
47
48         private void MainLoop()
49         {
50             _application.MainLoop();
51         }
52
53         // Create Items for Poster ScrollContainer
54         private void CreatePosters()
55         {
56             for (int j = 0; j < _totalPostersContainers; j++)
57             {
58                 View posterContainer = _postersContainer[j].Container;
59                 for (int i = 0; i < Constants.PostersItemsCount; i++)
60                 {
61                     if (j % _totalPostersContainers == 0)
62                     {
63                         View item = new ImageView(_imagePath + "/poster"+j+"/"+ (i % 6)+ ".jpg");
64                         item.SetName ("poster-item-" + _postersContainer[j].ItemCount);
65                         _postersContainer[j].Add(item);
66                     }
67                     else
68                     {
69                         View item = new ImageView(_imagePath + "/poster"+j+"/"+ (i % 6)+ ".jpg");
70                         item.SetName ("poster-item-" + _postersContainer[j].ItemCount);
71                         _postersContainer[j].Add(item);
72                     }
73                 }
74
75                 if (j == 0)
76                 {
77                     Show(_postersContainer[j]);
78                 }
79                 else
80                 {
81                     Hide(_postersContainer[j]);
82                 }
83
84                 _postersContainer[j].SetFocused(false);
85             }
86
87             _currentPostersContainerID = 0;
88         }
89
90         // Create Items for Menu ScrollContainer
91         private void CreateMenu()
92         {
93             View menuContainer = _menuContainer.Container;
94             menuContainer.Position = new Vector3(Constants.LauncherWidth, 0.0f, 0.0f);
95
96             for(int i = 0; i < Constants.MenuItemsCount; i++)
97             {
98                 View menuItem = new ImageView(_imagePath + "/menu/" + i % 7 + ".png");
99                 menuItem.SetName("menu-item-" + _menuContainer.ItemCount);
100                 _menuContainer.Add(menuItem);
101             }
102         }
103
104         private Actor OnKeyboardPreFocusChangeSignal(object source, KeyboardFocusManager.PreFocusChangeEventArgs e)
105         {
106             if (!e.Current && !e.Proposed)
107             {
108                 return _menuContainer;
109             }
110
111             Actor actor = _menuContainer.Container;
112
113             if (e.Direction == View.KeyboardFocus.Direction.UP)
114             {
115                 // Move the Focus to Poster ScrollContainer and hide Bottom Container (Menu ScrollContainer)
116                 if (_menuContainer.IsFocused)
117                 {
118                     actor = _postersContainer[_currentPostersContainerID].GetCurrentFocusedActor();
119                     _menuContainer.SetFocused(false);
120                     _postersContainer[_currentPostersContainerID].SetFocused(true);
121                     HideBottomContainer();
122
123                     // Also apply Focus animation on Focused item on Poster ScrollContainer
124                     FocusAnimation(_postersContainer[_currentPostersContainerID], FocusEffectDirection.BottomToTop);
125                 }
126             }
127             else if (e.Direction == View.KeyboardFocus.Direction.DOWN)
128             {
129                 // Show Bottom Container (Menu ScrollContainer) and move the Focus to it
130                 if (!_menuContainer.IsFocused)
131                 {
132                     ShowBottomContainer();
133                     actor = _menuContainer.GetCurrentFocusedActor();
134                     _postersContainer[_currentPostersContainerID].SetFocused(false);
135                     _menuContainer.SetFocused(true);
136
137                     // Also apply Focus animation on Focused item on Menu ScrollContainer
138                     FocusAnimation(_menuContainer, FocusEffectDirection.TopToBottom);
139                 }
140             }
141             else
142             {
143                 actor = e.Proposed;
144             }
145
146             if (e.Direction == View.KeyboardFocus.Direction.LEFT)
147             {
148                 if (_menuContainer.IsFocused)
149                 {
150                     int id = _menuContainer.FocusedItemID % _totalPostersContainers;
151                     if (id != _currentPostersContainerID)
152                     {
153                         Hide(_postersContainer[_currentPostersContainerID]);
154                         _currentPostersContainerID = id;
155
156                         Show(_postersContainer[_currentPostersContainerID]);
157                     }
158                 }
159             }
160             else if (e.Direction == View.KeyboardFocus.Direction.RIGHT)
161             {
162                 if (_menuContainer.IsFocused)
163                 {
164                     int id = _menuContainer.FocusedItemID % _totalPostersContainers;
165                     if (id != _currentPostersContainerID)
166                     {
167                         Hide(_postersContainer[_currentPostersContainerID]);
168                         _currentPostersContainerID = id;
169                         Show(_postersContainer[_currentPostersContainerID]);
170                     }
171                 }
172             }
173
174             return actor;
175         }
176
177         // Perform Focus animation Effect on the current Focused Item on ScrollContainer.
178         private void FocusAnimation(ScrollContainer scrollContainer, FocusEffectDirection direction)
179         {
180             _focusEffect.FocusAnimation(scrollContainer.GetCurrentFocusedActor(), scrollContainer.ItemSize, 1.0f, direction);
181         }
182
183         // Perform Show animation on ScrollContainer (used only for Poster Container)
184         private void Show(ScrollContainer scrollContainer)
185         {
186             scrollContainer.Add(scrollContainer.Container);
187
188             _hideScrollContainer = null;
189             _showAnimation = new Animation (0.35f);
190
191             // This animation will move Poster scrollContainer from bottom to top and make it non-transparent.
192             _showAnimation.AnimateTo(new Dali.Property(scrollContainer.Container, Actor.Property.COLOR_ALPHA), new Dali.Property.Value(1.0f));
193
194             scrollContainer.Container.PositionY = scrollContainer.Container.Position.y + 200.0f;
195             float targetPositionY = scrollContainer.Container.Position.y - 200.0f;
196             _showAnimation.AnimateTo(new Dali.Property(scrollContainer.Container, Actor.Property.POSITION_Y), new Dali.Property.Value(targetPositionY),
197                                      new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR));
198
199             _showAnimation.Play();
200         }
201
202         // Perform Hide animation on ScrollContainer (used only for Poster Container)
203         private void Hide(ScrollContainer scrollContainer)
204         {
205             if (_hideAnimation)
206             {
207                 _hideAnimation.Clear();
208                 _hideAnimation.Reset();
209             }
210
211             float duration = 0.35f;
212             _hideAnimation = new Animation(duration);
213
214             _hideAnimation.AnimateTo(new Dali.Property(scrollContainer.Container, Actor.Property.COLOR_ALPHA), new Dali.Property.Value(0.0f),
215                                      new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.0f, duration * 0.75f));
216
217             _hideAnimation.Finished += OnHideAnimationFinished;
218
219             _hideScrollContainer = scrollContainer;
220             _hideAnimation.Play();
221         }
222
223         // This removes all the items from the specified unused Poster ScrollContainer (hence Stage) to improve performance.
224         private void OnHideAnimationFinished(object source, Animation.FinishedEventArgs e)
225         {
226             if (_hideScrollContainer)
227             {
228                 _hideScrollContainer.Remove(_hideScrollContainer.Container);
229             }
230         }
231
232         // Hide Bottom Container (Menu ScrollContainer) when it is not focused
233         private void HideBottomContainer()
234         {
235             _topClipLayer.ClippingBox = new RectInteger(0,
236                                                         Convert.ToInt32(_stageSize.height * Constants.TopContainerPositionFactor),
237                                                         Convert.ToInt32((_stageSize.width)),
238                                                         Convert.ToInt32((_stageSize.height * Constants.TopClipLayerExpandHeightFactor)));  // X, Y, Width, Height
239
240             _hideBottomContainerAnimation.AnimateTo(new Property(_bottomContainer, Actor.Property.POSITION),
241                                                     new Property.Value(new Vector3(0.0f, _stageSize.height * Constants.BottomContainerHidePositionFactor, 0.0f)),
242                                                     new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
243             _hideBottomContainerAnimation.Play();
244         }
245
246         // Show (unhide) Bottom Container (Menu ScrollContainer) when it is focused
247         private void ShowBottomContainer()
248         {
249             _topClipLayer.ClippingBox = new RectInteger(0,
250                                                         Convert.ToInt32(_stageSize.height * Constants.TopContainerPositionFactor),
251                                                         Convert.ToInt32((_stageSize.width)),
252                                                         Convert.ToInt32((_stageSize.height * Constants.TopClipLayerHeightFactor)));  // X, Y, Width, Height
253
254             _showBottomContainerAnimation.AnimateTo(new Property(_bottomContainer, Actor.Property.POSITION),
255                                                     new Property.Value(new Vector3(0.0f, _stageSize.height * Constants.BottomContainerShowPositionFactor, 0.0f)),
256                                                     new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
257             _showBottomContainerAnimation.Play();
258         }
259
260         // First screen demo Application initialisation
261         private void OnInitialize(object source, AUIApplicationInitEventArgs e)
262         {
263             _hideScrollContainer = null;
264             _stage = Stage.GetCurrent();
265             _stageSize = _stage.GetSize();
266             //_stage.SetBackgroundColor(NDalic.TRANSPARENT);
267
268             _totalPostersContainers = Constants.TotalPostersContainers;
269             _imagePath = "./images/"; // Desktop
270             //_imagePath = "/home/owner/apps_rw/org.tizen.firstscreen/res/images/"; // Target
271
272             _postersContainer = new List<ScrollContainer> ();
273             _menuContainer = new ScrollContainer ();
274
275             _hideBottomContainerAnimation = new Animation(0.25f);
276             _showBottomContainerAnimation = new Animation(0.25f);
277
278             // Create a Top Container for poster items
279             _topContainer = new View();
280             _topContainer.Size = new Vector3(_stageSize.width, _stageSize.height * Constants.TopContainerHeightFactor, 0.0f);
281             _topContainer.Position = new Vector3(0.0f, _stageSize.y * Constants.TopContainerPositionFactor, 0.0f);
282             _topContainer.ParentOrigin = NDalic.ParentOriginTopLeft;
283             _topContainer.AnchorPoint = NDalic.AnchorPointTopLeft;
284
285             // Add a background to Top container
286             Property.Map visual = new Property.Map();
287             visual.Insert(NDalic.VISUAL_PROPERTY_TYPE, new Property.Value((int)VisualType.IMAGE));
288             visual.Insert(NDalic.IMAGE_VISUAL_URL, new Property.Value(_imagePath + "/background.png"));
289             _topContainer.Background = visual;
290             _topContainer.Name = "TopControl";
291
292             // Create a Bottom Container
293             _bottomContainer = new View();
294             _bottomContainer.Size = new Vector3(_stageSize.width, _stageSize.height * Constants.BottomContainerHeightFactor, 0.0f);
295             _bottomContainer.Position = new Vector3(0.0f, _stageSize.height * Constants.BottomContainerHidePositionFactor, 0.0f);
296             _bottomContainer.ParentOrigin = NDalic.ParentOriginTopLeft;
297             _bottomContainer.AnchorPoint = NDalic.AnchorPointTopLeft;
298
299             // Add a background to Bottom Container
300             visual = new Property.Map();
301             visual.Insert(NDalic.VISUAL_PROPERTY_TYPE, new Property.Value((int)VisualType.IMAGE));
302             visual.Insert(NDalic.IMAGE_VISUAL_URL, new Property.Value(_imagePath + "/background.png"));
303             _bottomContainer.Background = visual;
304             _bottomContainer.Name = "BottomControl";
305
306             // Add both Top and Bottom Containers to Stage
307             _stage.Add(_topContainer);
308             _stage.Add(_bottomContainer);
309
310             // Add a clip layer to Top Container
311             _topClipLayer = new Layer();
312             _topClipLayer.AnchorPoint = NDalic.AnchorPointBottomCenter;
313             _topClipLayer.ParentOrigin = NDalic.ParentOriginBottomCenter;
314             _topClipLayer.ClippingEnable = true;
315             _topClipLayer.ClippingBox = new RectInteger(0,
316                                                         Convert.ToInt32(_stageSize.height * Constants.TopContainerPositionFactor),
317                                                         Convert.ToInt32((_stageSize.width)),
318                                                         Convert.ToInt32((_stageSize.height * Constants.TopClipLayerHeightFactor)));  // X, Y, Width, Height
319             _topContainer.Add(_topClipLayer);
320
321             // Create a SpotLight for items / images of both Poster and Menu ScrollContainers
322             ImageView spotLight = new ImageView(_imagePath + "/highlight_spot.png");
323             spotLight.WidthResizePolicy = "USE_NATURAL_SIZE";
324             spotLight.HeightResizePolicy = "USE_NATURAL_SIZE";
325             spotLight.ParentOrigin = NDalic.ParentOriginCenter;
326             spotLight.AnchorPoint = NDalic.AnchorPointCenter;
327             spotLight.Name = "spotLight";
328
329             // Create a shadowBorder for items / images of Poster ScrollContainers
330             ImageView shadowBorder = new ImageView(_imagePath + "/thumbnail_shadow.9.png");
331             shadowBorder.ParentOrigin = NDalic.ParentOriginCenter;
332             shadowBorder.AnchorPoint = NDalic.AnchorPointCenter;
333             shadowBorder.WidthResizePolicy = "SIZE_FIXED_OFFSET_FROM_PARENT";
334             shadowBorder.HeightResizePolicy = "SIZE_FIXED_OFFSET_FROM_PARENT";
335             shadowBorder.SetSizeModeFactor(new Vector3(32.0f, 41.0f, 0.0f));
336             shadowBorder.Name = "poster shadowBorder";
337
338             // Create Poster Containers and add them to Top Clip layer
339             for (int i = 0; i < _totalPostersContainers; i++)
340             {
341                 _postersContainer.Add(new ScrollContainer());
342                 _postersContainer[i].Container.Name = "poster" + i;
343                 if (i == 0)
344                 {
345                     _postersContainer[i].ItemSize = new Vector3((_stageSize.width * Constants.Poster0ItemWidthFactor) - Constants.PostersContainerPadding,
346                                                                 _stageSize.height * Constants.PostersItemHeightFactor, 0.0f);
347                 }
348                 else
349                 {
350                     _postersContainer[i].ItemSize = new Vector3((_stageSize.width * Constants.Poster1ItemWidthFactor) - Constants.PostersContainerPadding,
351                                                                 _stageSize.height * Constants.PostersItemHeightFactor, 0.0f);
352                 }
353                 _postersContainer[i].Gap = Constants.PostersContainerPadding;
354                 _postersContainer[i].MarginX = Constants.PostersContainerMargin;
355                 _postersContainer[i].OffsetYFator = Constants.PostersContainerOffsetYFactor;
356                 _postersContainer[i].Width = _stageSize.width;
357                 _postersContainer[i].Height = _stageSize.height * Constants.PostersContainerHeightFactor;
358                 _postersContainer[i].ShadowBorder = shadowBorder;
359                 _postersContainer[i].ShadowBorder.Position = new Vector3(0.0f, 4.0f, 0.0f);
360                 _postersContainer[i].SpotLight = spotLight;
361                 _topClipLayer.Add(_postersContainer[i]);
362             }
363
364             // Add a clip layer to Bottom Container
365             _bottomClipLayer = new Layer();
366             _bottomClipLayer.AnchorPoint = NDalic.AnchorPointBottomCenter;
367             _bottomClipLayer.ParentOrigin = NDalic.ParentOriginBottomCenter;
368             _bottomClipLayer.ClippingEnable = true;
369             _bottomClipLayer.ClippingBox = new RectInteger(Convert.ToInt32(Constants.LauncherWidth),
370                                                            Convert.ToInt32(_stageSize.height * Constants.BottomContainerShowPositionFactor),
371                                                            Convert.ToInt32((_stageSize.width)),
372                                                            Convert.ToInt32((_stageSize.height - (_stageSize.height * Constants.BottomClipLayerHeightFactor))));  // X, Y, Width, Height
373             _bottomContainer.Add(_bottomClipLayer);
374
375             // Add Launcher items to Bottom Container. Launcher is used to display three images on left of Menu ScrollContainer
376             launcherIcon = new ImageView[Convert.ToInt32(Constants.LauncherItemsCount)];
377             for (int launcherIndex = 0; launcherIndex < Constants.LauncherItemsCount; launcherIndex++)
378             {
379                 launcherIcon[launcherIndex] = new ImageView(_imagePath + "/" + launcherIndex + "-normal.png");
380                 launcherIcon[launcherIndex].Name = "launcherIcon" + launcherIndex;
381                 launcherIcon[launcherIndex].WidthResizePolicy = "USE_NATURAL_SIZE";
382                 launcherIcon[launcherIndex].HeightResizePolicy = "USE_NATURAL_SIZE";
383                 launcherIcon[launcherIndex].ParentOrigin = NDalic.ParentOriginCenterLeft;
384                 launcherIcon[launcherIndex].AnchorPoint = NDalic.AnchorPointCenterLeft;
385                 launcherIcon[launcherIndex].Position = new Vector3(Constants.LauncherIconWidth * launcherIndex + Constants.LauncherLeftMargin, 0.0f, 0.0f);
386                 _bottomContainer.Add(launcherIcon[launcherIndex]);
387             }
388
389             // Add a shadow seperator image between last Launcher icon and Menu ScrollContainer
390             _launcherSeparator = new ImageView(_imagePath + "/focus_launcher_shadow_n.png");
391             _launcherSeparator.Name = "launcherSeparator";
392             _launcherSeparator.WidthResizePolicy = "USE_NATURAL_SIZE";
393             _launcherSeparator.HeightResizePolicy = "FILL_TO_PARENT";
394             _launcherSeparator.ParentOrigin = NDalic.ParentOriginCenterLeft;
395             _launcherSeparator.AnchorPoint = NDalic.AnchorPointCenterLeft;
396             _launcherSeparator.Position = new Vector3(Constants.LauncherIconWidth * Constants.LauncherItemsCount + Constants.LauncherLeftMargin, 0.0f, 0.0f);
397             _bottomContainer.Add(_launcherSeparator);
398
399             // Create Menu Container and add it to Bottom Clip Layer
400             Vector3 menuItemSize = new Vector3((_stageSize.width * Constants.MenuItemWidthFactor) - Constants.MenuContainerPadding,
401                                         _stageSize.height * Constants.MenuItemHeightFactor, 0.0f);
402             _menuContainer.Container.Name = "menu";
403             _menuContainer.ItemSize = menuItemSize;
404             _menuContainer.Gap = Constants.MenuContainerPadding;
405             _menuContainer.MarginX = Constants.MenuContainerMargin;
406             _menuContainer.OffsetYFator = Constants.MenuContainerOffsetYFactor;
407             _menuContainer.OffsetX = Constants.LauncherWidth;
408             _menuContainer.Width = _stageSize.width - Constants.LauncherWidth;
409             _menuContainer.Height = _stageSize.height * Constants.MenuContainerHeightFactor;
410             _menuContainer.ShadowBorder = new ImageView(_imagePath + "/focus_launcher_shadow.9.png");
411             _menuContainer.ShadowBorder.Name = "_menuContainer.ShadowBorder";
412             _menuContainer.ShadowBorder.Size = new Vector3(_menuContainer.ItemSize.width + 40.0f, _menuContainer.ItemSize.height + 50.0f, 0.0f);
413             _menuContainer.ShadowBorder.Position = new Vector3(0.0f, 5.0f, 0.0f);
414             _menuContainer.ShadowBorder.ParentOrigin = NDalic.ParentOriginCenter;
415             _menuContainer.ShadowBorder.AnchorPoint = NDalic.AnchorPointCenter;
416             _menuContainer.SpotLight = spotLight;
417             _bottomClipLayer.Add(_menuContainer);
418
419             CreatePosters(); // Create Items for Poster ScrollContainer
420             CreateMenu();    // Create Items for Menu ScrollContainer
421
422             // Initialize PreFocusChange event of KeyboardFocusManager
423             _keyboardFocusManager = KeyboardFocusManager.Get();
424             _keyboardFocusManager.PreFocusChange += OnKeyboardPreFocusChangeSignal;
425
426             _keyboardFocusIndicator = new ImageView(_imagePath + "/highlight_stroke.9.png");
427             _keyboardFocusIndicator.ParentOrigin = NDalic.ParentOriginCenter;
428             _keyboardFocusIndicator.AnchorPoint = NDalic.AnchorPointCenter;
429             _keyboardFocusIndicator.WidthResizePolicy = "FILL_TO_PARENT";
430             _keyboardFocusIndicator.HeightResizePolicy = "FILL_TO_PARENT";
431
432             _keyboardFocusManager.SetFocusIndicatorActor(_keyboardFocusIndicator);
433
434             _keyboardFocusManager.SetAsFocusGroup(_menuContainer, true);
435             _keyboardFocusManager.SetAsFocusGroup(_postersContainer[0], true);
436             _keyboardFocusManager.SetAsFocusGroup(_postersContainer[1], true);
437             _keyboardFocusManager.SetFocusGroupLoop(true);
438
439             _focusEffect = new FocusEffect();
440
441             // Move Fcous to Bottom Container (Menu ScrollContainer)
442             ShowBottomContainer();
443             _menuContainer.SetFocused(true);
444         }
445     }
446 }
447
448