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)
63 public override void OnInitialize()
65 // Initialize the propertiesControl
66 _arrowImage = "./images/arrow.png";
67 _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f);
74 // Create image visual for the arrow keys
75 _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new Dali.Property.Value(_arrowImage), Dali.Property.AccessMode.READ_WRITE);
76 _arrowVisual = VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
77 RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );
79 // Create a text field
80 _textField = new TextField();
81 _textField.ParentOrigin = NDalic.ParentOriginCenter;
82 _textField.AnchorPoint = NDalic.AnchorPointCenter;
83 _textField.WidthResizePolicy = "SIZE_RELATIVE_TO_PARENT";
84 _textField.HeightResizePolicy = "SIZE_RELATIVE_TO_PARENT";
85 _textField.SizeModeFactor = new Vector3( 1.0f, 0.45f, 1.0f );
86 _textField.PlaceholderText = "----";
87 _textField.BackgroundColor = _textBackgroundColor;
88 _textField.HorizontalAlignment = "Center";
89 _textField.VerticalAlignment = "Center";
90 _textField.SetKeyboardFocusable(true);
91 _textField.Name = "_textField";
95 _textField.KeyInputFocusGained += TextFieldKeyInputFocusGained;
96 _textField.KeyInputFocusLost += TextFieldKeyInputFocusLost;
99 public override Vector3 GetNaturalSize()
101 return new Vector3(150.0f, 150.0f, 0.0f);
104 public void TextFieldKeyInputFocusGained(object source, KeyInputFocusGainedEventArgs e)
106 // Make sure when the current spin that takes input focus also takes the keyboard focus
107 // For example, when you tap the spin directly
108 FocusManager.Instance.SetCurrentFocusActor(_textField);
111 public void TextFieldKeyInputFocusLost(object source, KeyInputFocusLostEventArgs e)
113 int previousValue = _currentValue;
115 // If the input value is invalid, change it back to the previous valid value
116 if(int.TryParse(_textField.Text, out _currentValue))
118 if (_currentValue < _minValue || _currentValue > _maxValue)
120 _currentValue = previousValue;
125 _currentValue = previousValue;
128 // Otherwise take the new value
129 this.Value = _currentValue;
132 public override Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, View.KeyboardFocus.Direction direction, bool loopEnabled)
134 // Respond to Up/Down keys to change the value while keeping the current spin focused
135 Actor nextFocusedActor = currentFocusedActor;
136 if (direction == View.KeyboardFocus.Direction.UP)
138 this.Value += this.Step;
139 nextFocusedActor = _textField;
141 else if (direction == View.KeyboardFocus.Direction.DOWN)
143 this.Value -= this.Step;
144 nextFocusedActor = _textField;
148 // Return a native empty handle as nothing can be focused in the left or right
149 nextFocusedActor = new Actor();
150 nextFocusedActor.Reset();
153 return nextFocusedActor;
157 [ScriptableProperty()]
162 return _currentValue;
167 Console.WriteLine ("Value set to " + value );
168 _currentValue = value;
170 // Make sure no invalid value is accepted
171 if (_currentValue < _minValue)
173 _currentValue = _minValue;
176 if (_currentValue > _maxValue)
178 _currentValue = _maxValue;
181 _textField.Text = _currentValue.ToString();
184 // MinValue property of type int:
185 [ScriptableProperty()]
198 // MaxValue property of type int:
199 [ScriptableProperty()]
212 // Step property of type int:
213 [ScriptableProperty()]
226 // WrappingEnabled property of type bool:
227 [ScriptableProperty()]
228 public bool WrappingEnabled
232 return _wrappingEnabled;
236 _wrappingEnabled = value;
240 // TextPointSize property of type int:
241 [ScriptableProperty()]
242 public int TextPointSize
251 _textField.PointSize = _pointSize;
255 // TextColor property of type Color:
256 [ScriptableProperty()]
257 public Color TextColor
265 Console.WriteLine ("TextColor set to " + value.R + "," + value.G + ","+ value.B);
268 _textField.TextColor = _textColor;
272 // MaxTextLength property of type int:
273 [ScriptableProperty()]
274 public int MaxTextLength
278 return _maxTextLength;
282 _maxTextLength = value;
283 _textField.MaxLength = _maxTextLength;
287 public TextField SpinText
299 // Indicator property of type string:
300 public string IndicatorImage
309 _arrowVisual = VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
310 RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );