nui 0.2.32 manual merge, mapping to dali 1.2.32
[platform/core/csapi/tizenfx.git] / NUISamples / NUISamples / NUISamples.TizenTV / 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 Tizen.NUI;
21
22 namespace CustomControlTest
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, CustomViewBehaviour.ViewBehaviourDefault)
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 = ParentOrigin.TopLeft;
58             //_container.AnchorPoint = AnchorPoint.TopLeft;
59             _container.FlexDirection = FlexContainer.FlexDirectionType.Row;
60             _container.WidthResizePolicy = ResizePolicyType.FillToParent;
61             _container.HeightResizePolicy = ResizePolicyType.FillToParent;
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 = ResizePolicyType.UseNaturalSize;
127                 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
128                 _images[i].SetImage("./images/star-highlight.png");
129             }
130
131             for(int i = rating; i < 5; i++)
132             {
133                 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
134                 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
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 : NUIApplication
168     {
169         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
170         delegate void CallbackDelegate();
171
172         public Example() : base()
173         {
174         }
175
176         public Example(string stylesheet) : base(stylesheet)
177         {
178         }
179
180         public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
181         {
182         }
183
184         protected override void OnCreate()
185         {
186             base.OnCreate();
187             Initialize();
188         }
189
190         public void Initialize()
191         {
192             Stage stage = Stage.Instance;
193             stage.BackgroundColor = Color.White;
194
195             // Create a container to layout the rows of image and rating vertically
196             FlexContainer container = new FlexContainer();
197
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;
203
204             stage.GetDefaultLayer().Add(container);
205
206             Random random = new Random();
207
208             for(int i = 0; i < 6; i++) // 6 rows in total
209             {
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);
217
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;
224                 image.Flex = 0.3f;
225                 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
226                 imageRow.Add(image);
227
228                 // Create a rating control
229                 StarRating view = new StarRating();
230
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);
235                 view.Flex = 0.7f;
236                 view.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
237                 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
238                 imageRow.Add(view);
239
240                 // Set the initial rating randomly between 1 and 5
241                 view.Rating = random.Next(1, 6);
242             }
243         }
244
245         /// <summary>
246         /// The main entry point for the application.
247         /// </summary>
248         [STAThread]
249         static void _Main(string[] args)
250         {
251             //System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);
252
253             Example example = new Example();
254             example.Run(args);
255         }
256     }
257 }