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;
20 using Tizen.NUI.UIComponents;
21 using Tizen.NUI.BaseComponents;
24 namespace CustomControlTest
27 // A custom control for star rating (draggable to change the rating)
28 class StarRating : CustomView
30 private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
31 private FlexContainer _container;
32 private ImageView[] _images;
33 private Vector3 _gestureDisplacement;
34 private int _currentValue;
35 private int _myRating;
36 private bool _myDragEnabled;
38 // Called by DALi Builder if it finds a StarRating control in a JSON file
39 static CustomView CreateInstance()
41 return new StarRating();
44 // static constructor registers the control type (only runs once)
47 // ViewRegistry registers control type with DALi type registery
48 // also uses introspection to find any properties that need to be registered with type registry
49 CustomViewRegistry.Instance.Register(CreateInstance, typeof(StarRating) );
52 public StarRating() : base(typeof(StarRating).Name, CustomViewBehaviour.ViewBehaviourDefault)
56 public override void OnInitialize()
58 // Create a container for the star images
59 _container = new FlexContainer();
61 _container.FlexDirection = FlexContainer.FlexDirectionType.Row;
62 _container.WidthResizePolicy = ResizePolicyType.FillToParent;
63 _container.HeightResizePolicy = ResizePolicyType.FillToParent;
68 _images = new ImageView[5];
70 for(int i = 0; i < 5; i++)
72 _images[i] = new ImageView(resources+"/images/star-dim.png");
73 _container.Add( _images[i] );
76 // Update the images according to the rating (dimmed star by default)
78 UpdateStartImages(_myRating);
80 // Enable pan gesture detection
81 EnableGestureDetection(Gesture.GestureType.Pan);
82 _myDragEnabled = true; // Allow dragging by default (can be disabled)
85 // Pan gesture handling
86 public override void OnPan(PanGesture gesture)
88 // Only handle pan gesture if dragging is allowed
91 switch (gesture.State)
93 case Gesture.StateType.Started:
95 _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
99 case Gesture.StateType.Continuing:
101 // Calculate the rating according to pan desture displacement
102 _gestureDisplacement.X += gesture.Displacement.X;
103 int delta = (int)Math.Ceiling(_gestureDisplacement.X / 40.0f);
104 _currentValue = _myRating + delta;
107 if(_currentValue < 0) _currentValue = 0;
108 if(_currentValue > 5) _currentValue = 5;
110 // Update the images according to the rating
111 UpdateStartImages(_currentValue);
116 _myRating = _currentValue;
123 // Update the images according to the rating
124 private void UpdateStartImages(int rating)
126 for(int i = 0; i < rating; i++)
128 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
129 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
130 _images[i].SetImage(resources+"/images/star-highlight.png");
133 for(int i = rating; i < 5; i++)
135 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
136 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
137 _images[i].SetImage(resources+"/images/star-dim.png");
141 // Rating property of type int:
151 UpdateStartImages(_myRating);
155 // DragEnabled property of type bool:
156 public bool DragEnabled
160 return _myDragEnabled;
164 _myDragEnabled = value;
169 class Example : NUIApplication
171 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
172 delegate void CallbackDelegate();
173 private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
176 public Example() : base()
180 public Example(string stylesheet) : base(stylesheet)
184 public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
188 protected override void OnCreate()
194 public void Initialize()
196 Window window = Window.Instance;
197 window.BackgroundColor = Color.White;
199 // Create a container to layout the rows of image and rating vertically
200 FlexContainer container = new FlexContainer();
202 container.ParentOrigin = ParentOrigin.TopLeft;
203 container.PivotPoint = PivotPoint.TopLeft;
204 container.FlexDirection = (int)FlexContainer.FlexDirectionType.Column;
205 container.WidthResizePolicy = ResizePolicyType.FillToParent;
206 container.HeightResizePolicy = ResizePolicyType.FillToParent;
208 window.Add(container);
210 Random random = new Random();
212 for(int i = 0; i < 6; i++) // 6 rows in total
214 // Create a container to layout the image and rating (in each row) horizontally
215 FlexContainer imageRow = new FlexContainer();
216 imageRow.ParentOrigin = ParentOrigin.TopLeft;
217 imageRow.PivotPoint = PivotPoint.TopLeft;
218 imageRow.FlexDirection = FlexContainer.FlexDirectionType.Row;
219 imageRow.Flex = 1.0f;
220 container.Add(imageRow);
222 // Add the image view to the row
223 ImageView image = new ImageView(resources+"/images/gallery-" + i + ".jpg");
224 image.Size2D = new Size2D(120, 120);
225 image.WidthResizePolicy = ResizePolicyType.Fixed;
226 image.HeightResizePolicy = ResizePolicyType.Fixed;
227 image.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
229 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
232 // Create a rating control
233 StarRating view = new StarRating();
235 // Add the rating control to the row
236 view.ParentOrigin = ParentOrigin.Center;
237 view.PivotPoint = PivotPoint.Center;
238 view.Size2D = new Size2D(200, 40);
240 view.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
241 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
244 // Set the initial rating randomly between 1 and 5
245 view.Rating = random.Next(1, 6);
250 /// The main entry point for the application.
253 static void _Main(string[] args)
255 //System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);
257 Example example = new Example();