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;
35 private Animation _animation;
36 private TextLabel _text;
38 public Example(Dali.Application application)
40 _application = application;
41 _application.Initialized += Initialize;
45 public void Initialize(object source, NUIApplicationInitEventArgs e)
50 private void CreateScrollView()
52 Stage stage = Stage.GetCurrent();
53 stage.BackgroundColor = Color.White;
55 // Create a scroll view
56 _scrollView = new ScrollView();
57 Size stageSize = stage.Size;
58 _scrollView.Size = new Position(stageSize.W, stageSize.H, 0.0f);
59 _scrollView.Position = new Position(0,0,0);
60 _scrollView.AnchorPoint = NDalic.AnchorPointTopLeft;
61 stage.Add(_scrollView);
63 // Add actors to a scroll view with 3 pages
66 for(int pageRow = 0; pageRow < pageRows; pageRow++)
68 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
70 View pageActor = new View();
71 pageActor.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.ALL_DIMENSIONS);
72 pageActor.AnchorPoint = NDalic.AnchorPointTopLeft;
73 pageActor.Position = new Position(pageColumn * stageSize.W, pageRow * stageSize.H, 0.0f);
75 // Add images in a 3x4 grid layout for each page
79 Position imageSize = new Position((stageSize.W / imageColumns) - margin, (stageSize.H / 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 Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.W * 0.5f + imageSize.X * 0.5f,
91 margin * 0.5f + (imageSize.Y + margin) * row - stageSize.H * 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.W));
105 RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
106 scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.W * 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 Rotation( 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 _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
123 _scrollView.Touched += OnTouch;
124 _scrollView.WheelMoved += Onwheel;
125 _scrollView.KeyInputFocusGained += OnKey;
126 _text = new TextLabel("View Touch Event Handler Test");
127 _text.AnchorPoint = NDalic.AnchorPointTopLeft;
128 _text.HorizontalAlignment = "CENTER";
129 _text.PointSize = 48.0f;
131 _scrollView.Add(_text);
134 // Callback for _animation finished signal handling
135 public void AnimationFinished(object sender, EventArgs e)
137 Console.WriteLine("Customized Animation Finished Event handler");
139 private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
141 Console.WriteLine("View Keyevent EVENT callback....");
144 private bool Onwheel(object source, View.WheelEventArgs e)
146 Console.WriteLine("View Wheel EVENT callback....");
150 private bool OnTouch(object source, View.TouchEventArgs e)
152 Console.WriteLine("View TOUCH EVENT callback....");
154 // Only animate the _text label when touch down happens
155 if( e.Touch.GetState(0) == PointStateType.DOWN )
157 Console.WriteLine("Customized Stage Touch event handler");
158 // Create a new _animation
164 _animation = new Animation(1.0f); // 1 second of duration
166 _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));
167 _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 // Connect the signal callback for animaiton finished signal
170 _animation.Finished += AnimationFinished;
172 // Play the _animation
178 private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
180 Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
182 // Set the correct scroll bar size after size negotiation of scroll view is done
183 _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
186 public void MainLoop()
188 _application.MainLoop ();
192 /// The main entry point for the application.
195 static void Main(string[] args)
197 Example example = new Example(Application.NewApplication());