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