Revert "[Tizen] fix LineWrap GET error"
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.Tizen / examples / custom-control.cs
1 /*
2  * Copyright (c) 2017 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.UIComponents;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI;
23
24 namespace CustomControlTest
25 {
26
27     // A custom control for star rating (draggable to change the rating)
28     class StarRating : CustomView
29     {
30         private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
31         private FlexContainer _container;
32         private ImageView[] _images;
33         private Vector3 _gestureDisplacement;
34         private int _currentValue;
35         private int _myRating;
36         private bool _myDragEnabled;
37
38         // Called by DALi Builder if it finds a StarRating control in a JSON file
39         static CustomView CreateInstance()
40         {
41           return new StarRating();
42         }
43
44         // static constructor registers the control type (only runs once)
45         static StarRating()
46         {
47           // ViewRegistry registers control type with DALi type registery
48           // also uses introspection to find any properties that need to be registered with type registry
49           CustomViewRegistry.Instance.Register(CreateInstance, typeof(StarRating) );
50         }
51
52         public StarRating() : base(typeof(StarRating).Name, CustomViewBehaviour.ViewBehaviourDefault)
53         {
54         }
55
56         public override void OnInitialize()
57         {
58             // Create a container for the star images
59             _container = new FlexContainer();
60
61             _container.FlexDirection = FlexContainer.FlexDirectionType.Row;
62             _container.WidthResizePolicy = ResizePolicyType.FillToParent;
63             _container.HeightResizePolicy = ResizePolicyType.FillToParent;
64
65             this.Add(_container);
66
67             // Create the images
68             _images = new ImageView[5];
69
70             for(int i = 0; i < 5; i++)
71             {
72                 _images[i] = new ImageView(resources+"/images/star-dim.png");
73                 _container.Add( _images[i] );
74             }
75
76             // Update the images according to the rating (dimmed star by default)
77             _myRating = 0;
78             UpdateStartImages(_myRating);
79
80             // Enable pan gesture detection
81             EnableGestureDetection(Gesture.GestureType.Pan);
82             _myDragEnabled = true; // Allow dragging by default (can be disabled)
83         }
84
85         // Pan gesture handling
86         public override void OnPan(PanGesture gesture)
87         {
88             // Only handle pan gesture if dragging is allowed
89             if(_myDragEnabled)
90             {
91                 switch (gesture.State)
92                 {
93                     case Gesture.StateType.Started:
94                     {
95                         _gestureDisplacement = new Vector3(0.0f, 0.0f, 0.0f);
96                         _currentValue = 0;
97                         break;
98                     }
99                     case Gesture.StateType.Continuing:
100                     {
101                         // Calculate the rating according to pan desture displacement
102                         _gestureDisplacement.X += gesture.Displacement.X;
103                         int delta = (int)Math.Ceiling(_gestureDisplacement.X / 40.0f);
104                         _currentValue = _myRating + delta;
105
106                         // Clamp the rating
107                         if(_currentValue < 0) _currentValue = 0;
108                         if(_currentValue > 5) _currentValue = 5;
109
110                         // Update the images according to the rating
111                         UpdateStartImages(_currentValue);
112                         break;
113                     }
114                     default:
115                     {
116                         _myRating = _currentValue;
117                         break;
118                     }
119                 }
120             }
121         }
122
123         // Update the images according to the rating
124         private void UpdateStartImages(int rating)
125         {
126             for(int i = 0; i < rating; i++)
127             {
128                 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
129                 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
130                 _images[i].SetImage(resources+"/images/star-highlight.png");
131             }
132
133             for(int i = rating; i < 5; i++)
134             {
135                 _images[i].WidthResizePolicy = ResizePolicyType.UseNaturalSize;
136                 _images[i].HeightResizePolicy = ResizePolicyType.UseNaturalSize;
137                 _images[i].SetImage(resources+"/images/star-dim.png");
138             }
139         }
140
141         // Rating property of type int:
142         public int Rating
143         {
144             get
145             {
146                 return _myRating;
147             }
148             set
149             {
150                 _myRating = value;
151                 UpdateStartImages(_myRating);
152             }
153         }
154
155         // DragEnabled property of type bool:
156         public bool DragEnabled
157         {
158             get
159             {
160                 return _myDragEnabled;
161             }
162             set
163             {
164                 _myDragEnabled = value;
165             }
166         }
167     }
168
169     class Example : NUIApplication
170     {
171         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
172         delegate void CallbackDelegate();
173         private const string resources = "/home/owner/apps_rw/NUISamples.TizenTV/res";
174
175
176         public Example() : base()
177         {
178         }
179
180         public Example(string stylesheet) : base(stylesheet)
181         {
182         }
183
184         public Example(string stylesheet, WindowMode windowMode) : base(stylesheet, windowMode)
185         {
186         }
187
188         protected override void OnCreate()
189         {
190             base.OnCreate();
191             Initialize();
192         }
193
194         public void Initialize()
195         {
196             Window window = Window.Instance;
197             window.BackgroundColor = Color.White;
198
199             // Create a container to layout the rows of image and rating vertically
200             FlexContainer container = new FlexContainer();
201
202             container.ParentOrigin = ParentOrigin.TopLeft;
203             container.PivotPoint = PivotPoint.TopLeft;
204             container.FlexDirection = (int)FlexContainer.FlexDirectionType.Column;
205             container.WidthResizePolicy = ResizePolicyType.FillToParent;
206             container.HeightResizePolicy = ResizePolicyType.FillToParent;
207
208             window.Add(container);
209
210             Random random = new Random();
211
212             for(int i = 0; i < 6; i++) // 6 rows in total
213             {
214                 // Create a container to layout the image and rating (in each row) horizontally
215                 FlexContainer imageRow = new FlexContainer();
216                 imageRow.ParentOrigin = ParentOrigin.TopLeft;
217                 imageRow.PivotPoint = PivotPoint.TopLeft;
218                 imageRow.FlexDirection = FlexContainer.FlexDirectionType.Row;
219                 imageRow.Flex = 1.0f;
220                 container.Add(imageRow);
221
222                 // Add the image view to the row
223                 ImageView image = new ImageView(resources+"/images/gallery-" + i + ".jpg");
224                 image.Size2D = new Size2D(120, 120);
225                 image.WidthResizePolicy = ResizePolicyType.Fixed;
226                 image.HeightResizePolicy = ResizePolicyType.Fixed;
227                 image.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
228                 image.Flex = 0.3f;
229                 image.FlexMargin = new Vector4(10.0f, 0.0f, 0.0f, 0.0f);
230                 imageRow.Add(image);
231
232                 // Create a rating control
233                 StarRating view = new StarRating();
234
235                 // Add the rating control to the row
236                 view.ParentOrigin = ParentOrigin.Center;
237                 view.PivotPoint = PivotPoint.Center;
238                 view.Size2D = new Size2D(200, 40);
239                 view.Flex = 0.7f;
240                 view.AlignSelf = (int)FlexContainer.Alignment.AlignCenter;
241                 view.FlexMargin = new Vector4(30.0f, 0.0f, 0.0f, 0.0f);
242                 imageRow.Add(view);
243
244                 // Set the initial rating randomly between 1 and 5
245                 view.Rating = random.Next(1, 6);
246             }
247         }
248
249         /// <summary>
250         /// The main entry point for the application.
251         /// </summary>
252         [STAThread]
253         static void _Main(string[] args)
254         {
255             //System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(MyCSharpExample.StarRating).TypeHandle);
256
257             Example example = new Example();
258             example.Run(args);
259         }
260     }
261 }