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 public StarRating() : base(ViewWrapperImpl.CustomViewBehaviour.VIEW_BEHAVIOUR_DEFAULT)
38 public override void OnInitialize()
40 // Create a container for the star images
41 _container = new FlexContainer();
43 _container.ParentOrigin = NDalic.ParentOriginTopLeft;
44 _container.AnchorPoint = NDalic.AnchorPointTopLeft;
45 _container.FlexDirection = (int)FlexContainer.FlexDirectionType.ROW;
46 _container.WidthResizePolicy = "FILL_TO_PARENT";
47 _container.HeightResizePolicy = "FILL_TO_PARENT";
52 _images = new ImageView[5];
54 for(int i = 0; i < 5; i++)
56 _images[i] = new ImageView("./images/star-dim.png");
57 _container.Add( _images[i] );
60 // Update the images according to the rating (dimmed star by default)
62 UpdateStartImages(_myRating);
64 // Enable pan gesture detection
65 EnableGestureDetection(Gesture.Type.Pan);
66 _myDragEnabled = true; // Allow dragging by default (can be disabled)
69 // Pan gesture handling
70 public override void OnPan(PanGesture gesture)
72 // Only handle pan gesture if dragging is allowed
75 switch (gesture.state)
77 case Gesture.State.Started:
79 _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
83 case Gesture.State.Continuing:
85 // Calculate the rating according to pan desture displacement
86 _gestureDisplacement.x += gesture.displacement.x;
87 int delta = (int)Math.Ceiling(_gestureDisplacement.x / 40.0f);
88 _currentValue = _myRating + delta;
91 if(_currentValue < 0) _currentValue = 0;
92 if(_currentValue > 5) _currentValue = 5;
94 // Update the images according to the rating
95 UpdateStartImages(_currentValue);
100 _myRating = _currentValue;
107 // Update the images according to the rating
108 private void UpdateStartImages(int rating)
110 for(int i = 0; i < rating; i++)
112 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
113 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
114 _images[i].SetImage("./images/star-highlight.png");
117 for(int i = rating; i < 5; i++)
119 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
120 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
121 _images[i].SetImage("./images/star-dim.png");
125 // Rating property of type int:
135 UpdateStartImages(_myRating);
139 // DragEnabled property of type bool:
140 public bool DragEnabled
144 return _myDragEnabled;
148 _myDragEnabled = value;
155 private Dali.Application _application;
157 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
158 delegate void CallbackDelegate();
160 public Example(Dali.Application application)
162 _application = application;
163 _application.Initialized += Initialize;
166 public void Initialize(object source, AUIApplicationInitEventArgs e)
168 Stage stage = Stage.GetCurrent();
169 stage.SetBackgroundColor( NDalic.WHITE );
171 // Create a container to layout the rows of image and rating vertically
172 FlexContainer container = new FlexContainer();
174 container.ParentOrigin = NDalic.ParentOriginTopLeft;
175 container.AnchorPoint = NDalic.AnchorPointTopLeft;
176 container.FlexDirection = (int)FlexContainer.FlexDirectionType.COLUMN;
177 container.WidthResizePolicy = "FILL_TO_PARENT";
178 container.HeightResizePolicy = "FILL_TO_PARENT";
180 stage.Add(container);
182 Random random = new Random();
184 for(int i = 0; i < 6; i++) // 6 rows in total
186 // Create a container to layout the image and rating (in each row) horizontally
187 FlexContainer imageRow = new FlexContainer();
188 imageRow.ParentOrigin = NDalic.ParentOriginTopLeft;
189 imageRow.AnchorPoint = NDalic.AnchorPointTopLeft;
190 imageRow.FlexDirection = (int)FlexContainer.FlexDirectionType.ROW;
191 imageRow.Flex = 1.0f;
192 container.Add(imageRow);
194 // Add the image view to the row
195 ImageView image = new ImageView("./images/gallery-" + i + ".jpg");
196 image.Size = new Vector3(120.0f, 120.0f, 0.0f);
197 image.WidthResizePolicy = "FIXED";
198 image.HeightResizePolicy = "FIXED";
199 image.AlignSelf = (int)FlexContainer.Alignment.ALIGN_CENTER;
201 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
204 // Create a rating control
205 StarRating view = new StarRating();
207 // Add the rating control to the row
208 view.ParentOrigin = NDalic.ParentOriginCenter;
209 view.AnchorPoint = NDalic.AnchorPointCenter;
210 view.Size = new Vector3(200.0f, 40.0f, 0.0f);
212 view.AlignSelf = (int)FlexContainer.Alignment.ALIGN_CENTER;
213 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
216 // Set the initial rating randomly between 1 and 5
217 view.Rating = random.Next(1, 6);
221 public void MainLoop()
223 _application.MainLoop ();
227 /// The main entry point for the application.
230 static void Main(string[] args)
232 Example example = new Example(Application.NewApplication());