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