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 private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
29 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
30 delegate void CallbackDelegate(IntPtr data);
32 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
33 delegate void ActorCallbackDelegate(IntPtr data);
35 private ScrollView _scrollView;
36 private ScrollBar _scrollBar;
37 private Animation _animation;
38 private TextLabel _text;
40 public Example() : base()
44 public Example(string stylesheet) : base(stylesheet)
48 public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
52 protected override void OnCreate()
58 public void Initialize()
60 InternalSetting.DefaultParentOriginAsTopLeft = false;
64 private void CreateScrollView()
66 Stage stage = Stage.Instance;
67 stage.BackgroundColor = Color.White;
69 // Create a scroll view
70 _scrollView = new ScrollView();
71 Size stageSize = new Size(stage.Size.Width, stage.Size.Height, 0.0f);
72 _scrollView.Size = new Size(stageSize.Width, stageSize.Height, 0.0f);
73 _scrollView.ParentOrigin = ParentOrigin.Center;
74 _scrollView.AnchorPoint = AnchorPoint.Center;
75 stage.GetDefaultLayer().Add(_scrollView);
77 // Add actors to a scroll view with 3 pages
80 for(int pageRow = 0; pageRow < pageRows; pageRow++)
82 for(int pageColumn = 0; pageColumn < pageColumns; pageColumn++)
84 View pageActor = new View();
85 pageActor.WidthResizePolicy = ResizePolicyType.FillToParent;
86 pageActor.HeightResizePolicy = ResizePolicyType.FillToParent;
87 pageActor.ParentOrigin = ParentOrigin.Center;
88 pageActor.AnchorPoint = AnchorPoint.Center;
89 pageActor.Position = new Position(pageColumn * stageSize.Width, pageRow * stageSize.Height, 0.0f);
91 // Add images in a 3x4 grid layout for each page
95 Position imageSize = new Position((stageSize.Width / imageColumns) - margin, (stageSize.Height / imageRows) - margin, 0.0f);
97 for(int row = 0; row < imageRows; row++)
99 for(int column = 0; column < imageColumns;column++)
101 int imageId = (row * imageColumns + column) % 2 + 1;
102 ImageView imageView = new ImageView(resources+"/images/image-" + imageId + ".jpg");
103 imageView.ParentOrigin = ParentOrigin.Center;
104 imageView.AnchorPoint = AnchorPoint.Center;
105 imageView.Size = new Size(imageSize.X, imageSize.Y, imageSize.Z);
106 imageView.Position = new Position( margin * 0.5f + (imageSize.X + margin) * column - stageSize.Width * 0.5f + imageSize.X * 0.5f,
107 margin * 0.5f + (imageSize.Y + margin) * row - stageSize.Height * 0.5f + imageSize.Y * 0.5f, 0.0f );
108 pageActor.Add(imageView);
112 _scrollView.Add(pageActor);
116 _scrollView.SetAxisAutoLock(true);
118 // Set scroll view to have 3 pages in X axis and allow page snapping,
119 // and also disable scrolling in Y axis.
120 RulerPtr scrollRulerX = new RulerPtr(new FixedRuler(stageSize.Width));
121 RulerPtr scrollRulerY = new RulerPtr(new DefaultRuler());
122 scrollRulerX.SetDomain(new RulerDomain(0.0f, stageSize.Width * pageColumns, true));
123 scrollRulerY.Disable();
124 _scrollView.SetRulerX(scrollRulerX);
125 _scrollView.SetRulerY(scrollRulerY);
127 // Create a horizontal scroll bar in the bottom of scroll view (which is optional)
128 _scrollBar = new ScrollBar(ScrollBar.Direction.Horizontal);
129 _scrollBar.ParentOrigin = ParentOrigin.BottomLeft;
130 _scrollBar.AnchorPoint = AnchorPoint.TopLeft;
131 _scrollBar.WidthResizePolicy = ResizePolicyType.FitToChildren;
132 _scrollBar.HeightResizePolicy = ResizePolicyType.FillToParent;
133 _scrollBar.Orientation = new Rotation( new Radian( new Degree( 270.0f ) ), Vector3.ZAxis );
134 _scrollView.Add(_scrollBar);
136 // Connect to the OnRelayout signal
137 _scrollView.OnRelayoutEvent += OnScrollViewRelayout;
138 //_scrollView.Touched += OnTouch;
139 _scrollView.WheelMoved += Onwheel;
140 _scrollView.FocusGained += OnKey;
141 _text = new TextLabel("View Touch Event Handler Test");
142 _text.ParentOrigin = ParentOrigin.Center;
143 _text.AnchorPoint = AnchorPoint.Center;
144 _text.HorizontalAlignment = "CENTER";
145 _text.PointSize = 20.0f;
147 _scrollView.Add(_text);
150 // Callback for _animation finished signal handling
151 public void AnimationFinished(object sender, EventArgs e)
153 Tizen.Log.Debug("NUI", "Customized Animation Finished Event handler");
155 private void OnKey(object source, EventArgs e)
157 Tizen.Log.Debug("NUI", "View Keyevent EVENT callback....");
160 private bool Onwheel(object source, View.WheelEventArgs e)
162 Tizen.Log.Debug("NUI", "View Wheel EVENT callback....");
166 private bool OnTouch(object source, View.TouchEventArgs e)
168 Tizen.Log.Debug("NUI", "View TOUCH EVENT callback....");
170 // Only animate the _text label when touch down happens
171 if( e.Touch.GetState(0) == PointStateType.Down )
173 Tizen.Log.Debug("NUI", "Customized Stage Touch event handler");
174 // Create a new _animation
180 _animation = new Animation(1); // 1 second of duration
182 _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 180.0f ) ), Vector3.XAxis ), 0, 500);
183 _animation.AnimateTo(_text, "Orientation", new Rotation( new Radian( new Degree( 0.0f ) ), Vector3.XAxis ), 500, 1000);
185 // Connect the signal callback for animaiton finished signal
186 _animation.Finished += AnimationFinished;
188 // Play the _animation
194 private void OnScrollViewRelayout(object source, EventArgs e)
196 Tizen.Log.Debug("NUI", "View OnRelayoutEventArgs EVENT callback....");
198 // Set the correct scroll bar size after size negotiation of scroll view is done
199 _scrollBar.Size = new Size(0.0f, _scrollView.GetRelayoutSize(DimensionType.Width), 0.0f);
203 /// The main entry point for the application.
206 static void _Main(string[] args)
208 Example example = new Example();