Merge "DALi C# binding" into devel/master
[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
40             CallbackDelegate initializeCallback = new CallbackDelegate( Initialize );
41             _application.InitSignal().Connect( initializeCallback );
42         }
43
44         private void Initialize(IntPtr appPtr)
45         {
46             CreateScrollView();
47         }
48
49         private void CreateScrollView()
50         {
51             Stage stage = Stage.GetCurrent();
52             stage.SetBackgroundColor(NDalic.WHITE);
53
54             // Create a scroll view
55             _scrollView = new ScrollView();
56             Vector2 stageSize = stage.GetSize();
57             _scrollView.Size = new Vector3(stageSize.x, stageSize.y, 0.0f);
58             _scrollView.ParentOrigin = NDalic.ParentOriginCenter;
59             _scrollView.AnchorPoint = NDalic.AnchorPointCenter;
60             stage.Add(_scrollView);
61
62             // Add actors to a scroll view with 3 pages
63             int pageRows = 1;
64             int pageColumns = 3;
65             for(int pageRow = 0; pageRow < pageRows; pageRow++)
66             {
67                 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
68                 {
69                     Control pageActor = new Control();
70                     pageActor.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
71                     pageActor.ParentOrigin = NDalic.ParentOriginCenter;
72                     pageActor.AnchorPoint = NDalic.AnchorPointCenter;
73                     pageActor.Position = new Vector3(pageColumn * stageSize.x, pageRow * stageSize.y, 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                     Vector3 imageSize = new Vector3((stageSize.x / imageColumns) - margin, (stageSize.y / 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 Vector3( margin * 0.5f + (imageSize.x + margin) * column - stageSize.x * 0.5f + imageSize.x * 0.5f,
91                                                    margin * 0.5f + (imageSize.y + margin) * row - stageSize.y * 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.width));
105             RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
106             scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.width * 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 Quaternion( 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             ActorCallbackDelegate ScrollViewRelayoutCallback = new ActorCallbackDelegate( OnScrollViewRelayout );
123             _scrollView.OnRelayoutSignal().Connect( ScrollViewRelayoutCallback );
124         }
125
126         private void OnScrollViewRelayout(IntPtr data)
127         {
128           // Set the correct scroll bar size after size negotiation of scroll view is done
129             _scrollBar.Size = new Vector3(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
130         }
131
132         public void MainLoop()
133         {
134             _application.MainLoop ();
135         }
136
137         /// <summary>
138         /// The main entry point for the application.
139         /// </summary>
140         [STAThread]
141         static void Main(string[] args)
142         {
143             Example example = new Example(Application.NewApplication());
144             example.MainLoop ();
145         }
146     }
147 }