[NUI]Add layouts and refine codes for Samples (#1595)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / ItemViewDemo / ItemViewSample.cs
index df3d620..5fc9425 100755 (executable)
@@ -1,6 +1,6 @@
 using System;
 using Tizen.NUI.BaseComponents;
-using Tizen.NUI.UIComponents;
+using Tizen.NUI.Components;
 
 namespace Tizen.NUI.Samples
 {
@@ -39,7 +39,6 @@ namespace Tizen.NUI.Samples
         private string DEPTH_LAYOUT_IMAGE = CommonResource.GetDaliResourcePath() + "ItemViewDemo/icon-item-view-layout-depth.png";
         private string DEPTH_LAYOUT_IMAGE_SELECTED = CommonResource.GetDaliResourcePath() + "ItemViewDemo/icon-item-view-layout-depth-selected.png";
 
-
         private ItemView mItemView;
         private ItemFactoryWrapper mItemFactoryWrapper;
         private PropertyMap mGridLayout;
@@ -52,15 +51,16 @@ namespace Tizen.NUI.Samples
         private LongPressGestureDetector mLongPressGestureDetector;
         private TapGestureDetector mTapDetector;
 
-        private PushButton mLayoutButton;
-        private PushButton mEditButton;
-        private PushButton mDeleteButton;
-        private PushButton mInsertButton;
-        private PushButton mReplaceButton;
+        private Button mLayoutButton;
+        private Button mEditButton;
+        private Button mDeleteButton;
+        private Button mInsertButton;
+        private Button mReplaceButton;
         private TextLabel mTitle;
-        private ImageView mToolBarBackground;
 
-        private Layer mToolBarLayer;
+        private Layer mDefaultLayer;
+        private View mRootView;
+        private View mToolBarLayer;
         private View mContentView;
 
         private int mDurationSeconds = 250;
@@ -115,22 +115,25 @@ namespace Tizen.NUI.Samples
                 {
                     case (int)AllImagesLayouts.SPIRAL_LAYOUT:
                         {
-                            mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE);
-                            mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE_SELECTED);
+                            var style = mLayoutButton.Style;
+                            style.BackgroundImage = new Selector<string>() { Normal = SPIRAL_LAYOUT_IMAGE, Selected = SPIRAL_LAYOUT_IMAGE_SELECTED };
+                            mLayoutButton.ApplyStyle(style);
                             break;
                         }
 
                     case (int)AllImagesLayouts.GRID_LAYOUT:
                         {
-                            mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(GRID_LAYOUT_IMAGE);
-                            mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(GRID_LAYOUT_IMAGE_SELECTED);
+                            var style = mLayoutButton.Style;
+                            style.BackgroundImage = new Selector<string>() { Normal = GRID_LAYOUT_IMAGE, Selected = GRID_LAYOUT_IMAGE_SELECTED };
+                            mLayoutButton.ApplyStyle(style);
                             break;
                         }
 
                     case (int)AllImagesLayouts.DEPTH_LAYOUT:
                         {
-                            mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(DEPTH_LAYOUT_IMAGE);
-                            mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(DEPTH_LAYOUT_IMAGE_SELECTED);
+                            var style = mLayoutButton.Style;
+                            style.BackgroundImage = new Selector<string>() { Normal = DEPTH_LAYOUT_IMAGE, Selected = DEPTH_LAYOUT_IMAGE_SELECTED };
+                            mLayoutButton.ApplyStyle(style);
                             break;
                         }
                     default:
@@ -165,40 +168,46 @@ namespace Tizen.NUI.Samples
 
         public void Activate()
         {
-            Window window = Window.Instance;
-            window.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
+            Window window = NUIApplication.GetDefaultWindow();
+            mDefaultLayer = window.GetDefaultLayer();
+            mDefaultLayer.Behavior = Layer.LayerBehavior.Layer3D;
             window.BackgroundColor = Color.Black;
-            CreateContentView();
-            //CreateToolBarView();
+
+            mRootView = new View()
+            {
+                Size = new Size(1920, 1080)
+            };
+            mRootView.Layout = new LinearLayout()
+            {
+                LinearOrientation = LinearLayout.Orientation.Vertical
+            };
+            mDefaultLayer.Add(mRootView);
+
             CreateToolBarLayer();
+            CreateContentView();
 
-            CreateLayoutButton();
-            CreateEditButton();
             CreateInsertButton();
             CreateReplaceButton();
             CreateDeleteButton();
 
+            CreateSpiralLayout();
             CreateGridLayout();
             CreateDepthLayout();
-            CreateSpiralLayout();
 
             mLayout = new PropertyArray();
             mLayout.PushBack(new PropertyValue(mSpiralLayout));
             mLayout.PushBack(new PropertyValue(mDepthLayout));
             mLayout.PushBack(new PropertyValue(mGridLayout));
 
-
             mItemFactoryWrapper = new ItemFactoryWrapper();
             mItemFactoryWrapper.GetNumberDelegate = GetNumberOfItems;
             mItemFactoryWrapper.NewItemDelegate = NewItemView;
 
-            mItemView = new ItemView(mItemFactoryWrapper);
-            mItemView.PositionUsesPivotPoint = true;
-            mItemView.ParentOrigin = Position.ParentOriginCenter;
-            mItemView.PivotPoint = Position.PivotPointCenter;
-            window.Add(mItemView);
-
-            window.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
+            mItemView = new ItemView(mItemFactoryWrapper)
+            { 
+                Size = new Size(800, 800, 800)
+            };
+            mContentView.Add(mItemView);
 
             mItemView.Layout = mLayout;
             mItemView.SetMinimumSwipeDistance(MIN_SWIPE_DISTANCE);
@@ -219,7 +228,7 @@ namespace Tizen.NUI.Samples
             {
                 case Gesture.StateType.Started:
                     {
-                        Size2D windowSize = Window.Instance.Size;
+                        Size windowSize = NUIApplication.GetDefaultWindow().Size;
                         ItemRange range = new ItemRange(0, 0);
                         mItemView.GetItemsRange(range);
 
@@ -244,23 +253,23 @@ namespace Tizen.NUI.Samples
 
         void SetLayout(int layoutId)
         {
-            Window window = Window.Instance;
+            Window window = NUIApplication.GetDefaultWindow();
             switch (mCurrentLayout)
             {
                 case (int)AllImagesLayouts.SPIRAL_LAYOUT:
                 case (int)AllImagesLayouts.DEPTH_LAYOUT:
                     {
-                        Window.Instance.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
+                        window.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer3D;
                         break;
                     }
                 case (int)AllImagesLayouts.GRID_LAYOUT:
                     {
-                        Window.Instance.GetDefaultLayer().Behavior = Layer.LayerBehavior.LayerUI;
+                        window.GetDefaultLayer().Behavior = Layer.LayerBehavior.LayerUI;
                         break;
                     }
             }
 
-            Size2D windowSize = Window.Instance.Size;
+            Size windowSize = window.Size;
 
             if (layoutId == (int)AllImagesLayouts.DEPTH_LAYOUT)
             {
@@ -279,8 +288,6 @@ namespace Tizen.NUI.Samples
             mItemView.ActivateLayout((uint)layoutId, new Vector3(800, 800, 800), 0.0f);
         }
 
-
-
         public uint GetNumberOfItems()
         {
             return ITEM_COUNT;
@@ -299,8 +306,6 @@ namespace Tizen.NUI.Samples
             propertyMap.Insert(ImageVisualProperty.FittingMode, new PropertyValue((int)VisualFittingModeType.FitKeepAspectRatio));
             ImageView actor = new ImageView();
             actor.Image = propertyMap;
-            actor.PositionZ = 0.0f;
-            actor.Position = new Position(1000.0f, 0, -1000.0f);
 
             // Add a border image child actor
             ImageView borderActor = new ImageView();
@@ -330,7 +335,7 @@ namespace Tizen.NUI.Samples
             checkBox.PivotPoint = PivotPoint.TopRight;
             checkBox.Size = spiralItemSize;
             checkBox.PositionZ = 0.1f;
-
+            
             PropertyMap solidColorProperty = new PropertyMap();
             solidColorProperty.Insert(Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
             solidColorProperty.Insert(ColorVisualProperty.MixColor, new PropertyValue(new Color(0.0f, 0.0f, 0.0f, 0.6f)));
@@ -361,111 +366,140 @@ namespace Tizen.NUI.Samples
 
         public void Deactivate()
         {
+            if (mEditButton != null)
+            {
+                mToolBarLayer.Remove(mEditButton);
+                mEditButton.Dispose();
+                mEditButton = null;
+            }
+
+            if (mTitle != null)
+            {
+                mToolBarLayer.Remove(mTitle);
+                mTitle.Dispose();
+                mTitle = null;
+            }
+
             if (mLayoutButton != null)
             {
-                mLayoutButton.GetParent().Remove(mLayoutButton);
+                mToolBarLayer.Remove(mLayoutButton);
                 mLayoutButton.Dispose();
+                mLayoutButton = null;
             }
 
-            if (mEditButton != null)
+            if (mToolBarLayer != null)
             {
-                mEditButton.GetParent().Remove(mEditButton);
-                mEditButton.Dispose();
+                mRootView.Remove(mToolBarLayer);
+                mToolBarLayer.Dispose();
+                mToolBarLayer = null;
             }
 
             if (mReplaceButton != null)
             {
                 mReplaceButton.GetParent().Remove(mReplaceButton);
                 mReplaceButton.Dispose();
+                mReplaceButton = null;
             }
 
             if (mInsertButton != null)
             {
                 mInsertButton.GetParent().Remove(mInsertButton);
                 mInsertButton.Dispose();
+                mInsertButton = null;
             }
 
             if (mDeleteButton != null)
             {
                 mDeleteButton.GetParent().Remove(mDeleteButton);
                 mDeleteButton.Dispose();
-            }
-
-            if (mTitle != null)
-            {
-                mTitle.GetParent().Remove(mTitle);
-                mTitle.Dispose();
-            }
-
-            if (mToolBarBackground != null)
-            {
-                mToolBarBackground.GetParent().Remove(mToolBarBackground);
-                mToolBarBackground.Dispose();
+                mDeleteButton = null;
             }
 
             if (mItemView != null)
             {
-                mItemView.GetParent().Remove(mItemView);
+                mContentView.Remove(mItemView);
                 mItemView.Dispose();
+                mItemView = null;
             }
 
             if (mContentView != null)
             {
-                Window.Instance.Remove(mContentView);
+                mRootView.Remove(mContentView);
                 mContentView.Dispose();
+                mContentView = null;
             }
 
-            if (mToolBarLayer != null)
+            if (mRootView != null)
             {
-                Window.Instance.RemoveLayer(mToolBarLayer);
-                mToolBarLayer.Dispose();
+                mDefaultLayer.Remove(mRootView);
+                mRootView.Dispose();
+                mRootView = null;
             }
 
-            Window.Instance.GetDefaultLayer().Behavior = Layer.LayerBehavior.Layer2D;
+            NUIApplication.GetDefaultWindow().GetDefaultLayer().Behavior = Layer.LayerBehavior.LayerUI;
         }
 
         public void CreateContentView()
         {
-            mContentView = new View();
-            mContentView.ParentOrigin = ParentOrigin.Center;
-            mContentView.PivotPoint = PivotPoint.Center;
-            mContentView.PositionUsesPivotPoint = true;
-            mContentView.WidthResizePolicy = ResizePolicyType.FillToParent;
-            mContentView.HeightResizePolicy = ResizePolicyType.FillToParent;
-            Window.Instance.Add(mContentView);
-
-            mContentView.LowerToBottom();
+            mContentView = new View()
+            {
+                Size = new Size(1920, 1080)
+            };
+            mContentView.Layout = new LinearLayout()
+            {
+                LinearOrientation = LinearLayout.Orientation.Horizontal,
+                LinearAlignment = LinearLayout.Alignment.Center
+            };
+            mRootView.Add(mContentView);
         }
 
 
         private void CreateToolBarLayer()
         {
-            mToolBarLayer = new Layer();
-            mToolBarLayer.Name = "TOOLBAR";
-            mToolBarLayer.SetAnchorPoint(PivotPoint.TopCenter);
-            mToolBarLayer.SetParentOrigin(ParentOrigin.TopCenter);
-            mToolBarLayer.SetResizePolicy(ResizePolicyType.FillToParent, DimensionType.Width);
-            mToolBarLayer.SetSize(0, 80);
-            Window.Instance.AddLayer(mToolBarLayer);
-
+            mToolBarLayer = new View()
+            {
+                Name = "TOOLBAR",
+                Size = new Size(1920, 80),
+                BackgroundImage = TOOLBAR_IMAGE
+            };
+            mToolBarLayer.Layout = new LinearLayout()
+            {
+                LinearOrientation = LinearLayout.Orientation.Horizontal,
+                LinearAlignment = LinearLayout.Alignment.Center
+            };
+            mRootView.Add(mToolBarLayer);
             mToolBarLayer.RaiseToTop();
 
-            mToolBarBackground = new ImageView();
-            mToolBarBackground.ParentOrigin = ParentOrigin.TopLeft;
-            mToolBarBackground.PivotPoint = PivotPoint.TopLeft;
-            mToolBarBackground.PositionUsesPivotPoint = true;
-            mToolBarBackground.WidthResizePolicy = ResizePolicyType.FillToParent;
-            mToolBarBackground.HeightResizePolicy = ResizePolicyType.FillToParent;
-            mToolBarBackground.ResourceUrl = TOOLBAR_IMAGE;
-            mToolBarLayer.Add(mToolBarBackground);
+            CreateEditButton();
+            CreateToolBarTitle();
+            CreateLayoutButton();
+        }
+
+        public void CreateEditButton()
+        {
+            mEditButton = new Button();
+            var mEditButtonStyle = new ButtonStyle
+            {
+                Text = null,
+                BackgroundColor = new Selector<Color>(),
+                BackgroundImage = new Selector<string>() { Normal = EDIT_IMAGE, Selected = EDIT_IMAGE_SELECTED }
+            };
+            mEditButton.ApplyStyle(mEditButtonStyle);
+            mEditButton.IsSelectable = true;
+            mEditButton.Size = new Size(34, 34);
+            mEditButton.LeaveRequired = true;
+            mEditButton.ClickEvent += (obj, e) =>
+            {
+                SwitchToNextMode();
+            };
+            mToolBarLayer.Add(mEditButton);
+        }
 
+        private void CreateToolBarTitle()
+        {
             mTitle = new TextLabel();
-            mTitle.ParentOrigin = ParentOrigin.TopLeft;
-            mTitle.PivotPoint = PivotPoint.TopLeft;
-            mTitle.PositionUsesPivotPoint = true;
+            mTitle.Size = new Size(1800, 80);
             mTitle.PointSize = 10.0f;
-            mTitle.WidthResizePolicy = ResizePolicyType.FillToParent;
-            mTitle.HeightResizePolicy = ResizePolicyType.FillToParent;
             mTitle.Text = APPLICATION_TITLE;
             mTitle.VerticalAlignment = VerticalAlignment.Center;
             mTitle.HorizontalAlignment = HorizontalAlignment.Center;
@@ -474,46 +508,30 @@ namespace Tizen.NUI.Samples
 
         private void CreateLayoutButton()
         {
-            mLayoutButton = new PushButton();
-            mLayoutButton.UnselectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE);
-            mLayoutButton.SelectedBackgroundVisual = CreateImageVisual(SPIRAL_LAYOUT_IMAGE_SELECTED);
-            mLayoutButton.Size2D = new Size2D(34, 34);
-            mLayoutButton.ParentOrigin = ParentOrigin.TopRight;
-            mLayoutButton.PivotPoint = PivotPoint.TopRight;
-            mLayoutButton.Position2D = new Position2D(0, 25);
-            mLayoutButton.PositionUsesPivotPoint = true;
+            mLayoutButton = new Button();
+            var mLayoutButtonStyle = new ButtonStyle
+            {
+                Text = null,
+                BackgroundImage = new Selector<string>()
+                {
+                    Normal = SPIRAL_LAYOUT_IMAGE,
+                    Selected = SPIRAL_LAYOUT_IMAGE_SELECTED
+                }
+            };
+            mLayoutButton.ApplyStyle(mLayoutButtonStyle);
+            mLayoutButton.IsSelectable = true;
+            mLayoutButton.Size = new Size(34, 34);
             mLayoutButton.LeaveRequired = true;
-            mLayoutButton.Clicked += (obj, e) =>
+            mLayoutButton.ClickEvent += (obj, e) =>
             {
                 mCurrentLayout = (mCurrentLayout + 1) % (int)mItemView.GetLayoutCount();
                 ChangeLayout();
-
                 SetLayoutTitle();
                 SetLayoutImage();
-                return true;
             };
             mToolBarLayer.Add(mLayoutButton);
         }
 
-        public void CreateEditButton()
-        {
-            mEditButton = new PushButton();
-            mEditButton.UnselectedBackgroundVisual = CreateImageVisual(EDIT_IMAGE);
-            mEditButton.SelectedBackgroundVisual = CreateImageVisual(EDIT_IMAGE_SELECTED);
-            mEditButton.Size2D = new Size2D(34, 34);
-            mEditButton.ParentOrigin = ParentOrigin.TopLeft;
-            mEditButton.PivotPoint = PivotPoint.TopLeft;
-            mEditButton.Position2D = new Position2D(5, 25);
-            mEditButton.PositionUsesPivotPoint = true;
-            mEditButton.LeaveRequired = true;
-            mEditButton.Clicked += (obj, e) =>
-            {
-                SwitchToNextMode();
-                return true;
-            };
-            mToolBarLayer.Add(mEditButton);
-        }
-
         private void SetLayoutTitle()
         {
             if (Mode.MODE_NORMAL == mMode)
@@ -539,18 +557,23 @@ namespace Tizen.NUI.Samples
 
         private void CreateDeleteButton()
         {
-            mDeleteButton = new PushButton();
+            mDeleteButton = new Button();
+            var mDeleteButtonStyle = new ButtonStyle
+            {
+                Text = null,
+                BackgroundColor = new Selector<Color>(),
+                BackgroundImage = new Selector<string>() { Normal = DELETE_IMAGE, Selected = DELETE_IMAGE_SELECTED }
+            };
+            mDeleteButton.ApplyStyle(mDeleteButtonStyle);
+            mDeleteButton.IsSelectable = true;
             mDeleteButton.ParentOrigin = ParentOrigin.BottomRight;
             mDeleteButton.PivotPoint = PivotPoint.BottomRight;
             mDeleteButton.PositionUsesPivotPoint = true;
-            mDeleteButton.Position2D = new Position2D(BUTTON_BORDER, BUTTON_BORDER);
             mDeleteButton.DrawMode = DrawModeType.Overlay2D;
-            mDeleteButton.UnselectedBackgroundVisual = CreateImageVisual(DELETE_IMAGE);
-            mDeleteButton.SelectedBackgroundVisual = CreateImageVisual(DELETE_IMAGE_SELECTED);
-            mDeleteButton.Size2D = new Size2D(50, 50);
+            mDeleteButton.Size = new Size(50, 50);
             mDeleteButton.LeaveRequired = true;
             mDeleteButton.Hide();
-            mDeleteButton.Clicked += (obj, e) =>
+            mDeleteButton.ClickEvent += (obj, e) =>
             {
                 ItemIdContainer removeList = new ItemIdContainer();
                 for (uint i = 0; i < mItemView.GetChildCount(); ++i)
@@ -571,25 +594,29 @@ namespace Tizen.NUI.Samples
                 {
                     mItemView.RemoveItems(removeList, 0.5f);
                 }
-                return true;
             };
-            Window.Instance.Add(mDeleteButton);
+            NUIApplication.GetDefaultWindow().Add(mDeleteButton);
         }
 
         private void CreateInsertButton()
         {
-            mInsertButton = new PushButton();
+            mInsertButton = new Button();
+            var mInsertButtonStyle = new ButtonStyle
+            {
+                Text = null,
+                BackgroundColor = new Selector<Color>(),
+                BackgroundImage = new Selector<string>() { Normal = INSERT_IMAGE, Selected = INSERT_IMAGE_SELECTED }
+            };
+            mInsertButton.ApplyStyle(mInsertButtonStyle);
+            mInsertButton.IsSelectable = true;
             mInsertButton.ParentOrigin = ParentOrigin.BottomRight;
             mInsertButton.PivotPoint = PivotPoint.BottomRight;
             mInsertButton.PositionUsesPivotPoint = true;
-            mInsertButton.Position2D = new Position2D(BUTTON_BORDER, BUTTON_BORDER);
             mInsertButton.DrawMode = DrawModeType.Overlay2D;
-            mInsertButton.UnselectedBackgroundVisual = CreateImageVisual(INSERT_IMAGE);
-            mInsertButton.SelectedBackgroundVisual = CreateImageVisual(INSERT_IMAGE_SELECTED);
-            mInsertButton.Size2D = new Size2D(50, 50);
+            mInsertButton.Size = new Size(50, 50);
             mInsertButton.LeaveRequired = true;
             mInsertButton.Hide();
-            mInsertButton.Clicked += (obj, e) =>
+            mInsertButton.ClickEvent += (obj, e) =>
             {
                 ItemContainer insertList = new ItemContainer();
                 Random random = new Random();
@@ -610,25 +637,29 @@ namespace Tizen.NUI.Samples
                 {
                     mItemView.InsertItems(insertList, 0.5f);
                 }
-                return true;
             };
-            Window.Instance.Add(mInsertButton);
+            NUIApplication.GetDefaultWindow().Add(mInsertButton);
         }
 
         private void CreateReplaceButton()
         {
-            mReplaceButton = new PushButton();
+            mReplaceButton = new Button();
+            var mReplaceButtonStyle = new ButtonStyle
+            {
+                Text = null,
+                BackgroundColor = new Selector<Color>(),
+                BackgroundImage = new Selector<string>() { Normal = REPLACE_IMAGE, Selected = REPLACE_IMAGE_SELECTED }
+            };
+            mReplaceButton.ApplyStyle(mReplaceButtonStyle);
+            mReplaceButton.IsSelectable = true;
             mReplaceButton.ParentOrigin = ParentOrigin.BottomRight;
             mReplaceButton.PivotPoint = PivotPoint.BottomRight;
             mReplaceButton.PositionUsesPivotPoint = true;
-            mReplaceButton.Position2D = new Position2D(BUTTON_BORDER, BUTTON_BORDER);
             mReplaceButton.DrawMode = DrawModeType.Overlay2D;
-            mReplaceButton.UnselectedBackgroundVisual = CreateImageVisual(REPLACE_IMAGE);
-            mReplaceButton.SelectedBackgroundVisual = CreateImageVisual(REPLACE_IMAGE_SELECTED);
-            mReplaceButton.Size2D = new Size2D(50, 50);
+            mReplaceButton.Size = new Size(50, 50);
             mReplaceButton.LeaveRequired = true;
             mReplaceButton.Hide();
-            mReplaceButton.Clicked += (obj, e) =>
+            mReplaceButton.ClickEvent += (obj, e) =>
             {
                 ItemContainer replaceList = new ItemContainer();
                 Random random = new Random();
@@ -650,9 +681,8 @@ namespace Tizen.NUI.Samples
                 {
                     mItemView.ReplaceItems(replaceList, 0.5f);
                 }
-                return true;
             };
-            Window.Instance.Add(mReplaceButton);
+            NUIApplication.GetDefaultWindow().Add(mReplaceButton);
         }
 
         void SwitchToNextMode()
@@ -1002,4 +1032,4 @@ namespace Tizen.NUI.Samples
         }
 
     }
-}
+}
\ No newline at end of file