[Tizen] temporal API to make users use Container class
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.Tizen / examples / scroll-view.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using Tizen.NUI;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Constants;
24
25 namespace ScrollViewTest
26 {
27   class Example : NUIApplication
28   {
29     private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
30
31     [UnmanagedFunctionPointer(CallingConvention.StdCall)]
32     delegate void CallbackDelegate(IntPtr data);
33
34     [UnmanagedFunctionPointer(CallingConvention.StdCall)]
35     delegate void ActorCallbackDelegate(IntPtr data);
36
37     private ScrollView _scrollView;
38     private ScrollBar _scrollBar;
39     private Animation _animation;
40     private TextLabel _text;
41
42     public Example() : base()
43     {
44     }
45
46     public Example(string stylesheet) : base(stylesheet)
47     {
48     }
49
50     public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
51     {
52     }
53
54     protected override void OnCreate()
55     {
56         base.OnCreate();
57         Initialize();
58     }
59
60     public void Initialize()
61     {
62       CreateScrollView();
63     }
64
65     private void CreateScrollView()
66     {
67       Window window = Window.Instance;
68       window.BackgroundColor = Color.White;
69
70       // Create a scroll view
71       _scrollView = new ScrollView();
72       Size windowSize = new Size(window.Size.Width, window.Size.Height, 0.0f);
73       _scrollView.Size2D = new Size2D((int)windowSize.Width, (int)windowSize.Height);
74       _scrollView.ParentOrigin = ParentOrigin.Center;
75       _scrollView.PivotPoint = PivotPoint.Center;
76       window.Add(_scrollView);
77
78       // Add actors to a scroll view with 3 pages
79       int pageRows = 1;
80       int pageColumns = 3;
81       for(int pageRow = 0; pageRow < pageRows; pageRow++)
82       {
83         for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
84         {
85           View pageActor = new View();
86           pageActor.WidthResizePolicy = ResizePolicyType.FillToParent;
87           pageActor.HeightResizePolicy = ResizePolicyType.FillToParent;
88           pageActor.ParentOrigin = ParentOrigin.Center;
89           pageActor.PivotPoint = PivotPoint.Center;
90           pageActor.Position = new Position(pageColumn * windowSize.Width, pageRow * windowSize.Height, 0.0f);
91
92           // Add images in a 3x4 grid layout for each page
93           int imageRows = 4;
94           int imageColumns = 3;
95           float margin = 10.0f;
96           Position imageSize = new Position((windowSize.Width / imageColumns) - margin, (windowSize.Height / imageRows) - margin, 0.0f);
97
98           for(int row = 0; row < imageRows; row++)
99           {
100             for(int column = 0; column < imageColumns;column++)
101             {
102               int imageId = (row * imageColumns + column) % 2 + 1;
103               ImageView imageView = new ImageView(resources+"/images/image-" + imageId + ".jpg");
104               imageView.ParentOrigin = ParentOrigin.Center;
105               imageView.PivotPoint = PivotPoint.Center;
106               imageView.Size2D = new Size2D((int)imageSize.X, (int)imageSize.Y);
107               imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - windowSize.Width * 0.5f + imageSize.X * 0.5f,
108                   margin * 0.5f + (imageSize.Y + margin) * row - windowSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f );
109               pageActor.Add(imageView);
110             }
111           }
112
113           _scrollView.Add(pageActor);
114         }
115       }
116
117       _scrollView.SetAxisAutoLock(true);
118
119       // Set scroll view to have 3 pages in X axis and allow page snapping,
120       // and also disable scrolling in Y axis.
121        PropertyMap rulerMap = new PropertyMap();
122        rulerMap.Add((int)ScrollModeType.XAxisScrollEnabled, new PropertyValue(true));
123        rulerMap.Add((int)ScrollModeType.XAxisSnapToInterval, new PropertyValue(windowSize.Width));
124        rulerMap.Add((int)ScrollModeType.XAxisScrollBoundary, new PropertyValue(windowSize.Width * pageColumns ) );
125        rulerMap.Add((int)ScrollModeType.YAxisScrollEnabled, new PropertyValue( false ) );
126        _scrollView.ScrollMode = rulerMap;
127
128       // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
129       _scrollBar = new ScrollBar(ScrollBar.Direction.Horizontal);
130       _scrollBar.ParentOrigin = ParentOrigin.BottomLeft;
131       _scrollBar.PivotPoint = PivotPoint.TopLeft;
132       _scrollBar.WidthResizePolicy = ResizePolicyType.FitToChildren;
133       _scrollBar.HeightResizePolicy = ResizePolicyType.FillToParent;
134       _scrollBar.Orientation = new Rotation( new Radian( new Degree( 270.0f ) ), Vector3.ZAxis );
135       _scrollView.Add(_scrollBar);
136
137       // Connect to the OnRelayout signal
138       _scrollView.Relayout += OnScrollViewRelayout;
139       //_scrollView.Touched += OnTouch;
140       _scrollView.WheelEvent += Onwheel;
141       _scrollView.FocusGained += OnKey;
142       _text = new TextLabel("View Touch Event Handler Test");
143       _text.ParentOrigin = ParentOrigin.Center;
144       _text.PivotPoint = PivotPoint.Center;
145       _text.HorizontalAlignment = HorizontalAlignment.Center;
146             _text.PointSize = 20.0f;
147
148       _scrollView.Add(_text);
149     }
150
151     // Callback for _animation finished signal handling
152     public void AnimationFinished(object sender, EventArgs e)
153     {
154       Tizen.Log.Debug("NUI", "Customized Animation Finished Event handler");
155     }
156     private void OnKey(object source, EventArgs e)
157     {
158       Tizen.Log.Debug("NUI", "View Keyevent EVENT callback....");
159     }
160
161     private bool Onwheel(object source, View.WheelEventArgs e)
162     {
163       Tizen.Log.Debug("NUI", "View Wheel EVENT callback....");
164       return true;
165     }
166
167     private bool OnTouch(object source, View.TouchEventArgs e)
168     {
169       Tizen.Log.Debug("NUI", "View TOUCH EVENT callback....");
170
171       // Only animate the _text label when touch down happens
172       if( e.Touch.GetState(0) == PointStateType.Down )
173       {
174         Tizen.Log.Debug("NUI", "Customized Window Touch event handler");
175         // Create a new _animation
176         if( _animation )
177         {
178           _animation.Reset();
179         }
180
181         _animation = new Animation(1); // 1 second of duration
182
183         _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 180.0f ) ), Vector3.XAxis ), 0, 500);
184         _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 0.0f ) ), Vector3.XAxis ), 500, 1000);
185
186         // Connect the signal callback for animaiton finished signal
187         _animation.Finished += AnimationFinished;
188
189         // Play the _animation
190         _animation.Play();
191       }
192       return true;
193     }
194
195     private void OnScrollViewRelayout(object source, EventArgs e)
196     {
197       Tizen.Log.Debug("NUI", "View OnRelayoutEventArgs EVENT callback....");
198
199       // Set the correct scroll bar size after size negotiation of scroll view is done
200       _scrollBar.Size2D = new Size2D(0, (int)_scrollView.GetRelayoutSize(DimensionType.Width));
201     }
202
203     /// <summary>
204     /// The main entry point for the application.
205     /// </summary>
206     [STAThread]
207       static void _Main(string[] args)
208       {
209         Example example = new Example();
210         example.Run(args);
211       }
212   }
213 }