dali ver 1.2.27 devel-master branch code sync
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / devel-src / spin.cs
1 /*
2  * Copyright (c) 2016 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
22 // A spin control (for continously changing values when users can easily predict a set of values)
23
24 namespace Tizen.NUI
25 {
26 public class Spin : CustomView
27   {
28     private VisualBase _arrowVisual;
29     private TextField _textField;
30     private int _arrowVisualPropertyIndex;
31     private string _arrowImage;
32     private int _currentValue;
33     private int _minValue;
34     private int _maxValue;
35     private int _singleStep;
36     private bool _wrappingEnabled;
37     private string _fontFamily;
38     private string _fontStyle;
39     private int _pointSize;
40     private Color _textColor;
41     private Color _textBackgroundColor;
42     private int _maxTextLength;
43
44     // Called by DALi Builder if it finds a Spin control in a JSON file
45     static CustomView CreateInstance()
46     {
47       return new Spin();
48     }
49
50     // static constructor registers the control type (only runs once)
51     static Spin()
52     {
53       // ViewRegistry registers control type with DALi type registery
54       // also uses introspection to find any properties that need to be registered with type registry
55       ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
56     }
57
58     public Spin() : base(typeof(Spin).Name, ViewWrapperImpl.CustomViewBehaviour.REQUIRES_KEYBOARD_NAVIGATION_SUPPORT | ViewWrapperImpl.CustomViewBehaviour.DISABLE_STYLE_CHANGE_SIGNALS)
59     {
60
61     }
62     public override void OnInitialize()
63     {
64       //Console.WriteLine ("[Spin]OnInitialize()!");
65
66       // Initialize the propertiesControl
67       _arrowImage = "/home/owner/apps_rw/NUISamples.TizenTV/res/images/arrow.png";
68       _textBackgroundColor = new Color(1.0f, 1.0f, 0.6f, 1.0f);
69       _currentValue = 0;
70       _minValue = 0;
71       _maxValue = 0;
72       _singleStep = 1;
73       _maxTextLength = 0;
74
75       // Create image visual for the arrow keys
76       _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new Tizen.NUI.Property.Value(_arrowImage), Tizen.NUI.Property.AccessMode.READ_WRITE);
77       _arrowVisual =  VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
78       RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );
79
80       // Create a text field
81       _textField = new TextField();
82       _textField.ParentOrigin = NDalic.ParentOriginCenter;
83       _textField.AnchorPoint = NDalic.AnchorPointCenter;
84       _textField.WidthResizePolicy = "SIZE_RELATIVE_TO_PARENT";
85       _textField.HeightResizePolicy = "SIZE_RELATIVE_TO_PARENT";
86       _textField.SizeModeFactor = new Vector3( 1.0f, 0.45f, 1.0f );
87       _textField.PlaceholderText = "----";
88       _textField.BackgroundColor = _textBackgroundColor;
89       _textField.HorizontalAlignment = "Center";
90       _textField.VerticalAlignment = "Center";
91       _textField.SetKeyboardFocusable(true);
92       _textField.Name = "_textField";
93
94       this.Add(_textField);
95
96       _textField.KeyInputFocusGained += TextFieldKeyInputFocusGained;
97       _textField.KeyInputFocusLost += TextFieldKeyInputFocusLost;
98     }
99
100     public override Vector3 GetNaturalSize()
101     {
102         //Console.WriteLine ("[Spin]GetNaturalSize()!");
103
104       return new Vector3(150.0f, 150.0f, 0.0f);
105     }
106
107     public void TextFieldKeyInputFocusGained(object source, KeyInputFocusGainedEventArgs e)
108     {
109         //Console.WriteLine ("[Spin]TextFieldKeyInputFocusGained()!");
110
111       // Make sure when the current spin that takes input focus also takes the keyboard focus
112       // For example, when you tap the spin directly
113       FocusManager.Instance.SetCurrentFocusActor(_textField);
114     }
115
116     public void TextFieldKeyInputFocusLost(object source, KeyInputFocusLostEventArgs e)
117     {
118         //Console.WriteLine ("[Spin]TextFieldKeyInputFocusLost()!");
119       int previousValue = _currentValue;
120
121       // If the input value is invalid, change it back to the previous valid value
122       if(int.TryParse(_textField.Text, out _currentValue))
123       {
124         if (_currentValue < _minValue || _currentValue > _maxValue)
125         {
126           _currentValue = previousValue;
127         }
128       }
129       else
130       {
131         _currentValue = previousValue;
132       }
133
134       // Otherwise take the new value
135       this.Value = _currentValue;
136     }
137
138     public override Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, View.KeyboardFocus.Direction direction, bool loopEnabled)
139     {
140       // Respond to Up/Down keys to change the value while keeping the current spin focused
141         //Console.WriteLine ("[Spin]GetNextKeyboardFocusableActor()!");
142
143       Actor nextFocusedActor = currentFocusedActor;
144       if (direction == View.KeyboardFocus.Direction.UP)
145       {
146         this.Value += this.Step;
147         nextFocusedActor = _textField;
148       }
149       else if (direction == View.KeyboardFocus.Direction.DOWN)
150       {
151         this.Value -= this.Step;
152         nextFocusedActor = _textField;
153       }
154       else
155       {
156         // Return a native empty handle as nothing can be focused in the left or right
157         nextFocusedActor = new Actor();
158         nextFocusedActor.Reset();
159       }
160
161       return nextFocusedActor;
162     }
163
164
165     [ScriptableProperty()]
166     public int Value
167     {
168       get
169       {
170         return _currentValue;
171       }
172       set
173       {
174
175         //Console.WriteLine ("[Spin]Value set to "  + value );
176         _currentValue = value;
177
178         // Make sure no invalid value is accepted
179         if (_currentValue < _minValue)
180         {
181           _currentValue = _minValue;
182         }
183
184         if (_currentValue > _maxValue)
185         {
186           _currentValue = _maxValue;
187         }
188
189         _textField.Text = _currentValue.ToString();
190       }
191     }
192     // MinValue property of type int:
193     [ScriptableProperty()]
194     public int MinValue
195     {
196       get
197       {
198         return _minValue;
199       }
200       set
201       {
202         _minValue = value;
203       }
204     }
205
206     // MaxValue property of type int:
207     [ScriptableProperty()]
208     public int MaxValue
209     {
210       get
211       {
212         return _maxValue;
213       }
214       set
215       {
216         _maxValue = value;
217       }
218     }
219
220     // Step property of type int:
221     [ScriptableProperty()]
222     public int Step
223     {
224       get
225       {
226         return _singleStep;
227       }
228       set
229       {
230         _singleStep = value;
231       }
232     }
233
234     // WrappingEnabled property of type bool:
235     [ScriptableProperty()]
236     public bool WrappingEnabled
237     {
238       get
239       {
240         return _wrappingEnabled;
241       }
242       set
243       {
244         _wrappingEnabled = value;
245       }
246     }
247
248     // TextPointSize property of type int:
249     [ScriptableProperty()]
250     public int TextPointSize
251     {
252       get
253       {
254         return _pointSize;
255       }
256       set
257       {
258         _pointSize = value;
259         _textField.PointSize = _pointSize;
260       }
261     }
262
263     // TextColor property of type Color:
264     [ScriptableProperty()]
265     public Color TextColor
266     {
267       get
268       {
269         return _textColor;
270       }
271       set
272       {
273           //Console.WriteLine ("[Spin]TextColor set to "  + value.R + "," + value.G + ","+ value.B);
274
275         _textColor = value;
276         _textField.TextColor = _textColor;
277       }
278     }
279
280     // MaxTextLength property of type int:
281     [ScriptableProperty()]
282     public int MaxTextLength
283     {
284       get
285       {
286         return _maxTextLength;
287       }
288       set
289       {
290         _maxTextLength = value;
291         _textField.MaxLength = _maxTextLength;
292       }
293     }
294
295     public TextField SpinText
296     {
297       get
298       {
299         return _textField;
300       }
301       set
302       {
303         _textField = value;
304       }
305     }
306
307     // Indicator property of type string:
308     public string IndicatorImage
309     {
310       get
311       {
312         return _arrowImage;
313       }
314       set
315       {
316         _arrowImage = value;
317         _arrowVisual =  VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
318         RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );
319       }
320   }
321 }
322 }