Ensure text-controller returns false when it doesn't use key.
[platform/core/uifw/dali-toolkit.git] / plugins / dali-sharp / examples / date-picker.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 namespace MyCSharpExample
23 {
24     // A spin control (for continously changing values when users can easily predict a set of values)
25
26     class Example
27     {
28         private Dali.Application _application;
29         private FlexContainer _container;   // Flex container to hold spin controls
30         private Spin _spinYear;  // spin control for year
31         private Spin _spinMonth; // spin control for month
32         private Spin _spinDay;   // spin control for day
33
34         public Example(Dali.Application application)
35         {
36             _application = application;
37             _application.Initialized += Initialize;
38         }
39
40         public void Initialize(object source, NUIApplicationInitEventArgs e)
41         {
42
43             Stage stage = Stage.GetCurrent();
44             stage.BackgroundColor = Color.White;
45
46             // Create a container for the spins
47             _container = new FlexContainer();
48
49             _container.ParentOrigin = NDalic.ParentOriginCenter;
50             _container.AnchorPoint = NDalic.AnchorPointCenter;
51             _container.FlexDirection = (int)FlexContainer.FlexDirectionType.ROW;
52             _container.Size = new Vector3(480.0f, 150.0f, 0.0f);
53
54             stage.Add(_container);
55
56             // Create a Spin control for year
57             _spinYear = new Spin();
58             _spinYear.ParentOrigin = NDalic.ParentOriginCenter;
59             _spinYear.AnchorPoint = NDalic.AnchorPointCenter;
60             _spinYear.Flex = 0.3f;
61             _spinYear.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
62             _container.Add(_spinYear);
63
64             _spinYear.MinValue = 1900;
65             _spinYear.MaxValue = 2100;
66             _spinYear.Value = 2016;
67             _spinYear.Step = 1;
68             _spinYear.MaxTextLength = 4;
69             _spinYear.TextPointSize = 26;
70             _spinYear.TextColor = Color.White;
71             _spinYear.SetFocusable(true);
72             _spinYear.Name = "_spinYear";
73
74             // Create a Spin control for month
75             _spinMonth = new Spin();
76             _spinMonth.ParentOrigin = NDalic.ParentOriginCenter;
77             _spinMonth.AnchorPoint = NDalic.AnchorPointCenter;
78             _spinMonth.Flex = 0.3f;
79             _spinMonth.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
80             _container.Add(_spinMonth);
81
82             _spinMonth.MinValue = 1;
83             _spinMonth.MaxValue = 12;
84             _spinMonth.Value = 10;
85             _spinMonth.Step = 1;
86             _spinMonth.MaxTextLength = 2;
87             _spinMonth.TextPointSize = 26;
88             _spinMonth.TextColor = Color.White;
89             _spinMonth.SetFocusable(true);
90             _spinMonth.Name = "_spinMonth";
91
92             // Create a Spin control for day
93             _spinDay = new Spin();
94             _spinDay.ParentOrigin = NDalic.ParentOriginCenter;
95             _spinDay.AnchorPoint = NDalic.AnchorPointCenter;
96             _spinDay.Flex = 0.3f;
97             _spinDay.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
98             _container.Add(_spinDay);
99
100             _spinDay.MinValue = 1;
101             _spinDay.MaxValue = 31;
102             _spinDay.Value = 26;
103             _spinDay.Step = 1;
104             _spinDay.MaxTextLength = 2;
105             _spinDay.TextPointSize = 26;
106             _spinDay.TextColor = Color.White;
107             _spinDay.SetKeyboardFocusable(true);
108             _spinDay.Name = "_spinDay";
109
110             FocusManager FocusManager = FocusManager.Instance;
111             FocusManager.PreFocusChange += OnPreFocusChange;
112             FocusManager.FocusedActorEnterKeyPressed += OnFocusedActorEnterKeyPressed;
113
114         }
115
116         private Actor OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
117         {
118             Actor nextFocusActor = e.Proposed;
119
120             // When nothing has been focused initially, focus the text field in the first spin
121             if (!e.Current && !e.Proposed)
122             {
123                 nextFocusActor = _spinYear.SpinText;
124             }
125             else if(e.Direction == View.Focus.Direction.LEFT)
126             {
127                 // Move the focus to the spin in the left of the current focused spin
128                 if(e.Current == _spinMonth.SpinText)
129                 {
130                     nextFocusActor = _spinYear.SpinText;
131                 }
132                 else if(e.Current == _spinDay.SpinText)
133                 {
134                     nextFocusActor = _spinMonth.SpinText;
135                 }
136             }
137             else if(e.Direction == View.Focus.Direction.RIGHT)
138             {
139                 // Move the focus to the spin in the right of the current focused spin
140                 if(e.Current == _spinYear.SpinText)
141                 {
142                     nextFocusActor = _spinMonth.SpinText;
143                 }
144                 else if(e.Current == _spinMonth.SpinText)
145                 {
146                     nextFocusActor = _spinDay.SpinText;
147                 }
148             }
149
150             return nextFocusActor;
151         }
152
153         private void OnFocusedActorEnterKeyPressed(object source, FocusManager.FocusedActorEnterKeyEventArgs e)
154         {
155             // Make the text field in the current focused spin to take the key input
156             KeyInputFocusManager manager = KeyInputFocusManager.Get();
157
158             if (e.Actor == _spinYear.SpinText)
159             {
160                 if (manager.GetCurrentFocusControl() != _spinYear.SpinText)
161                 {
162                     manager.SetFocus(_spinYear.SpinText);
163                 }
164             }
165             else if (e.Actor == _spinMonth.SpinText)
166             {
167                 if (manager.GetCurrentFocusControl() != _spinMonth.SpinText)
168                 {
169                     manager.SetFocus(_spinMonth.SpinText);
170                 }
171             }
172             else if (e.Actor == _spinDay.SpinText)
173             {
174                 if (manager.GetCurrentFocusControl() != _spinDay.SpinText)
175                 {
176                     manager.SetFocus(_spinDay.SpinText);
177                 }
178             }
179         }
180
181         public void MainLoop()
182         {
183             _application.MainLoop ();
184         }
185
186         /// <summary>
187         /// The main entry point for the application.
188         /// </summary>
189         [STAThread]
190         static void Main(string[] args)
191         {
192             Example example = new Example(Application.NewApplication());
193             example.MainLoop ();
194         }
195     }
196 }