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