dbc388c397faaefcdbaa759ef8fdae2aaf72f2e3
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / date-picker-using-json.cs
1 /*
2  * Copyright (c) 2016 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 Spin _spinYear;  // spin control for year
30         private Spin _spinMonth; // spin control for month
31         private Spin _spinDay;   // spin control for day
32         private Builder _builder; // DALi Builder
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             // load date JSON template...
47
48             _builder = new Builder ();
49
50             // Optional constant to see logging information coming out
51             // of DALi JSON parser (builder)
52             Property.Map constants = new  Property.Map();
53             constants.Insert( "CONFIG_SCRIPT_LOG_LEVEL",  new Property.Value( "Verbose") );
54             _builder.AddConstants( constants );
55
56             _builder.LoadFromFile( "./json/date-picker.json" );
57
58             // create the date-picker from the template in the json file
59             BaseHandle handle =  _builder.Create( "date-picker");
60
61             Actor actorTree =  Actor.DownCast( handle );
62
63             stage.Add( actorTree );
64
65             Actor year  = actorTree.FindChildByName("Year");
66             Actor month  =  actorTree.FindChildByName("Month" );
67             Actor day  = actorTree.FindChildByName("Day");
68
69             // need to get the actual C# View associated with the actor,
70             _spinYear = (Spin ) ViewRegistry.GetCustomViewFromActor( year );
71             _spinMonth = (Spin ) ViewRegistry.GetCustomViewFromActor( month );
72             _spinDay = (Spin ) ViewRegistry.GetCustomViewFromActor( day );
73
74             _spinYear.Value = 2099;
75             _spinMonth.Value = 5;
76             _spinDay.Value = 23;
77
78
79             _spinYear.SetKeyboardFocusable(true);
80             _spinMonth.SetKeyboardFocusable(true);
81             _spinDay.SetKeyboardFocusable(true);
82
83
84             FocusManager keyboardFocusManager = FocusManager.Instance;
85             keyboardFocusManager.PreFocusChange += OnKeyboardPreFocusChange;
86             keyboardFocusManager.FocusedActorEnterKeyPressed += OnFocusedActorEnterKeyPressed;
87
88         }
89
90         private Actor OnKeyboardPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
91         {
92             Actor nextFocusActor = e.Proposed;
93
94             // When nothing has been focused initially, focus the text field in the first spin
95             if (!e.Current && !e.Proposed)
96             {
97                 nextFocusActor = _spinYear.SpinText;
98             }
99             else if(e.Direction == View.KeyboardFocus.Direction.LEFT)
100             {
101                 // Move the focus to the spin in the left of the current focused spin
102                 if(e.Current == _spinMonth.SpinText)
103                 {
104                     nextFocusActor = _spinYear.SpinText;
105                 }
106                 else if(e.Current == _spinDay.SpinText)
107                 {
108                     nextFocusActor = _spinMonth.SpinText;
109                 }
110             }
111             else if(e.Direction == View.KeyboardFocus.Direction.RIGHT)
112             {
113                 // Move the focus to the spin in the right of the current focused spin
114                 if(e.Current == _spinYear.SpinText)
115                 {
116                     nextFocusActor = _spinMonth.SpinText;
117                 }
118                 else if(e.Current == _spinMonth.SpinText)
119                 {
120                     nextFocusActor = _spinDay.SpinText;
121                 }
122             }
123
124             return nextFocusActor;
125         }
126
127         private void OnFocusedActorEnterKeyPressed(object source, FocusManager.FocusedActorEnterKeyEventArgs e)
128         {
129             // Make the text field in the current focused spin to take the key input
130             KeyInputFocusManager manager = KeyInputFocusManager.Get();
131
132             if (e.Actor == _spinYear.SpinText)
133             {
134                 if (manager.GetCurrentFocusControl() != _spinYear.SpinText)
135                 {
136                     manager.SetFocus(_spinYear.SpinText);
137                 }
138             }
139             else if (e.Actor == _spinMonth.SpinText)
140             {
141                 if (manager.GetCurrentFocusControl() != _spinMonth.SpinText)
142                 {
143                     manager.SetFocus(_spinMonth.SpinText);
144                 }
145             }
146             else if (e.Actor == _spinDay.SpinText)
147             {
148                 if (manager.GetCurrentFocusControl() != _spinDay.SpinText)
149                 {
150                     manager.SetFocus(_spinDay.SpinText);
151                 }
152             }
153         }
154
155         public void MainLoop()
156         {
157             _application.MainLoop ();
158         }
159
160         /// <summary>
161         /// The main entry point for the application.
162         /// </summary>
163         [STAThread]
164         static void Main(string[] args)
165         {
166             Example example = new Example(Application.NewApplication());
167             example.MainLoop ();
168         }
169     }
170 }