Move Event Handlers to View class
[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 int _currentPostersContainerID;
11         private int _totalPostersContainers;
12
13         private Application _application;
14         private Stage _stage;
15         private Vector2 _stageSize;
16
17         private List<ScrollContainer> _postersContainer;
18
19         private ScrollContainer _menuContainer;
20         private Vector3 _menuItemSize;
21
22         private Layer _bottomClipLayer;
23         private Layer _topClipLayer;
24         private View _topContainer;
25         private View _bottomContainer;
26
27         private EdenEffect _edenEffect;
28         private string _imagePath;
29
30         private ImageView _keyboardFocusIndicator;
31         private ImageView _launcherSeparator;
32         private ImageView[] launcherIcon;
33         private Animation _showBottomContainerAnimation;
34         private Animation _hideBottomContainerAnimation;
35
36         public 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         public 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].AddItem(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].AddItem(item);
72                     }
73                 }
74
75                 if (j == 0)
76                 {
77                     _postersContainer[j].Show();
78                 }
79                 else
80                 {
81                     _postersContainer[j].Hide();
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.AddItem(menuItem);
101             }
102         }
103
104         private Actor OnKeyboardPreFocusChangeSignal(object source, KeyboardFocusManager.PreFocusChangeEventArgs e)
105         {
106             Actor actor = _menuContainer.ItemRoot;
107
108             if (e.Direction == View.KeyboardFocus.Direction.UP)
109             {
110                 // Move the Focus to Poster ScrollContainer and hide Bottom Container (Menu ScrollContainer)
111                 if (_menuContainer.IsFocused)
112                 {
113                     actor = _postersContainer[_currentPostersContainerID].GetCurrentFocusedActor();
114                     _menuContainer.SetFocused(false);
115                     _postersContainer[_currentPostersContainerID].SetFocused(true);
116                     HideBottomContainer();
117
118                     // Also apply Focus animation on Focused item on Poster ScrollContainer
119                     _postersContainer[_currentPostersContainerID].FocusAnimation(_edenEffect, EdenEffectDirection.BottomToTop);
120                 }
121             }
122             else if (e.Direction == View.KeyboardFocus.Direction.DOWN)
123             {
124                 // Show Bottom Container (Menu ScrollContainer) and move the Focus to it
125                 if (!_menuContainer.IsFocused)
126                 {
127                     ShowBottomContainer();
128                     actor = _menuContainer.GetCurrentFocusedActor();
129                     _postersContainer[_currentPostersContainerID].SetFocused(false);
130                     _menuContainer.SetFocused(true);
131
132                     // Also apply Focus animation on Focused item on Menu ScrollContainer
133                     _menuContainer.FocusAnimation(_edenEffect, EdenEffectDirection.TopToBottom);
134                 }
135             }
136             else if (e.Direction == View.KeyboardFocus.Direction.LEFT)
137             {
138                 if (_menuContainer.IsFocused)
139                 {
140                     // Move the Focus to the left item/image of currently focused item on Menu ScrollContainer
141                     actor = _menuContainer.FocusPrevious();
142
143                     int id = _menuContainer.FocusedItemID % _totalPostersContainers;
144                     if (id != _currentPostersContainerID)
145                     {
146                         _postersContainer[_currentPostersContainerID].Hide();
147                         _currentPostersContainerID = id;
148
149                         _postersContainer[_currentPostersContainerID].Show();
150                     }
151                 }
152                 else
153                 {
154                     // Move the Focus to the left item/image of currently focused item on Poster ScrollContainer
155                     actor = _postersContainer[_currentPostersContainerID].FocusPrevious();
156                 }
157             }
158             else if (e.Direction == View.KeyboardFocus.Direction.RIGHT)
159             {
160                 if (_menuContainer.IsFocused)
161                 {
162                     // Move the Focus to the right item/image of currently focused item on Menu ScrollContainer
163                     actor = _menuContainer.FocusNext();
164
165                     int id = _menuContainer.FocusedItemID % _totalPostersContainers;
166                     if (id != _currentPostersContainerID)
167                     {
168                         _postersContainer[_currentPostersContainerID].Hide();
169                         _currentPostersContainerID = id;
170                         _postersContainer[_currentPostersContainerID].Show();
171                     }
172                 }
173                 else
174                 {
175                     // Move the Focus to the right item/image of currently focused item on Poster ScrollContainer
176                     actor = _postersContainer[_currentPostersContainerID].FocusNext();
177                 }
178             }
179
180             return actor;
181         }
182
183         // Hide Bottom Container (Menu ScrollContainer) when it is not focused
184         private void HideBottomContainer()
185         {
186             _topClipLayer.ClippingBox = new RectInteger(0,
187                                                         Convert.ToInt32(_stageSize.height * Constants.TopContainerPositionFactor),
188                                                         Convert.ToInt32((_stageSize.width)),
189                                                         Convert.ToInt32((_stageSize.height * Constants.TopClipLayerExpandHeightFactor)));  // X, Y, Width, Height
190
191             _hideBottomContainerAnimation.AnimateTo(new Property(_bottomContainer, Actor.Property.POSITION),
192                                                     new Property.Value(new Vector3(0.0f, _stageSize.height * Constants.BottomContainerHidePositionFactor, 0.0f)),
193                                                     new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
194             _hideBottomContainerAnimation.Play();
195         }
196
197         // Show (unhide) Bottom Container (Menu ScrollContainer) when it is focused
198         private void ShowBottomContainer()
199         {
200             _topClipLayer.ClippingBox = new RectInteger(0,
201                                                         Convert.ToInt32(_stageSize.height * Constants.TopContainerPositionFactor),
202                                                         Convert.ToInt32((_stageSize.width)),
203                                                         Convert.ToInt32((_stageSize.height * Constants.TopClipLayerHeightFactor)));  // X, Y, Width, Height
204
205             _showBottomContainerAnimation.AnimateTo(new Property(_bottomContainer, Actor.Property.POSITION),
206                                                     new Property.Value(new Vector3(0.0f, _stageSize.height * Constants.BottomContainerShowPositionFactor, 0.0f)),
207                                                     new AlphaFunction(AlphaFunction.BuiltinFunction.EASE_OUT_SINE));
208             _showBottomContainerAnimation.Play();
209         }
210
211         // First screen demo Application initialisation
212         private void OnInitialize(object source, AUIApplicationInitEventArgs e)
213         {
214             _stage = Stage.GetCurrent();
215             _stageSize = _stage.GetSize();
216 //            _stage.SetBackgroundColor(NDalic.TRANSPARENT);
217
218             _totalPostersContainers = Constants.TotalPostersContainers;
219             _imagePath = "./images/"; // Desktop
220 //            _imagePath = "/home/owner/apps_rw/org.tizen.firstscreen/res/images/"; // Target
221
222             _postersContainer = new List<ScrollContainer> ();
223             _menuContainer = new ScrollContainer ();
224
225             _hideBottomContainerAnimation = new Animation(0.25f);
226             _showBottomContainerAnimation = new Animation(0.25f);
227
228             // Create a Top Container for poster items
229             _topContainer = new View();
230             _topContainer.Size = new Vector3(_stageSize.width, _stageSize.height * Constants.TopContainerHeightFactor, 0.0f);
231             _topContainer.Position = new Vector3(0.0f, _stageSize.y * Constants.TopContainerPositionFactor, 0.0f);
232             _topContainer.ParentOrigin = NDalic.ParentOriginTopLeft;
233             _topContainer.AnchorPoint = NDalic.AnchorPointTopLeft;
234
235             // Add a background to Top container
236             Property.Map visual = new Property.Map();
237             visual.Insert(NDalic.VISUAL_PROPERTY_TYPE, new Property.Value((int)VisualType.IMAGE));
238             visual.Insert(NDalic.IMAGE_VISUAL_URL, new Property.Value(_imagePath + "/background.png"));
239             _topContainer.Background = visual;
240             _topContainer.Name = "TopControl";
241
242             // Create a Bottom Container
243             _bottomContainer = new View();
244             _bottomContainer.Size = new Vector3(_stageSize.width, _stageSize.height * Constants.BottomContainerHeightFactor, 0.0f);
245             _bottomContainer.Position = new Vector3(0.0f, _stageSize.height * Constants.BottomContainerHidePositionFactor, 0.0f);
246             _bottomContainer.ParentOrigin = NDalic.ParentOriginTopLeft;
247             _bottomContainer.AnchorPoint = NDalic.AnchorPointTopLeft;
248
249             // Add a background to Bottom Container
250             visual = new Property.Map();
251             visual.Insert(NDalic.VISUAL_PROPERTY_TYPE, new Property.Value((int)VisualType.IMAGE));
252             visual.Insert(NDalic.IMAGE_VISUAL_URL, new Property.Value(_imagePath + "/background.png"));
253             _bottomContainer.Background = visual;
254             _bottomContainer.Name = "BottomControl";
255
256             // Add both Top and Bottom Containers to Stage
257             _stage.Add(_topContainer);
258             _stage.Add(_bottomContainer);
259
260             // Add a clip layer to Top Container
261             _topClipLayer = new Layer();
262             _topClipLayer.AnchorPoint = NDalic.AnchorPointBottomCenter;
263             _topClipLayer.ParentOrigin = NDalic.ParentOriginBottomCenter;
264             _topClipLayer.ClippingEnable = true;
265             _topClipLayer.ClippingBox = new RectInteger(0,
266                                                         Convert.ToInt32(_stageSize.height * Constants.TopContainerPositionFactor),
267                                                         Convert.ToInt32((_stageSize.width)),
268                                                         Convert.ToInt32((_stageSize.height * Constants.TopClipLayerHeightFactor)));  // X, Y, Width, Height
269             _topContainer.Add(_topClipLayer);
270
271             // Create a SpotLight for items / images of both Poster and Menu ScrollContainers
272             ImageView spotLight = new ImageView(_imagePath + "/highlight_spot.png");
273             spotLight.WidthResizePolicy = "USE_NATURAL_SIZE";
274             spotLight.HeightResizePolicy = "USE_NATURAL_SIZE";
275             spotLight.ParentOrigin = NDalic.ParentOriginCenter;
276             spotLight.AnchorPoint = NDalic.AnchorPointCenter;
277             spotLight.Name = "spotLight";
278
279             // Create a shadowBorder for items / images of Poster ScrollContainers
280             ImageView shadowBorder = new ImageView(_imagePath + "/thumbnail_shadow.9.png");
281             shadowBorder.ParentOrigin = NDalic.ParentOriginCenter;
282             shadowBorder.AnchorPoint = NDalic.AnchorPointCenter;
283             shadowBorder.WidthResizePolicy = "SIZE_FIXED_OFFSET_FROM_PARENT";
284             shadowBorder.HeightResizePolicy = "SIZE_FIXED_OFFSET_FROM_PARENT";
285             shadowBorder.SetSizeModeFactor(new Vector3(32.0f, 41.0f, 0.0f));
286             shadowBorder.Name = "poster shadowBorder";
287
288             // Create Poster Containers and add them to Top Clip layer
289             for (int i = 0; i < _totalPostersContainers; i++)
290             {
291                 _postersContainer.Add(new ScrollContainer());
292                 _postersContainer[i].Container.Name = "poster" + i;
293                 if (i == 0)
294                 {
295                     _postersContainer[i].ItemSize = new Vector3((_stageSize.width * Constants.Poster0ItemWidthFactor) - Constants.PostersContainerPadding,
296                                                                 _stageSize.height * Constants.PostersItemHeightFactor, 0.0f);
297                 }
298                 else
299                 {
300                     _postersContainer[i].ItemSize = new Vector3(_stageSize.width * Constants.Poster1ItemWidthFactor,
301                                                                 _stageSize.height * Constants.PostersItemHeightFactor, 0.0f);
302                 }
303                 _postersContainer[i].Padding = Constants.PostersContainerPadding;
304                 _postersContainer[i].MarginX = Constants.PostersContainerMargin;
305                 _postersContainer[i].OffsetY = Constants.PostersContainerOffsetYFactor;
306                 _postersContainer[i].Width = _stageSize.width;
307                 _postersContainer[i].Height = _stageSize.height * Constants.PostersContainerHeightFactor;
308                 _postersContainer[i].ShadowBorder = shadowBorder;
309                 _postersContainer[i].ShadowBorder.Position = new Vector3(0.0f, 4.0f, 0.0f);
310                 _postersContainer[i].SpotLight = spotLight;
311                 _topClipLayer.Add(_postersContainer[i].Container);
312             }
313
314             // Add a clip layer to Bottom Container
315             _bottomClipLayer = new Layer();
316             _bottomClipLayer.AnchorPoint = NDalic.AnchorPointBottomCenter;
317             _bottomClipLayer.ParentOrigin = NDalic.ParentOriginBottomCenter;
318             _bottomClipLayer.ClippingEnable = true;
319             _bottomClipLayer.ClippingBox = new RectInteger(Convert.ToInt32(Constants.LauncherWidth),
320                                                            Convert.ToInt32(_stageSize.height * Constants.BottomContainerShowPositionFactor),
321                                                            Convert.ToInt32((_stageSize.width)),
322                                                            Convert.ToInt32((_stageSize.height - (_stageSize.height * Constants.BottomClipLayerHeightFactor))));  // X, Y, Width, Height
323             _bottomContainer.Add(_bottomClipLayer);
324
325             // Add Launcher items to Bottom Container. Launcher is used to display three images on left of Menu ScrollContainer
326             launcherIcon = new ImageView[Convert.ToInt32(Constants.LauncherItemsCount)];
327             for (int launcherIndex = 0; launcherIndex < Constants.LauncherItemsCount; launcherIndex++)
328             {
329                 launcherIcon[launcherIndex] = new ImageView(_imagePath + "/" + launcherIndex + "-normal.png");
330                 launcherIcon[launcherIndex].Name = "launcherIcon" + launcherIndex;
331                 launcherIcon[launcherIndex].WidthResizePolicy = "USE_NATURAL_SIZE";
332                 launcherIcon[launcherIndex].HeightResizePolicy = "USE_NATURAL_SIZE";
333                 launcherIcon[launcherIndex].ParentOrigin = NDalic.ParentOriginCenterLeft;
334                 launcherIcon[launcherIndex].AnchorPoint = NDalic.AnchorPointCenterLeft;
335                 launcherIcon[launcherIndex].Position = new Vector3(Constants.LauncherIconWidth * launcherIndex + Constants.LauncherLeftMargin, 0.0f, 0.0f);
336                 _bottomContainer.Add(launcherIcon[launcherIndex]);
337             }
338
339             // Add a shadow seperator image between last Launcher icon and Menu ScrollContainer
340             _launcherSeparator = new ImageView(_imagePath + "/eden_launcher_shadow_n.png");
341             _launcherSeparator.Name = "launcherSeparator";
342             _launcherSeparator.WidthResizePolicy = "USE_NATURAL_SIZE";
343             _launcherSeparator.HeightResizePolicy = "FILL_TO_PARENT";
344             _launcherSeparator.ParentOrigin = NDalic.ParentOriginCenterLeft;
345             _launcherSeparator.AnchorPoint = NDalic.AnchorPointCenterLeft;
346             _launcherSeparator.Position = new Vector3(Constants.LauncherIconWidth * Constants.LauncherItemsCount + Constants.LauncherLeftMargin, 0.0f, 0.0f);
347             _bottomContainer.Add(_launcherSeparator);
348
349             // Create Menu Container and add it to Bottom Clip Layer
350             _menuItemSize = new Vector3((_stageSize.width * Constants.MenuItemWidthFactor) - Constants.MenuContainerPadding,
351                                         _stageSize.height * Constants.MenuItemHeightFactor, 0.0f);
352             _menuContainer.Container.Name = "menu";
353             _menuContainer.ItemSize = _menuItemSize;
354             _menuContainer.Padding = Constants.MenuContainerPadding;
355             _menuContainer.MarginX = Constants.MenuContainerMargin;
356             _menuContainer.OffsetY = Constants.MenuContainerOffsetYFactor;
357             _menuContainer.OffsetX = Constants.LauncherWidth;
358             _menuContainer.Width = _stageSize.width - Constants.LauncherWidth;
359             _menuContainer.Height = _stageSize.height * Constants.MenuContainerHeightFactor;
360             _menuContainer.ShadowBorder = new ImageView(_imagePath + "/eden_launcher_shadow.9.png");
361             _menuContainer.ShadowBorder.Name = "_menuContainer.ShadowBorder";
362             _menuContainer.ShadowBorder.Size = new Vector3(_menuContainer.ItemSize.width + 40.0f, _menuContainer.ItemSize.height + 50.0f, 0.0f);
363             _menuContainer.ShadowBorder.Position = new Vector3(0.0f, 5.0f, 0.0f);
364             _menuContainer.ShadowBorder.ParentOrigin = NDalic.ParentOriginCenter;
365             _menuContainer.ShadowBorder.AnchorPoint = NDalic.AnchorPointCenter;
366             _menuContainer.SpotLight = spotLight;
367             _bottomClipLayer.Add(_menuContainer.Container);
368
369             CreatePosters(); // Create Items for Poster ScrollContainer
370             CreateMenu();    // Create Items for Menu ScrollContainer
371
372             // Initialize PreFocusChange event of KeyboardFocusManager
373             KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.Get();
374             keyboardFocusManager.PreFocusChange += OnKeyboardPreFocusChangeSignal;
375
376             _keyboardFocusIndicator = new ImageView(_imagePath + "/highlight_stroke.9.png");
377             _keyboardFocusIndicator.ParentOrigin = NDalic.ParentOriginCenter;
378             _keyboardFocusIndicator.AnchorPoint = NDalic.AnchorPointCenter;
379             _keyboardFocusIndicator.WidthResizePolicy = "FILL_TO_PARENT";
380             _keyboardFocusIndicator.HeightResizePolicy = "FILL_TO_PARENT";
381
382             keyboardFocusManager.SetFocusIndicatorActor(_keyboardFocusIndicator);
383
384             _edenEffect = new EdenEffect();
385
386             // Move Fcous to Bottom Container (Menu ScrollContainer)
387             ShowBottomContainer();
388             _menuContainer.SetFocused(true);
389         }
390     }
391 }
392