2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Runtime.InteropServices;
6 using Tizen.NUI.BaseComponents;
7 using Tizen.NUI.Components;
9 namespace Tizen.NUI.Samples
11 public class CircularText : IExample
13 private string VERSION_3_ES = "#version 300 es\n";
15 private static readonly string VERTEX_SHADER =
16 "precision mediump float;\n"+
17 "in vec2 aPosition;\n"+
18 "in vec2 aTexCoord;\n"+
20 "uniform vec3 uSize;\n"+
21 "uniform mat4 uMvpMatrix;\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"+
30 private static readonly string FRAGMENT_SHADER =
31 "precision mediump float;\n"+
33 "out vec4 FragColor;\n"+
34 "uniform sampler2D sAlbedo;\n"+
35 "uniform vec4 uColor;\n"+
38 " vec4 color = texture( sAlbedo, vUV );\n"+
39 " FragColor = vec4( color.rgb, uColor.a * color.a );\n"+
46 public Vec2(float xIn, float yIn)
53 private struct TexturedQuadVertex
59 private Renderer CreateRenderer()
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);
74 TexturedQuadVertex[] texturedQuadVertexData = new TexturedQuadVertex[4] { vertex1, vertex2, vertex3, vertex4 };
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);
81 const int vertexCount = 4;
84 float* pc = (float*)Marshal.UnsafeAddrOfPinnedArrayElement(texturedQuadVertexData, 0);
85 IntPtr pA = new IntPtr(pc);
86 vertexBuffer.SetData(pA, vertexCount);
89 Geometry geometry = new Geometry();
90 geometry.AddVertexBuffer(vertexBuffer);
91 geometry.SetType(Geometry.Type.TRIANGLE_STRIP);
94 Shader shader = new Shader( VERSION_3_ES + VERTEX_SHADER, VERSION_3_ES + FRAGMENT_SHADER );
96 // Create the renderer
97 Renderer renderer = new Renderer( geometry, shader );
102 private uint GetBytesPerPixel(PixelFormat pixelFormat)
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:
123 case PixelFormat.RGB888:
128 case PixelFormat.RGB8888:
129 case PixelFormat.BGR8888:
130 case PixelFormat.RGBA8888:
131 case PixelFormat.BGRA8888:
141 TextureSet CreateTextureSet( RendererParameters textParameters, List<string> embeddedItems )
143 EmbeddedItemInfo[] embeddedItemLayout = new EmbeddedItemInfo[0];
144 PixelBuffer pixelBuffer = TextUtils.Render( textParameters, ref embeddedItemLayout );
146 uint dstWidth = pixelBuffer.GetWidth();
147 uint dstHeight = pixelBuffer.GetHeight();
150 int length = embeddedItemLayout.Length;
151 for(int i = 0; i < length; i++)
153 EmbeddedItemInfo itemLayout = embeddedItemLayout[i];
154 int width = (int)itemLayout.Size.Width;
155 int height = (int)itemLayout.Size.Height;
157 int x = (int)itemLayout.Position.X;
158 int y = (int)itemLayout.Position.Y;
160 PixelBuffer itemPixelBuffer = ImageLoader.LoadImageFromFile(embeddedItems[index++]);
162 if( itemPixelBuffer == null ) continue;
164 itemPixelBuffer.Resize( (ushort)width, (ushort)height );
165 itemPixelBuffer.Rotate( itemLayout.Angle );
168 width = (int)itemPixelBuffer.GetWidth();
169 height = (int)itemPixelBuffer.GetHeight();
171 PixelFormat itemPixelFormat = itemPixelBuffer.GetPixelFormat();
173 // Check if the item is out of the buffer.
174 if( ( x + width < 0 ) ||
177 ( y - height > dstHeight ) )
179 // The embedded item is completely out of the buffer.
183 // Crop if it exceeds the boundaries of the destination buffer.
188 int newWidth = width;
189 int newHeight = height;
196 cropX = Math.Abs( x );
204 if( cropX + newWidth > dstWidth )
207 newWidth -= (int)( ( cropX + newWidth ) - dstWidth );
213 newHeight += layoutY;
214 cropY = Math.Abs(layoutY);
218 if( cropY + newHeight > dstHeight )
221 newHeight -= (int)( ( cropY + newHeight ) - dstHeight );
226 int uiNewWidth = newWidth;
227 int uiNewHeight = newHeight;
231 itemPixelBuffer.Crop( (ushort)uiCropX, (ushort)uiCropY, (ushort)uiNewWidth, (ushort)uiNewHeight );
234 // Blend the item pixel buffer with the text's color according its blending mode.
235 if( ColorBlendingMode.Multiply == itemLayout.ColorBlendingMode )
237 PixelBuffer buffer = new PixelBuffer( (uint)uiNewWidth, (uint)uiNewHeight, itemPixelFormat );
239 IntPtr bufferIntPtr = buffer.GetBuffer();
240 IntPtr itemBufferIntPtr = itemPixelBuffer.GetBuffer();
242 uint bytesPerPixel = GetBytesPerPixel(itemPixelFormat);
243 uint size = (uint)(uiNewWidth * uiNewHeight * bytesPerPixel);
247 byte *bufferPtr = (byte *)bufferIntPtr.ToPointer();
248 byte *itemBufferPtr = (byte *)itemBufferIntPtr.ToPointer();
250 for (uint j = 0; j < size; j += bytesPerPixel)
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 );
257 itemBufferPtr += bytesPerPixel;
258 bufferPtr += bytesPerPixel;
262 itemPixelBuffer = buffer;
265 TextUtils.UpdateBuffer(itemPixelBuffer, pixelBuffer, (uint)layoutX, (uint)layoutY, true);
268 PixelData pixelData = PixelBuffer.Convert( pixelBuffer );
270 Texture texture = new Texture( TextureType.TEXTURE_2D,
271 pixelData.GetPixelFormat(),
272 pixelData.GetWidth(),
273 pixelData.GetHeight() );
274 texture.Upload(pixelData);
276 TextureSet textureSet = new TextureSet();
277 textureSet.SetTexture( 0u, texture );
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";
290 public void Activate()
292 Window window = NUIApplication.GetDefaultWindow();
297 BackgroundColor = Color.Yellow,
298 ParentOrigin = ParentOrigin.TopLeft,
299 PivotPoint = PivotPoint.TopLeft,
300 PositionUsesPivotPoint = true,
304 string image1 = "<item 'width'=26 'height'=26 'url'='" + IMAGE1 + "'/>";
305 string image2 = "<item 'width'=26 'height'=26/>";
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;
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);
334 TextureSet textureSet = CreateTextureSet( textParameters, embeddedItems );
335 Renderer renderer = CreateRenderer();
336 renderer.SetTextures( textureSet );
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;
345 actor.AddRenderer( renderer );
350 public void Deactivate()
354 NUIApplication.GetDefaultWindow().Remove(root);