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;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Constants;
25 namespace ScrollViewTest
27 class Example : NUIApplication
29 private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
31 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
32 delegate void CallbackDelegate(IntPtr data);
34 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
35 delegate void ActorCallbackDelegate(IntPtr data);
37 private ScrollView _scrollView;
38 private ScrollBar _scrollBar;
39 private Animation _animation;
40 private TextLabel _text;
42 public Example() : base()
46 public Example(string stylesheet) : base(stylesheet)
50 public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
54 protected override void OnCreate()
60 public void Initialize()
65 private void CreateScrollView()
67 Window window = Window.Instance;
68 window.BackgroundColor = Color.White;
70 // Create a scroll view
71 _scrollView = new ScrollView();
72 Size windowSize = new Size(window.Size.Width, window.Size.Height, 0.0f);
73 _scrollView.Size2D = new Size2D((int)windowSize.Width, (int)windowSize.Height);
74 _scrollView.ParentOrigin = ParentOrigin.Center;
75 _scrollView.PivotPoint = PivotPoint.Center;
76 window.Add(_scrollView);
78 // Add actors to a scroll view with 3 pages
81 for(int pageRow = 0; pageRow < pageRows; pageRow++)
83 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
85 View pageActor = new View();
86 pageActor.WidthResizePolicy = ResizePolicyType.FillToParent;
87 pageActor.HeightResizePolicy = ResizePolicyType.FillToParent;
88 pageActor.ParentOrigin = ParentOrigin.Center;
89 pageActor.PivotPoint = PivotPoint.Center;
90 pageActor.Position = new Position(pageColumn * windowSize.Width, pageRow * windowSize.Height, 0.0f);
92 // Add images in a 3x4 grid layout for each page
96 Position imageSize = new Position((windowSize.Width / imageColumns) - margin, (windowSize.Height / imageRows) - margin, 0.0f);
98 for(int row = 0; row < imageRows; row++)
100 for(int column = 0; column < imageColumns;column++)
102 int imageId = (row * imageColumns + column) % 2 + 1;
103 ImageView imageView = new ImageView(resources+"/images/image-" + imageId + ".jpg");
104 imageView.ParentOrigin = ParentOrigin.Center;
105 imageView.PivotPoint = PivotPoint.Center;
106 imageView.Size2D = new Size2D((int)imageSize.X, (int)imageSize.Y);
107 imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - windowSize.Width * 0.5f + imageSize.X * 0.5f,
108 margin * 0.5f + (imageSize.Y + margin) * row - windowSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f );
109 pageActor.Add(imageView);
113 _scrollView.Add(pageActor);
117 _scrollView.SetAxisAutoLock(true);
119 // Set scroll view to have 3 pages in X axis and allow page snapping,
120 // and also disable scrolling in Y axis.
121 PropertyMap rulerMap = new PropertyMap();
122 rulerMap.Add((int)ScrollModeType.XAxisScrollEnabled, new PropertyValue(true));
123 rulerMap.Add((int)ScrollModeType.XAxisSnapToInterval, new PropertyValue(windowSize.Width));
124 rulerMap.Add((int)ScrollModeType.XAxisScrollBoundary, new PropertyValue(windowSize.Width * pageColumns ) );
125 rulerMap.Add((int)ScrollModeType.YAxisScrollEnabled, new PropertyValue( false ) );
126 _scrollView.ScrollMode = rulerMap;
128 // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
129 _scrollBar = new ScrollBar(ScrollBar.Direction.Horizontal);
130 _scrollBar.ParentOrigin = ParentOrigin.BottomLeft;
131 _scrollBar.PivotPoint = PivotPoint.TopLeft;
132 _scrollBar.WidthResizePolicy = ResizePolicyType.FitToChildren;
133 _scrollBar.HeightResizePolicy = ResizePolicyType.FillToParent;
134 _scrollBar.Orientation = new Rotation( new Radian( new Degree( 270.0f ) ), Vector3.ZAxis );
135 _scrollView.Add(_scrollBar);
137 // Connect to the OnRelayout signal
138 _scrollView.Relayout += OnScrollViewRelayout;
139 //_scrollView.Touched += OnTouch;
140 _scrollView.WheelEvent += Onwheel;
141 _scrollView.FocusGained += OnKey;
142 _text = new TextLabel("View Touch Event Handler Test");
143 _text.ParentOrigin = ParentOrigin.Center;
144 _text.PivotPoint = PivotPoint.Center;
145 _text.HorizontalAlignment = HorizontalAlignment.Center;
146 _text.PointSize = 20.0f;
148 _scrollView.Add(_text);
151 // Callback for _animation finished signal handling
152 public void AnimationFinished(object sender, EventArgs e)
154 Tizen.Log.Debug("NUI", "Customized Animation Finished Event handler");
156 private void OnKey(object source, EventArgs e)
158 Tizen.Log.Debug("NUI", "View Keyevent EVENT callback....");
161 private bool Onwheel(object source, View.WheelEventArgs e)
163 Tizen.Log.Debug("NUI", "View Wheel EVENT callback....");
167 private bool OnTouch(object source, View.TouchEventArgs e)
169 Tizen.Log.Debug("NUI", "View TOUCH EVENT callback....");
171 // Only animate the _text label when touch down happens
172 if( e.Touch.GetState(0) == PointStateType.Down )
174 Tizen.Log.Debug("NUI", "Customized Window Touch event handler");
175 // Create a new _animation
181 _animation = new Animation(1); // 1 second of duration
183 _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 180.0f ) ), Vector3.XAxis ), 0, 500);
184 _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 0.0f ) ), Vector3.XAxis ), 500, 1000);
186 // Connect the signal callback for animaiton finished signal
187 _animation.Finished += AnimationFinished;
189 // Play the _animation
195 private void OnScrollViewRelayout(object source, EventArgs e)
197 Tizen.Log.Debug("NUI", "View OnRelayoutEventArgs EVENT callback....");
199 // Set the correct scroll bar size after size negotiation of scroll view is done
200 _scrollBar.Size2D = new Size2D(0, (int)_scrollView.GetRelayoutSize(DimensionType.Width));
204 /// The main entry point for the application.
207 static void _Main(string[] args)
209 Example example = new Example();