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