[NUI] Merge NUI.Samples to TizenFX (#1222)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / ItemViewDemo / ItemViewSample.cs
1 using System;
2 using System.Collections.Generic;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.Components;
5 using Tizen.NUI.UIComponents;
6
7 namespace Tizen.NUI.Samples
8 {
9     public class ZItemViewSample : IExample
10     {
11         private const float MIN_SWIPE_DISTANCE = 15.0f;
12         private const float MIN_SWIPE_SPEED = 5.0f;
13         private const float SCROLL_TO_ITEM_ANIMATION_TIME = 5.0f;
14         private const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT = 1.0f;
15         private const float DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE = 0.8f;
16         private const float DEPTH_LAYOUT_COLUMNS = 3.0f;
17         private const int BUTTON_BORDER = -10;
18         private const float ITEM_BORDER_SIZE = 2.0f;
19         private const int ITEM_COUNT = 300;
20
21         private string APPLICATION_TITLE = "ItemView";
22         private string SPIRAL_LABEL = "Spiral";
23         private string GRID_LABEL = "Grid";
24         private string DEPTH_LABEL = "Depth";
25
26         private string TOOLBAR_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/top-bar.png";
27         private string EDIT_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-edit.png";
28         private string EDIT_IMAGE_SELECTED = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-edit-selected.png";
29         private string DELETE_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-delete.png";
30         private string DELETE_IMAGE_SELECTED = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-delete-selected.png";
31         private string REPLACE_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-replace.png";
32         private string REPLACE_IMAGE_SELECTED = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-replace-selected.png";
33         private string INSERT_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-insert.png";
34         private string INSERT_IMAGE_SELECTED = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-insert-selected.png.png";
35         private string SELECTED_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/item-select-check.png";
36
37         private string SPIRAL_LAYOUT_IMAGE = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-item-view-layout-spiral.png";
38         private string SPIRAL_LAYOUT_IMAGE_SELECTED = CommonResource.GetFHResourcePath() + "ItemViewDemo/icon-item-view-layout-spiral-selected.png";
39         private string GRID_LAYOUT_IMAGE = CommonResource.GetFHResourcePath() +  "ItemViewDemo/icon-item-view-layout-grid.png";
40         private string GRID_LAYOUT_IMAGE_SELECTED = CommonResource.GetFHResourcePath() +  "ItemViewDemo/icon-item-view-layout-grid-selected.png";
41         private string DEPTH_LAYOUT_IMAGE = CommonResource.GetFHResourcePath() +  "ItemViewDemo/icon-item-view-layout-depth.png";
42         private string DEPTH_LAYOUT_IMAGE_SELECTED = CommonResource.GetFHResourcePath() +  "ItemViewDemo/icon-item-view-layout-depth-selected.png";
43
44         
45         private ItemView mItemView;
46         private ItemFactoryWrapper mItemFactoryWrapper;
47         private PropertyMap mGridLayout;
48         private PropertyMap mDepthLayout;
49         private PropertyMap mSpiralLayout;
50         private PropertyArray mLayout;
51         private int mCurrentLayout = (int)AllImagesLayouts.SPIRAL_LAYOUT;
52         private Mode mMode = Mode.MODE_NORMAL;
53
54         private LongPressGestureDetector mLongPressGestureDetector;
55         private TapGestureDetector mTapDetector;
56
57         private PushButton mLayoutButton;
58         private PushButton mEditButton;
59         private PushButton mDeleteButton;
60         private PushButton mInsertButton;
61         private PushButton mReplaceButton;
62         private TextLabel mTitle;
63         private ImageView mToolBarBackground;
64
65         private Layer mToolBarLayer;
66         //private Layer mContentLayer;
67         private View mContentView;
68         //private View mToolBarView;
69
70         private int mDurationSeconds = 250;
71
72         public static Vector3 DepthLayoutItemSizeFunctionPortrait(float layoutWidth)
73         {
74             float width = (layoutWidth / (DEPTH_LAYOUT_COLUMNS + 1.0f)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_PORTRAIT;
75
76             // 1x1 aspect ratio
77             return new Vector3(width, width, width);
78         }
79
80         public static Vector3 DepthLayoutItemSizeFunctionLandscape(float layoutWidth)
81         {
82             float width = (layoutWidth / (DEPTH_LAYOUT_COLUMNS + 1.0f)) * DEPTH_LAYOUT_ITEM_SIZE_FACTOR_LANDSCAPE;
83
84             // 1x1 aspect ratio
85             return new Vector3(width, width, width);
86         }
87
88         private void SetTitle(string title)
89         {
90             if(mTitle != null)
91             {
92                 mTitle.Text = title;
93             }
94         }
95
96         public void CreateGridLayout()
97         {
98             mGridLayout = new PropertyMap();
99             mGridLayout.Insert((int)DefaultItemLayoutProperty.TYPE, new PropertyValue((int)DefaultItemLayoutType.GRID));
100             //mGridLayout.Add((int)DefaultItemLayoutProperty.GRID_COLUMN_NUMBER, new Dali.Property.Value(_columnNumber))
101             //     .Add((int)DefaultItemLayoutProperty.GRID_ROW_SPACING, new Dali.Property.Value(_rowSpacing))
102             //     .Add((int)DefaultItemLayoutProperty.GRID_COLUMN_SPACING, new Dali.Property.Value(_columnSpacing))
103             //     .Add((int)DefaultItemLayoutProperty.GRID_TOP_MARGIN, new Dali.Property.Value(TopMargin))
104             //     .Add((int)DefaultItemLayoutProperty.GRID_BOTTOM_MARGIN, new Dali.Property.Value(BottomMargin))
105             //     .Add((int)DefaultItemLayoutProperty.GRID_BOTTOM_MARGIN, new Dali.Property.Value(SideMargin));
106         }
107
108         public void CreateDepthLayout()
109         {
110             mDepthLayout = new PropertyMap();
111             mDepthLayout.Insert((int)DefaultItemLayoutProperty.TYPE, new PropertyValue((int)DefaultItemLayoutType.DEPTH));
112         }
113
114         public void CreateSpiralLayout()
115         {
116             mSpiralLayout = new PropertyMap();
117             mSpiralLayout.Insert((int)DefaultItemLayoutProperty.TYPE, new PropertyValue((int)DefaultItemLayoutType.SPIRAL));
118         }
119
120         void SetLayoutImage()
121         {
122             if (mLayoutButton)
123             {
124                 switch (mCurrentLayout)
125                 {
126                     case (int)AllImagesLayouts.SPIRAL_LAYOUT:
127                         {
128                             mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE);
129                             mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE_SELECTED);
130                             break;
131                         }
132
133                     case (int)AllImagesLayouts.GRID_LAYOUT:
134                         {
135                             mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(GRID_LAYOUT_IMAGE);
136                             mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(GRID_LAYOUT_IMAGE_SELECTED);
137                             break;
138                         }
139
140                     case (int)AllImagesLayouts.DEPTH_LAYOUT:
141                         {
142                             mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(DEPTH_LAYOUT_IMAGE);
143                             mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(DEPTH_LAYOUT_IMAGE_SELECTED);
144                             break;
145                         }
146                     default:
147                         break;
148                 }
149             }
150         }
151
152         void ChangeLayout()
153         {
154             Animation animation = new Animation(mDurationSeconds);
155             animation.Finished += AnimationFinished;
156             animation.AnimateTo(mItemView, "Opacity", 0.0f);
157             animation.Play();
158         }
159
160         void AnimationFinished(object sender, EventArgs e)
161         {
162             SetLayout(mCurrentLayout);
163             Animation animation = new Animation(mDurationSeconds);
164             animation.AnimateTo(mItemView, "Opacity", 1.0f);
165             animation.Play();
166         }
167
168         private PropertyMap CreateImageVisual(string url)
169         {
170             PropertyMap temp = new PropertyMap();
171             temp.Insert(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
172             temp.Insert(ImageVisualProperty.URL, new PropertyValue(url));
173             return temp;
174         }
175
176         public void Activate()
177         {
178             Window window = Window.Instance;
179             window.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
180             window.BackgroundColor = Color.Black;
181             CreateContentView();
182             //CreateToolBarView();
183             CreateToolBarLayer();
184
185             CreateLayoutButton();
186             CreateEditButton();
187             CreateInsertButton();
188             CreateReplaceButton();
189             CreateDeleteButton();
190
191             CreateGridLayout();
192             CreateDepthLayout();
193             CreateSpiralLayout();
194
195             mLayout = new PropertyArray();
196             mLayout.PushBack(new PropertyValue(mSpiralLayout));
197             mLayout.PushBack(new PropertyValue(mDepthLayout));
198             mLayout.PushBack(new PropertyValue(mGridLayout));
199
200
201             mItemFactoryWrapper = new ItemFactoryWrapper();
202             mItemFactoryWrapper.GetNumberDelegate = GetNumberOfItems;
203             mItemFactoryWrapper.NewItemDelegate = NewItemView;
204
205             mItemView = new ItemView(mItemFactoryWrapper);
206             //mItemView.BackgroundColor = Color.Blue;
207             mItemView.ParentOrigin = Position.ParentOriginCenter;
208             mItemView.PivotPoint = Position.PivotPointCenter;
209             mItemView.PositionUsesPivotPoint = true;
210             window.Add(mItemView);
211
212             window.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
213
214             mItemView.Layout = mLayout;
215             mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
216             mItemView.SetMinimumSwipeSpeed(MIN_SWIPE_SPEED);
217
218             SetLayout((int)mCurrentLayout);
219             SetLayoutTitle();
220             SetLayoutImage();
221
222             mLongPressGestureDetector = new LongPressGestureDetector();
223             mLongPressGestureDetector.Attach(mItemView);
224             mLongPressGestureDetector.Detected += OnLongPressGestureDetected;
225         }
226
227         private void OnLongPressGestureDetected(object source, LongPressGestureDetector.DetectedEventArgs e)
228         {
229             switch (e.LongPressGesture.State)
230             {
231                 case Gesture.StateType.Started:
232                     {
233                         Size2D windowSize = Window.Instance.Size;
234                         ItemRange range = new ItemRange(0, 0);
235                         mItemView.GetItemsRange(range);
236
237                         uint item = (e.LongPressGesture.ScreenPoint.Y < 0.5f * (float)windowSize.Height) ? range.begin : range.end;
238                         
239                         mItemView.ScrollToItem(item, SCROLL_TO_ITEM_ANIMATION_TIME);
240
241                         break;
242                     }
243                 case Gesture.StateType.Finished:
244                     {
245                         PropertyMap attributes = new PropertyMap();
246                         mItemView.DoAction("stopScrolling", attributes);
247                         break;
248                     }
249                 default:
250                     {
251                         break;
252                     }
253             }
254         }
255
256         void SetLayout(int layoutId)
257         {
258             Window window = Window.Instance;
259             switch (mCurrentLayout)
260             {
261                 case (int)AllImagesLayouts.SPIRAL_LAYOUT:
262                 case (int)AllImagesLayouts.DEPTH_LAYOUT:
263                     {
264                         Window.Instance.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
265                         break;
266                     }
267                 case (int)AllImagesLayouts.GRID_LAYOUT:
268                     {
269                         Window.Instance.GetDefaultLayer().Behavior = Layer.LayerBehavior.LayerUI;
270                         break;
271                     }
272             }
273
274             // Set the new orientation to the layout
275             //Issue
276             //mItemView.GetLayout(layoutId)->SetOrientation(static_cast<ControlOrientation::Type>(mOrientation / 90));
277
278             Size2D windowSize = Window.Instance.Size;
279
280             if (layoutId == (int)AllImagesLayouts.DEPTH_LAYOUT)
281             {
282                 mDepthLayout.Insert((int)DefaultItemLayoutProperty.ITEM_SIZE, new PropertyValue(DepthLayoutItemSizeFunctionPortrait(800)));
283                 mLayout.Clear();
284                 mLayout.PushBack(new PropertyValue(mSpiralLayout));
285                 mLayout.PushBack(new PropertyValue(mDepthLayout));
286                 mLayout.PushBack(new PropertyValue(mGridLayout));
287                 mItemView.Layout = mLayout;
288
289                 // Set up the depth layout according to the new orientation
290                 // issue
291                 //if (Toolkit::IsVertical(mDepthLayout->GetOrientation()))
292                 //{
293                 //    mDepthLayout->SetItemSize(DepthLayoutItemSizeFunctionPortrait(stageSize.width));
294                 //}
295                 //else
296                 //{
297                 //    mDepthLayout->SetItemSize(DepthLayoutItemSizeFunctionLandscape(stageSize.height));
298                 //}
299             }
300
301             // Enable anchoring for depth layout only
302             mItemView.SetAnchoring(layoutId == (int)AllImagesLayouts.DEPTH_LAYOUT);
303
304             // Activate the layout
305             mItemView.ActivateLayout((uint)layoutId, new Vector3(800, 800, 800), 0.0f);
306             //mItemView.ActivateLayout((uint)layoutId, new Vector3(windowSize.Width, windowSize.Height, windowSize.Width), 0.0f);
307         }
308
309
310
311         public uint GetNumberOfItems()
312         {
313             return ITEM_COUNT;
314         }
315
316         public View NewItemView(uint itemId)
317         {
318             // Create an image view for this item
319             string imagePath = CommonResource.GetFHResourcePath() + "ItemViewDemo/gallery/gallery-medium-";
320             uint id = itemId % 53;
321             imagePath += id.ToString();
322             imagePath += ".jpg";
323             PropertyMap propertyMap = new PropertyMap();
324             propertyMap.Insert(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
325             propertyMap.Insert(ImageVisualProperty.URL, new PropertyValue(imagePath));
326             propertyMap.Insert(ImageVisualProperty.FittingMode, new PropertyValue((int)VisualFittingModeType.FitKeepAspectRatio));
327             ImageView actor = new ImageView();
328             actor.Image = propertyMap;
329             actor.PositionZ = 0.0f;
330             actor.Position = new Position(1000.0f, 0, -1000.0f);
331
332             // Add a border image child actor
333             ImageView borderActor = new ImageView();
334             borderActor.ParentOrigin = ParentOrigin.Center;
335             borderActor.PivotPoint = PivotPoint.Center;
336             borderActor.PositionUsesPivotPoint = true;
337             borderActor.HeightResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
338             borderActor.WidthResizePolicy = ResizePolicyType.SizeFixedOffsetFromParent;
339             borderActor.SetSizeModeFactor(new Vector3(2.0f * ITEM_BORDER_SIZE, 2.0f * ITEM_BORDER_SIZE, 0.0f));
340             //borderActor.SetColorMode(ColorMode.UseParentColor);
341
342             PropertyMap borderProperty = new PropertyMap();
343             borderProperty.Insert(Visual.Property.Type, new PropertyValue((int)Visual.Type.Border));
344             borderProperty.Insert(BorderVisualProperty.Color, new PropertyValue(Color.White));
345             borderProperty.Insert(BorderVisualProperty.Size, new PropertyValue(ITEM_BORDER_SIZE));
346             borderProperty.Insert(BorderVisualProperty.AntiAliasing, new PropertyValue(true));
347             borderActor.Image = borderProperty;
348
349             actor.Add(borderActor);
350
351             Size spiralItemSize = new Size(30, 30, 0);
352             //static_cast < ItemLayout &> (*mSpiralLayout).GetItemSize(0u, Vector3(Stage::GetCurrent().GetSize()), spiralItemSize);
353             // Add a checkbox child actor; invisible until edit-mode is enabled
354             ImageView checkBox = new ImageView();
355             checkBox.Name = "CheckBox";
356             //checkBox.SetColorMode(ColorMode.UseParentColor);
357             checkBox.ParentOrigin = ParentOrigin.TopRight;
358             checkBox.PivotPoint = PivotPoint.TopRight;
359             checkBox.Size = spiralItemSize;
360             checkBox.PositionZ = 0.1f;
361
362             PropertyMap solidColorProperty = new PropertyMap();
363             solidColorProperty.Insert(Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
364             solidColorProperty.Insert(ColorVisualProperty.MixColor, new PropertyValue(new Color(0.0f, 0.0f, 0.0f, 0.6f)));
365             checkBox.Image = solidColorProperty;
366
367             if (Mode.MODE_REMOVE_MANY != mMode &&
368                 Mode.MODE_INSERT_MANY != mMode &&
369                 Mode.MODE_REPLACE_MANY != mMode)
370             {
371                 checkBox.Hide();
372             }
373             borderActor.Add(checkBox);
374
375             ImageView tick = new ImageView(SELECTED_IMAGE);
376             tick.Name = "Tick";
377             //tick.SetColorMode(ColorMode.UseParentColor);
378             tick.ParentOrigin = ParentOrigin.TopRight;
379             tick.PivotPoint = PivotPoint.TopRight;
380             tick.Size = spiralItemSize;
381             tick.Hide();
382             checkBox.Add(tick);
383
384             if(mTapDetector)
385             {
386                 mTapDetector.Attach(actor);
387             }
388             return actor;
389         }
390
391         public void Deactivate()
392         {
393             if(mLayoutButton != null)
394             {
395                 mLayoutButton.GetParent().Remove(mLayoutButton);
396                 mLayoutButton.Dispose();
397             }
398
399             if (mEditButton != null)
400             {
401                 mEditButton.GetParent().Remove(mEditButton);
402                 mEditButton.Dispose();
403             }
404
405             if (mReplaceButton != null)
406             {
407                 mReplaceButton.GetParent().Remove(mReplaceButton);
408                 mReplaceButton.Dispose();
409             }
410
411             if (mInsertButton != null)
412             {
413                 mInsertButton.GetParent().Remove(mInsertButton);
414                 mInsertButton.Dispose();
415             }
416
417             if (mDeleteButton != null)
418             {
419                 mDeleteButton.GetParent().Remove(mDeleteButton);
420                 mDeleteButton.Dispose();
421             }
422
423             if (mTitle != null)
424             {
425                 mTitle.GetParent().Remove(mTitle);
426                 mTitle.Dispose();
427             }
428
429             if (mToolBarBackground != null)
430             {
431                 mToolBarBackground.GetParent().Remove(mToolBarBackground);
432                 mToolBarBackground.Dispose();
433             }
434
435             if(mItemView != null)
436             {
437                 mItemView.GetParent().Remove(mItemView);
438                 mItemView.Dispose();
439             }
440
441             if (mContentView != null)
442             {
443                 Window.Instance.Remove(mContentView);
444                 mContentView.Dispose();
445             }
446
447             if(mToolBarLayer != null)
448             {
449                 Window.Instance.RemoveLayer(mToolBarLayer);
450                 mToolBarLayer.Dispose();
451             }
452
453             Window.Instance.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer2D;
454         }
455
456         public void CreateContentView()
457         {
458             mContentView = new View();
459             mContentView.ParentOrigin = ParentOrigin.Center;
460             mContentView.PivotPoint = PivotPoint.Center;
461             mContentView.PositionUsesPivotPoint = true;
462             mContentView.WidthResizePolicy = ResizePolicyType.FillToParent;
463             mContentView.HeightResizePolicy = ResizePolicyType.FillToParent;
464             Window.Instance.Add(mContentView);
465
466             mContentView.LowerToBottom();
467         }
468
469
470         //public void CreateToolBarView()
471         //{
472         //    mToolBarView = new View();
473         //    mToolBarView.Name = "TOOLBAR";
474         //    mToolBarView.BackgroundImage = TOOLBAR_IMAGE;
475         //    mToolBarView.ParentOrigin = ParentOrigin.TopCenter;
476         //    mToolBarView.PivotPoint = PivotPoint.TopCenter;
477         //    mToolBarView.PositionUsesPivotPoint = true;
478         //    mToolBarView.WidthResizePolicy = ResizePolicyType.FillToParent;
479         //    mToolBarView.Size2D = new Size2D(0, 80);
480         //    Window.Instance.Add(mToolBarView);
481
482         //    mToolBarView.RaiseToTop();
483
484         //    mTitle = new TextLabel();
485         //    mTitle.ParentOrigin = ParentOrigin.TopLeft;
486         //    mTitle.PivotPoint = PivotPoint.TopLeft;
487         //    mTitle.PositionUsesPivotPoint = true;
488         //    mTitle.PointSize = 10.0f;
489         //    mTitle.WidthResizePolicy = ResizePolicyType.FillToParent;
490         //    mTitle.HeightResizePolicy = ResizePolicyType.FillToParent;
491         //    mTitle.Text = APPLICATION_TITLE;
492         //    mTitle.VerticalAlignment = VerticalAlignment.Center;
493         //    mTitle.HorizontalAlignment = HorizontalAlignment.Center;
494         //    mToolBarView.Add(mTitle);
495         //}
496
497         private void CreateToolBarLayer()
498         {
499             mToolBarLayer = new Layer();
500             mToolBarLayer.Name = "TOOLBAR";
501             //mToolBarLayer.BackgroundImage = TOOLBAR_IMAGE;
502             //mToolBarLayer.PositionUsesPivotPoint = true;
503             mToolBarLayer.SetAnchorPoint(PivotPoint.TopCenter);
504             mToolBarLayer.SetParentOrigin(ParentOrigin.TopCenter);
505             mToolBarLayer.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.Width);
506             mToolBarLayer.SetSize(0, 80);
507             Window.Instance.AddLayer(mToolBarLayer);
508
509             mToolBarLayer.RaiseToTop();
510
511             mToolBarBackground = new ImageView();
512             mToolBarBackground.ParentOrigin = ParentOrigin.TopLeft;
513             mToolBarBackground.PivotPoint = PivotPoint.TopLeft;
514             mToolBarBackground.PositionUsesPivotPoint = true;
515             mToolBarBackground.WidthResizePolicy = ResizePolicyType.FillToParent;
516             mToolBarBackground.HeightResizePolicy = ResizePolicyType.FillToParent;
517             mToolBarBackground.ResourceUrl = TOOLBAR_IMAGE;
518             mToolBarLayer.Add(mToolBarBackground);
519
520             mTitle = new TextLabel();
521             mTitle.ParentOrigin = ParentOrigin.TopLeft;
522             mTitle.PivotPoint = PivotPoint.TopLeft;
523             mTitle.PositionUsesPivotPoint = true;
524             mTitle.PointSize = 10.0f;
525             mTitle.WidthResizePolicy = ResizePolicyType.FillToParent;
526             mTitle.HeightResizePolicy = ResizePolicyType.FillToParent;
527             mTitle.Text = APPLICATION_TITLE;
528             mTitle.VerticalAlignment = VerticalAlignment.Center;
529             mTitle.HorizontalAlignment = HorizontalAlignment.Center;
530             mToolBarLayer.Add(mTitle);
531         }
532
533         private void CreateLayoutButton()
534         {
535             mLayoutButton = new PushButton();
536             mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE);
537             mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE_SELECTED);
538             mLayoutButton.Size2D = new Size2D(34, 34);
539             mLayoutButton.ParentOrigin = ParentOrigin.TopRight;
540             mLayoutButton.PivotPoint = PivotPoint.TopRight;
541             mLayoutButton.Position2D = new Position2D(0, 25);
542             mLayoutButton.PositionUsesPivotPoint = true;
543             mLayoutButton.LeaveRequired = true;
544             mLayoutButton.Clicked += (obj, e) =>
545             {
546                 mCurrentLayout = (mCurrentLayout + 1) % (int)mItemView.GetLayoutCount();
547                 ChangeLayout();
548
549                 SetLayoutTitle();
550                 SetLayoutImage();
551                 return true;
552             };
553             //mToolBarView.Add(mLayoutButton);
554             mToolBarLayer.Add(mLayoutButton);
555         }
556
557         public void CreateEditButton()
558         {
559             mEditButton = new PushButton();
560             mEditButton.UnselectedBackgroundVisual = CreateImageVisual(EDIT_IMAGE);
561             mEditButton.SelectedBackgroundVisual = CreateImageVisual(EDIT_IMAGE_SELECTED);
562             mEditButton.Size2D = new Size2D(34, 34);
563             mEditButton.ParentOrigin = ParentOrigin.TopLeft;
564             mEditButton.PivotPoint = PivotPoint.TopLeft;
565             mEditButton.Position2D = new Position2D(5, 25);
566             mEditButton.PositionUsesPivotPoint = true;
567             mEditButton.LeaveRequired = true;
568             mEditButton.Clicked += (obj, e) =>
569             {
570                 SwitchToNextMode();
571                 return true;
572             };
573             //mToolBarView.Add(mEditButton);
574             mToolBarLayer.Add(mEditButton);
575         }
576
577         private void SetLayoutTitle()
578         {
579             if (Mode.MODE_NORMAL == mMode)
580             {
581                 string title = APPLICATION_TITLE;
582                 switch (mCurrentLayout)
583                 {
584                     case (int)AllImagesLayouts.SPIRAL_LAYOUT:
585                         title = title + ": " + SPIRAL_LABEL;
586                         break;
587                     case (int)AllImagesLayouts.GRID_LAYOUT:
588                         title = title + ": " + GRID_LABEL;
589                         break;
590                     case (int)AllImagesLayouts.DEPTH_LAYOUT:
591                         title = title + ": " + DEPTH_LABEL;
592                         break;
593                     default:
594                         break;
595                 }
596                 SetTitle(title);
597             }
598         }
599
600         private void CreateDeleteButton()
601         {
602             mDeleteButton = new PushButton();
603             //mDeleteButton.BackgroundColor = Color.Red;
604             mDeleteButton.ParentOrigin = ParentOrigin.BottomRight;
605             mDeleteButton.PivotPoint = PivotPoint.BottomRight;
606             mDeleteButton.PositionUsesPivotPoint = true;
607             mDeleteButton.Position2D = new Position2D(BUTTON_BORDER, BUTTON_BORDER);
608             mDeleteButton.DrawMode = DrawModeType.Overlay2D;
609             mDeleteButton.UnselectedBackgroundVisual = CreateImageVisual(DELETE_IMAGE);
610             mDeleteButton.SelectedBackgroundVisual = CreateImageVisual(DELETE_IMAGE_SELECTED);
611             mDeleteButton.Size2D = new Size2D(50, 50);
612             mDeleteButton.LeaveRequired = true;
613             mDeleteButton.Hide();
614             mDeleteButton.Clicked += (obj, e) =>
615             {
616                 ItemIdContainer removeList = new ItemIdContainer();
617                 for (uint i = 0; i < mItemView.GetChildCount(); ++i)
618                 //for (uint i = 0; i < ITEM_COUNT; ++i)
619                 {
620                     View child = mItemView.GetChildAt(i);
621                     if(child != null)
622                     {
623                         View tick = child.FindChildByName("Tick");
624
625                         if (tick != null && tick.Visibility)
626                         {
627                             removeList.Add(mItemView.GetItemId(child));
628                         }
629                     }
630                 }
631
632                 if (removeList.Count != 0)
633                 {
634                     mItemView.RemoveItems(removeList, 0.5f);
635                 }
636                 return true;
637             };
638             Window.Instance.Add(mDeleteButton);
639         }
640
641         private void CreateInsertButton()
642         {
643             mInsertButton = new PushButton();
644             //mInsertButton.BackgroundColor = Color.Red;
645             mInsertButton.ParentOrigin = ParentOrigin.BottomRight;
646             mInsertButton.PivotPoint = PivotPoint.BottomRight;
647             mInsertButton.PositionUsesPivotPoint = true;
648             mInsertButton.Position2D = new Position2D(BUTTON_BORDER, BUTTON_BORDER);
649             mInsertButton.DrawMode = DrawModeType.Overlay2D;
650             mInsertButton.UnselectedBackgroundVisual = CreateImageVisual(INSERT_IMAGE);
651             mInsertButton.SelectedBackgroundVisual = CreateImageVisual(INSERT_IMAGE_SELECTED);
652             mInsertButton.Size2D = new Size2D(50, 50);
653             mInsertButton.LeaveRequired = true;
654             mInsertButton.Hide();
655             mInsertButton.Clicked += (obj, e) =>
656             {
657                 ItemContainer insertList = new ItemContainer();
658                 Random random = new Random();
659                 for (uint i = 0; i < mItemView.GetChildCount(); ++i)
660                 //for (uint i = 0; i < ITEM_COUNT; ++i)
661                 {
662                     View child = mItemView.GetChildAt(i);
663                     if(child != null)
664                     {
665                         View tick = child.FindChildByName("Tick");
666
667                         if (tick != null && tick.Visibility)
668                         {
669                             insertList.Add(new Item(mItemView.GetItemId(child), NewItemView((uint)random.Next(52))));
670                         }
671                     }
672                 }
673                 if (insertList.Count != 0)
674                 {
675                     mItemView.InsertItems(insertList, 0.5f);
676                 }
677                 return true;
678             };
679             Window.Instance.Add(mInsertButton);
680         }
681
682         private void CreateReplaceButton()
683         {
684             mReplaceButton = new PushButton();
685             //mReplaceButton.BackgroundColor = Color.Red;
686             mReplaceButton.ParentOrigin = ParentOrigin.BottomRight;
687             mReplaceButton.PivotPoint = PivotPoint.BottomRight;
688             mReplaceButton.PositionUsesPivotPoint = true;
689             mReplaceButton.Position2D = new Position2D(BUTTON_BORDER, BUTTON_BORDER);
690             mReplaceButton.DrawMode = DrawModeType.Overlay2D;
691             mReplaceButton.UnselectedBackgroundVisual = CreateImageVisual(REPLACE_IMAGE);
692             mReplaceButton.SelectedBackgroundVisual = CreateImageVisual(REPLACE_IMAGE_SELECTED);
693             mReplaceButton.Size2D = new Size2D(50, 50);
694             mReplaceButton.LeaveRequired = true;
695             mReplaceButton.Hide();
696             mReplaceButton.Clicked += (obj, e) =>
697             {
698                 ItemContainer replaceList = new ItemContainer();
699                 Random random = new Random();
700                 for (uint i = 0; i < mItemView.GetChildCount(); ++i)
701                 //for (uint i = 0; i < ITEM_COUNT; ++i)
702                 {
703                     View child = mItemView.GetChildAt(i);
704                     if(child != null)
705                     {
706                         View tick = child.FindChildByName("Tick");
707
708                         if (tick != null && tick.Visibility)
709                         {
710                             replaceList.Add(new Item(mItemView.GetItemId(child), NewItemView((uint)random.Next(52))));
711                         }
712                     }
713                 }
714
715                 if (replaceList.Count != 0)
716                 {
717                     mItemView.ReplaceItems(replaceList, 0.5f);
718                 }
719                 return true;
720             };
721             Window.Instance.Add(mReplaceButton);
722         }
723
724         void SwitchToNextMode()
725         {
726             switch (mMode)
727             {
728                 case Mode.MODE_REMOVE:
729                     {
730                         ExitRemoveMode();
731                         mMode = Mode.MODE_REMOVE_MANY;
732                         EnterRemoveManyMode();
733                         break;
734                     }
735
736                 case Mode.MODE_REMOVE_MANY:
737                     {
738                         ExitRemoveManyMode();
739                         mMode = Mode.MODE_INSERT;
740                         EnterInsertMode();
741                         break;
742                     }
743
744                 case Mode.MODE_INSERT:
745                     {
746                         ExitInsertMode();
747                         mMode = Mode.MODE_INSERT_MANY;
748                         EnterInsertManyMode();
749                         break;
750                     }
751
752                 case Mode.MODE_INSERT_MANY:
753                     {
754                         ExitInsertManyMode();
755                         mMode = Mode.MODE_REPLACE;
756                         EnterReplaceMode();
757                         break;
758                     }
759
760                 case Mode.MODE_REPLACE:
761                     {
762                         ExitReplaceMode();
763                         mMode = Mode.MODE_REPLACE_MANY;
764                         EnterReplaceManyMode();
765                         break;
766                     }
767
768                 case Mode.MODE_REPLACE_MANY:
769                     {
770                         ExitReplaceManyMode();
771                         mMode = Mode.MODE_NORMAL;
772                         SetLayoutTitle();
773                         break;
774                     }
775
776                 case Mode.MODE_NORMAL:
777                 default:
778                     {
779                         mMode = Mode.MODE_REMOVE;
780                         EnterRemoveMode();
781                         break;
782                     }
783             }
784         }
785
786         void EnterRemoveMode()
787         {
788             SetTitle("Edit: Remove");
789             mTapDetector = new TapGestureDetector();
790
791             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
792             //for (uint i = 0; i < ITEM_COUNT; ++i)
793             {
794                 if(mItemView.GetChildAt(i) != null)
795                 {
796                     mTapDetector.Attach(mItemView.GetChildAt(i));
797                 }
798             }
799
800             mTapDetector.Detected += (obj, e) =>
801             {
802                 View item = e.View;
803                 if(item != null)
804                 {
805                     mItemView.RemoveItem(mItemView.GetItemId(item), 0.5f);
806                 }
807             };
808         }
809
810         void ExitRemoveMode()
811         {
812             mTapDetector.DetachAll();
813             mTapDetector.Reset();
814         }
815
816         void EnterRemoveManyMode()
817         {
818             SetTitle("Edit: Remove Many");
819             mDeleteButton.Show();
820
821             mTapDetector = new TapGestureDetector();
822
823             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
824             //for (uint i = 0; i < ITEM_COUNT; ++i)
825             {
826                 View child = mItemView.GetChildAt(i);
827                 if(child != null)
828                 {
829                     View box = child.FindChildByName("CheckBox");
830                     if (box)
831                     {
832                         mTapDetector.Attach(child);
833                         box.Show();
834                     }
835                 }
836             }
837
838             mTapDetector.Detected += (obj, e) =>
839             {
840                 View view = e.View;
841                 if(view != null)
842                 {
843                     Console.WriteLine("haha");
844                 }
845                 else
846                 {
847                     Console.WriteLine("hehe");
848                 }
849                 View tick = view.FindChildByName("Tick");
850                 if (tick)
851                 {
852                     if(tick.Visibility)
853                     {
854                         tick.Hide();
855                     }
856                     else
857                     {
858                         tick.Show();
859                     }
860                 }
861             };
862         }
863
864         void ExitRemoveManyMode()
865         {
866             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
867             //for (uint i = 0; i < ITEM_COUNT; ++i)
868             {
869                 View child = mItemView.GetChildAt(i);
870                 if(child != null)
871                 {
872                     View box = child.FindChildByName("CheckBox");
873
874                     if (box)
875                     {
876                         box.Hide();
877
878                         View tick = box.FindChildByName("Tick");
879                         if (tick)
880                         {
881                             tick.Hide();
882                         }
883                     }
884                 }
885             }
886             mTapDetector.Reset();
887             mDeleteButton.Hide();
888         }
889
890         void EnterInsertMode()
891         {
892             SetTitle("Edit: Insert");
893             mTapDetector = new TapGestureDetector();
894
895             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
896             //for (uint i = 0; i < ITEM_COUNT; ++i)
897             {
898                 if(mItemView.GetChildAt(i) != null)
899                 {
900                     mTapDetector.Attach(mItemView.GetChildAt(i));
901                 }
902                 
903             }
904             mTapDetector.Detected += (obj, e) =>
905             {
906                 if(e.View != null)
907                 {
908                     uint id = mItemView.GetItemId(e.View);
909                     Random random = new Random();
910
911                     View newActor = NewItemView((uint)random.Next(52));
912
913                     mItemView.InsertItem(new Item(id, newActor), 0.5f);
914                 }
915                 else
916                 {
917                     Tizen.Log.Fatal("NUI", "e.View is null when EnterInsertMode!");
918                 }
919             };
920         }
921
922         void ExitInsertMode()
923         {
924             mTapDetector.DetachAll();
925             mTapDetector.Reset();
926         }
927         void EnterInsertManyMode()
928         {
929             SetTitle("Edit: Insert Many");
930             mInsertButton.Show();
931
932             mTapDetector = new TapGestureDetector();
933
934             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
935             //for (uint i = 0; i < ITEM_COUNT; ++i)
936             {
937                 View child = mItemView.GetChildAt(i);
938                 if(child != null)
939                 {
940                     View box = child.FindChildByName("CheckBox");
941
942                     if (box)
943                     {
944                         mTapDetector.Attach(child);
945                         box.Show();
946                     }
947                 }
948             }
949             mTapDetector.Detected += (obj, e) =>
950             {
951                 View tick = (e.View).FindChildByName("Tick");
952                 if (tick)
953                 {
954                     if (tick.Visibility)
955                     {
956                         tick.Hide();
957                     }
958                     else
959                     {
960                         tick.Show();
961                     }
962                 }
963             };
964         }
965
966         void ExitInsertManyMode()
967         {
968             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
969             //for (uint i = 0; i < ITEM_COUNT; ++i)
970             {
971                 View child = mItemView.GetChildAt(i);
972                 if(child != null)
973                 {
974                     View box = child.FindChildByName("CheckBox");
975
976                     if (box)
977                     {
978                         box.Hide();
979
980                         View tick = box.FindChildByName("Tick");
981                         if (tick)
982                         {
983                             tick.Hide();
984                         }
985                     }
986                 }
987             }
988             mTapDetector.Reset();
989             mInsertButton.Hide();
990         }
991
992         void EnterReplaceMode()
993         {
994             SetTitle("Edit: Replace");
995             mTapDetector = new TapGestureDetector();
996
997             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
998             //for (uint i = 0; i < ITEM_COUNT; ++i)
999             {
1000                 if(mItemView.GetChildAt(i) != null)
1001                 {
1002                     mTapDetector.Attach(mItemView.GetChildAt(i));
1003                 } 
1004             }
1005
1006             mTapDetector.Detected += (obj, e) =>
1007             {
1008                 Random random = new Random();
1009                 mItemView.ReplaceItem(new Item(mItemView.GetItemId(e.View), NewItemView((uint)random.Next(52))), 0.5f);
1010             };
1011         }
1012
1013         void ExitReplaceMode()
1014         {
1015             mTapDetector.DetachAll();
1016             mTapDetector.Reset();
1017         }
1018
1019
1020         void EnterReplaceManyMode()
1021         {
1022             SetTitle("Edit: Replace Many");
1023             mReplaceButton.Show();
1024
1025             mTapDetector = new TapGestureDetector();
1026
1027             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
1028             //for (uint i = 0; i < ITEM_COUNT; ++i)
1029             {
1030                 View child = mItemView.GetChildAt(i);
1031                 View box = child.FindChildByName("CheckBox");
1032
1033                 if (box)
1034                 {
1035                     mTapDetector.Attach(child);
1036                     box.Show();
1037                 }
1038             }
1039
1040             mTapDetector.Detected += (obj, e) =>
1041             {
1042                 View tick = (e.View).FindChildByName("Tick");
1043                 if (tick)
1044                 {
1045                     if (tick.Visibility)
1046                     {
1047                         tick.Hide();
1048                     }
1049                     else
1050                     {
1051                         tick.Show();
1052                     }
1053                 }
1054             };
1055         }
1056         void ExitReplaceManyMode()
1057         {
1058             for (uint i = 0; i < mItemView.GetChildCount(); ++i)
1059             //for (uint i = 0; i < ITEM_COUNT; ++i)
1060             {
1061                 View child = mItemView.GetChildAt(i);
1062                 View box = child.FindChildByName("CheckBox");
1063
1064                 if (box)
1065                 {
1066                     box.Hide();
1067
1068                     View tick = box.FindChildByName("Tick");
1069                     if (tick)
1070                     {
1071                         tick.Hide();
1072                     }
1073                 }
1074             }
1075             mTapDetector.Reset();
1076             mReplaceButton.Hide();
1077         }
1078         
1079     }
1080 }