2 * Copyright (c) 2016 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;
22 // A spin control (for continously changing values when users can easily predict a set of values)
26 public class Spin : CustomView
\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
44 // Called by DALi Builder if it finds a Spin control in a JSON file
\r
45 static CustomView CreateInstance()
\r
50 // static constructor registers the control type (only runs once)
\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
57 public Spin() : base(ViewBehaviour.RequiresKeyboardNavigationSupport | ViewBehaviour.DisableStyleChangeSignals)
\r
62 public override void OnInitialize()
\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
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
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
92 this.Add(_textField);
\r
94 _textField.FocusGained += TextFieldKeyInputFocusGained;
\r
95 _textField.FocusLost += TextFieldKeyInputFocusLost;
\r
98 public override Size GetNaturalSize()
\r
100 return new Size(150.0f, 150.0f, 0.0f);
\r
103 public void TextFieldKeyInputFocusGained(object source, EventArgs e)
\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
110 public void TextFieldKeyInputFocusLost(object source, EventArgs e)
\r
112 int previousValue = _currentValue;
\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
117 if (_currentValue < _minValue || _currentValue > _maxValue)
\r
119 _currentValue = previousValue;
\r
124 _currentValue = previousValue;
\r
127 // Otherwise take the new value
\r
128 this.Value = _currentValue;
\r
131 public override View GetNextFocusableView(View currentFocusedActor, View.FocusDirection direction, bool loopEnabled)
\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
137 this.Value += this.Step;
\r
138 nextFocusedActor = _textField;
\r
140 else if (direction == View.FocusDirection.Down)
\r
142 this.Value -= this.Step;
\r
143 nextFocusedActor = _textField;
\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
152 return nextFocusedActor;
\r
156 [ScriptableProperty()]
\r
161 return _currentValue;
\r
166 Console.WriteLine("Value set to " + value);
\r
167 _currentValue = value;
\r
169 // Make sure no invalid value is accepted
\r
170 if (_currentValue < _minValue)
\r
172 _currentValue = _minValue;
\r
175 if (_currentValue > _maxValue)
\r
177 _currentValue = _maxValue;
\r
180 _textField.Text = _currentValue.ToString();
\r
183 // MinValue property of type int:
\r
184 [ScriptableProperty()]
\r
185 public int MinValue
\r
197 // MaxValue property of type int:
\r
198 [ScriptableProperty()]
\r
199 public int MaxValue
\r
211 // Step property of type int:
\r
212 [ScriptableProperty()]
\r
217 return _singleStep;
\r
221 _singleStep = value;
\r
225 // WrappingEnabled property of type bool:
\r
226 [ScriptableProperty()]
\r
227 public bool WrappingEnabled
\r
231 return _wrappingEnabled;
\r
235 _wrappingEnabled = value;
\r
239 // TextPointSize property of type int:
\r
240 [ScriptableProperty()]
\r
241 public int TextPointSize
\r
249 _pointSize = value;
\r
250 _textField.PointSize = _pointSize;
\r
254 // TextColor property of type Color:
\r
255 [ScriptableProperty()]
\r
256 public Color TextColor
\r
264 Console.WriteLine("TextColor set to " + value.R + "," + value.G + "," + value.B);
\r
266 _textColor = value;
\r
267 _textField.TextColor = _textColor;
\r
271 // MaxTextLength property of type int:
\r
272 [ScriptableProperty()]
\r
273 public int MaxTextLength
\r
277 return _maxTextLength;
\r
281 _maxTextLength = value;
\r
282 _textField.MaxLength = _maxTextLength;
\r
286 public TextField SpinText
\r
294 _textField = value;
\r
298 // Indicator property of type string:
\r
299 public string IndicatorImage
\r
303 return _arrowImage;
\r
307 _arrowImage = value;
\r
308 _arrowVisual = VisualFactory.Get().CreateVisual(_arrowImage, new Uint16Pair(150, 150));
\r
309 RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual);
\r