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, AUIApplicationInitEventArgs 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.ParentOrigin = NDalic.ParentOriginCenter;
60 _scrollView.AnchorPoint = NDalic.AnchorPointCenter;
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.ParentOrigin = NDalic.ParentOriginCenter;
73 pageActor.AnchorPoint = NDalic.AnchorPointCenter;
74 pageActor.Position = new Position(pageColumn * stageSize.W, pageRow * stageSize.H, 0.0f);
76 // Add images in a 3x4 grid layout for each page
80 Position imageSize = new Position((stageSize.W / imageColumns) - margin, (stageSize.H / imageRows) - margin, 0.0f);
82 for(int row = 0; row < imageRows; row++)
84 for(int column = 0; column < imageColumns;column++)
86 int imageId = (row * imageColumns + column) % 2 + 1;
87 ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
88 imageView.ParentOrigin = NDalic.ParentOriginCenter;
89 imageView.AnchorPoint = NDalic.AnchorPointCenter;
90 imageView.Size = imageSize;
91 imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.W * 0.5f + imageSize.X * 0.5f,
92 margin * 0.5f + (imageSize.Y + margin) * row - stageSize.H * 0.5f + imageSize.Y * 0.5f, 0.0f );
93 pageActor.Add(imageView);
97 _scrollView.Add(pageActor);
101 _scrollView.SetAxisAutoLock(true);
103 // Set scroll view to have 3 pages in X axis and allow page snapping,
104 // and also disable scrolling in Y axis.
105 RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.W));
106 RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
107 scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.W * pageColumns, true));
108 scrollRulerY.Disable();
109 _scrollView.SetRulerX(scrollRulerX);
110 _scrollView.SetRulerY(scrollRulerY);
112 // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
113 _scrollBar = new ScrollBar();
114 _scrollBar.ParentOrigin = NDalic.ParentOriginBottomLeft;
115 _scrollBar.AnchorPoint = NDalic.AnchorPointTopLeft;
116 _scrollBar.SetResizePolicy(ResizePolicyType.FIT_TO_CHILDREN, DimensionType.WIDTH);
117 _scrollBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.HEIGHT);
118 _scrollBar.Orientation = new Quaternion( new Radian( new Degree( 270.0f ) ), Vector3.ZAXIS );
119 _scrollBar.SetScrollDirection(ScrollBar.Direction.Horizontal);
120 _scrollView.Add(_scrollBar);
122 // Connect to the OnRelayout signal
123 _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
124 _scrollView.Touched += OnTouch;
125 _scrollView.WheelMoved += Onwheel;
126 _scrollView.KeyInputFocusGained += OnKey;
127 _text = new TextLabel("View Touch Event Handler Test");
128 _text.ParentOrigin = NDalic.ParentOriginCenter;
129 _text.AnchorPoint = NDalic.AnchorPointCenter;
130 _text.HorizontalAlignment = "CENTER";
131 _text.PointSize = 48.0f;
133 _scrollView.Add(_text);
136 // Callback for _animation finished signal handling
137 public void AnimationFinished(object source, Animation.FinishedEventArgs e)
139 Console.WriteLine("Customized Animation Finished Event handler");
140 Console.WriteLine("Animation finished: duration = " + e.Animation.Duration);
142 private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
144 Console.WriteLine("View Keyevent EVENT callback....");
147 private bool Onwheel(object source, View.WheelEventArgs e)
149 Console.WriteLine("View Wheel EVENT callback....");
153 private bool OnTouch(object source, View.TouchEventArgs e)
155 Console.WriteLine("View TOUCH EVENT callback....");
157 // Only animate the _text label when touch down happens
158 if( e.TouchData.GetState(0) == PointStateType.DOWN )
160 Console.WriteLine("Customized Stage Touch event handler");
161 // Create a new _animation
167 _animation = new Animation(1.0f); // 1 second of duration
169 _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Quaternion( new Radian( new Degree( 180.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.0f, 0.5f));
170 _animation.AnimateTo(new Property(_text, Actor.Property.ORIENTATION), new Property.Value(new Quaternion( new Radian( new Degree( 0.0f ) ), Vector3.XAXIS )), new AlphaFunction(AlphaFunction.BuiltinFunction.LINEAR), new TimePeriod(0.5f, 0.5f));
172 // Connect the signal callback for animaiton finished signal
173 _animation.Finished += AnimationFinished;
175 // Play the _animation
181 private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
183 Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
185 // Set the correct scroll bar size after size negotiation of scroll view is done
186 _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.WIDTH), 0.0f);
189 public void MainLoop()
191 _application.MainLoop ();
195 /// The main entry point for the application.
198 static void Main(string[] args)
200 Example example = new Example(Application.NewApplication());