Fix control properties for C# custom view
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / custom-control.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using Dali;
21
22 namespace MyCSharpExample
23 {
24     // A custom control for star rating (draggable to change the rating)
25     class StarRating : CustomView
26     {
27         private FlexContainer _container;
28         private ImageView[] _images;
29         private Vector3 _gestureDisplacement;
30         private int _currentValue;
31         private int _myRating;
32         private bool _myDragEnabled;
33
34         // Called by DALi Builder if it finds a StarRating control in a JSON file
35         static CustomView CreateInstance()
36         {
37           return new StarRating();
38         }
39
40         // static constructor registers the control type (only runs once)
41         static StarRating()
42         {
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) );
46         }
47
48         public StarRating() : base(typeof(StarRating).Name, ViewWrapperImpl.CustomViewBehaviour.VIEW_BEHAVIOUR_DEFAULT)
49         {
50         }
51
52         public override void OnInitialize()
53         {
54             // Create a container for the star images
55             _container = new FlexContainer();
56
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";
62
63             this.Add(_container);
64
65             // Create the images
66             _images = new ImageView[5];
67
68             for(int i = 0; i < 5; i++)
69             {
70                 _images[i] = new ImageView("./images/star-dim.png");
71                 _container.Add( _images[i] );
72             }
73
74             // Update the images according to the rating (dimmed star by default)
75             _myRating = 0;
76             UpdateStartImages(_myRating);
77
78             // Enable pan gesture detection
79             EnableGestureDetection(Gesture.GestureType.Pan);
80             _myDragEnabled = true; // Allow dragging by default (can be disabled)
81         }
82
83         // Pan gesture handling
84         public override void OnPan(PanGesture gesture)
85         {
86             // Only handle pan gesture if dragging is allowed
87             if(_myDragEnabled)
88             {
89                 switch (gesture.State)
90                 {
91                     case Gesture.StateType.Started:
92                     {
93                         _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
94                         _currentValue = 0;
95                         break;
96                     }
97                     case Gesture.StateType.Continuing:
98                     {
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;
103
104                         // Clamp the rating
105                         if(_currentValue < 0) _currentValue = 0;
106                         if(_currentValue > 5) _currentValue = 5;
107
108                         // Update the images according to the rating
109                         UpdateStartImages(_currentValue);
110                         break;
111                     }
112                     default:
113                     {
114                         _myRating = _currentValue;
115                         break;
116                     }
117                 }
118             }
119         }
120
121         // Update the images according to the rating
122         private void UpdateStartImages(int rating)
123         {
124             for(int i = 0; i < rating; i++)
125             {
126                 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
127                 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
128                 _images[i].SetImage("./images/star-highlight.png");
129             }
130
131             for(int i = rating; i < 5; i++)
132             {
133                 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
134                 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
135                 _images[i].SetImage("./images/star-dim.png");
136             }
137         }
138
139         // Rating property of type int:
140         public int Rating
141         {
142             get
143             {
144                 return _myRating;
145             }
146             set
147             {
148                 _myRating = value;
149                 UpdateStartImages(_myRating);
150             }
151         }
152
153         // DragEnabled property of type bool:
154         public bool DragEnabled
155         {
156             get
157             {
158                 return _myDragEnabled;
159             }
160             set
161             {
162                 _myDragEnabled = value;
163             }
164         }
165     }
166
167     class Example
168     {
169         private Dali.Application _application;
170
171         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
172         delegate void CallbackDelegate();
173
174         public Example(Dali.Application application)
175         {
176             _application = application;
177             _application.Initialized += Initialize;
178         }
179
180         public void Initialize(object source, NUIApplicationInitEventArgs e)
181         {
182             Stage stage = Stage.GetCurrent();
183             stage.BackgroundColor = Color.White;
184
185             // Create a container to layout the rows of image and rating vertically
186             FlexContainer container = new FlexContainer();
187
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";
193
194             stage.Add(container);
195
196             Random random = new Random();
197
198             for(int i = 0; i < 6; i++) // 6 rows in total
199             {
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);
207
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;
214                 image.Flex = 0.3f;
215                 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
216                 imageRow.Add(image);
217
218                 // Create a rating control
219                 StarRating view = new StarRating();
220
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);
225                 view.Flex = 0.7f;
226                 view.AlignSelf = (int)FlexContainer.Alignment.ALIGN_CENTER;
227                 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
228                 imageRow.Add(view);
229
230                 // Set the initial rating randomly between 1 and 5
231                 view.Rating = random.Next(1, 6);
232             }
233         }
234
235         public void MainLoop()
236         {
237             _application.MainLoop ();
238         }
239
240         /// <summary>
241         /// The main entry point for the application.
242         /// </summary>
243         [STAThread]
244         static void Main(string[] args)
245         {
246             System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);
247
248             Example example = new Example(Application.NewApplication());
249             example.MainLoop ();
250         }
251     }
252 }