Manual merge for nui v0.2.35.
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.TizenTV / examples / scroll-view.cs
1 /*
2  * Copyright (c) 2016 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.Constants;
23
24 namespace ScrollViewTest
25 {
26   class Example : NUIApplication
27   {
28     private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
29
30     [UnmanagedFunctionPointer(CallingConvention.StdCall)]
31     delegate void CallbackDelegate(IntPtr data);
32
33     [UnmanagedFunctionPointer(CallingConvention.StdCall)]
34     delegate void ActorCallbackDelegate(IntPtr data);
35
36     private ScrollView _scrollView;
37     private ScrollBar _scrollBar;
38     private Animation _animation;
39     private TextLabel _text;
40
41     public Example() : base()
42     {
43     }
44
45     public Example(string stylesheet) : base(stylesheet)
46     {
47     }
48
49     public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
50     {
51     }
52
53     protected override void OnCreate()
54     {
55         base.OnCreate();
56         Initialize();
57     }
58
59     public void Initialize()
60     {
61       CreateScrollView();
62     }
63
64     private void CreateScrollView()
65     {
66       Stage stage = Stage.Instance;
67       stage.BackgroundColor = Color.White;
68
69       // Create a scroll view
70       _scrollView = new ScrollView();
71       Size stageSize = new Size(stage.Size.Width, stage.Size.Height, 0.0f);
72       _scrollView.Size = new Size(stageSize.Width, stageSize.Height, 0.0f);
73       _scrollView.ParentOrigin = ParentOrigin.Center;
74       _scrollView.AnchorPoint = AnchorPoint.Center;
75       stage.GetDefaultLayer().Add(_scrollView);
76
77       // Add actors to a scroll view with 3 pages
78       int pageRows = 1;
79       int pageColumns = 3;
80       for(int pageRow = 0; pageRow < pageRows; pageRow++)
81       {
82         for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
83         {
84           View pageActor = new View();
85           pageActor.WidthResizePolicy = ResizePolicyType.FillToParent;
86           pageActor.HeightResizePolicy = ResizePolicyType.FillToParent;
87           pageActor.ParentOrigin = ParentOrigin.Center;
88           pageActor.AnchorPoint = AnchorPoint.Center;
89           pageActor.Position = new Position(pageColumn * stageSize.Width, pageRow * stageSize.Height, 0.0f);
90
91           // Add images in a 3x4 grid layout for each page
92           int imageRows = 4;
93           int imageColumns = 3;
94           float margin = 10.0f;
95           Position imageSize = new Position((stageSize.Width / imageColumns) - margin, (stageSize.Height / imageRows) - margin, 0.0f);
96
97           for(int row = 0; row < imageRows; row++)
98           {
99             for(int column = 0; column < imageColumns;column++)
100             {
101               int imageId = (row * imageColumns + column) % 2 + 1;
102               ImageView imageView = new ImageView(resources+"/images/image-" + imageId + ".jpg");
103               imageView.ParentOrigin = ParentOrigin.Center;
104               imageView.AnchorPoint = AnchorPoint.Center;
105               imageView.Size = new Size(imageSize.X, imageSize.Y, imageSize.Z);
106               imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.Width * 0.5f + imageSize.X * 0.5f,
107                   margin * 0.5f + (imageSize.Y + margin) * row - stageSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f );
108               pageActor.Add(imageView);
109             }
110           }
111
112           _scrollView.Add(pageActor);
113         }
114       }
115
116       _scrollView.SetAxisAutoLock(true);
117
118       // Set scroll view to have 3 pages in X axis and allow page snapping,
119       // and also disable scrolling in Y axis.
120       RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.Width));
121       RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
122       scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.Width * pageColumns, true));
123       scrollRulerY.Disable();
124       _scrollView.SetRulerX(scrollRulerX);
125       _scrollView.SetRulerY(scrollRulerY);
126
127       // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
128       _scrollBar = new ScrollBar(ScrollBar.Direction.Horizontal);
129       _scrollBar.ParentOrigin = ParentOrigin.BottomLeft;
130       _scrollBar.AnchorPoint = AnchorPoint.TopLeft;
131       _scrollBar.WidthResizePolicy = ResizePolicyType.FitToChildren;
132       _scrollBar.HeightResizePolicy = ResizePolicyType.FillToParent;
133       _scrollBar.Orientation = new Rotation( new Radian( new Degree( 270.0f ) ), Vector3.ZAxis );
134       _scrollView.Add(_scrollBar);
135
136       // Connect to the OnRelayout signal
137       _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
138       //_scrollView.Touched += OnTouch;
139       _scrollView.WheelMoved += Onwheel;
140       _scrollView.FocusGained += OnKey;
141       _text = new TextLabel("View Touch Event Handler Test");
142       _text.ParentOrigin = ParentOrigin.Center;
143       _text.AnchorPoint = AnchorPoint.Center;
144       _text.HorizontalAlignment = HorizontalAlignment.HorizontalAlignCenter;
145             _text.PointSize = 20.0f;
146
147       _scrollView.Add(_text);
148     }
149
150     // Callback for _animation finished signal handling
151     public void AnimationFinished(object sender, EventArgs e)
152     {
153       Tizen.Log.Debug("NUI", "Customized Animation Finished Event handler");
154     }
155     private void OnKey(object source, EventArgs e)
156     {
157       Tizen.Log.Debug("NUI", "View Keyevent EVENT callback....");
158     }
159
160     private bool Onwheel(object source, View.WheelEventArgs e)
161     {
162       Tizen.Log.Debug("NUI", "View Wheel EVENT callback....");
163       return true;
164     }
165
166     private bool OnTouch(object source, View.TouchEventArgs e)
167     {
168       Tizen.Log.Debug("NUI", "View TOUCH EVENT callback....");
169
170       // Only animate the _text label when touch down happens
171       if( e.Touch.GetState(0) == PointStateType.Down )
172       {
173         Tizen.Log.Debug("NUI", "Customized Stage Touch event handler");
174         // Create a new _animation
175         if( _animation )
176         {
177           _animation.Reset();
178         }
179
180         _animation = new Animation(1); // 1 second of duration
181
182         _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 180.0f ) ), Vector3.XAxis ), 0, 500);
183         _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 0.0f ) ), Vector3.XAxis ), 500, 1000);
184
185         // Connect the signal callback for animaiton finished signal
186         _animation.Finished += AnimationFinished;
187
188         // Play the _animation
189         _animation.Play();
190       }
191       return true;
192     }
193
194     private void OnScrollViewRelayout(object source, EventArgs e)
195     {
196       Tizen.Log.Debug("NUI", "View OnRelayoutEventArgs EVENT callback....");
197
198       // Set the correct scroll bar size after size negotiation of scroll view is done
199       _scrollBar.Size = new Size(0.0f, _scrollView.GetRelayoutSize(DimensionType.Width), 0.0f);
200     }
201
202     /// <summary>
203     /// The main entry point for the application.
204     /// </summary>
205     [STAThread]
206       static void _Main(string[] args)
207       {
208         Example example = new Example();
209         example.Run(args);
210       }
211   }
212 }