[Tizen] temporal API to make users use Container class
[platform/core/csapi/nui.git] / NUISamples / examples / visual-view-test2.cs
1 using System;
2 using Tizen.NUI;
3 using Tizen.NUI.UIComponents;
4 using Tizen.NUI.BaseComponents;
5
6 namespace VisualViewTest2
7 {
8     public class VisualSample : NUIApplication
9     {
10         const int num = 2;
11         VisualView[] view;
12
13         TextLabel guide;
14         ImageVisual imageMap;
15         ImageVisual imageMap2;
16
17         ImageVisual highlightImageMap;
18         ImageVisual dimImageMap;
19
20         TextVisual textMap1;
21         TextVisual textMap2;
22
23         Window _window;
24
25         int imgIndex;
26
27         readonly string resourcePath = "/home/owner/apps_rw/NUISamples.TizenTV/res/images/";
28
29         protected override void OnCreate()
30         {
31             base.OnCreate();
32
33             view = new VisualView[2];
34
35             for (int i = 0; i < num; i++)
36             {
37                 view[i] = new VisualView();
38                 view[i].Size2D = new Size2D(600, 600);
39                 view[i].BackgroundColor = Color.Blue;
40                 view[i].Position = new Position(400 + i * 800, 600, 0);
41                 view[i].Focusable = true;
42                 view[i].Name = "MyView" + i;
43                 Window.Instance.Add(view[i]);
44                 view[i].FocusGained += VisualSample_FocusGained;
45                 view[i].FocusLost += VisualSample_FocusLost;
46                 view[i].KeyEvent += VisualSample_KeyEvent;
47             }
48
49             view[0].RightFocusableView = view[1];
50             view[1].LeftFocusableView = view[0];
51
52             imageMap = new ImageVisual();
53             imageMap.URL = resourcePath + "gallery-" + imgIndex++ + ".jpg";
54             imageMap.AnchorPoint = Visual.AlignType.TopBegin;
55             imageMap.Origin = Visual.AlignType.TopBegin;
56             imageMap.Position = new Vector2(0, 0);
57             imageMap.PositionPolicy = VisualTransformPolicyType.Absolute;
58             imageMap.Size = new Size2D(500, 500);
59             imageMap.SizePolicy = VisualTransformPolicyType.Absolute;
60             imageMap.DepthIndex = 0;
61             view[0].AddVisual("bgVisual", imageMap);
62
63
64             highlightImageMap = new ImageVisual();
65             highlightImageMap.URL = resourcePath + "star-highlight.png";
66             highlightImageMap.AnchorPoint = Visual.AlignType.TopBegin;
67             highlightImageMap.Origin = Visual.AlignType.TopBegin;
68             highlightImageMap.Size = new Vector2(40, 40);
69             highlightImageMap.SizePolicy = VisualTransformPolicyType.Absolute;
70             highlightImageMap.Position = new Vector2(10, 10);
71             highlightImageMap.PositionPolicy = VisualTransformPolicyType.Absolute;
72             highlightImageMap.DepthIndex = 1;
73             view[0].AddVisual("iconVisual", highlightImageMap);
74
75
76             textMap1 = new TextVisual();
77             textMap1.Text = "Hello";
78             textMap1.AnchorPoint = Visual.AlignType.TopBegin;
79             textMap1.Origin = Visual.AlignType.TopBegin;
80             textMap1.PointSize = 20;
81             textMap1.Position = new Vector2(60, 210);
82             textMap1.PositionPolicy = VisualTransformPolicyType.Absolute;
83             textMap1.Size = new Vector2(600, 200);
84             textMap1.SizePolicy = VisualTransformPolicyType.Absolute;
85             textMap1.TextColor = Color.Red;
86             textMap1.DepthIndex = 5;
87             view[0].AddVisual("textVisual", textMap1);
88
89
90
91             imageMap2 = new ImageVisual();
92             imageMap2.URL = resourcePath + "gallery-" + imgIndex + ".jpg";
93             imageMap2.AnchorPoint = Visual.AlignType.TopBegin;
94             imageMap2.Origin = Visual.AlignType.TopBegin;
95             imageMap2.Position = new Vector2(0, 0);
96             imageMap2.PositionPolicy = VisualTransformPolicyType.Absolute;
97             imageMap2.Size = new Vector2(500, 500);
98             imageMap2.SizePolicy = VisualTransformPolicyType.Absolute;
99             imageMap2.DepthIndex = 0;
100             view[1].AddVisual("bgVisual", imageMap2);
101
102             dimImageMap = new ImageVisual();
103             dimImageMap.URL = resourcePath + "star-dim.png";
104             dimImageMap.Size = new Vector2(40, 40);
105             dimImageMap.SizePolicy = VisualTransformPolicyType.Absolute;
106             dimImageMap.AnchorPoint = Visual.AlignType.TopBegin;
107             dimImageMap.Origin = Visual.AlignType.TopBegin;
108             dimImageMap.Position = new Vector2(10, 10);
109             dimImageMap.PositionPolicy = VisualTransformPolicyType.Absolute;
110             dimImageMap.DepthIndex = 1;
111             view[1].AddVisual("iconVisual", dimImageMap);
112
113             textMap2 = new TextVisual();
114             textMap2.Text = "I'm";
115             textMap2.PointSize = 20;
116             textMap2.AnchorPoint = Visual.AlignType.TopBegin;
117             textMap2.Origin = Visual.AlignType.TopBegin;
118             textMap2.Position = new Vector2(60, 210);
119             textMap2.PositionPolicy = VisualTransformPolicyType.Absolute;
120             textMap2.Size = new Vector2(600, 200);
121             textMap2.SizePolicy = VisualTransformPolicyType.Absolute;
122             textMap2.TextColor = Color.Black;
123             textMap2.DepthIndex = 5;
124             view[1].AddVisual("textVisual", textMap2);
125
126
127             guide = new TextLabel();
128             guide.PivotPoint = PivotPoint.TopLeft;
129             guide.Size2D = new Size2D(800, 200);
130             guide.Padding = new Vector4(50, 50, 50, 50);
131             guide.MultiLine = true;
132             guide.BackgroundColor = Color.Magenta;
133             guide.PointSize = 10;
134             guide.TextColor = Color.Black;
135             guide.Text = "Left/Right - Move focus\n" +
136                 "Up/Down - Change Text\n" +
137                 "Enter - Change BG image\n";
138             Window.Instance.Add(guide);
139
140             Window.Instance.KeyEvent += Instance_Key;
141             FocusManager.Instance.SetCurrentFocusView(view[0]);
142             Window.Instance.TouchEvent += Instance_Touch;
143             _window = Window.Instance;
144             _window.FocusChanged += _window_WindowFocusChanged;
145
146         }
147
148         private void _window_WindowFocusChanged(object sender, Window.FocusChangedEventArgs e)
149         {
150             Tizen.Log.Fatal("NUI", "window focus changed!() focus gained=" + e.FocusGained);
151         }
152
153         private void Instance_Touch(object sender, Window.TouchEventArgs e)
154         {
155             FocusManager.Instance.SetCurrentFocusView(view[0]);
156         }
157
158         private bool VisualSample_KeyEvent(object source, View.KeyEventArgs e)
159         {
160             Tizen.Log.Fatal("NUI", "View_KeyEvent" + e.Key.State.ToString() + ", Pressed-" + e.Key.KeyPressedName);
161
162             if (e.Key.State == Key.StateType.Down)
163             {
164                 if (source.Equals(view[0]))
165                 {
166                     if (e.Key.KeyPressedName == "Up")
167                     {
168                         textMap1.PointSize = 14;
169                         textMap1.TextColor = Color.Red;
170                         textMap1.Text = "Hello NY!";
171                         //this.VersionCheckTest();
172                         /*
173                            DALI_KEY_VOLUME_UP      = 200,      ///< Volume up key @SINCE_1_0.0
174                            DALI_KEY_VOLUME_DOWN    = 201,       ///< Volume down key @SINCE_1_0.0
175                         */
176                         try
177                         {
178                             Tizen.Log.Fatal("NUI", "GrabKeyTopmost (200==vol up) ret=" + _window.GrabKeyTopmost(200));
179                         }
180                         catch (Exception except)
181                         {
182                             Tizen.Log.Fatal("NUI", "Exception!!! GrabKeyTopmost (200==vol up) msg=" + except.Message);
183                         }
184
185                     }
186                     else if (e.Key.KeyPressedName == "Down")
187                     {
188                         textMap1.PointSize = 17;
189                         textMap1.TextColor = Color.Blue;
190                         textMap1.Text = "Goodbye NY.";
191
192                         Tizen.Log.Fatal("NUI", "UngrabKeyTopmost (200==vol up) ret=" + _window.UngrabKeyTopmost(200));
193
194                     }
195                     else if (e.Key.KeyPressedName == "Return")
196                     {
197                         imgIndex = (imgIndex + 1) % 6;
198                         imageMap.URL = resourcePath + "gallery-" + imgIndex + ".jpg";
199                         //Tizen.Log.Fatal("NUI", "get native ecore wayland hander=" + _window.GetNativeWindowHandler());
200                     }
201
202                 }
203                 else
204                 {
205                     if (e.Key.KeyPressedName == "Up")
206                     {
207                         textMap2.PointSize = 14;
208                         textMap2.TextColor = Color.Red;
209                         textMap2.Text = "I'm happy!";
210                         Tizen.Log.Fatal("NUI", "grab key (201==vol down) ret=" + _window.GrabKey(201, Window.KeyGrabMode.Topmost));
211                     }
212
213                     if (e.Key.KeyPressedName == "Down")
214                     {
215                         textMap2.PointSize = 17;
216                         textMap2.TextColor = Color.Blue;
217                         textMap2.Text = "I'm unhappy";
218                         Tizen.Log.Fatal("NUI", "ungrab key (201==vol down) ret=" + _window.UngrabKey(201));
219                     }
220                     else if (e.Key.KeyPressedName == "Return")
221                     {
222                         imgIndex = (imgIndex + 1) % 6;
223                         imageMap2.URL = resourcePath + "gallery-" + imgIndex + ".jpg";
224                         //Tizen.Log.Fatal("NUI", "get native ecore wayland hander=" + _window.GetNativeWindowHandler());
225                     }
226                 }
227             }
228             return false;
229         }
230
231         private void Instance_Key(object sender, Window.KeyEventArgs e)
232         {
233             View currentFocusView = FocusManager.Instance.GetCurrentFocusView();
234
235             Tizen.Log.Fatal("NUI", "Window_KeyEvent" + e.Key.State.ToString() + ", Pressed-" + e.Key.KeyPressedName);
236             //Tizen.Log.Fatal("NUI", " CurrentFocusView : " + currentFocusView.HasBody() + currentFocusView?.Name);
237         }
238
239         private void VisualSample_FocusLost(object sender, EventArgs e)
240         {
241             VisualView view = sender as VisualView;
242             view.BackgroundColor = Color.Green;
243             view.Scale = new Vector3(1.0f, 1.0f, 1.0f);
244
245             view.AddVisual("iconVisual", dimImageMap);
246
247             if (view.Name == "MyView1")
248             {
249                 imageMap2.MixColor = new Color(1, 0, 0, 0.5f);
250                 imageMap2.Opacity = 0.5f;
251             }
252             else if (view.Name == "MyView0")
253             {
254                 imageMap.MixColor = new Color(1, 0, 0, 0.5f);
255                 imageMap.Opacity = 0.5f;
256             }
257
258             Tizen.Log.Fatal("NUI", "FocusLost-" + view.Name);
259         }
260
261         private void VisualSample_FocusGained(object sender, EventArgs e)
262         {
263             VisualView view = sender as VisualView;
264             view.BackgroundColor = Color.Yellow;
265             view.Scale = new Vector3(1.2f, 1.2f, 1.0f);
266
267             view.AddVisual("iconVisual", highlightImageMap);
268
269             if (view.Name == "MyView1")
270             {
271                 imageMap2.MixColor = new Color(1, 1, 1, 1);
272                 imageMap2.Opacity = 1.0f;
273             }
274             else if (view.Name == "MyView0")
275             {
276                 imageMap.MixColor = new Color(1, 1, 1, 1);
277                 imageMap.Opacity = 1.0f;
278             }
279
280             Tizen.Log.Fatal("NUI", "FocusGained-" + view.Name);
281         }
282
283         static void _Main(string[] args)
284         {
285             VisualSample sample = new VisualSample();
286             sample.Run(args);
287         }
288     }
289 }