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 CustomControlTest
24 // A custom control for star rating (draggable to change the rating)
25 class StarRating : CustomView
27 private FlexContainer _container;
28 private ImageView[] _images;
29 private Vector3 _gestureDisplacement;
30 private int _currentValue;
31 private int _myRating;
32 private bool _myDragEnabled;
34 // Called by DALi Builder if it finds a StarRating control in a JSON file
35 static CustomView CreateInstance()
37 return new StarRating();
40 // static constructor registers the control type (only runs once)
43 // ViewRegistry registers control type with DALi type registery
44 // also uses introspection to find any properties that need to be registered with type registry
45 ViewRegistry.Instance.Register(CreateInstance, typeof(StarRating) );
48 public StarRating() : base(typeof(StarRating).Name, CustomViewBehaviour.ViewBehaviourDefault)
52 public override void OnInitialize()
54 // Create a container for the star images
55 _container = new FlexContainer();
57 //_container.ParentOrigin = ParentOrigin.TopLeft;
58 //_container.AnchorPoint = AnchorPoint.TopLeft;
59 _container.FlexDirection = FlexContainer.FlexDirectionType.Row;
60 _container.WidthResizePolicy = ResizePolicyType.FillToParent;
61 _container.HeightResizePolicy = ResizePolicyType.FillToParent;
66 _images = new ImageView[5];
68 for(int i = 0; i < 5; i++)
70 _images[i] = new ImageView("./images/star-dim.png");
71 _container.Add( _images[i] );
74 // Update the images according to the rating (dimmed star by default)
76 UpdateStartImages(_myRating);
78 // Enable pan gesture detection
79 EnableGestureDetection(Gesture.GestureType.Pan);
80 _myDragEnabled = true; // Allow dragging by default (can be disabled)
83 // Pan gesture handling
84 public override void OnPan(PanGesture gesture)
86 // Only handle pan gesture if dragging is allowed
89 switch (gesture.State)
91 case Gesture.StateType.Started:
93 _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
97 case Gesture.StateType.Continuing:
99 // Calculate the rating according to pan desture displacement
100 _gestureDisplacement.X += gesture.Displacement.X;
101 int delta = (int)Math.Ceiling(_gestureDisplacement.X / 40.0f);
102 _currentValue = _myRating + delta;
105 if(_currentValue < 0) _currentValue = 0;
106 if(_currentValue > 5) _currentValue = 5;
108 // Update the images according to the rating
109 UpdateStartImages(_currentValue);
114 _myRating = _currentValue;
121 // Update the images according to the rating
122 private void UpdateStartImages(int rating)
124 for(int i = 0; i < rating; i++)
126 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
127 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
128 _images[i].SetImage("./images/star-highlight.png");
131 for(int i = rating; i < 5; i++)
133 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
134 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
135 _images[i].SetImage("./images/star-dim.png");
139 // Rating property of type int:
149 UpdateStartImages(_myRating);
153 // DragEnabled property of type bool:
154 public bool DragEnabled
158 return _myDragEnabled;
162 _myDragEnabled = value;
167 class Example : NUIApplication
169 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
170 delegate void CallbackDelegate();
172 public Example() : base()
176 public Example(string stylesheet) : base(stylesheet)
180 public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
184 protected override void OnCreate()
190 public void Initialize()
192 Stage stage = Stage.Instance;
193 stage.BackgroundColor = Color.White;
195 // Create a container to layout the rows of image and rating vertically
196 FlexContainer container = new FlexContainer();
198 container.ParentOrigin = ParentOrigin.TopLeft;
199 container.AnchorPoint = AnchorPoint.TopLeft;
200 container.FlexDirection = (int)FlexContainer.FlexDirectionType.Column;
201 container.WidthResizePolicy = ResizePolicyType.FillToParent;
202 container.HeightResizePolicy = ResizePolicyType.FillToParent;
204 stage.GetDefaultLayer().Add(container);
206 Random random = new Random();
208 for(int i = 0; i < 6; i++) // 6 rows in total
210 // Create a container to layout the image and rating (in each row) horizontally
211 FlexContainer imageRow = new FlexContainer();
212 imageRow.ParentOrigin = ParentOrigin.TopLeft;
213 imageRow.AnchorPoint = AnchorPoint.TopLeft;
214 imageRow.FlexDirection = FlexContainer.FlexDirectionType.Row;
215 imageRow.Flex = 1.0f;
216 container.Add(imageRow);
218 // Add the image view to the row
219 ImageView image = new ImageView("./images/gallery-" + i + ".jpg");
220 image.Size = new Vector3(120.0f, 120.0f, 0.0f);
221 image.WidthResizePolicy = ResizePolicyType.Fixed;
222 image.HeightResizePolicy = ResizePolicyType.Fixed;
223 image.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
225 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
228 // Create a rating control
229 StarRating view = new StarRating();
231 // Add the rating control to the row
232 view.ParentOrigin = ParentOrigin.Center;
233 view.AnchorPoint = AnchorPoint.Center;
234 view.Size = new Vector3(200.0f, 40.0f, 0.0f);
236 view.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
237 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
240 // Set the initial rating randomly between 1 and 5
241 view.Rating = random.Next(1, 6);
246 /// The main entry point for the application.
249 static void _Main(string[] args)
251 //System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);
253 Example example = new Example();