f8ea8c8f6c3228b6bdab2a2dba708ffff1c3a205
[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
36         public Example(Dali.Application application)
37         {
38             _application = application;
39             _application.Initialized += new Dali.AUIApplicationInitEventHandler(Initialize);
40         }
41
42
43         public void Initialize(object source, AUIApplicationInitEventArgs e)
44         {
45             CreateScrollView();
46         }
47
48         private void CreateScrollView()
49         {
50             Stage stage = Stage.GetCurrent();
51             stage.SetBackgroundColor(NDalic.WHITE);
52
53             // Create a scroll view
54             _scrollView = new ScrollView();
55             Vector2 stageSize = stage.GetSize();
56             _scrollView.Size = new Vector3(stageSize.x, stageSize.y, 0.0f);
57             _scrollView.ParentOrigin = NDalic.ParentOriginCenter;
58             _scrollView.AnchorPoint = NDalic.AnchorPointCenter;
59             stage.Add(_scrollView);
60
61             // Add actors to a scroll view with 3 pages
62             int pageRows = 1;
63             int pageColumns = 3;
64             for(int pageRow = 0; pageRow < pageRows; pageRow++)
65             {
66                 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
67                 {
68                     View pageActor = new View();
69                     pageActor.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
70                     pageActor.ParentOrigin = NDalic.ParentOriginCenter;
71                     pageActor.AnchorPoint = NDalic.AnchorPointCenter;
72                     pageActor.Position = new Vector3(pageColumn * stageSize.x, pageRow * stageSize.y, 0.0f);
73
74                     // Add images in a 3x4 grid layout for each page
75                     int imageRows = 4;
76                     int imageColumns = 3;
77                     float margin = 10.0f;
78                     Vector3 imageSize = new Vector3((stageSize.x / imageColumns) - margin, (stageSize.y / imageRows) - margin, 0.0f);
79
80                     for(int row = 0; row < imageRows; row++)
81                     {
82                         for(int column = 0; column < imageColumns;column++)
83                         {
84                             int imageId = (row * imageColumns + column) % 2 + 1;
85                             ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
86                             imageView.ParentOrigin = NDalic.ParentOriginCenter;
87                             imageView.AnchorPoint = NDalic.AnchorPointCenter;
88                             imageView.Size = imageSize;
89                             imageView.Position = new Vector3( margin * 0.5f + (imageSize.x + margin) * column - stageSize.x * 0.5f + imageSize.x * 0.5f,
90                                                    margin * 0.5f + (imageSize.y + margin) * row - stageSize.y * 0.5f + imageSize.y * 0.5f, 0.0f );
91                             pageActor.Add(imageView);
92                         }
93                     }
94
95                     _scrollView.Add(pageActor);
96                 }
97             }
98
99             _scrollView.SetAxisAutoLock(true);
100
101             // Set scroll view to have 3 pages in X axis and allow page snapping,
102             // and also disable scrolling in Y axis.
103             RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.width));
104             RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
105             scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.width * pageColumns, true));
106             scrollRulerY.Disable();
107             _scrollView.SetRulerX(scrollRulerX);
108             _scrollView.SetRulerY(scrollRulerY);
109
110             // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
111             _scrollBar = new ScrollBar();
112             _scrollBar.ParentOrigin = NDalic.ParentOriginBottomLeft;
113             _scrollBar.AnchorPoint = NDalic.AnchorPointTopLeft;
114             _scrollBar.SetResizePolicy(ResizePolicyType.FIT_TO_CHILDREN, DimensionType.WIDTH);
115             _scrollBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
116             _scrollBar.Orientation = new Quaternion( new Radian( new Degree( 270.0f ) ), Vector3.ZAXIS );
117             _scrollBar.SetScrollDirection(ScrollBar.Direction.Horizontal);
118             _scrollView.Add(_scrollBar);
119
120             // Connect to the OnRelayout signal
121             _scrollView.OnRelayoutEvent += new Dali.View.OnRelayoutEventHandler(OnScrollViewRelayout);
122         }
123
124         private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
125         {
126           // Set the correct scroll bar size after size negotiation of scroll view is done
127             _scrollBar.Size = new Vector3(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
128         }
129
130         public void MainLoop()
131         {
132             _application.MainLoop ();
133         }
134
135         /// <summary>
136         /// The main entry point for the application.
137         /// </summary>
138         [STAThread]
139         static void Main(string[] args)
140         {
141             Example example = new Example(Application.NewApplication());
142             example.MainLoop ();
143         }
144     }
145 }