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