Release 4.0.0-preview1-00301
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / CustomView / Spin.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;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.BaseComponents;
23
24 // A spin control (for continously changing values when users can easily predict a set of values)
25
26 namespace Tizen.NUI
27 {
28     ///<summary>
29     ///Spins the CustomView class.
30     /// </summary>
31     public class Spin : CustomView
32     {
33         private VisualBase _arrowVisual;
34         private TextField _textField;
35         private int _arrowVisualPropertyIndex;
36         private string _arrowImage;
37         private int _currentValue;
38         private int _minValue;
39         private int _maxValue;
40         private int _singleStep;
41         private bool _wrappingEnabled;
42         private int _pointSize;
43         private Color _textColor;
44         private Color _textBackgroundColor;
45         private int _maxTextLength;
46
47         // Called by DALi Builder if it finds a Spin control in a JSON file
48         static CustomView CreateInstance()
49         {
50             return new Spin();
51         }
52
53         // static constructor registers the control type (only runs once)
54         static Spin()
55         {
56             // ViewRegistry registers control type with DALi type registery
57             // also uses introspection to find any properties that need to be registered with type registry
58             CustomViewRegistry.Instance.Register(CreateInstance, typeof(Spin));
59         }
60
61         /// <summary>
62         /// Creates an initialized spin.
63         /// </summary>
64         /// <since_tizen> 3 </since_tizen>
65         public Spin() : base(typeof(Spin).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
66         {
67         }
68
69         /// <summary>
70         /// Overrides the method of OnInitialize() for the CustomView class.<br />
71         /// This method is called after the control has been initialized.<br />
72         /// Derived classes should do any second phase initialization by overriding this method.<br />
73         /// </summary>
74         /// <since_tizen> 3 </since_tizen>
75         public override void OnInitialize()
76         {
77             // Initialize the propertiesControl
78             //_arrowImage = "/home/tengxb/Workspace/nui-debug/examples/res/images/arrow.png";
79             _arrowImage = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "picture.png";
80             _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f);
81             _currentValue = 0;
82             _minValue = 0;
83             _maxValue = 0;
84             _singleStep = 1;
85             _maxTextLength = 0;
86
87             // Create image visual for the arrow keys
88             _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new PropertyValue(_arrowImage), Tizen.NUI.PropertyAccessMode.ReadWrite);
89             _arrowVisual = VisualFactory.Instance.CreateVisual(
90                 new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image))
91                                  .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage))
92                                  .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150))
93                                  .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150)));
94             RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
95
96             // Create a text field
97             _textField = new TextField();
98             _textField.PivotPoint = Tizen.NUI.PivotPoint.Center;
99             _textField.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
100             _textField.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
101             _textField.SizeModeFactor = new Vector3(1.0f, 0.45f, 1.0f);
102             _textField.PlaceholderText = "----";
103             _textField.BackgroundColor = _textBackgroundColor;
104             _textField.HorizontalAlignment = HorizontalAlignment.Center;
105             _textField.VerticalAlignment = VerticalAlignment.Center;
106             _textField.Focusable = (true);
107             _textField.Name = "_textField";
108             _textField.Position2D = new Position2D(0, 40);
109
110             this.Add(_textField);
111
112             _textField.FocusGained += TextFieldKeyInputFocusGained;
113             _textField.FocusLost += TextFieldKeyInputFocusLost;
114         }
115
116         /// <summary>
117         /// Overrides the method of GetNaturalSize() for the CustomView class.<br />
118         /// Returns the natural size of the actor.<br />
119         /// </summary>
120         /// <returns> Natural size of this spin itself.</returns>
121         /// <since_tizen> 3 </since_tizen>
122         public override Size2D GetNaturalSize()
123         {
124             return new Size2D(150, 150);
125         }
126
127         /// <summary>
128         /// An event handler is used when the TextField in the spin gets the key focus.<br />
129         /// Make sure when the current spin that takes input focus, also takes the keyboard focus.<br />
130         /// For example, when you tap the spin directly.<br />
131         /// </summary>
132         /// <param name="source">Sender of this event.</param>
133         /// <param name="e">Event arguments.</param>
134         /// <since_tizen> 3 </since_tizen>
135         public void TextFieldKeyInputFocusGained(object source, EventArgs e)
136         {
137             FocusManager.Instance.SetCurrentFocusView(_textField);
138         }
139
140         /// <summary>
141         /// An event handler when the TextField in the spin looses it's key focus.
142         /// </summary>
143         /// <param name="source"></param>
144         /// <param name="e"></param>
145         /// <since_tizen> 3 </since_tizen>
146         public void TextFieldKeyInputFocusLost(object source, EventArgs e)
147         {
148             int previousValue = _currentValue;
149
150             // If the input value is invalid, change it back to the previous valid value
151             if (int.TryParse(_textField.Text, out _currentValue))
152             {
153                 if (_currentValue < _minValue || _currentValue > _maxValue)
154                 {
155                     _currentValue = previousValue;
156                 }
157             }
158             else
159             {
160                 _currentValue = previousValue;
161             }
162
163             // Otherwise take the new value
164             this.Value = _currentValue;
165         }
166
167         /// <summary>
168         /// Overrides the method of GetNextKeyboardFocusableView() for the CustomView class.<br />
169         /// Gets the next key focusable view in this view towards the given direction.<br />
170         /// A view needs to override this function in order to support two-dimensional key navigation.<br />
171         /// </summary>
172         /// <param name="currentFocusedView">The current focused view.</param>
173         /// <param name="direction">The direction to move the focus towards.</param>
174         /// <param name="loopEnabled">Whether the focus movement should be looped within the control.</param>
175         /// <returns>The next keyboard focusable view in this control or an empty handle if no view can be focused.</returns>
176         /// <since_tizen> 3 </since_tizen>
177         public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
178         {
179             // Respond to Up/Down keys to change the value while keeping the current spin focused
180             View nextFocusedView = currentFocusedView;
181             if (direction == View.FocusDirection.Up)
182             {
183                 this.Value += this.Step;
184                 nextFocusedView = _textField;
185             }
186             else if (direction == View.FocusDirection.Down)
187             {
188                 this.Value -= this.Step;
189                 nextFocusedView = _textField;
190             }
191             else
192             {
193                 // Return null
194                 return null;
195             }
196
197             return nextFocusedView;
198         }
199
200         /// <summary>
201         /// Value to be set in the spin.
202         /// </summary>
203         /// <since_tizen> 3 </since_tizen>
204         [ScriptableProperty()]
205         public int Value
206         {
207             get
208             {
209                 return _currentValue;
210             }
211             set
212             {
213
214                 Tizen.Log.Debug("NUI", "Value set to " + value);
215                 _currentValue = value;
216
217                 // Make sure no invalid value is accepted
218                 if (_currentValue < _minValue)
219                 {
220                     _currentValue = _minValue;
221                 }
222
223                 if (_currentValue > _maxValue)
224                 {
225                     _currentValue = _maxValue;
226                 }
227
228                 _textField.Text = _currentValue.ToString();
229             }
230         }
231
232         /// <summary>
233         /// Minimum value of the spin value.
234         /// </summary>
235         /// <since_tizen> 3 </since_tizen>
236         // MinValue property of type int:
237         [ScriptableProperty()]
238         public int MinValue
239         {
240             get
241             {
242                 return _minValue;
243             }
244             set
245             {
246                 _minValue = value;
247             }
248         }
249
250         /// <summary>
251         /// Maximum value of the spin value.
252         /// </summary>
253         /// <since_tizen> 3 </since_tizen>
254         // MaxValue property of type int:
255         [ScriptableProperty()]
256         public int MaxValue
257         {
258             get
259             {
260                 return _maxValue;
261             }
262             set
263             {
264                 _maxValue = value;
265             }
266         }
267
268         /// <summary>
269         /// Increasing, decreasing step of the spin value when up or down keys are pressed.
270         /// </summary>
271         /// <since_tizen> 3 </since_tizen>
272         // Step property of type int:
273         [ScriptableProperty()]
274         public int Step
275         {
276             get
277             {
278                 return _singleStep;
279             }
280             set
281             {
282                 _singleStep = value;
283             }
284         }
285
286         /// <summary>
287         /// Wrapping enabled status.
288         /// </summary>
289         /// <since_tizen> 3 </since_tizen>
290         // WrappingEnabled property of type bool:
291         [ScriptableProperty()]
292         public bool WrappingEnabled
293         {
294             get
295             {
296                 return _wrappingEnabled;
297             }
298             set
299             {
300                 _wrappingEnabled = value;
301             }
302         }
303
304         /// <summary>
305         /// Text point size of the spin value.
306         /// </summary>
307         /// <since_tizen> 3 </since_tizen>
308         // TextPointSize property of type int:
309         [ScriptableProperty()]
310         public int TextPointSize
311         {
312             get
313             {
314                 return _pointSize;
315             }
316             set
317             {
318                 _pointSize = value;
319                 _textField.PointSize = _pointSize;
320             }
321         }
322
323         /// <summary>
324         /// The color of the spin value.
325         /// </summary>
326         /// <since_tizen> 3 </since_tizen>
327         // TextColor property of type Color:
328         [ScriptableProperty()]
329         public Color TextColor
330         {
331             get
332             {
333                 return _textColor;
334             }
335             set
336             {
337                 Tizen.Log.Debug("NUI", "TextColor set to " + value.R + "," + value.G + "," + value.B);
338
339                 _textColor = value;
340                 _textField.TextColor = _textColor;
341             }
342         }
343
344         /// <summary>
345         /// Maximum text lengh of the spin value.
346         /// </summary>
347         /// <since_tizen> 3 </since_tizen>
348         // MaxTextLength property of type int:
349         [ScriptableProperty()]
350         public int MaxTextLength
351         {
352             get
353             {
354                 return _maxTextLength;
355             }
356             set
357             {
358                 _maxTextLength = value;
359                 _textField.MaxLength = _maxTextLength;
360             }
361         }
362
363         /// <summary>
364         /// Reference of TextField of the spin.
365         /// </summary>
366         /// <since_tizen> 3 </since_tizen>
367         public TextField SpinText
368         {
369             get
370             {
371                 return _textField;
372             }
373             set
374             {
375                 _textField = value;
376             }
377         }
378
379         /// <summary>
380         /// Show indicator image, for example, up or down arrow image.
381         /// </summary>
382         /// <since_tizen> 3 </since_tizen>
383         public string IndicatorImage
384         {
385             get
386             {
387                 return _arrowImage;
388             }
389             set
390             {
391                 _arrowImage = value;
392                 _arrowVisual = VisualFactory.Instance.CreateVisual(
393                     new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image))
394                                  .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage))
395                                  .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150))
396                                  .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150)));
397                 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
398             }
399         }
400     }
401 }