Revert "[NUI] Dialog and AlertDialog code refactoring with adding DialogPage"
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CircularTextSample.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Runtime.InteropServices;
5 using Tizen.NUI;
6 using Tizen.NUI.BaseComponents;
7 using Tizen.NUI.Components;
8
9 namespace Tizen.NUI.Samples
10 {
11     public class CircularText : IExample
12     {
13         private string VERSION_3_ES = "#version 300 es\n";
14
15         private static readonly string VERTEX_SHADER =
16                         "precision mediump float;\n"+
17                         "in vec2 aPosition;\n"+
18                         "in vec2 aTexCoord;\n"+
19                         "out vec2 vUV;\n"+
20                         "uniform vec3 uSize;\n"+
21                         "uniform mat4 uMvpMatrix;\n"+
22                         "void main()\n"+
23                         "{\n"+
24                         "  vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n"+
25                         "  vertexPosition.xyz *= uSize;\n"+
26                         "  gl_Position = uMvpMatrix * vertexPosition;\n"+
27                         "  vUV = aTexCoord;\n"+
28                         "}\n";
29
30         private static readonly string FRAGMENT_SHADER =
31                         "precision mediump float;\n"+
32                         "in vec2 vUV;\n"+
33                         "out vec4 FragColor;\n"+
34                         "uniform sampler2D sAlbedo;\n"+
35                         "uniform vec4 uColor;\n"+
36                         "void main()\n"+
37                         "{\n"+
38                         "  vec4 color = texture( sAlbedo, vUV );\n"+
39                         "  FragColor = vec4( color.rgb, uColor.a * color.a );\n"+
40                         "}\n";
41
42         public struct Vec2
43         {
44             float x;
45             float y;
46             public Vec2(float xIn, float yIn)
47             {
48                 x = xIn;
49                 y = yIn;
50             }
51         }
52
53         private struct TexturedQuadVertex
54         {
55             public Vec2 position;
56             public Vec2 texCoord;
57         };
58
59         private Renderer CreateRenderer()
60         {
61             TexturedQuadVertex vertex1 = new TexturedQuadVertex();
62             TexturedQuadVertex vertex2 = new TexturedQuadVertex();
63             TexturedQuadVertex vertex3 = new TexturedQuadVertex();
64             TexturedQuadVertex vertex4 = new TexturedQuadVertex();
65             vertex1.position = new Vec2(-0.5f, -0.5f);
66             vertex2.position = new Vec2( 0.5f, -0.5f);
67             vertex3.position = new Vec2(-0.5f,  0.5f);
68             vertex4.position = new Vec2( 0.5f,  0.5f);
69             vertex1.texCoord = new Vec2( 0.0f,  0.0f);
70             vertex2.texCoord = new Vec2( 1.0f,  0.0f);
71             vertex3.texCoord = new Vec2( 0.0f,  1.0f);
72             vertex4.texCoord = new Vec2( 1.0f,  1.0f);
73
74             TexturedQuadVertex[] texturedQuadVertexData = new TexturedQuadVertex[4] { vertex1, vertex2, vertex3, vertex4 };
75
76             PropertyMap property = new PropertyMap();
77             property.Add("aPosition", new PropertyValue((int)PropertyType.Vector2));
78             property.Add("aTexCoord", new PropertyValue((int)PropertyType.Vector2));
79             PropertyBuffer vertexBuffer = new PropertyBuffer(property);
80
81             const int vertexCount = 4;
82             unsafe
83             {
84                 float* pc = (float*)Marshal.UnsafeAddrOfPinnedArrayElement(texturedQuadVertexData, 0);
85                 IntPtr pA = new IntPtr(pc);
86                 vertexBuffer.SetData(pA, vertexCount);
87             }
88
89             Geometry geometry = new Geometry();
90             geometry.AddVertexBuffer(vertexBuffer);
91             geometry.SetType(Geometry.Type.TRIANGLE_STRIP);
92
93             // Create the shader
94             Shader shader = new Shader( VERSION_3_ES + VERTEX_SHADER, VERSION_3_ES + FRAGMENT_SHADER );
95
96             // Create the renderer
97             Renderer renderer = new Renderer( geometry, shader );
98
99             return renderer;
100         }
101
102         private uint GetBytesPerPixel(PixelFormat pixelFormat)
103         {
104             switch (pixelFormat)
105             {
106                 case PixelFormat.L8:
107                 case PixelFormat.A8:
108                 {
109                 return 1;
110                 }
111
112                 case PixelFormat.LA88:
113                 case PixelFormat.RGB565:
114                 case PixelFormat.RGBA4444:
115                 case PixelFormat.RGBA5551:
116                 case PixelFormat.BGR565:
117                 case PixelFormat.BGRA4444:
118                 case PixelFormat.BGRA5551:
119                 {
120                 return 2;
121                 }
122
123                 case PixelFormat.RGB888:
124                 {
125                 return 3;
126                 }
127
128                 case PixelFormat.RGB8888:
129                 case PixelFormat.BGR8888:
130                 case PixelFormat.RGBA8888:
131                 case PixelFormat.BGRA8888:
132                 {
133                 return 4;
134                 }
135                 default:
136                 return 4;
137             }
138             return 0;
139         }
140
141         TextureSet CreateTextureSet( RendererParameters textParameters, List<string> embeddedItems )
142         {
143             EmbeddedItemInfo[] embeddedItemLayout = new EmbeddedItemInfo[0];
144             PixelBuffer pixelBuffer = TextUtils.Render( textParameters,  ref embeddedItemLayout );
145
146             uint dstWidth = pixelBuffer.GetWidth();
147             uint dstHeight = pixelBuffer.GetHeight();
148
149             int index = 0;
150             int length = embeddedItemLayout.Length;
151             for(int i = 0; i < length; i++)
152             {
153                 EmbeddedItemInfo itemLayout = embeddedItemLayout[i];
154                 int width = (int)itemLayout.Size.Width;
155                 int height = (int)itemLayout.Size.Height;
156
157                 int x = (int)itemLayout.Position.X;
158                 int y = (int)itemLayout.Position.Y;
159
160                 PixelBuffer itemPixelBuffer = ImageLoading.LoadImageFromFile(embeddedItems[index++]);
161
162                 if( itemPixelBuffer == null ) continue;
163
164                 itemPixelBuffer.Resize( (ushort)width, (ushort)height );
165                 itemPixelBuffer.Rotate( itemLayout.Angle );
166
167
168                 width = (int)itemPixelBuffer.GetWidth();
169                 height = (int)itemPixelBuffer.GetHeight();
170
171                 PixelFormat itemPixelFormat = itemPixelBuffer.GetPixelFormat();
172
173                 // Check if the item is out of the buffer.
174                 if( ( x + width < 0 ) ||
175                     ( x > dstWidth ) ||
176                     ( y < 0 ) ||
177                     ( y - height > dstHeight ) )
178                 {
179                     // The embedded item is completely out of the buffer.
180                     continue;
181                 }
182
183                 // Crop if it exceeds the boundaries of the destination buffer.
184                 int layoutX = 0;
185                 int layoutY = 0;
186                 int cropX = 0;
187                 int cropY = 0;
188                 int newWidth = width;
189                 int newHeight = height;
190
191                 bool crop = false;
192
193                 if( 0 > x )
194                 {
195                     newWidth += x;
196                     cropX = Math.Abs( x );
197                     crop = true;
198                 }
199                 else
200                 {
201                     layoutX = x;
202                 }
203
204                 if( cropX + newWidth > dstWidth )
205                 {
206                     crop = true;
207                     newWidth -= (int)( ( cropX + newWidth ) - dstWidth );
208                 }
209
210                 layoutY = y;
211                 if( 0 > layoutY )
212                 {
213                     newHeight += layoutY;
214                     cropY = Math.Abs(layoutY);
215                     crop = true;
216                 }
217
218                 if( cropY + newHeight > dstHeight )
219                 {
220                     crop = true;
221                     newHeight -= (int)( ( cropY + newHeight ) - dstHeight );
222                 }
223
224                 int uiCropX = cropX;
225                 int uiCropY = cropY;
226                 int uiNewWidth = newWidth;
227                 int uiNewHeight = newHeight;
228
229                 if( crop )
230                 {
231                     itemPixelBuffer.Crop( (ushort)uiCropX, (ushort)uiCropY, (ushort)uiNewWidth, (ushort)uiNewHeight );
232                 }
233
234                 // Blend the item pixel buffer with the text's color according its blending mode.
235                 if( ColorBlendingMode.Multiply == itemLayout.ColorBlendingMode )
236                 {
237                     PixelBuffer buffer = new PixelBuffer( (uint)uiNewWidth, (uint)uiNewHeight, itemPixelFormat );
238
239                     IntPtr bufferIntPtr = buffer.GetBuffer();
240                     IntPtr itemBufferIntPtr = itemPixelBuffer.GetBuffer();
241
242                     uint bytesPerPixel = GetBytesPerPixel(itemPixelFormat);
243                     uint size = (uint)(uiNewWidth * uiNewHeight * bytesPerPixel);
244
245
246                     unsafe {
247                         byte *bufferPtr = (byte *)bufferIntPtr.ToPointer();
248                         byte *itemBufferPtr = (byte *)itemBufferIntPtr.ToPointer();
249
250                         for (uint j = 0; j < size; j += bytesPerPixel)
251                         {
252                             *(bufferPtr + 0) = (byte)( ( *(itemBufferPtr + 0u) ) * textParameters.TextColor.R );
253                             *(bufferPtr + 1) = (byte)( ( *(itemBufferPtr + 1u) ) * textParameters.TextColor.G );
254                             *(bufferPtr + 2) = (byte)( ( *(itemBufferPtr + 2u) ) * textParameters.TextColor.B );
255                             *(bufferPtr + 3) = (byte)( ( *(itemBufferPtr + 3u) ) * textParameters.TextColor.A );
256
257                             itemBufferPtr += bytesPerPixel;
258                             bufferPtr += bytesPerPixel;
259                         }
260                     }
261
262                     itemPixelBuffer = buffer;
263                 }
264
265                 TextUtils.UpdateBuffer(itemPixelBuffer, pixelBuffer, (uint)layoutX, (uint)layoutY, true);
266             }
267
268             PixelData pixelData = PixelBuffer.Convert( pixelBuffer );
269
270             Texture texture = new Texture( TextureType.TEXTURE_2D,
271                                             pixelData.GetPixelFormat(),
272                                             pixelData.GetWidth(),
273                                             pixelData.GetHeight() );
274             texture.Upload(pixelData);
275
276             TextureSet textureSet = new TextureSet();
277             textureSet.SetTexture( 0u, texture );
278
279             return textureSet;
280
281         }
282
283
284
285         private View root;
286         static string DEMO_IMAGE_DIR = CommonResource.GetDaliResourcePath() + "DaliDemo/";
287         static string IMAGE1 = DEMO_IMAGE_DIR + "application-icon-1.png";
288         static string IMAGE2 = DEMO_IMAGE_DIR + "application-icon-6.png";
289
290         public void Activate()
291         {
292             Window window = NUIApplication.GetDefaultWindow();
293
294             root = new View()
295             {
296                 Size = window.Size,
297                 BackgroundColor = Color.Yellow,
298                 ParentOrigin = ParentOrigin.TopLeft,
299                 PivotPoint = PivotPoint.TopLeft,
300                 PositionUsesPivotPoint = true,
301             };
302             window.Add(root);
303
304             string image1 = "<item 'width'=26 'height'=26 'url'='" + IMAGE1 + "'/>";
305             string image2 = "<item 'width'=26 'height'=26/>";
306
307             RendererParameters textParameters = new RendererParameters();
308             textParameters.Text = "Hello " + image1 + " world " + image2 + " this " + image1 + " is " + image2 + " a " + image1 + " demo " + image2 + " of " + image1 + " circular " + image2 + " text " + image1 + " width " + image2 + " icons.";
309             textParameters.HorizontalAlignment = HorizontalAlignment.Center;
310             textParameters.VerticalAlignment = VerticalAlignment.Center;
311             textParameters.CircularAlignment = CircularAlignment.Center;
312             textParameters.FontFamily = "SamsungUI";
313             textParameters.FontWeight = "";
314             textParameters.FontWidth = "";
315             textParameters.FontSlant = "";
316             textParameters.Layout = TextLayout.Circular;
317             textParameters.TextColor = Color.Black;
318             textParameters.FontSize = 25;
319             textParameters.TextWidth = 360u;
320             textParameters.TextHeight = 360u;
321             textParameters.Radius = 180u;
322             textParameters.BeginAngle = 15;
323             textParameters.IncrementAngle = 360;
324             textParameters.EllipsisEnabled = true;
325             textParameters.MarkupEnabled = true;
326
327             List<string> embeddedItems = new List<string>();
328             embeddedItems.Add(IMAGE2);
329             embeddedItems.Add(IMAGE2);
330             embeddedItems.Add(IMAGE2);
331             embeddedItems.Add(IMAGE2);
332             embeddedItems.Add(IMAGE2);
333
334             TextureSet textureSet = CreateTextureSet( textParameters, embeddedItems );
335             Renderer renderer = CreateRenderer();
336             renderer.SetTextures( textureSet );
337
338             View actor = new View();
339             actor.PivotPoint = PivotPoint.TopLeft;
340             actor.ParentOrigin = ParentOrigin.TopLeft;
341             actor.Position = new Position(0, 0, 0);
342             actor.Size = new Size( 360, 360 );
343             actor.Color = Color.White;
344
345             actor.AddRenderer( renderer );
346             root.Add(actor);
347         }
348
349
350         public void Deactivate()
351         {
352             if (root != null)
353             {
354                 NUIApplication.GetDefaultWindow().Remove(root);
355                 root.Dispose();
356             }
357         }
358     }
359 }