987d718e7b78de23c640646678458fd70f41d0b7
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CanvasViewSamsple.cs
1 using System;
2 using System.Collections.Generic;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.BaseComponents.VectorGraphics;
5
6 namespace Tizen.NUI.Samples
7 {
8     using log = Tizen.Log;
9
10     public class CanvasViewSample : IExample
11     {
12         const string tag = "NUITEST";
13
14         private View root;
15         private CanvasView canvasView;
16         private Shape shape;
17         private Timer timer;
18         private int count = 0;
19
20         public void Activate()
21         {
22             Window window = NUIApplication.GetDefaultWindow();
23
24             root = new View()
25             {
26                 Size = window.Size,
27                 ParentOrigin = ParentOrigin.Center,
28                 PivotPoint = PivotPoint.Center,
29                 PositionUsesPivotPoint = true,
30             };
31             window.Add(root);
32
33             canvasView = new CanvasView(window.Size)
34             {
35                 Size = window.Size,
36                 ParentOrigin = ParentOrigin.Center,
37                 PivotPoint = PivotPoint.Center,
38                 PositionUsesPivotPoint = true,
39             };
40
41             Shape shape1 = new Shape()
42             {
43                 FillColor = new Color(0.5f, 1.0f, 0.0f, 1.0f),
44                 StrokeColor = new Color(0.5f, 0.0f, 0.0f, 0.5f),
45                 StrokeWidth = 10.0f,
46             };
47             shape1.Translate(100.0f, 100.0f);
48             shape1.Scale(1.2f);
49             shape1.Rotate(45.0f);
50             shape1.AddRect(-50.0f, -50.0f, 100.0f, 100.0f, 10.0f, 10.0f);
51
52             canvasView.AddDrawable(shape1);
53
54             Shape shape2 = new Shape()
55             {
56                 Opacity = 0.5f,
57                 FillColor = new Color(0.0f, 0.0f, 1.0f, 1.0f),
58                 StrokeColor = new Color(1.0f, 1.0f, 0.0f, 1.0f),
59                 StrokeWidth = 10.0f,
60                 StrokeDash = new List<float>() { 15.0f, 30.0f }.AsReadOnly(),
61             };
62             shape2.AddCircle(0.0f, 0.0f, 150.0f, 100.0f);
63             shape2.Transform(new float[] { 0.6f, 0.0f, 350.0f, 0.0f, 0.6f, 100.0f, 0.0f, 0.0f, 1.0f });
64
65             canvasView.AddDrawable(shape2);
66
67             Shape shape3 = new Shape()
68             {
69                 StrokeColor = new Color(0.0f, 0.5f, 0.0f, 0.5f),
70                 StrokeWidth = 10.0f,
71                 StrokeJoin = Shape.StrokeJoinType.Miter,
72             };
73             shape3.Translate(100.0f, 300.0f);
74             shape3.AddArc(0.0f, 0.0f, 80.0f, 10.0f, 120.0f, true);
75
76             canvasView.AddDrawable(shape3);
77
78             Shape shape4 = new Shape()
79             {
80                 Opacity = 0.5f,
81                 FillColor = new Color(0.0f, 0.5f, 0.0f, 0.5f),
82                 StrokeColor = new Color(0.5f, 0.0f, 0.5f, 0.5f),
83                 StrokeWidth = 30.0f,
84                 FillRule = Shape.FillRuleType.EvenOdd,
85                 StrokeJoin = Shape.StrokeJoinType.Round,
86             };
87             shape4.Scale(0.5f);
88             shape4.Translate(350.0f, 300.0f);
89             shape4.AddMoveTo(0.0f, -160.0f);
90             shape4.AddLineTo(125.0f, 160.0f);
91             shape4.AddLineTo(-180.0f, -45.0f);
92             shape4.AddLineTo(180.0f, -45.0f);
93             shape4.AddLineTo(-125.0f, 160.0f);
94             shape4.Close();
95
96             canvasView.AddDrawable(shape4);
97
98             shape = new Shape()
99             {
100                 Opacity = 0.5f,
101                 FillColor = new Color(0.0f, 1.0f, 1.0f, 1.0f),
102                 StrokeColor = new Color(0.5f, 1.0f, 0.5f, 1.0f),
103                 StrokeWidth = 30.0f,
104                 StrokeCap = Shape.StrokeCapType.Round,
105             };
106             shape.Translate(250.0f, 550.0f);
107             shape.Scale(0.5f);
108             shape.AddMoveTo(-1.0f, -165.0f);
109             shape.AddLineTo(53.0f, -56.0f);
110             shape.AddLineTo(174.0f, -39.0f);
111             shape.AddLineTo(87.0f, 45.0f);
112             shape.AddLineTo(107.0f, 166.0f);
113             shape.AddLineTo(-1.0f, 110.0f);
114             shape.AddLineTo(-103.0f, 166.0f);
115             shape.AddLineTo(-88.0f, 46.0f);
116             shape.AddLineTo(-174.0f, -38.0f);
117             shape.AddLineTo(-54.0f, -56.0f);
118             shape.Close();
119
120             canvasView.AddDrawable(shape);
121
122             // Test Getter
123             log.Debug(tag, "Shape2 Color : " + shape2.FillColor.R + " " + shape2.FillColor.G + " " + shape2.FillColor.B + " " + shape2.FillColor.A + "\n");
124             log.Debug(tag, "Shape2 StrokeColor : " + shape2.StrokeColor.R + " " + shape2.StrokeColor.G + " " + shape2.StrokeColor.B + " " + shape2.StrokeColor.A + "\n");
125
126             log.Debug(tag, "Shape3 StrokeCap : " + shape3.StrokeCap + "\n");
127
128             log.Debug(tag, "Shape4 FillRule : " + shape4.FillRule + "\n");
129             log.Debug(tag, "Shape4 StrokeWidth : " + shape4.StrokeWidth + "\n");
130             log.Debug(tag, "Shape4 StrokeJoin : " + shape4.StrokeJoin + "\n");
131             log.Debug(tag, "Shape4 Opacity : " + shape4.Opacity + "\n");
132
133             for (int i = 0; i < shape2.StrokeDash.Count; i++)
134             {
135                 log.Debug(tag, "Shape4 StrokeDash : " + shape2.StrokeDash[i] + "\n");
136             }
137
138             // Exception test.
139             try
140             {
141                 shape2.Transform(new float[] { 0.6f, 0.0f });
142             }
143             catch (ArgumentException e)
144             {
145                 log.Debug(tag, "Transform : " + e.Message + "\n");
146             }
147             try
148             {
149                 shape2.Transform(null);
150             }
151             catch (ArgumentException e)
152             {
153                 log.Debug(tag, "Transform : " + e.Message + "\n");
154             }
155             try
156             {
157                 shape2.StrokeDash = null;
158             }
159             catch (ArgumentException e)
160             {
161                 log.Debug(tag, "StrokeDash setter : " + e.Message + "\n");
162             }
163
164             root.Add(canvasView);
165
166             timer = new Timer(1000 / 32);
167             timer.Tick += onTick;
168             timer.Start();
169         }
170
171         public void Deactivate()
172         {
173             if (root != null)
174             {
175                 NUIApplication.GetDefaultWindow().Remove(root);
176                 canvasView.Dispose();
177                 root.Dispose();
178             }
179         }
180
181         private bool onTick(object o, Timer.TickEventArgs e)
182         {
183             shape.Rotate((float)(count * 2.0f));
184             shape.Scale((float)(count % 100) * 0.01f + 0.6f);
185             count++;
186
187             return true;
188         }
189     }
190 }