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
28 private VisualBase _arrowVisual;
29 private TextField _textField;
30 private int _arrowVisualPropertyIndex;
31 private string _arrowImage;
32 private int _currentValue;
33 private int _minValue;
34 private int _maxValue;
35 private int _singleStep;
36 private bool _wrappingEnabled;
37 private string _fontFamily;
38 private string _fontStyle;
39 private int _pointSize;
40 private Color _textColor;
41 private Color _textBackgroundColor;
42 private int _maxTextLength;
44 // Called by DALi Builder if it finds a Spin control in a JSON file
45 static CustomView CreateInstance()
50 // static constructor registers the control type (only runs once)
53 // ViewRegistry registers control type with DALi type registery
54 // also uses introspection to find any properties that need to be registered with type registry
55 ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
58 public Spin() : base(typeof(Spin).Name, ViewWrapperImpl.CustomViewBehaviour.REQUIRES_KEYBOARD_NAVIGATION_SUPPORT | ViewWrapperImpl.CustomViewBehaviour.DISABLE_STYLE_CHANGE_SIGNALS)
62 public override void OnInitialize()
64 //Console.WriteLine ("[Spin]OnInitialize()!");
66 // Initialize the propertiesControl
67 _arrowImage = "/home/owner/apps_rw/NUISamples.TizenTV/res/images/arrow.png";
68 _textBackgroundColor = new Color(1.0f, 1.0f, 0.6f, 1.0f);
75 // Create image visual for the arrow keys
76 _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new Tizen.NUI.Property.Value(_arrowImage), Tizen.NUI.Property.AccessMode.READ_WRITE);
77 _arrowVisual = VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
78 RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );
80 // Create a text field
81 _textField = new TextField();
82 _textField.ParentOrigin = NDalic.ParentOriginCenter;
83 _textField.AnchorPoint = NDalic.AnchorPointCenter;
84 _textField.WidthResizePolicy = "SIZE_RELATIVE_TO_PARENT";
85 _textField.HeightResizePolicy = "SIZE_RELATIVE_TO_PARENT";
86 _textField.SizeModeFactor = new Vector3( 1.0f, 0.45f, 1.0f );
87 _textField.PlaceholderText = "----";
88 _textField.BackgroundColor = _textBackgroundColor;
89 _textField.HorizontalAlignment = "Center";
90 _textField.VerticalAlignment = "Center";
91 _textField.SetKeyboardFocusable(true);
92 _textField.Name = "_textField";
96 _textField.KeyInputFocusGained += TextFieldKeyInputFocusGained;
97 _textField.KeyInputFocusLost += TextFieldKeyInputFocusLost;
100 public override Vector3 GetNaturalSize()
102 //Console.WriteLine ("[Spin]GetNaturalSize()!");
104 return new Vector3(150.0f, 150.0f, 0.0f);
107 public void TextFieldKeyInputFocusGained(object source, KeyInputFocusGainedEventArgs e)
109 //Console.WriteLine ("[Spin]TextFieldKeyInputFocusGained()!");
111 // Make sure when the current spin that takes input focus also takes the keyboard focus
112 // For example, when you tap the spin directly
113 FocusManager.Instance.SetCurrentFocusActor(_textField);
116 public void TextFieldKeyInputFocusLost(object source, KeyInputFocusLostEventArgs e)
118 //Console.WriteLine ("[Spin]TextFieldKeyInputFocusLost()!");
119 int previousValue = _currentValue;
121 // If the input value is invalid, change it back to the previous valid value
122 if(int.TryParse(_textField.Text, out _currentValue))
124 if (_currentValue < _minValue || _currentValue > _maxValue)
126 _currentValue = previousValue;
131 _currentValue = previousValue;
134 // Otherwise take the new value
135 this.Value = _currentValue;
138 public override Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, View.KeyboardFocus.Direction direction, bool loopEnabled)
140 // Respond to Up/Down keys to change the value while keeping the current spin focused
141 //Console.WriteLine ("[Spin]GetNextKeyboardFocusableActor()!");
143 Actor nextFocusedActor = currentFocusedActor;
144 if (direction == View.KeyboardFocus.Direction.UP)
146 this.Value += this.Step;
147 nextFocusedActor = _textField;
149 else if (direction == View.KeyboardFocus.Direction.DOWN)
151 this.Value -= this.Step;
152 nextFocusedActor = _textField;
156 // Return a native empty handle as nothing can be focused in the left or right
157 nextFocusedActor = new Actor();
158 nextFocusedActor.Reset();
161 return nextFocusedActor;
165 [ScriptableProperty()]
170 return _currentValue;
175 //Console.WriteLine ("[Spin]Value set to " + value );
176 _currentValue = value;
178 // Make sure no invalid value is accepted
179 if (_currentValue < _minValue)
181 _currentValue = _minValue;
184 if (_currentValue > _maxValue)
186 _currentValue = _maxValue;
189 _textField.Text = _currentValue.ToString();
192 // MinValue property of type int:
193 [ScriptableProperty()]
206 // MaxValue property of type int:
207 [ScriptableProperty()]
220 // Step property of type int:
221 [ScriptableProperty()]
234 // WrappingEnabled property of type bool:
235 [ScriptableProperty()]
236 public bool WrappingEnabled
240 return _wrappingEnabled;
244 _wrappingEnabled = value;
248 // TextPointSize property of type int:
249 [ScriptableProperty()]
250 public int TextPointSize
259 _textField.PointSize = _pointSize;
263 // TextColor property of type Color:
264 [ScriptableProperty()]
265 public Color TextColor
273 //Console.WriteLine ("[Spin]TextColor set to " + value.R + "," + value.G + ","+ value.B);
276 _textField.TextColor = _textColor;
280 // MaxTextLength property of type int:
281 [ScriptableProperty()]
282 public int MaxTextLength
286 return _maxTextLength;
290 _maxTextLength = value;
291 _textField.MaxLength = _maxTextLength;
295 public TextField SpinText
307 // Indicator property of type string:
308 public string IndicatorImage
317 _arrowVisual = VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
318 RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );