2 * Copyright (c) 2017 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 private Dali.Application _application;
27 private ScrollView _scrollView;
28 private ScrollBar _scrollBar;
29 private Animation _animation;
30 private TextLabel _text;
32 public Example(Dali.Application application)
34 _application = application;
35 _application.Initialized += Initialize;
38 public void Initialize(object source, NUIApplicationInitEventArgs e)
43 private void CreateScrollView()
45 Window window = Window.Instance;
46 window.BackgroundColor = Color.White;
48 // Create a scroll view
49 _scrollView = new ScrollView();
50 Size2D windowSize = window.Size;
51 _scrollView.Size = new Position(windowSize.Width, windowSize.Height, 0.0f);
52 _scrollView.ParentOrigin = NDalic.ParentOriginCenter;
53 _scrollView.PivotPoint = NDalic.AnchorPointCenter;
54 window.Add(_scrollView);
56 // Add actors to a scroll view with 3 pages
59 for (int pageRow = 0; pageRow < pageRows; pageRow++)
61 for (int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
63 View pageActor = new View();
64 pageActor.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.AllDimensions);
65 pageActor.ParentOrigin = NDalic.ParentOriginCenter;
66 pageActor.PivotPoint = NDalic.AnchorPointCenter;
67 pageActor.Position = new Position(pageColumn * windowSize.Width, pageRow * windowSize.Height, 0.0f);
69 // Add images in a 3x4 grid layout for each page
73 Position imageSize = new Position((windowSize.Width / imageColumns) - margin, (windowSize.Height / imageRows) - margin, 0.0f);
75 for (int row = 0; row < imageRows; row++)
77 for (int column = 0; column < imageColumns; column++)
79 int imageId = (row * imageColumns + column) % 2 + 1;
80 ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
81 imageView.ParentOrigin = NDalic.ParentOriginCenter;
82 imageView.PivotPoint = NDalic.AnchorPointCenter;
83 imageView.Size = imageSize;
84 imageView.Position = new Position(margin * 0.5f + (imageSize.X + margin) * column - windowSize.Width * 0.5f + imageSize.X * 0.5f,
85 margin * 0.5f + (imageSize.Y + margin) * row - windowSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f);
86 pageActor.Add(imageView);
90 _scrollView.Add(pageActor);
94 _scrollView.SetAxisAutoLock(true);
96 // Set scroll view to have 3 pages in X axis and allow page snapping,
97 // and also disable scrolling in Y axis.
98 Property.Map rulerMap = new Property.Map();
99 rulerMap.Add((int)Dali.Constants.ScrollModeType.XAxisScrollEnabled, new Property.Value(true));
100 rulerMap.Add((int)Dali.Constants.ScrollModeType.XAxisSnapToInterval, new Property.Value(windowSize.Width));
101 rulerMap.Add((int)Dali.Constants.ScrollModeType.XAxisScrollBoundary, new Property.Value(windowSize.Width * pageColumns ) );
102 rulerMap.Add((int)Dali.Constants.ScrollModeType.YAxisScrollEnabled, new Property.Value( false ) );
103 _scrollView.ScrollMode = rulerMap;
105 // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
106 _scrollBar = new ScrollBar();
107 _scrollBar.ParentOrigin = NDalic.ParentOriginBottomLeft;
108 _scrollBar.PivotPoint = NDalic.AnchorPointTopLeft;
109 _scrollBar.SetResizePolicy(ResizePolicyType.FIT_TO_CHILDREN, DimensionType.Width);
110 _scrollBar.SetResizePolicy(ResizePolicyType.FILL_TO_PARENT, DimensionType.Height);
111 _scrollBar.Orientation = new Rotation(new Radian(new Degree(270.0f)), Vector3.ZAXIS);
112 _scrollBar.SetScrollDirection(ScrollBar.Direction.Horizontal);
113 _scrollView.Add(_scrollBar);
115 // Connect to the OnRelayout signal
116 _scrollView.Relayout += OnScrollViewRelayout;
117 _scrollView.Touched += OnTouch;
118 _scrollView.WheelRolled += Onwheel;
119 _scrollView.FocusGained += OnKey;
120 _text = new TextLabel("View Touch Event Handler Test");
121 _text.PivotPoint = NDalic.AnchorPointTopLeft;
122 _text.HorizontalAlignment = "CENTER";
123 _text.PointSize = 48.0f;
125 _scrollView.Add(_text);
128 // Callback for _animation finished signal handling
129 public void AnimationFinished(object sender, EventArgs e)
131 Console.WriteLine("Customized Animation Finished Event handler");
134 private void OnKey(object source, View.KeyInputFocusGainedEventArgs e)
136 Console.WriteLine("View Keyevent EVENT callback....");
139 private bool Onwheel(object source, View.WheelEventArgs e)
141 Console.WriteLine("View Wheel EVENT callback....");
145 private bool OnTouch(object source, View.TouchEventArgs e)
147 Console.WriteLine("View TOUCH EVENT callback....");
149 // Only animate the _text label when touch down happens
150 if (e.Touch.GetState(0) == PointStateType.DOWN)
152 Console.WriteLine("Customized Window Touch event handler");
153 // Create a new _animation
159 _animation = new Animation(1.0f); // 1 second of duration
161 _animation.AnimateTo(new Property(_text, View.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));
162 _animation.AnimateTo(new Property(_text, View.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));
164 // Connect the signal callback for animaiton finished signal
165 _animation.Finished += AnimationFinished;
167 // Play the _animation
173 private void OnScrollViewRelayout(object source, View.OnRelayoutEventArgs e)
175 Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
177 // Set the correct scroll bar size after size negotiation of scroll view is done
178 _scrollBar.Size = new Position(0.0f, _scrollView.GetRelayoutSize(DimensionType.Width), 0.0f);
181 public void MainLoop()
183 _application.MainLoop();
187 /// The main entry point for the application.
190 static void Main(string[] args)
192 Example example = new Example(Application.NewApplication());