2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Runtime.InteropServices;
22 namespace MyCSharpExample
26 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
27 delegate void CallbackDelegate(IntPtr data);
29 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
30 delegate void ActorCallbackDelegate(IntPtr data);
32 private Dali.Application _application;
33 private ScrollView _scrollView;
34 private ScrollBar _scrollBar;
36 public Example(Dali.Application application)
38 _application = application;
40 CallbackDelegate initializeCallback = new CallbackDelegate( Initialize );
41 _application.InitSignal().Connect( initializeCallback );
44 private void Initialize(IntPtr appPtr)
49 private void CreateScrollView()
51 Stage stage = Stage.GetCurrent();
52 stage.SetBackgroundColor(NDalic.WHITE);
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);
62 // Add actors to a scroll view with 3 pages
65 for(int pageRow = 0; pageRow < pageRows; pageRow++)
67 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
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);
75 // Add images in a 3x4 grid layout for each page
79 Vector3 imageSize = new Vector3((stageSize.x / imageColumns) - margin, (stageSize.y / imageRows) - margin, 0.0f);
81 for(int row = 0; row < imageRows; row++)
83 for(int column = 0; column < imageColumns;column++)
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);
96 _scrollView.Add(pageActor);
100 _scrollView.SetAxisAutoLock(true);
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);
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);
121 // Connect to the OnRelayout signal
122 ActorCallbackDelegate ScrollViewRelayoutCallback = new ActorCallbackDelegate( OnScrollViewRelayout );
123 _scrollView.OnRelayoutSignal().Connect( ScrollViewRelayoutCallback );
126 private void OnScrollViewRelayout(IntPtr data)
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);
132 public void MainLoop()
134 _application.MainLoop ();
138 /// The main entry point for the application.
141 static void Main(string[] args)
143 Example example = new Example(Application.NewApplication());