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 ViewRegistry.Instance.Register(CreateInstance, typeof(Spin));
65 /// Creates an initialized Spin.
67 public Spin() : base(typeof(Spin).Name, 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.Get().CreateVisual(_arrowImage, new Uint16Pair(150, 150));
90 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
92 // Create a text field
93 _textField = new TextField();
94 _textField.AnchorPoint = Tizen.NUI.AnchorPoint.Center;
95 _textField.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
96 _textField.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
97 _textField.SizeModeFactor = new Vector3(1.0f, 0.45f, 1.0f);
98 _textField.PlaceholderText = "----";
99 _textField.BackgroundColor = _textBackgroundColor;
100 _textField.HorizontalAlignment = HorizontalAlignment.Center;
101 _textField.VerticalAlignment = VerticalAlignment.Center;
102 _textField.Focusable = (true);
103 _textField.Name = "_textField";
104 _textField.Position2D = new Position2D(0, 40);
106 this.Add(_textField);
108 _textField.FocusGained += TextFieldKeyInputFocusGained;
109 _textField.FocusLost += TextFieldKeyInputFocusLost;
113 /// Override method of GetNaturalSize() for CustomView class.<br>
114 /// Return the natural size of the actor.<br>
116 /// <returns> Natural size of this Spin itself</returns>
117 public override Size GetNaturalSize()
119 return new Size(150.0f, 150.0f, 0.0f);
123 /// Event handler when the TextField in Spin gets the Key focus.<br>
124 /// Make sure when the current spin that takes input focus also takes the keyboard focus.<br>
125 /// For example, when you tap the spin directly.<br>
127 /// <param name="source">Sender of this event</param>
128 /// <param name="e">Event arguments</param>
129 public void TextFieldKeyInputFocusGained(object source, EventArgs e)
131 FocusManager.Instance.SetCurrentFocusView(_textField);
135 /// Event handler when the TextField in Spin looses it's Key focus
137 /// <param name="source"></param>
138 /// <param name="e"></param>
139 public void TextFieldKeyInputFocusLost(object source, EventArgs e)
141 int previousValue = _currentValue;
143 // If the input value is invalid, change it back to the previous valid value
144 if (int.TryParse(_textField.Text, out _currentValue))
146 if (_currentValue < _minValue || _currentValue > _maxValue)
148 _currentValue = previousValue;
153 _currentValue = previousValue;
156 // Otherwise take the new value
157 this.Value = _currentValue;
161 /// Override method of GetNextKeyboardFocusableView() for CustomView class.<br>
162 /// Gets the next key focusable view in this View towards the given direction.<br>
163 /// A View needs to override this function in order to support two dimensional key navigation.<br>
165 /// <param name="currentFocusedView">The current focused view</param>
166 /// <param name="direction">The direction to move the focus towards</param>
167 /// <param name="loopEnabled">Whether the focus movement should be looped within the control</param>
168 /// <returns>The next keyboard focusable view in this control or an empty handle if no view can be focused</returns>
169 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
171 // Respond to Up/Down keys to change the value while keeping the current spin focused
172 View nextFocusedView = currentFocusedView;
173 if (direction == View.FocusDirection.Up)
175 this.Value += this.Step;
176 nextFocusedView = _textField;
178 else if (direction == View.FocusDirection.Down)
180 this.Value -= this.Step;
181 nextFocusedView = _textField;
185 // Return a native empty handle as nothing can be focused in the left or right
186 nextFocusedView = new View();
187 nextFocusedView.Reset();
190 return nextFocusedView;
194 /// Value to be set in Spin.
196 [ScriptableProperty()]
201 return _currentValue;
206 Tizen.Log.Debug("NUI", "Value set to " + value);
207 _currentValue = value;
209 // Make sure no invalid value is accepted
210 if (_currentValue < _minValue)
212 _currentValue = _minValue;
215 if (_currentValue > _maxValue)
217 _currentValue = _maxValue;
220 _textField.Text = _currentValue.ToString();
225 /// Minimum Value of Spin Value.
227 // MinValue property of type int:
228 [ScriptableProperty()]
242 /// Maximum Value of Spin Value.
244 // MaxValue property of type int:
245 [ScriptableProperty()]
259 /// Increasing, decresing step of Spin Value when Up or Down key is pressed.
261 // Step property of type int:
262 [ScriptableProperty()]
276 /// Wrapping enabled status.
278 // WrappingEnabled property of type bool:
279 [ScriptableProperty()]
280 public bool WrappingEnabled
284 return _wrappingEnabled;
288 _wrappingEnabled = value;
293 /// Text point size of Spin Value.
295 // TextPointSize property of type int:
296 [ScriptableProperty()]
297 public int TextPointSize
306 _textField.PointSize = _pointSize;
311 /// The color of Spin Value.
313 // TextColor property of type Color:
314 [ScriptableProperty()]
315 public Color TextColor
323 Tizen.Log.Debug("NUI", "TextColor set to " + value.R + "," + value.G + "," + value.B);
326 _textField.TextColor = _textColor;
331 /// Maximum text lengh of Spin Value.
333 // MaxTextLength property of type int:
334 [ScriptableProperty()]
335 public int MaxTextLength
339 return _maxTextLength;
343 _maxTextLength = value;
344 _textField.MaxLength = _maxTextLength;
349 /// Reference of TextField of Spin.
351 public TextField SpinText
364 /// Show indicator image, for example Up/Down Arrow image.
366 // Indicator property of type string:
367 public string IndicatorImage
376 _arrowVisual = VisualFactory.Get().CreateVisual(_arrowImage, new Uint16Pair(150, 150));
377 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);