ade1b72dd0c38483b30e5485828a44e896ec7388
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / examples / date-picker-using-json.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 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-template.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# Spin object associated with the actor,
70             _spinYear = View.DownCast<Spin>( year );
71             _spinMonth = View.DownCast<Spin>( month );
72             _spinDay = View.DownCast<Spin>( day );
73
74             _spinYear.Value = 2099;
75             _spinMonth.Value = 5;
76             _spinDay.Value = 23;
77
78             _spinYear.SetKeyboardFocusable(true);
79             _spinMonth.SetKeyboardFocusable(true);
80             _spinDay.SetKeyboardFocusable(true);
81
82             FocusManager keyboardFocusManager = FocusManager.Instance;
83             keyboardFocusManager.PreFocusChange += OnKeyboardPreFocusChange;
84             keyboardFocusManager.FocusedActorEnterKeyPressed += OnFocusedActorEnterKeyPressed;
85
86         }
87
88         private Actor OnKeyboardPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
89         {
90             Actor nextFocusActor = e.Proposed;
91
92             // When nothing has been focused initially, focus the text field in the first spin
93             if (!e.Current && !e.Proposed)
94             {
95                 nextFocusActor = _spinYear.SpinText;
96             }
97             else if(e.Direction == View.KeyboardFocus.Direction.LEFT)
98             {
99                 // Move the focus to the spin in the left of the current focused spin
100                 if(e.Current == _spinMonth.SpinText)
101                 {
102                     nextFocusActor = _spinYear.SpinText;
103                 }
104                 else if(e.Current == _spinDay.SpinText)
105                 {
106                     nextFocusActor = _spinMonth.SpinText;
107                 }
108             }
109             else if(e.Direction == View.KeyboardFocus.Direction.RIGHT)
110             {
111                 // Move the focus to the spin in the right of the current focused spin
112                 if(e.Current == _spinYear.SpinText)
113                 {
114                     nextFocusActor = _spinMonth.SpinText;
115                 }
116                 else if(e.Current == _spinMonth.SpinText)
117                 {
118                     nextFocusActor = _spinDay.SpinText;
119                 }
120             }
121
122             return nextFocusActor;
123         }
124
125         private void OnFocusedActorEnterKeyPressed(object source, FocusManager.FocusedActorEnterKeyEventArgs e)
126         {
127             // Make the text field in the current focused spin to take the key input
128             KeyInputFocusManager manager = KeyInputFocusManager.Get();
129
130             if (e.Actor == _spinYear.SpinText)
131             {
132                 if (manager.GetCurrentFocusControl() != _spinYear.SpinText)
133                 {
134                     manager.SetFocus(_spinYear.SpinText);
135                 }
136             }
137             else if (e.Actor == _spinMonth.SpinText)
138             {
139                 if (manager.GetCurrentFocusControl() != _spinMonth.SpinText)
140                 {
141                     manager.SetFocus(_spinMonth.SpinText);
142                 }
143             }
144             else if (e.Actor == _spinDay.SpinText)
145             {
146                 if (manager.GetCurrentFocusControl() != _spinDay.SpinText)
147                 {
148                     manager.SetFocus(_spinDay.SpinText);
149                 }
150             }
151         }
152
153         public void MainLoop()
154         {
155             _application.MainLoop ();
156         }
157
158         /// <summary>
159         /// The main entry point for the application.
160         /// </summary>
161         [STAThread]
162         static void Main(string[] args)
163         {
164             Example example = new Example(Application.NewApplication());
165             example.MainLoop ();
166         }
167     }
168 }