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