Separate C# classes into Internal and Public
[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         private Dali.Application _application;
27         private ScrollView _scrollView;
28         private ScrollBar _scrollBar;
29         private Animation _animation;
30         private TextLabel _text;
31
32         public Example(Dali.Application application)
33         {
34             _application = application;
35             _application.Initialized += Initialize;
36         }
37
38         public void Initialize(object source, NUIApplicationInitEventArgs e)
39         {
40             CreateScrollView();
41         }
42
43         private void CreateScrollView()
44         {
45             Window window = Window.Instance;
46             window.BackgroundColor = Color.White;
47
48             // Create a scroll view
49             _scrollView = new ScrollView();
50             Size2D windowSize = window.Size;
51             _scrollView.Size = new Position(windowSize.Width, windowSize.Height, 0.0f);
52             _scrollView.ParentOrigin = NDalic.ParentOriginCenter;
53             _scrollView.PivotPoint = NDalic.AnchorPointCenter;
54             window.Add(_scrollView);
55
56             // Add actors to a scroll view with 3 pages
57             int pageRows = 1;
58             int pageColumns = 3;
59             for (int pageRow = 0; pageRow < pageRows; pageRow++)
60             {
61                 for (int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
62                 {
63                     View pageActor = new View();
64                     pageActor.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.AllDimensions);
65                     pageActor.ParentOrigin = NDalic.ParentOriginCenter;
66                     pageActor.PivotPoint = NDalic.AnchorPointCenter;
67                     pageActor.Position = new Position(pageColumn * windowSize.Width, pageRow * windowSize.Height, 0.0f);
68
69                     // Add images in a 3x4 grid layout for each page
70                     int imageRows = 4;
71                     int imageColumns = 3;
72                     float margin = 10.0f;
73                     Position imageSize = new Position((windowSize.Width / imageColumns) - margin, (windowSize.Height / imageRows) - margin, 0.0f);
74
75                     for (int row = 0; row < imageRows; row++)
76                     {
77                         for (int column = 0; column < imageColumns; column++)
78                         {
79                             int imageId = (row * imageColumns + column) % 2 + 1;
80                             ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
81                             imageView.ParentOrigin = NDalic.ParentOriginCenter;
82                             imageView.PivotPoint = NDalic.AnchorPointCenter;
83                             imageView.Size = imageSize;
84                             imageView.Position = new Position(margin * 0.5f + (imageSize.X + margin) * column - windowSize.Width * 0.5f + imageSize.X * 0.5f,
85                                                  margin * 0.5f + (imageSize.Y + margin) * row - windowSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f);
86                             pageActor.Add(imageView);
87                         }
88                     }
89
90                     _scrollView.Add(pageActor);
91                 }
92             }
93
94             _scrollView.SetAxisAutoLock(true);
95
96             // Set scroll view to have 3 pages in X axis and allow page snapping,
97             // and also disable scrolling in Y axis.
98             Property.Map rulerMap = new Property.Map();
99             rulerMap.Add((int)Dali.Constants.ScrollModeType.XAxisScrollEnabled, new Property.Value(true));
100             rulerMap.Add((int)Dali.Constants.ScrollModeType.XAxisSnapToInterval, new Property.Value(windowSize.Width));
101             rulerMap.Add((int)Dali.Constants.ScrollModeType.XAxisScrollBoundary, new Property.Value(windowSize.Width * pageColumns ) );
102             rulerMap.Add((int)Dali.Constants.ScrollModeType.YAxisScrollEnabled, new Property.Value( false ) );
103             _scrollView.ScrollMode = rulerMap;
104
105             // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
106             _scrollBar = new ScrollBar();
107             _scrollBar.ParentOrigin = NDalic.ParentOriginBottomLeft;
108             _scrollBar.PivotPoint = NDalic.AnchorPointTopLeft;
109             _scrollBar.SetResizePolicy(ResizePolicyType.FIT_TO_CHILDREN, DimensionType.Width);
110             _scrollBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.Height);
111             _scrollBar.Orientation = new Rotation(new Radian(new Degree(270.0f)), Vector3.ZAXIS);
112             _scrollBar.SetScrollDirection(ScrollBar.Direction.Horizontal);
113             _scrollView.Add(_scrollBar);
114
115             // Connect to the OnRelayout signal
116             _scrollView.Relayout += OnScrollViewRelayout;
117             _scrollView.Touched += OnTouch;
118             _scrollView.WheelRolled += Onwheel;
119             _scrollView.FocusGained += OnKey;
120             _text = new TextLabel("View Touch Event Handler Test");
121             _text.PivotPoint = NDalic.AnchorPointTopLeft;
122             _text.HorizontalAlignment = "CENTER";
123             _text.PointSize = 48.0f;
124
125             _scrollView.Add(_text);
126         }
127
128         // Callback for _animation finished signal handling
129         public void AnimationFinished(object sender, EventArgs e)
130         {
131             Console.WriteLine("Customized Animation Finished Event handler");
132         }
133
134         private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
135         {
136             Console.WriteLine("View Keyevent EVENT callback....");
137         }
138
139         private bool Onwheel(object source, View.WheelEventArgs e)
140         {
141             Console.WriteLine("View Wheel EVENT callback....");
142             return true;
143         }
144
145         private bool OnTouch(object source, View.TouchEventArgs e)
146         {
147             Console.WriteLine("View TOUCH EVENT callback....");
148
149             // Only animate the _text label when touch down happens
150             if (e.Touch.GetState(0) == PointStateType.DOWN)
151             {
152                 Console.WriteLine("Customized Window Touch event handler");
153                 // Create a new _animation
154                 if (_animation)
155                 {
156                     _animation.Reset();
157                 }
158
159                 _animation = new Animation(1.0f); // 1 second of duration
160
161                 _animation.AnimateTo(new Property(_text, View.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));
162                 _animation.AnimateTo(new Property(_text, View.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));
163
164                 // Connect the signal callback for animaiton finished signal
165                 _animation.Finished += AnimationFinished;
166
167                 // Play the _animation
168                 _animation.Play();
169             }
170             return true;
171         }
172
173         private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
174         {
175             Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
176
177             // Set the correct scroll bar size after size negotiation of scroll view is done
178             _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.Width), 0.0f);
179         }
180
181         public void MainLoop()
182         {
183             _application.MainLoop();
184         }
185
186         /// <summary>
187         /// The main entry point for the application.
188         /// </summary>
189         [STAThread]
190         static void Main(string[] args)
191         {
192             Example example = new Example(Application.NewApplication());
193             example.MainLoop();
194         }
195     }
196 }