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 MyCSharpExample
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, ViewWrapperImpl.CustomViewBehaviour.VIEW_BEHAVIOUR_DEFAULT)
52 public override void OnInitialize()
54 // Create a container for the star images
55 _container = new FlexContainer();
57 _container.ParentOrigin = NDalic.ParentOriginTopLeft;
58 _container.AnchorPoint = NDalic.AnchorPointTopLeft;
59 _container.FlexDirection = (int)FlexContainer.FlexDirectionType.ROW;
60 _container.WidthResizePolicy = "FILL_TO_PARENT";
61 _container.HeightResizePolicy = "FILL_TO_PARENT";
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 = "USE_NATURAL_SIZE";
127 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
128 _images[i].SetImage("./images/star-highlight.png");
131 for(int i = rating; i < 5; i++)
133 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
134 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
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;
169 private Dali.Application _application;
171 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
172 delegate void CallbackDelegate();
174 public Example(Dali.Application application)
176 _application = application;
177 _application.Initialized += Initialize;
180 public void Initialize(object source, NUIApplicationInitEventArgs e)
182 Stage stage = Stage.GetCurrent();
183 stage.BackgroundColor = Color.White;
185 // Create a container to layout the rows of image and rating vertically
186 FlexContainer container = new FlexContainer();
188 container.ParentOrigin = NDalic.ParentOriginTopLeft;
189 container.AnchorPoint = NDalic.AnchorPointTopLeft;
190 container.FlexDirection = (int)FlexContainer.FlexDirectionType.COLUMN;
191 container.WidthResizePolicy = "FILL_TO_PARENT";
192 container.HeightResizePolicy = "FILL_TO_PARENT";
194 stage.Add(container);
196 Random random = new Random();
198 for(int i = 0; i < 6; i++) // 6 rows in total
200 // Create a container to layout the image and rating (in each row) horizontally
201 FlexContainer imageRow = new FlexContainer();
202 imageRow.ParentOrigin = NDalic.ParentOriginTopLeft;
203 imageRow.AnchorPoint = NDalic.AnchorPointTopLeft;
204 imageRow.FlexDirection = (int)FlexContainer.FlexDirectionType.ROW;
205 imageRow.Flex = 1.0f;
206 container.Add(imageRow);
208 // Add the image view to the row
209 ImageView image = new ImageView("./images/gallery-" + i + ".jpg");
210 image.Size = new Vector3(120.0f, 120.0f, 0.0f);
211 image.WidthResizePolicy = "FIXED";
212 image.HeightResizePolicy = "FIXED";
213 image.AlignSelf = (int)FlexContainer.Alignment.ALIGN_CENTER;
215 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
218 // Create a rating control
219 StarRating view = new StarRating();
221 // Add the rating control to the row
222 view.ParentOrigin = NDalic.ParentOriginCenter;
223 view.AnchorPoint = NDalic.AnchorPointCenter;
224 view.Size = new Vector3(200.0f, 40.0f, 0.0f);
226 view.AlignSelf = (int)FlexContainer.Alignment.ALIGN_CENTER;
227 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
230 // Set the initial rating randomly between 1 and 5
231 view.Rating = random.Next(1, 6);
235 public void MainLoop()
237 _application.MainLoop ();
241 /// The main entry point for the application.
244 static void Main(string[] args)
246 System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);
248 Example example = new Example(Application.NewApplication());