1 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 // This File has been auto-generated by SWIG and then modified using DALi Ruby Scripts
17 // Some have been manually changed
20 using System.Runtime.InteropServices;
22 using Tizen.NUI.UIComponents;
23 using Tizen.NUI.BaseComponents;
25 // A spin control (for continously changing values when users can easily predict a set of values)
30 ///Spin CustomView class
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 string _fontFamily;
44 private string _fontStyle;
45 private int _pointSize;
46 private Color _textColor;
47 private Color _textBackgroundColor;
48 private int _maxTextLength;
50 // Called by DALi Builder if it finds a Spin control in a JSON file
51 static CustomView CreateInstance()
56 // static constructor registers the control type (only runs once)
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));
65 /// Creates an initialized Spin.
67 public Spin() : base(typeof(Spin).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
72 /// Override method of OnInitialize() for 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>
76 public override void OnInitialize()
78 // Initialize the propertiesControl
79 _arrowImage = "/home/tengxb/Workspace/nui-debug/examples/res/images/arrow.png";
80 _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f);
87 // Create image visual for the arrow keys
88 _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new PropertyValue(_arrowImage), Tizen.NUI.PropertyAccessMode.ReadWrite);
89 _arrowVisual = VisualFactory.Instance.CreateVisual(
90 new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image))
91 .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage))
92 .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150))
93 .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150)));
94 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
96 // Create a text field
97 _textField = new TextField();
98 _textField.PivotPoint = Tizen.NUI.PivotPoint.Center;
99 _textField.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
100 _textField.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
101 _textField.SizeModeFactor = new Vector3(1.0f, 0.45f, 1.0f);
102 _textField.PlaceholderText = "----";
103 _textField.BackgroundColor = _textBackgroundColor;
104 _textField.HorizontalAlignment = HorizontalAlignment.Center;
105 _textField.VerticalAlignment = VerticalAlignment.Center;
106 _textField.Focusable = (true);
107 _textField.Name = "_textField";
108 _textField.Position2D = new Position2D(0, 40);
110 this.Add(_textField);
112 _textField.FocusGained += TextFieldKeyInputFocusGained;
113 _textField.FocusLost += TextFieldKeyInputFocusLost;
117 /// Override method of GetNaturalSize() for CustomView class.<br>
118 /// Return the natural size of the actor.<br>
120 /// <returns> Natural size of this Spin itself</returns>
121 public override Size2D GetNaturalSize()
123 return new Size2D(150, 150);
127 /// Event handler when the TextField in Spin gets the Key focus.<br>
128 /// Make sure when the current spin that takes input focus also takes the keyboard focus.<br>
129 /// For example, when you tap the spin directly.<br>
131 /// <param name="source">Sender of this event</param>
132 /// <param name="e">Event arguments</param>
133 public void TextFieldKeyInputFocusGained(object source, EventArgs e)
135 FocusManager.Instance.SetCurrentFocusView(_textField);
139 /// Event handler when the TextField in Spin looses it's Key focus
141 /// <param name="source"></param>
142 /// <param name="e"></param>
143 public void TextFieldKeyInputFocusLost(object source, EventArgs e)
145 int previousValue = _currentValue;
147 // If the input value is invalid, change it back to the previous valid value
148 if (int.TryParse(_textField.Text, out _currentValue))
150 if (_currentValue < _minValue || _currentValue > _maxValue)
152 _currentValue = previousValue;
157 _currentValue = previousValue;
160 // Otherwise take the new value
161 this.Value = _currentValue;
165 /// Override method of GetNextKeyboardFocusableView() for CustomView class.<br>
166 /// Gets the next key focusable view in this View towards the given direction.<br>
167 /// A View needs to override this function in order to support two dimensional key navigation.<br>
169 /// <param name="currentFocusedView">The current focused view</param>
170 /// <param name="direction">The direction to move the focus towards</param>
171 /// <param name="loopEnabled">Whether the focus movement should be looped within the control</param>
172 /// <returns>The next keyboard focusable view in this control or an empty handle if no view can be focused</returns>
173 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
175 // Respond to Up/Down keys to change the value while keeping the current spin focused
176 View nextFocusedView = currentFocusedView;
177 if (direction == View.FocusDirection.Up)
179 this.Value += this.Step;
180 nextFocusedView = _textField;
182 else if (direction == View.FocusDirection.Down)
184 this.Value -= this.Step;
185 nextFocusedView = _textField;
193 return nextFocusedView;
197 /// Value to be set in Spin.
199 [ScriptableProperty()]
204 return _currentValue;
209 Tizen.Log.Debug("NUI", "Value set to " + value);
210 _currentValue = value;
212 // Make sure no invalid value is accepted
213 if (_currentValue < _minValue)
215 _currentValue = _minValue;
218 if (_currentValue > _maxValue)
220 _currentValue = _maxValue;
223 _textField.Text = _currentValue.ToString();
228 /// Minimum Value of Spin Value.
230 // MinValue property of type int:
231 [ScriptableProperty()]
245 /// Maximum Value of Spin Value.
247 // MaxValue property of type int:
248 [ScriptableProperty()]
262 /// Increasing, decresing step of Spin Value when Up or Down key is pressed.
264 // Step property of type int:
265 [ScriptableProperty()]
279 /// Wrapping enabled status.
281 // WrappingEnabled property of type bool:
282 [ScriptableProperty()]
283 public bool WrappingEnabled
287 return _wrappingEnabled;
291 _wrappingEnabled = value;
296 /// Text point size of Spin Value.
298 // TextPointSize property of type int:
299 [ScriptableProperty()]
300 public int TextPointSize
309 _textField.PointSize = _pointSize;
314 /// The color of Spin Value.
316 // TextColor property of type Color:
317 [ScriptableProperty()]
318 public Color TextColor
326 Tizen.Log.Debug("NUI", "TextColor set to " + value.R + "," + value.G + "," + value.B);
329 _textField.TextColor = _textColor;
334 /// Maximum text lengh of Spin Value.
336 // MaxTextLength property of type int:
337 [ScriptableProperty()]
338 public int MaxTextLength
342 return _maxTextLength;
346 _maxTextLength = value;
347 _textField.MaxLength = _maxTextLength;
352 /// Reference of TextField of Spin.
354 public TextField SpinText
367 /// Show indicator image, for example Up/Down Arrow image.
369 // Indicator property of type string:
370 public string IndicatorImage
379 _arrowVisual = VisualFactory.Instance.CreateVisual(
380 new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image))
381 .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage))
382 .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150))
383 .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150)));
384 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);