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