/* * Copyright(c) 2017 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.Runtime.InteropServices; using Tizen.NUI; using Tizen.NUI.UIComponents; using Tizen.NUI.BaseComponents; // A spin control (for continously changing values when users can easily predict a set of values) namespace Tizen.NUI { /// ///Spins the CustomView class. /// public class Spin : CustomView { private VisualBase _arrowVisual; private TextField _textField; private int _arrowVisualPropertyIndex; private string _arrowImage; private int _currentValue; private int _minValue; private int _maxValue; private int _singleStep; private bool _wrappingEnabled; private int _pointSize; private Color _textColor; private Color _textBackgroundColor; private int _maxTextLength; // Called by DALi Builder if it finds a Spin control in a JSON file static CustomView CreateInstance() { return new Spin(); } // static constructor registers the control type (only runs once) static Spin() { // ViewRegistry registers control type with DALi type registery // also uses introspection to find any properties that need to be registered with type registry CustomViewRegistry.Instance.Register(CreateInstance, typeof(Spin)); } /// /// Creates an initialized spin. /// /// 3 public Spin() : base(typeof(Spin).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport) { } /// /// Overrides the method of OnInitialize() for the CustomView class.
/// This method is called after the control has been initialized.
/// Derived classes should do any second phase initialization by overriding this method.
///
/// 3 public override void OnInitialize() { // Initialize the propertiesControl //_arrowImage = "/home/tengxb/Workspace/nui-debug/examples/res/images/arrow.png"; _arrowImage = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "picture.png"; _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f); _currentValue = 0; _minValue = 0; _maxValue = 0; _singleStep = 1; _maxTextLength = 0; // Create image visual for the arrow keys _arrowVisualPropertyIndex = RegisterProperty("ArrowImage", new PropertyValue(_arrowImage), Tizen.NUI.PropertyAccessMode.ReadWrite); _arrowVisual = VisualFactory.Instance.CreateVisual( new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image)) .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage)) .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150)) .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150))); RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual); // Create a text field _textField = new TextField(); _textField.PivotPoint = Tizen.NUI.PivotPoint.Center; _textField.WidthResizePolicy = ResizePolicyType.SizeRelativeToParent; _textField.HeightResizePolicy = ResizePolicyType.SizeRelativeToParent; _textField.SizeModeFactor = new Vector3(1.0f, 0.45f, 1.0f); _textField.PlaceholderText = "----"; _textField.BackgroundColor = _textBackgroundColor; _textField.HorizontalAlignment = HorizontalAlignment.Center; _textField.VerticalAlignment = VerticalAlignment.Center; _textField.Focusable = (true); _textField.Name = "_textField"; _textField.Position2D = new Position2D(0, 40); this.Add(_textField); _textField.FocusGained += TextFieldKeyInputFocusGained; _textField.FocusLost += TextFieldKeyInputFocusLost; } /// /// Overrides the method of GetNaturalSize() for the CustomView class.
/// Returns the natural size of the actor.
///
/// Natural size of this spin itself. /// 3 public override Size2D GetNaturalSize() { return new Size2D(150, 150); } /// /// An event handler is used when the TextField in the spin gets the key focus.
/// Make sure when the current spin that takes input focus, also takes the keyboard focus.
/// For example, when you tap the spin directly.
///
/// Sender of this event. /// Event arguments. /// 3 public void TextFieldKeyInputFocusGained(object source, EventArgs e) { FocusManager.Instance.SetCurrentFocusView(_textField); } /// /// An event handler when the TextField in the spin looses it's key focus. /// /// /// /// 3 public void TextFieldKeyInputFocusLost(object source, EventArgs e) { int previousValue = _currentValue; // If the input value is invalid, change it back to the previous valid value if (int.TryParse(_textField.Text, out _currentValue)) { if (_currentValue < _minValue || _currentValue > _maxValue) { _currentValue = previousValue; } } else { _currentValue = previousValue; } // Otherwise take the new value this.Value = _currentValue; } /// /// Overrides the method of GetNextKeyboardFocusableView() for the CustomView class.
/// Gets the next key focusable view in this view towards the given direction.
/// A view needs to override this function in order to support two-dimensional key navigation.
///
/// The current focused view. /// The direction to move the focus towards. /// Whether the focus movement should be looped within the control. /// The next keyboard focusable view in this control or an empty handle if no view can be focused. /// 3 public override View GetNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled) { // Respond to Up/Down keys to change the value while keeping the current spin focused View nextFocusedView = currentFocusedView; if (direction == View.FocusDirection.Up) { this.Value += this.Step; nextFocusedView = _textField; } else if (direction == View.FocusDirection.Down) { this.Value -= this.Step; nextFocusedView = _textField; } else { // Return null return null; } return nextFocusedView; } /// /// Value to be set in the spin. /// /// 3 [ScriptableProperty()] public int Value { get { return _currentValue; } set { Tizen.Log.Debug("NUI", "Value set to " + value); _currentValue = value; // Make sure no invalid value is accepted if (_currentValue < _minValue) { _currentValue = _minValue; } if (_currentValue > _maxValue) { _currentValue = _maxValue; } _textField.Text = _currentValue.ToString(); } } /// /// Minimum value of the spin value. /// /// 3 // MinValue property of type int: [ScriptableProperty()] public int MinValue { get { return _minValue; } set { _minValue = value; } } /// /// Maximum value of the spin value. /// /// 3 // MaxValue property of type int: [ScriptableProperty()] public int MaxValue { get { return _maxValue; } set { _maxValue = value; } } /// /// Increasing, decreasing step of the spin value when up or down keys are pressed. /// /// 3 // Step property of type int: [ScriptableProperty()] public int Step { get { return _singleStep; } set { _singleStep = value; } } /// /// Wrapping enabled status. /// /// 3 // WrappingEnabled property of type bool: [ScriptableProperty()] public bool WrappingEnabled { get { return _wrappingEnabled; } set { _wrappingEnabled = value; } } /// /// Text point size of the spin value. /// /// 3 // TextPointSize property of type int: [ScriptableProperty()] public int TextPointSize { get { return _pointSize; } set { _pointSize = value; _textField.PointSize = _pointSize; } } /// /// The color of the spin value. /// /// 3 // TextColor property of type Color: [ScriptableProperty()] public Color TextColor { get { return _textColor; } set { Tizen.Log.Debug("NUI", "TextColor set to " + value.R + "," + value.G + "," + value.B); _textColor = value; _textField.TextColor = _textColor; } } /// /// Maximum text lengh of the spin value. /// /// 3 // MaxTextLength property of type int: [ScriptableProperty()] public int MaxTextLength { get { return _maxTextLength; } set { _maxTextLength = value; _textField.MaxLength = _maxTextLength; } } /// /// Reference of TextField of the spin. /// /// 3 public TextField SpinText { get { return _textField; } set { _textField = value; } } /// /// Show indicator image, for example, up or down arrow image. /// /// 3 public string IndicatorImage { get { return _arrowImage; } set { _arrowImage = value; _arrowVisual = VisualFactory.Instance.CreateVisual( new PropertyMap().Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image)) .Add(ImageVisualProperty.URL, new PropertyValue(_arrowImage)) .Add(ImageVisualProperty.DesiredHeight, new PropertyValue(150)) .Add(ImageVisualProperty.DesiredWidth, new PropertyValue(150))); RegisterVisual(_arrowVisualPropertyIndex, _arrowVisual); } } } }