Merge "Dali C#: Common Interface Define related changes" into devel/master
[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         public StarRating() : base(ViewWrapperImpl.CustomViewBehaviour.VIEW_BEHAVIOUR_DEFAULT)
35         {
36         }
37
38         public override void OnInitialize()
39         {
40             // Create a container for the star images
41             _container = new FlexContainer();
42
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";
48
49             this.Add(_container);
50
51             // Create the images
52             _images = new ImageView[5];
53
54             for(int i = 0; i < 5; i++)
55             {
56                 _images[i] = new ImageView("./images/star-dim.png");
57                 _container.Add( _images[i] );
58             }
59
60             // Update the images according to the rating (dimmed star by default)
61             _myRating = 0;
62             UpdateStartImages(_myRating);
63
64             // Enable pan gesture detection
65             EnableGestureDetection(Gesture.Type.Pan);
66             _myDragEnabled = true; // Allow dragging by default (can be disabled)
67         }
68
69         // Pan gesture handling
70         public override void OnPan(PanGesture gesture)
71         {
72             // Only handle pan gesture if dragging is allowed
73             if(_myDragEnabled)
74             {
75                 switch (gesture.state)
76                 {
77                     case Gesture.State.Started:
78                     {
79                         _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
80                         _currentValue = 0;
81                         break;
82                     }
83                     case Gesture.State.Continuing:
84                     {
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;
89
90                         // Clamp the rating
91                         if(_currentValue < 0) _currentValue = 0;
92                         if(_currentValue > 5) _currentValue = 5;
93
94                         // Update the images according to the rating
95                         UpdateStartImages(_currentValue);
96                         break;
97                     }
98                     default:
99                     {
100                         _myRating = _currentValue;
101                         break;
102                     }
103                 }
104             }
105         }
106
107         // Update the images according to the rating
108         private void UpdateStartImages(int rating)
109         {
110             for(int i = 0; i < rating; i++)
111             {
112                 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
113                 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
114                 _images[i].SetImage("./images/star-highlight.png");
115             }
116
117             for(int i = rating; i < 5; i++)
118             {
119                 _images[i].WidthResizePolicy = "USE_NATURAL_SIZE";
120                 _images[i].HeightResizePolicy = "USE_NATURAL_SIZE";
121                 _images[i].SetImage("./images/star-dim.png");
122             }
123         }
124
125         // Rating property of type int:
126         public int Rating
127         {
128             get
129             {
130                 return _myRating;
131             }
132             set
133             {
134                 _myRating = value;
135                 UpdateStartImages(_myRating);
136             }
137         }
138
139         // DragEnabled property of type bool:
140         public bool DragEnabled
141         {
142             get
143             {
144                 return _myDragEnabled;
145             }
146             set
147             {
148                 _myDragEnabled = value;
149             }
150         }
151     }
152
153     class Example
154     {
155         private Dali.Application _application;
156
157         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
158         delegate void CallbackDelegate();
159
160         public Example(Dali.Application application)
161         {
162             _application = application;
163             _application.Initialized += Initialize;
164         }
165
166         public void Initialize(object source, NUIApplicationInitEventArgs e)
167         {
168             Stage stage = Stage.GetCurrent();
169             stage.BackgroundColor = Color.White;
170
171             // Create a container to layout the rows of image and rating vertically
172             FlexContainer container = new FlexContainer();
173
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";
179
180             stage.Add(container);
181
182             Random random = new Random();
183
184             for(int i = 0; i < 6; i++) // 6 rows in total
185             {
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);
193
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;
200                 image.Flex = 0.3f;
201                 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
202                 imageRow.Add(image);
203
204                 // Create a rating control
205                 StarRating view = new StarRating();
206
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);
211                 view.Flex = 0.7f;
212                 view.AlignSelf = (int)FlexContainer.Alignment.ALIGN_CENTER;
213                 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
214                 imageRow.Add(view);
215
216                 // Set the initial rating randomly between 1 and 5
217                 view.Rating = random.Next(1, 6);
218             }
219         }
220
221         public void MainLoop()
222         {
223             _application.MainLoop ();
224         }
225
226         /// <summary>
227         /// The main entry point for the application.
228         /// </summary>
229         [STAThread]
230         static void Main(string[] args)
231         {
232             Example example = new Example(Application.NewApplication());
233             example.MainLoop ();
234         }
235     }
236 }