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;
23 // A spin control (for continously changing values when users can easily predict a set of values)
28 ///Spin CustomView class
30 public class Spin : CustomView
32 private VisualBase _arrowVisual;
33 private TextField _textField;
34 private int _arrowVisualPropertyIndex;
35 private string _arrowImage;
36 private int _currentValue;
37 private int _minValue;
38 private int _maxValue;
39 private int _singleStep;
40 private bool _wrappingEnabled;
41 private string _fontFamily;
42 private string _fontStyle;
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 InternalSetting.DefaultParentOriginAsTopLeft = false;
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/owner/apps_rw/NUISamples.TizenTV/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.ParentOrigin = Tizen.NUI.ParentOrigin.Center;
95 _textField.AnchorPoint = Tizen.NUI.AnchorPoint.Center;
96 _textField.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent;
97 _textField.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent;
98 _textField.SizeModeFactor = new Vector3(1.0f, 0.45f, 1.0f);
99 _textField.PlaceholderText = "----";
100 _textField.BackgroundColor = _textBackgroundColor;
101 _textField.HorizontalAlignment = "Center";
102 _textField.VerticalAlignment = "Center";
103 _textField.Focusable = (true);
104 _textField.Name = "_textField";
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);