[dali_1.2.30] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / views / spin.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using Dali;
21
22 // A spin control (for continously changing values when users can easily predict a set of values)
23
24 namespace Dali
25 {
26 public class Spin : CustomView
27   {
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;
43
44     // Called by DALi Builder if it finds a Spin control in a JSON file
45     static CustomView CreateInstance()
46     {
47       return new Spin();
48     }
49
50     // static constructor registers the control type (only runs once)
51     static Spin()
52     {
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) );
56     }
57
58     public Spin() : base(typeof(Spin).Name, ViewWrapperImpl.CustomViewBehaviour.REQUIRES_KEYBOARD_NAVIGATION_SUPPORT)
59     {
60
61     }
62
63     public override void OnInitialize()
64     {
65       // Initialize the propertiesControl
66       _arrowImage = "./images/arrow.png";
67       _textBackgroundColor = new Color(0.6f, 0.6f, 0.6f, 1.0f);
68       _currentValue = 0;
69       _minValue = 0;
70       _maxValue = 0;
71       _singleStep = 1;
72       _maxTextLength = 0;
73
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 );
78
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";
92
93       this.Add(_textField);
94
95       _textField.KeyInputFocusGained += TextFieldKeyInputFocusGained;
96       _textField.KeyInputFocusLost += TextFieldKeyInputFocusLost;
97     }
98
99     public override Vector3 GetNaturalSize()
100     {
101       return new Vector3(150.0f, 150.0f, 0.0f);
102     }
103
104     public void TextFieldKeyInputFocusGained(object source, KeyInputFocusGainedEventArgs e)
105     {
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);
109     }
110
111     public void TextFieldKeyInputFocusLost(object source, KeyInputFocusLostEventArgs e)
112     {
113       int previousValue = _currentValue;
114
115       // If the input value is invalid, change it back to the previous valid value
116       if(int.TryParse(_textField.Text, out _currentValue))
117       {
118         if (_currentValue < _minValue || _currentValue > _maxValue)
119         {
120           _currentValue = previousValue;
121         }
122       }
123       else
124       {
125         _currentValue = previousValue;
126       }
127
128       // Otherwise take the new value
129       this.Value = _currentValue;
130     }
131
132     public override Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, View.KeyboardFocus.Direction direction, bool loopEnabled)
133     {
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)
137       {
138         this.Value += this.Step;
139         nextFocusedActor = _textField;
140       }
141       else if (direction == View.KeyboardFocus.Direction.DOWN)
142       {
143         this.Value -= this.Step;
144         nextFocusedActor = _textField;
145       }
146       else
147       {
148         // Return a native empty handle as nothing can be focused in the left or right
149         nextFocusedActor = new Actor();
150         nextFocusedActor.Reset();
151       }
152
153       return nextFocusedActor;
154     }
155
156
157     [ScriptableProperty()]
158     public int Value
159     {
160       get
161       {
162         return _currentValue;
163       }
164       set
165       {
166
167         Console.WriteLine ("Value set to "  + value );
168         _currentValue = value;
169
170         // Make sure no invalid value is accepted
171         if (_currentValue < _minValue)
172         {
173           _currentValue = _minValue;
174         }
175
176         if (_currentValue > _maxValue)
177         {
178           _currentValue = _maxValue;
179         }
180
181         _textField.Text = _currentValue.ToString();
182       }
183     }
184     // MinValue property of type int:
185     [ScriptableProperty()]
186     public int MinValue
187     {
188       get
189       {
190         return _minValue;
191       }
192       set
193       {
194         _minValue = value;
195       }
196     }
197
198     // MaxValue property of type int:
199     [ScriptableProperty()]
200     public int MaxValue
201     {
202       get
203       {
204         return _maxValue;
205       }
206       set
207       {
208         _maxValue = value;
209       }
210     }
211
212     // Step property of type int:
213     [ScriptableProperty()]
214     public int Step
215     {
216       get
217       {
218         return _singleStep;
219       }
220       set
221       {
222         _singleStep = value;
223       }
224     }
225
226     // WrappingEnabled property of type bool:
227     [ScriptableProperty()]
228     public bool WrappingEnabled
229     {
230       get
231       {
232         return _wrappingEnabled;
233       }
234       set
235       {
236         _wrappingEnabled = value;
237       }
238     }
239
240     // TextPointSize property of type int:
241     [ScriptableProperty()]
242     public int TextPointSize
243     {
244       get
245       {
246         return _pointSize;
247       }
248       set
249       {
250         _pointSize = value;
251         _textField.PointSize = _pointSize;
252       }
253     }
254
255     // TextColor property of type Color:
256     [ScriptableProperty()]
257     public Color TextColor
258     {
259       get
260       {
261         return _textColor;
262       }
263       set
264       {
265           Console.WriteLine ("TextColor set to "  + value.R + "," + value.G + ","+ value.B);
266
267         _textColor = value;
268         _textField.TextColor = _textColor;
269       }
270     }
271
272     // MaxTextLength property of type int:
273     [ScriptableProperty()]
274     public int MaxTextLength
275     {
276       get
277       {
278         return _maxTextLength;
279       }
280       set
281       {
282         _maxTextLength = value;
283         _textField.MaxLength = _maxTextLength;
284       }
285     }
286
287     public TextField SpinText
288     {
289       get
290       {
291         return _textField;
292       }
293       set
294       {
295         _textField = value;
296       }
297     }
298
299     // Indicator property of type string:
300     public string IndicatorImage
301     {
302       get
303       {
304         return _arrowImage;
305       }
306       set
307       {
308         _arrowImage = value;
309         _arrowVisual =  VisualFactory.Get().CreateVisual( _arrowImage, new Uint16Pair(150, 150) );
310         RegisterVisual( _arrowVisualPropertyIndex, _arrowVisual );
311       }
312   }
313 }
314 }