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