c4cf845ec159a59b4054ac2276e7479e950808cb
[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, NUIApplicationInitEventArgs 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.Position = new Position(0,0,0);
60       _scrollView.AnchorPoint = NDalic.AnchorPointTopLeft;
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.AnchorPoint = NDalic.AnchorPointTopLeft;
73           pageActor.Position = new Position(pageColumn * stageSize.W, pageRow * stageSize.H, 0.0f);
74
75           // Add images in a 3x4 grid layout for each page
76           int imageRows = 4;
77           int imageColumns = 3;
78           float margin = 10.0f;
79           Position imageSize = new Position((stageSize.W / imageColumns) - margin, (stageSize.H / imageRows) - margin, 0.0f);
80
81           for(int row = 0; row < imageRows; row++)
82           {
83             for(int column = 0; column < imageColumns;column++)
84             {
85               int imageId = (row * imageColumns + column) % 2 + 1;
86               ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
87               imageView.ParentOrigin = NDalic.ParentOriginCenter;
88               imageView.AnchorPoint = NDalic.AnchorPointCenter;
89               imageView.Size = imageSize;
90               imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.W * 0.5f + imageSize.X * 0.5f,
91                   margin * 0.5f + (imageSize.Y + margin) * row - stageSize.H * 0.5f + imageSize.Y * 0.5f, 0.0f );
92               pageActor.Add(imageView);
93             }
94           }
95
96           _scrollView.Add(pageActor);
97         }
98       }
99
100       _scrollView.SetAxisAutoLock(true);
101
102       // Set scroll view to have 3 pages in X axis and allow page snapping,
103       // and also disable scrolling in Y axis.
104       RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.W));
105       RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
106       scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.W * pageColumns, true));
107       scrollRulerY.Disable();
108       _scrollView.SetRulerX(scrollRulerX);
109       _scrollView.SetRulerY(scrollRulerY);
110
111       // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
112       _scrollBar = new ScrollBar();
113       _scrollBar.ParentOrigin = NDalic.ParentOriginBottomLeft;
114       _scrollBar.AnchorPoint = NDalic.AnchorPointTopLeft;
115       _scrollBar.SetResizePolicy(ResizePolicyType.FIT_TO_CHILDREN, DimensionType.WIDTH);
116       _scrollBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
117       _scrollBar.Orientation = new Rotation( new Radian( new Degree( 270.0f ) ), Vector3.ZAXIS );
118       _scrollBar.SetScrollDirection(ScrollBar.Direction.Horizontal);
119       _scrollView.Add(_scrollBar);
120
121       // Connect to the OnRelayout signal
122       _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
123       _scrollView.Touched += OnTouch;
124       _scrollView.WheelMoved += Onwheel;
125       _scrollView.KeyInputFocusGained += OnKey;
126       _text = new TextLabel("View Touch Event Handler Test");
127       _text.AnchorPoint = NDalic.AnchorPointTopLeft;
128       _text.HorizontalAlignment = "CENTER";
129       _text.PointSize = 48.0f;
130
131       _scrollView.Add(_text);
132     }
133
134     // Callback for _animation finished signal handling
135     public void AnimationFinished(object sender, EventArgs e)
136     {
137       Console.WriteLine("Customized Animation Finished Event handler");
138     }
139     private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
140     {
141       Console.WriteLine("View Keyevent EVENT callback....");
142     }
143
144     private bool Onwheel(object source, View.WheelEventArgs e)
145     {
146       Console.WriteLine("View Wheel EVENT callback....");
147       return true;
148     }
149
150     private bool OnTouch(object source, View.TouchEventArgs e)
151     {
152       Console.WriteLine("View TOUCH EVENT callback....");
153
154       // Only animate the _text label when touch down happens
155       if( e.Touch.GetState(0) == PointStateType.DOWN )
156       {
157         Console.WriteLine("Customized Stage Touch event handler");
158         // Create a new _animation
159         if( _animation )
160         {
161           _animation.Reset();
162         }
163
164         _animation = new Animation(1.0f); // 1 second of duration
165
166         _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Rotation( new Radian( new Degree( 180.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.Linear), new TimePeriod(0.0f, 0.5f));
167         _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Rotation( new Radian( new Degree( 0.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.Linear), new TimePeriod(0.5f, 0.5f));
168
169         // Connect the signal callback for animaiton finished signal
170         _animation.Finished += AnimationFinished;
171
172         // Play the _animation
173         _animation.Play();
174       }
175       return true;
176     }
177
178     private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
179     {
180       Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
181
182       // Set the correct scroll bar size after size negotiation of scroll view is done
183       _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
184     }
185
186     public void MainLoop()
187     {
188       _application.MainLoop ();
189     }
190
191     /// <summary>
192     /// The main entry point for the application.
193     /// </summary>
194     [STAThread]
195       static void Main(string[] args)
196       {
197         Example example = new Example(Application.NewApplication());
198         example.MainLoop ();
199       }
200   }
201 }