Merge "Dali C# binding - Implement the pure C# classes" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / 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 Dali;
21
22 namespace MyCSharpExample
23 {
24   class Example
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 Dali.Application _application;
33     private ScrollView _scrollView;
34     private ScrollBar _scrollBar;
35     private Animation _animation;
36     private TextLabel _text;
37
38     public Example(Dali.Application application)
39     {
40       _application = application;
41       _application.Initialized += Initialize;
42     }
43
44
45     public void Initialize(object source, AUIApplicationInitEventArgs e)
46     {
47       CreateScrollView();
48     }
49
50     private void CreateScrollView()
51     {
52       Stage stage = Stage.GetCurrent();
53       stage.BackgroundColor = Color.White;
54
55       // Create a scroll view
56       _scrollView = new ScrollView();
57       Size stageSize = stage.Size;
58       _scrollView.Size = new Position(stageSize.W, stageSize.H, 0.0f);
59       _scrollView.ParentOrigin = NDalic.ParentOriginCenter;
60       _scrollView.AnchorPoint = NDalic.AnchorPointCenter;
61       stage.Add(_scrollView);
62
63       // Add actors to a scroll view with 3 pages
64       int pageRows = 1;
65       int pageColumns = 3;
66       for(int pageRow = 0; pageRow < pageRows; pageRow++)
67       {
68         for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
69         {
70           View pageActor = new View();
71           pageActor.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
72           pageActor.ParentOrigin = NDalic.ParentOriginCenter;
73           pageActor.AnchorPoint = NDalic.AnchorPointCenter;
74           pageActor.Position = new Position(pageColumn * stageSize.W, pageRow * stageSize.H, 0.0f);
75
76           // Add images in a 3x4 grid layout for each page
77           int imageRows = 4;
78           int imageColumns = 3;
79           float margin = 10.0f;
80           Position imageSize = new Position((stageSize.W / imageColumns) - margin, (stageSize.H / imageRows) - margin, 0.0f);
81
82           for(int row = 0; row < imageRows; row++)
83           {
84             for(int column = 0; column < imageColumns;column++)
85             {
86               int imageId = (row * imageColumns + column) % 2 + 1;
87               ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
88               imageView.ParentOrigin = NDalic.ParentOriginCenter;
89               imageView.AnchorPoint = NDalic.AnchorPointCenter;
90               imageView.Size = imageSize;
91               imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.W * 0.5f + imageSize.X * 0.5f,
92                   margin * 0.5f + (imageSize.Y + margin) * row - stageSize.H * 0.5f + imageSize.Y * 0.5f, 0.0f );
93               pageActor.Add(imageView);
94             }
95           }
96
97           _scrollView.Add(pageActor);
98         }
99       }
100
101       _scrollView.SetAxisAutoLock(true);
102
103       // Set scroll view to have 3 pages in X axis and allow page snapping,
104       // and also disable scrolling in Y axis.
105       RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.W));
106       RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
107       scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.W * pageColumns, true));
108       scrollRulerY.Disable();
109       _scrollView.SetRulerX(scrollRulerX);
110       _scrollView.SetRulerY(scrollRulerY);
111
112       // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
113       _scrollBar = new ScrollBar();
114       _scrollBar.ParentOrigin = NDalic.ParentOriginBottomLeft;
115       _scrollBar.AnchorPoint = NDalic.AnchorPointTopLeft;
116       _scrollBar.SetResizePolicy(ResizePolicyType.FIT_TO_CHILDREN, DimensionType.WIDTH);
117       _scrollBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
118       _scrollBar.Orientation = new Quaternion( new Radian( new Degree( 270.0f ) ), Vector3.ZAXIS );
119       _scrollBar.SetScrollDirection(ScrollBar.Direction.Horizontal);
120       _scrollView.Add(_scrollBar);
121
122       // Connect to the OnRelayout signal
123       _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
124       _scrollView.Touched += OnTouch;
125       _scrollView.WheelMoved += Onwheel;
126       _scrollView.KeyInputFocusGained += OnKey;
127       _text = new TextLabel("View Touch Event Handler Test");
128       _text.ParentOrigin = NDalic.ParentOriginCenter;
129       _text.AnchorPoint = NDalic.AnchorPointCenter;
130       _text.HorizontalAlignment = "CENTER";
131       _text.PointSize = 48.0f;
132
133       _scrollView.Add(_text);
134     }
135
136     // Callback for _animation finished signal handling
137     public void AnimationFinished(object source, Animation.FinishedEventArgs e)
138     {
139       Console.WriteLine("Customized Animation Finished Event handler");
140       Console.WriteLine("Animation finished: duration = " + e.Animation.Duration);
141     }
142     private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
143     {
144       Console.WriteLine("View Keyevent EVENT callback....");
145     }
146
147     private bool Onwheel(object source, View.WheelEventArgs e)
148     {
149       Console.WriteLine("View Wheel EVENT callback....");
150       return true;
151     }
152
153     private bool OnTouch(object source, View.TouchEventArgs e)
154     {
155       Console.WriteLine("View TOUCH EVENT callback....");
156
157       // Only animate the _text label when touch down happens
158       if( e.TouchData.GetState(0) == PointStateType.DOWN )
159       {
160         Console.WriteLine("Customized Stage Touch event handler");
161         // Create a new _animation
162         if( _animation )
163         {
164           _animation.Reset();
165         }
166
167         _animation = new Animation(1.0f); // 1 second of duration
168
169         _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Quaternion( new Radian( new Degree( 180.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.0f, 0.5f));
170         _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Quaternion( new Radian( new Degree( 0.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.5f, 0.5f));
171
172         // Connect the signal callback for animaiton finished signal
173         _animation.Finished += AnimationFinished;
174
175         // Play the _animation
176         _animation.Play();
177       }
178       return true;
179     }
180
181     private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
182     {
183       Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
184
185       // Set the correct scroll bar size after size negotiation of scroll view is done
186       _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
187     }
188
189     public void MainLoop()
190     {
191       _application.MainLoop ();
192     }
193
194     /// <summary>
195     /// The main entry point for the application.
196     /// </summary>
197     [STAThread]
198       static void Main(string[] args)
199       {
200         Example example = new Example(Application.NewApplication());
201         example.MainLoop ();
202       }
203   }
204 }