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("Spin", CreateInstance, typeof(Spin) );
57 public Spin() : base(ViewWrapperImpl.CustomViewBehaviour.REQUIRES_KEYBOARD_NAVIGATION_SUPPORT | ViewWrapperImpl.CustomViewBehaviour.DISABLE_STYLE_CHANGE_SIGNALS)
62 public override void OnInitialize()
64 // Initialize the propertiesControl
65 _arrowImage = "./images/arrow.png";
66 _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f);
73 // Create image visual for the arrow keys
74 _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new Dali.Property.Value(_arrowImage), Dali.Property.AccessMode.READ_WRITE);
75 _arrowVisual = VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
76 RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );
78 // Create a text field
79 _textField = new TextField();
80 _textField.ParentOrigin = NDalic.ParentOriginCenter;
81 _textField.AnchorPoint = NDalic.AnchorPointCenter;
82 _textField.WidthResizePolicy = "SIZE_RELATIVE_TO_PARENT";
83 _textField.HeightResizePolicy = "SIZE_RELATIVE_TO_PARENT";
84 _textField.SizeModeFactor = new Vector3( 1.0f, 0.45f, 1.0f );
85 _textField.PlaceholderText = "----";
86 _textField.BackgroundColor = _textBackgroundColor;
87 _textField.HorizontalAlignment = "Center";
88 _textField.VerticalAlignment = "Center";
89 _textField.SetKeyboardFocusable(true);
90 _textField.Name = "_textField";
94 _textField.KeyInputFocusGained += TextFieldKeyInputFocusGained;
95 _textField.KeyInputFocusLost += TextFieldKeyInputFocusLost;
98 public override Vector3 GetNaturalSize()
100 return new Vector3(150.0f, 150.0f, 0.0f);
103 public void TextFieldKeyInputFocusGained(object source, KeyInputFocusGainedEventArgs e)
105 // Make sure when the current spin that takes input focus also takes the keyboard focus
106 // For example, when you tap the spin directly
107 FocusManager.Instance.SetCurrentFocusActor(_textField);
110 public void TextFieldKeyInputFocusLost(object source, KeyInputFocusLostEventArgs e)
112 int previousValue = _currentValue;
114 // If the input value is invalid, change it back to the previous valid value
115 if(int.TryParse(_textField.Text, out _currentValue))
117 if (_currentValue < _minValue || _currentValue > _maxValue)
119 _currentValue = previousValue;
124 _currentValue = previousValue;
127 // Otherwise take the new value
128 this.Value = _currentValue;
131 public override Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, View.KeyboardFocus.Direction direction, bool loopEnabled)
133 // Respond to Up/Down keys to change the value while keeping the current spin focused
134 Actor nextFocusedActor = currentFocusedActor;
135 if (direction == View.KeyboardFocus.Direction.UP)
137 this.Value += this.Step;
138 nextFocusedActor = _textField;
140 else if (direction == View.KeyboardFocus.Direction.DOWN)
142 this.Value -= this.Step;
143 nextFocusedActor = _textField;
147 // Return a native empty handle as nothing can be focused in the left or right
148 nextFocusedActor = new Actor();
149 nextFocusedActor.Reset();
152 return nextFocusedActor;
156 [ScriptableProperty()]
161 return _currentValue;
166 Console.WriteLine ("Value set to " + value );
167 _currentValue = value;
169 // Make sure no invalid value is accepted
170 if (_currentValue < _minValue)
172 _currentValue = _minValue;
175 if (_currentValue > _maxValue)
177 _currentValue = _maxValue;
180 _textField.Text = _currentValue.ToString();
183 // MinValue property of type int:
184 [ScriptableProperty()]
197 // MaxValue property of type int:
198 [ScriptableProperty()]
211 // Step property of type int:
212 [ScriptableProperty()]
225 // WrappingEnabled property of type bool:
226 [ScriptableProperty()]
227 public bool WrappingEnabled
231 return _wrappingEnabled;
235 _wrappingEnabled = value;
239 // TextPointSize property of type int:
240 [ScriptableProperty()]
241 public int TextPointSize
250 _textField.PointSize = _pointSize;
254 // TextColor property of type Color:
255 [ScriptableProperty()]
256 public Color TextColor
264 Console.WriteLine ("TextColor set to " + value.R + "," + value.G + ","+ value.B);
267 _textField.TextColor = _textColor;
271 // MaxTextLength property of type int:
272 [ScriptableProperty()]
273 public int MaxTextLength
277 return _maxTextLength;
281 _maxTextLength = value;
282 _textField.MaxLength = _maxTextLength;
286 public TextField SpinText
298 // Indicator property of type string:
299 public string IndicatorImage
308 _arrowVisual = VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
309 RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );