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