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