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 ScrollViewTest
24 class Example : NUIApplication
26 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
27 delegate void CallbackDelegate(IntPtr data);
29 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
30 delegate void ActorCallbackDelegate(IntPtr data);
32 private ScrollView _scrollView;
33 private ScrollBar _scrollBar;
34 private Animation _animation;
35 private TextLabel _text;
37 public Example() : base()
41 public Example(string stylesheet) : base(stylesheet)
45 public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
49 protected override void OnCreate()
55 public void Initialize()
60 private void CreateScrollView()
62 Stage stage = Stage.Instance;
63 stage.BackgroundColor = Color.White;
65 // Create a scroll view
66 _scrollView = new ScrollView();
67 Size stageSize = new Size(stage.Size.Width, stage.Size.Height, 0.0f);
68 _scrollView.Size = new Size(stageSize.Width, stageSize.Height, 0.0f);
69 _scrollView.ParentOrigin = ParentOrigin.Center;
70 _scrollView.AnchorPoint = AnchorPoint.Center;
71 stage.GetDefaultLayer().Add(_scrollView);
73 // Add actors to a scroll view with 3 pages
76 for(int pageRow = 0; pageRow < pageRows; pageRow++)
78 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
80 View pageActor = new View();
81 pageActor.WidthResizePolicy = ResizePolicyType.FillToParent;
82 pageActor.HeightResizePolicy = ResizePolicyType.FillToParent;
83 pageActor.ParentOrigin = ParentOrigin.Center;
84 pageActor.AnchorPoint = AnchorPoint.Center;
85 pageActor.Position = new Position(pageColumn * stageSize.Width, pageRow * stageSize.Height, 0.0f);
87 // Add images in a 3x4 grid layout for each page
91 Position imageSize = new Position((stageSize.Width / imageColumns) - margin, (stageSize.Height / imageRows) - margin, 0.0f);
93 for(int row = 0; row < imageRows; row++)
95 for(int column = 0; column < imageColumns;column++)
97 int imageId = (row * imageColumns + column) % 2 + 1;
98 ImageView imageView = new ImageView("images/image-" + imageId + ".jpg");
99 imageView.ParentOrigin = ParentOrigin.Center;
100 imageView.AnchorPoint = AnchorPoint.Center;
101 imageView.Size = new Size(imageSize.X, imageSize.Y, imageSize.Z);
102 imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.Width * 0.5f + imageSize.X * 0.5f,
103 margin * 0.5f + (imageSize.Y + margin) * row - stageSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f );
104 pageActor.Add(imageView);
108 _scrollView.Add(pageActor);
112 _scrollView.SetAxisAutoLock(true);
114 // Set scroll view to have 3 pages in X axis and allow page snapping,
115 // and also disable scrolling in Y axis.
116 RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.Width));
117 RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
118 scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.Width * pageColumns, true));
119 scrollRulerY.Disable();
120 _scrollView.SetRulerX(scrollRulerX);
121 _scrollView.SetRulerY(scrollRulerY);
123 // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
124 _scrollBar = new ScrollBar(ScrollBar.Direction.Horizontal);
125 _scrollBar.ParentOrigin = ParentOrigin.BottomLeft;
126 _scrollBar.AnchorPoint = AnchorPoint.TopLeft;
127 _scrollBar.WidthResizePolicy = ResizePolicyType.FitToChildren;
128 _scrollBar.HeightResizePolicy = ResizePolicyType.FillToParent;
129 _scrollBar.Orientation = new Rotation( new Radian( new Degree( 270.0f ) ), Vector3.ZAxis );
130 _scrollView.Add(_scrollBar);
132 // Connect to the OnRelayout signal
133 _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
134 _scrollView.Touched += OnTouch;
135 _scrollView.WheelMoved += Onwheel;
136 _scrollView.FocusGained += OnKey;
137 _text = new TextLabel("View Touch Event Handler Test");
138 _text.ParentOrigin = ParentOrigin.Center;
139 _text.AnchorPoint = AnchorPoint.Center;
140 _text.HorizontalAlignment = "CENTER";
141 _text.PointSize = 48.0f;
143 _scrollView.Add(_text);
146 // Callback for _animation finished signal handling
147 public void AnimationFinished(object sender, EventArgs e)
149 Console.WriteLine("Customized Animation Finished Event handler");
151 private void OnKey(object source, EventArgs e)
153 Console.WriteLine("View Keyevent EVENT callback....");
156 private bool Onwheel(object source, View.WheelEventArgs e)
158 Console.WriteLine("View Wheel EVENT callback....");
162 private bool OnTouch(object source, View.TouchEventArgs e)
164 Console.WriteLine("View TOUCH EVENT callback....");
166 // Only animate the _text label when touch down happens
167 if( e.Touch.GetState(0) == PointStateType.Down )
169 Console.WriteLine("Customized Stage Touch event handler");
170 // Create a new _animation
176 _animation = new Animation(1); // 1 second of duration
178 _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 180.0f ) ), Vector3.XAxis ), 0, 500);
179 _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 0.0f ) ), Vector3.XAxis ), 500, 1000);
181 // Connect the signal callback for animaiton finished signal
182 _animation.Finished += AnimationFinished;
184 // Play the _animation
190 private void OnScrollViewRelayout(object source, EventArgs e)
192 Console.WriteLine("View OnRelayoutEventArgs EVENT callback....");
194 // Set the correct scroll bar size after size negotiation of scroll view is done
195 _scrollBar.Size = new Size(0.0f, _scrollView.GetRelayoutSize(DimensionType.Width), 0.0f);
199 /// The main entry point for the application.
202 static void _Main(string[] args)
204 Example example = new Example();