2 * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Runtime.InteropServices;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.BaseComponents;
24 // A spin control (for continously changing values when users can easily predict a set of values)
29 ///Spins the CustomView class.
31 /// <since_tizen> 3 </since_tizen>
32 public class Spin : CustomView
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;
48 // Called by DALi Builder if it finds a Spin control in a JSON file
49 static CustomView CreateInstance()
54 // static constructor registers the control type (only runs once)
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));
63 /// Creates an initialized spin.
65 /// <since_tizen> 3 </since_tizen>
66 public Spin() : base(typeof(Spin).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
71 /// Overrides the method of OnInitialize() for the CustomView class.<br />
72 /// This method is called after the control has been initialized.<br />
73 /// Derived classes should do any second phase initialization by overriding this method.<br />
75 /// <since_tizen> 3 </since_tizen>
76 public override void OnInitialize()
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);
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);
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);
111 this.Add(_textField);
113 _textField.FocusGained += TextFieldKeyInputFocusGained;
114 _textField.FocusLost += TextFieldKeyInputFocusLost;
118 /// Overrides the method of GetNaturalSize() for the CustomView class.<br />
119 /// Returns the natural size of the actor.<br />
121 /// <returns> Natural size of this spin itself.</returns>
122 /// <since_tizen> 3 </since_tizen>
123 public override Size2D GetNaturalSize()
125 return new Size2D(150, 150);
129 /// An event handler is used when the TextField in the spin gets the key focus.<br />
130 /// Make sure when the current spin that takes input focus, also takes the keyboard focus.<br />
131 /// For example, when you tap the spin directly.<br />
133 /// <param name="source">Sender of this event.</param>
134 /// <param name="e">Event arguments.</param>
135 /// <since_tizen> 3 </since_tizen>
136 public void TextFieldKeyInputFocusGained(object source, EventArgs e)
138 FocusManager.Instance.SetCurrentFocusView(_textField);
142 /// An event handler when the TextField in the spin looses it's key focus.
144 /// <param name="source"></param>
145 /// <param name="e"></param>
146 /// <since_tizen> 3 </since_tizen>
147 public void TextFieldKeyInputFocusLost(object source, EventArgs e)
149 int previousValue = _currentValue;
151 // If the input value is invalid, change it back to the previous valid value
152 if (int.TryParse(_textField.Text, out _currentValue))
154 if (_currentValue < _minValue || _currentValue > _maxValue)
156 _currentValue = previousValue;
161 _currentValue = previousValue;
164 // Otherwise take the new value
165 this.Value = _currentValue;
169 /// Overrides the method of GetNextKeyboardFocusableView() for the CustomView class.<br />
170 /// Gets the next key focusable view in this view towards the given direction.<br />
171 /// A view needs to override this function in order to support two-dimensional key navigation.<br />
173 /// <param name="currentFocusedView">The current focused view.</param>
174 /// <param name="direction">The direction to move the focus towards.</param>
175 /// <param name="loopEnabled">Whether the focus movement should be looped within the control.</param>
176 /// <returns>The next keyboard focusable view in this control or an empty handle if no view can be focused.</returns>
177 /// <since_tizen> 3 </since_tizen>
178 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
180 // Respond to Up/Down keys to change the value while keeping the current spin focused
181 View nextFocusedView = currentFocusedView;
182 if (direction == View.FocusDirection.Up)
184 this.Value += this.Step;
185 nextFocusedView = _textField;
187 else if (direction == View.FocusDirection.Down)
189 this.Value -= this.Step;
190 nextFocusedView = _textField;
198 return nextFocusedView;
202 /// Value to be set in the spin.
204 /// <since_tizen> 3 </since_tizen>
205 [ScriptableProperty()]
210 return _currentValue;
215 NUILog.Debug("Value set to " + value);
216 _currentValue = value;
218 // Make sure no invalid value is accepted
219 if (_currentValue < _minValue)
221 _currentValue = _minValue;
224 if (_currentValue > _maxValue)
226 _currentValue = _maxValue;
229 _textField.Text = _currentValue.ToString();
234 /// Minimum value of the spin value.
236 /// <since_tizen> 3 </since_tizen>
237 // MinValue property of type int:
238 /// <since_tizen> 3 </since_tizen>
239 [ScriptableProperty()]
253 /// Maximum value of the spin value.
255 /// <since_tizen> 3 </since_tizen>
256 // MaxValue property of type int:
257 /// <since_tizen> 3 </since_tizen>
258 [ScriptableProperty()]
272 /// Increasing, decreasing step of the spin value when up or down keys are pressed.
274 /// <since_tizen> 3 </since_tizen>
275 // Step property of type int:
276 /// <since_tizen> 3 </since_tizen>
277 [ScriptableProperty()]
291 /// Wrapping enabled status.
293 /// <since_tizen> 3 </since_tizen>
294 // WrappingEnabled property of type bool:
295 /// <since_tizen> 3 </since_tizen>
296 [ScriptableProperty()]
297 public bool WrappingEnabled
301 return _wrappingEnabled;
305 _wrappingEnabled = value;
310 /// Text point size of the spin value.
312 /// <since_tizen> 3 </since_tizen>
313 // TextPointSize property of type int:
314 /// <since_tizen> 3 </since_tizen>
315 [ScriptableProperty()]
316 public int TextPointSize
325 _textField.PointSize = _pointSize;
330 /// The color of the spin value.
332 /// <since_tizen> 3 </since_tizen>
333 // TextColor property of type Color:
334 /// <since_tizen> 3 </since_tizen>
335 [ScriptableProperty()]
336 public Color TextColor
344 NUILog.Debug("TextColor set to " + value.R + "," + value.G + "," + value.B);
347 _textField.TextColor = _textColor;
352 /// Maximum text lengh of the spin value.
354 /// <since_tizen> 3 </since_tizen>
355 // MaxTextLength property of type int:
356 /// <since_tizen> 3 </since_tizen>
357 [ScriptableProperty()]
358 public int MaxTextLength
362 return _maxTextLength;
366 _maxTextLength = value;
367 _textField.MaxLength = _maxTextLength;
372 /// Reference of TextField of the spin.
374 /// <since_tizen> 3 </since_tizen>
375 public TextField SpinText
388 /// Show indicator image, for example, up or down arrow image.
390 /// <since_tizen> 3 </since_tizen>
391 public string IndicatorImage
400 _arrowVisual = VisualFactory.Instance.CreateVisual(
401 new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image))
402 .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage))
403 .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150))
404 .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150)));
405 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);