[4.0][NUISamples] Add some examples for nui
[platform/core/csapi/tizenfx.git] / test / NUITestSample / NUITestSample / examples / geometry-test.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 System.Runtime.InteropServices;
20 using Tizen.NUI;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Constants;
24 using System.Linq;
25
26 namespace GeometryTest
27 {
28     class Example : NUIApplication
29     {
30         public Example():base()
31         {
32         }
33
34         protected override void OnCreate()
35         {
36             base.OnCreate();
37             Initialize();
38         }
39
40                 [StructLayout(LayoutKind.Sequential)]
41                 public struct Vec2
42                 {
43                         float x;
44                         float y;
45                         public Vec2(float xIn, float yIn)
46                         {
47                                 x = xIn;
48                                 y = yIn;
49                         }
50                 }
51
52                 public struct TexturedQuadVertex
53                 {
54                         public Vec2 position;
55                         // public Vec2 textureCoordinates;
56                 };
57
58                 public static byte[] Struct2Bytes(TexturedQuadVertex[] obj)
59                 {
60                         int size = Marshal.SizeOf(obj);
61                         byte[] bytes = new byte[size];
62                         IntPtr ptr = Marshal.AllocHGlobal(size);
63                         Marshal.StructureToPtr(obj, ptr, false);
64                         Marshal.Copy(ptr, bytes, 0, size);
65                         Marshal.FreeHGlobal(ptr);
66                         return bytes;
67                 }
68                 
69         private void Initialize()
70         {
71             // Connect the signal callback for window touched signal
72             Window window = Window.Instance;
73                         window.BackgroundColor = Color.White;
74
75                         /* Vertex shader */
76                         const string VERTEX_SHADER = "" +
77                                 "attribute mediump vec2 aPosition;\n" +
78                                 "uniform   mediump mat4 uMvpMatrix;\n" +
79                                 "uniform   mediump vec3 uSize;\n" +
80                                 "\n" +
81                                 "void main()\n" +
82                                 "{\n" +
83                                         "mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n" +
84                                         "vertexPosition.xyz *= uSize;\n"+
85                                         "gl_Position = uMvpMatrix * vertexPosition;\n" +
86                                 "}\n";
87
88                         /* Fragment shader */
89                         const string FRAGMENT_SHADER = "" +
90                                 "uniform mediump vec4 uColor;\n" +
91                                 "\n" +
92                                 "void main()\n" +
93                                 "{\n" +
94                                         "gl_FragColor = uColor;\n" +
95                                 "}\n";
96
97                         TexturedQuadVertex vertex1 = new TexturedQuadVertex();
98                         vertex1.position = new Vec2(-1.0f, -1.0f);
99                         TexturedQuadVertex vertex2 = new TexturedQuadVertex();
100                         vertex2.position = new Vec2(1.0f, 1.0f);
101
102                         TexturedQuadVertex[] texturedQuadVertexData;
103                         texturedQuadVertexData = new TexturedQuadVertex[2] { vertex1, vertex2 };
104
105                         int lenght = Marshal.SizeOf(vertex1);
106                         IntPtr pA = Marshal.AllocHGlobal(lenght * 2);
107
108                         for (int i = 0; i < 2; i++)
109                         {
110                                 Marshal.StructureToPtr(texturedQuadVertexData[i], pA + i * lenght, true);
111                         }
112
113                         /* Create Shader */
114             Shader shader = new Shader(VERTEX_SHADER, FRAGMENT_SHADER);
115                         
116                         /* Create Property buffer */
117                         PropertyMap vertexFormat = new PropertyMap();
118                         vertexFormat.Add("aPosition", new PropertyValue((int)PropertyType.Vector2));
119
120                         PropertyBuffer vertexBuffer = new PropertyBuffer( vertexFormat );
121                         vertexBuffer.SetData(pA, 4);
122
123                         /* Create geometry */
124                         Geometry geometry = new Geometry();
125                         geometry.AddVertexBuffer(vertexBuffer);
126                         geometry.SetType(Geometry.Type.LINES);
127
128                         /* Create renderer */
129                         Renderer renderer = new Renderer(geometry, shader);
130
131                         /* Create view */
132                         View view = new View()
133                         {
134                                 Size2D = new Size2D(300, 300),
135                                 ParentOrigin = ParentOrigin.Center,
136                                 PivotPoint = PivotPoint.Center,
137                                 PositionUsesPivotPoint = true,
138                                 BackgroundColor = Color.Black,
139                         };
140                         view.AddRenderer(renderer);
141
142             window.Add(view);
143         }
144
145         /// <summary>
146         /// The main entry point for the application.
147         /// </summary>
148         [STAThread]
149         static void _Main(string[] args)
150         {
151             Example example = new Example();
152             example.Run(args);
153         }
154     }
155 }