Merge "SVACE issue resolved" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-sharp / examples / scroll-view.cs
1 /*
2  * Copyright (c) 2017 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       Size2D stageSize = stage.Size;
58       _scrollView.Size = new Position(stageSize.Width, stageSize.Height, 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.Width, pageRow * stageSize.Height, 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.Width / imageColumns) - margin, (stageSize.Height/ 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.Width * 0.5f + imageSize.X * 0.5f,
92                   margin * 0.5f + (imageSize.Y + margin) * row - stageSize.Height * 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.Width));
106       RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
107       scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.Width * 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 Rotation( 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.AnchorPoint = NDalic.AnchorPointTopLeft;
129       _text.HorizontalAlignment = "CENTER";
130       _text.PointSize = 48.0f;
131
132       _scrollView.Add(_text);
133     }
134
135     // Callback for _animation finished signal handling
136     public void AnimationFinished(object sender, EventArgs e)
137     {
138       Console.WriteLine("Customized Animation Finished Event handler");
139     }
140     private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
141     {
142       Console.WriteLine("View Keyevent EVENT callback....");
143     }
144
145     private bool Onwheel(object source, View.WheelEventArgs e)
146     {
147       Console.WriteLine("View Wheel EVENT callback....");
148       return true;
149     }
150
151     private bool OnTouch(object source, View.TouchEventArgs e)
152     {
153       Console.WriteLine("View TOUCH EVENT callback....");
154
155       // Only animate the _text label when touch down happens
156       if( e.Touch.GetState(0) == PointStateType.DOWN )
157       {
158         Console.WriteLine("Customized Stage Touch event handler");
159         // Create a new _animation
160         if( _animation )
161         {
162           _animation.Reset();
163         }
164
165         _animation = new Animation(1.0f); // 1 second of duration
166
167         _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));
168         _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));
169
170         // Connect the signal callback for animaiton finished signal
171         _animation.Finished += AnimationFinished;
172
173         // Play the _animation
174         _animation.Play();
175       }
176       return true;
177     }
178
179     private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
180     {
181       Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
182
183       // Set the correct scroll bar size after size negotiation of scroll view is done
184       _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
185     }
186
187     public void MainLoop()
188     {
189       _application.MainLoop ();
190     }
191
192     /// <summary>
193     /// The main entry point for the application.
194     /// </summary>
195     [STAThread]
196       static void Main(string[] args)
197       {
198         Example example = new Example(Application.NewApplication());
199         example.MainLoop ();
200       }
201   }
202 }