Size2D get error fix
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.TizenTV / examples / view-navi-property.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 Tizen.NUI;
20 using Tizen.NUI.BaseComponents;
21
22 namespace MyCSharpExample
23 {
24     class Example : NUIApplication
25     {
26         const int num = 2;
27         View[] view;
28
29         View lastFocusedView;
30
31         protected override void OnCreate()
32         {
33             base.OnCreate();
34             Initialize();
35         }
36
37         public void Initialize()
38         {
39             view = new View[2];
40
41             for (int i = 0; i < num; i++)
42             {
43                 view[i] = new View();
44                 view[i].Size2D = new Size2D(200, 200);
45                 view[i].BackgroundColor = Color.Blue;
46                 view[i].Position = new Position(300 + i * 300, 300, 0);
47                 view[i].Name = "MyView" + i;
48                 view[i].Focusable = true;
49                 Window.Instance.Add(view[i]);
50                 view[i].FocusGained += FocusNavigationSample_FocusGained;
51                 view[i].FocusLost += FocusNavigationSample_FocusLost;
52                 view[i].Key += FocusNavigationSample_KeyEvent;
53             }
54
55             view[0].RightFocusableView = view[1];
56             view[0].LeftFocusableView = view[1];
57             view[1].RightFocusableView = view[0];
58             view[1].LeftFocusableView = view[0];
59
60             FocusManager.Instance.SetCurrentFocusView(view[0]);
61             FocusManager.Instance.PreFocusChange += Instance_PreFocusChange;
62
63             Window.Instance.Touched += Instance_Touch;
64         }
65
66         private void Instance_Touch(object sender, Window.TouchedEventArgs e)
67         {
68             Tizen.Log.Debug("NUI", "window touched! set key focus as view[0]!");
69             FocusManager.Instance.SetCurrentFocusView(view[0]);
70         }
71
72         private bool FocusNavigationSample_KeyEvent(object source, View.KeyEventArgs e)
73         {
74             Tizen.Log.Debug("NUI", "...");
75             View view = source as View;
76
77             Tizen.Log.Debug("NUI", "NUI-1 " + "View-" + view.Name + ", Pressed-" + e.Key.KeyPressedName + e.Key.State.ToString());
78
79             return false;
80         }
81
82         private void FocusNavigationSample_FocusLost(object sender, EventArgs e)
83         {
84             Tizen.Log.Debug("NUI", "...");
85             View view = sender as View;
86             view.BackgroundColor = Color.Blue;
87             view.Scale = new Vector3(1.0f, 1.0f, 1.0f);
88
89             Tizen.Log.Debug("NUI", "NUI-2 " + "FocusLost-" + view.Name);
90         }
91
92         private void FocusNavigationSample_FocusGained(object sender, EventArgs e)
93         {
94             Tizen.Log.Debug("NUI", "...");
95             View view = sender as View;
96             view.BackgroundColor = Color.Red;
97             view.Scale = new Vector3(1.2f, 1.2f, 1.0f);
98
99             Tizen.Log.Debug("NUI", "NUI-3 " + "FocusGained-" + view.Name);
100         }
101
102         private View Instance_PreFocusChange(object source, FocusManager.PreFocusChangeEventArgs e)
103         {
104             Tizen.Log.Debug("NUI", "...");
105             View currentView = (e.CurrentView) ?? lastFocusedView;
106             View nextView = null;
107
108             Tizen.Log.Debug("NUI", "NUI-4 " + "PreFocusChange-" + e.Direction);
109
110             if (currentView != null && currentView.HasBody())
111                 Tizen.Log.Debug("NUI", "NUI-5 " + " Current-" + currentView.Name);
112
113             if (currentView)
114             {
115                 switch (e.Direction)
116                 {
117                     case View.FocusDirection.Left:
118                         nextView = currentView.LeftFocusableView;
119                         if (nextView == null)
120                             Tizen.Log.Debug("NUI", "NUI-6 " + "LeftFocusableView is NULL!!!!");
121                         else
122                             Tizen.Log.Debug("NUI", "NUI-7 " + currentView.Name + ".LeftFocusableView =" + nextView.Name);
123                         break;
124                     case View.FocusDirection.Right:
125                         nextView = currentView.RightFocusableView;
126                         if (nextView == null)
127                             Tizen.Log.Debug("NUI", "NUI-8 " + "RightFocusableView is NULL!!!!");
128                         else
129                             Tizen.Log.Debug("NUI", "NUI-9 " + currentView.Name + ".RightFocusableView =" + nextView.Name);
130                         break;
131                     case View.FocusDirection.Up:
132                         nextView = currentView.UpFocusableView;
133                         if (nextView == null)
134                             Tizen.Log.Debug("NUI", "NUI-10 " + "UpFocusableView is NULL!!!!");
135                         else
136                             Tizen.Log.Debug("NUI", "NUI-11 " + currentView.Name + ".UpFocusableView =" + nextView.Name);
137                         break;
138                     case View.FocusDirection.Down:
139                         nextView = currentView.DownFocusableView;
140                         if (nextView == null)
141                             Tizen.Log.Debug("NUI", "NUI-12 " + "DownFocusableView is NULL!!!!");
142                         else
143                             Tizen.Log.Debug("NUI", "NUI-13 " + currentView.Name + ".DownFocusableView =" + nextView.Name);
144                         break;
145                     default:
146                         nextView = null;  //added
147                         break;
148                 }
149             }
150
151             if (e.ProposedView == null)
152             {
153                 Tizen.Log.Debug("NUI", "NUI-14 " + "ProposedView in NULL!!");
154             }
155             else if (e.ProposedView.HasBody())
156             {
157                 Tizen.Log.Debug("NUI", "NUI-15 " + "ProposedView-" + e.ProposedView.Name);
158             }
159             else
160             {
161                 Tizen.Log.Debug("NUI", "NUI-16 " + "ProposedView does NOT have body!!!" + e.ProposedView);
162             }
163
164             nextView = nextView ?? (e.ProposedView) ?? currentView;
165             lastFocusedView = nextView;
166
167             if (nextView != null && nextView.HasBody())
168                 Tizen.Log.Debug("NUI", "NUI-17 " + "Next-" + nextView.Name);
169
170             return nextView;
171         }
172
173         [STAThread]
174         static void _Main(string[] args)
175         {
176             Example example = new Example();
177             example.Run(args);
178         }
179     }
180 }