2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Runtime.InteropServices;
22 namespace MyCSharpExample
24 // A spin control (for continously changing values when users can easily predict a set of values)
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
34 public Example(Dali.Application application)
36 _application = application;
37 _application.Initialized += Initialize;
40 public void Initialize(object source, NUIApplicationInitEventArgs e)
43 Stage stage = Stage.GetCurrent();
44 stage.BackgroundColor = Color.White;
46 // Create a container for the spins
47 _container = new FlexContainer();
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);
54 stage.Add(_container);
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);
64 _spinYear.MinValue = 1900;
65 _spinYear.MaxValue = 2100;
66 _spinYear.Value = 2016;
68 _spinYear.MaxTextLength = 4;
69 _spinYear.TextPointSize = 26;
70 _spinYear.TextColor = Color.White;
71 _spinYear.SetFocusable(true);
72 _spinYear.Name = "_spinYear";
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);
82 _spinMonth.MinValue = 1;
83 _spinMonth.MaxValue = 12;
84 _spinMonth.Value = 10;
86 _spinMonth.MaxTextLength = 2;
87 _spinMonth.TextPointSize = 26;
88 _spinMonth.TextColor = Color.White;
89 _spinMonth.SetFocusable(true);
90 _spinMonth.Name = "_spinMonth";
92 // Create a Spin control for day
93 _spinDay = new Spin();
94 _spinDay.ParentOrigin = NDalic.ParentOriginCenter;
95 _spinDay.AnchorPoint = NDalic.AnchorPointCenter;
97 _spinDay.FlexMargin = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
98 _container.Add(_spinDay);
100 _spinDay.MinValue = 1;
101 _spinDay.MaxValue = 31;
104 _spinDay.MaxTextLength = 2;
105 _spinDay.TextPointSize = 26;
106 _spinDay.TextColor = Color.White;
107 _spinDay.SetKeyboardFocusable(true);
108 _spinDay.Name = "_spinDay";
110 FocusManager FocusManager = FocusManager.Instance;
111 FocusManager.PreFocusChange += OnPreFocusChange;
112 FocusManager.FocusedActorEnterKeyPressed += OnFocusedActorEnterKeyPressed;
116 private Actor OnPreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
118 Actor nextFocusActor = e.Proposed;
120 // When nothing has been focused initially, focus the text field in the first spin
121 if (!e.Current && !e.Proposed)
123 nextFocusActor = _spinYear.SpinText;
125 else if(e.Direction == View.Focus.Direction.LEFT)
127 // Move the focus to the spin in the left of the current focused spin
128 if(e.Current == _spinMonth.SpinText)
130 nextFocusActor = _spinYear.SpinText;
132 else if(e.Current == _spinDay.SpinText)
134 nextFocusActor = _spinMonth.SpinText;
137 else if(e.Direction == View.Focus.Direction.RIGHT)
139 // Move the focus to the spin in the right of the current focused spin
140 if(e.Current == _spinYear.SpinText)
142 nextFocusActor = _spinMonth.SpinText;
144 else if(e.Current == _spinMonth.SpinText)
146 nextFocusActor = _spinDay.SpinText;
150 return nextFocusActor;
153 private void OnFocusedActorEnterKeyPressed(object source, FocusManager.FocusedActorEnterKeyEventArgs e)
155 // Make the text field in the current focused spin to take the key input
156 KeyInputFocusManager manager = KeyInputFocusManager.Get();
158 if (e.Actor == _spinYear.SpinText)
160 if (manager.GetCurrentFocusControl() != _spinYear.SpinText)
162 manager.SetFocus(_spinYear.SpinText);
165 else if (e.Actor == _spinMonth.SpinText)
167 if (manager.GetCurrentFocusControl() != _spinMonth.SpinText)
169 manager.SetFocus(_spinMonth.SpinText);
172 else if (e.Actor == _spinDay.SpinText)
174 if (manager.GetCurrentFocusControl() != _spinDay.SpinText)
176 manager.SetFocus(_spinDay.SpinText);
181 public void MainLoop()
183 _application.MainLoop ();
187 /// The main entry point for the application.
190 static void Main(string[] args)
192 Example example = new Example(Application.NewApplication());