Beispiel #1
0
 /// <summary>
 /// Creates a 3D text (TextMeshPro) object using the specified parameters.
 /// </summary>
 /// <returns>The 3D text instance</returns>
 public static TextMeshPro CreateWorldText(string text, Transform parent = null, Vector3 position = default, Quaternion?rotation = null, int fontSize = 32,
                                           Color?color = null,
                                           HorizontalAlignmentOptions horizontalAlignment = HorizontalAlignmentOptions.Left,
                                           VerticalAlignmentOptions verticalAlignment     = VerticalAlignmentOptions.Top, int sortingOrder = 1000)
 {
     return(CreateWorldText(text, parent, position, rotation ?? Quaternion.identity, fontSize, color ?? Color.white, horizontalAlignment, verticalAlignment, sortingOrder));
 }
Beispiel #2
0
        static public WorldSpaceRect GetCaretRect(Entity targetText, EntityManager mgr, int caretPos)
        {
            var          rect         = mgr.GetComponentData <WorldSpaceRect>(targetText);
            var          elementScale = mgr.GetComponentData <ElementScale>(targetText);
            TextRenderer settings     = mgr.GetComponentData <TextRenderer>(targetText);
            var          textData     = mgr.GetBuffer <TextData>(targetText);

            VerticalAlignmentOptions   verticalAlignment   = (VerticalAlignmentOptions)settings.Alignment;
            HorizontalAlignmentOptions horizontalAlignment = (HorizontalAlignmentOptions)settings.Alignment;

            var   font                 = mgr.GetComponentData <TextFontAsset>(settings.Font);
            var   glyphData            = mgr.GetBuffer <FontGlyphData>(settings.Font);
            float styleSpaceMultiplier = 1.0f + (settings.Bold ? font.BoldSpace * 0.01f : font.NormalSpace * 0.01f);

            float2 canvasScale = settings.Size * elementScale.Value / font.PointSize;
            NativeList <TextLineInfo> lines = new NativeList <TextLineInfo>(Allocator.Temp);

            CalculateLines(ref rect, canvasScale, styleSpaceMultiplier, ref glyphData, ref textData, lines);

            float  textBlockHeight      = lines.Length * font.LineHeight * canvasScale.y;
            float2 alignedStartPosition = TextUtils.GetAlignedStartPosition(ref rect, ref settings, ref font, textBlockHeight, canvasScale);

            for (int i = lines.Length - 1; i >= 0; i--)
            {
                if (lines[i].CharacterOffset <= caretPos)
                {
                    float2 currentCharacter = new float2(GetAlignedLinePosition(ref rect, lines[i].LineWidth, horizontalAlignment),
                                                         alignedStartPosition.y - font.LineHeight * canvasScale.y * i);

                    for (int j = lines[i].CharacterOffset; j < math.min(caretPos, textData.Length); j++)
                    {
                        if (TextUtils.GetGlyph(textData[j].Value, ref glyphData, out FontGlyphData ch))
                        {
                            currentCharacter += (new float2(ch.Metrics.horizontalAdvance * styleSpaceMultiplier, 0.0f) *
                                                 canvasScale);
                        }
                    }
                    return(new WorldSpaceRect()
                    {
                        Min = currentCharacter + new float2(0.0f, font.DescentLine * canvasScale.y),
                        Max = new float2(currentCharacter.x + (2.0f), currentCharacter.y + font.LineHeight * canvasScale.y)
                    });
                }
            }

            return(new WorldSpaceRect()
            {
                Min = rect.Min,
                Max = new float2(rect.Min.x + 2.0f, rect.Max.y)
            });
        }
Beispiel #3
0
        public static float2 GetAlignedStartPosition(ref WorldSpaceRect rect, ref TextRenderer settings, ref TextFontAsset font, float textBlockHeight, float2 scale)
        {
            float startY = 0.0f;
            VerticalAlignmentOptions   vertical   = (VerticalAlignmentOptions)settings.Alignment;
            HorizontalAlignmentOptions horizontal = (HorizontalAlignmentOptions)settings.Alignment;

            if ((vertical & VerticalAlignmentOptions.Bottom) == VerticalAlignmentOptions.Bottom)
            {
                startY = rect.Min.y - font.DescentLine * scale.y + textBlockHeight - font.LineHeight * scale.y;
            }
            else if ((vertical & VerticalAlignmentOptions.Middle) == VerticalAlignmentOptions.Middle)
            {
                startY = (rect.Min.y + rect.Max.y) * 0.5f - (font.AscentLine) * scale.y + textBlockHeight * 0.5f;
            }
            else if ((vertical & VerticalAlignmentOptions.Top) == VerticalAlignmentOptions.Top)
            {
                startY = rect.Max.y - (font.AscentLine) * scale.y;
            }
            return(new float2(rect.Min.x, startY));
        }
Beispiel #4
0
        private void UpdateVerticalAlignment(VerticalAlignmentOptions verticalAlignment)
        {
            CellFormat.Alignment.Vertical = verticalAlignment;

            //if (ChildCells != null)
            //{
            //    foreach (Cell cell in ChildCells)
            //        cell.VerticalAlignment = verticalAlignment;
            //}
        }
Beispiel #5
0
            private void PopulateMesh(WorldSpaceRect rect, ElementScale scale, WorldSpaceMask mask, TextRenderer settings, float4 color, ref DynamicBuffer <TextData> textBuffer, ref DynamicBuffer <ControlVertexData> vertices, ref DynamicBuffer <ControlVertexIndex> triangles)
            {
                VerticalAlignmentOptions   verticalAlignment   = (VerticalAlignmentOptions)settings.Alignment;
                HorizontalAlignmentOptions horizontalAlignment = (HorizontalAlignmentOptions)settings.Alignment;

                var font      = FontAssetFromEntity[settings.Font];
                var glyphData = FontGlyphDataFromEntity[settings.Font];

                float2 canvasScale = settings.Size * scale.Value / font.PointSize;


                float stylePadding         = 1.25f + (settings.Bold ? font.BoldStyle / 4.0f : font.NormalStyle / 4.0f);
                float styleSpaceMultiplier = 1.0f + (settings.Bold ? font.BoldSpace * 0.01f : font.NormalSpace * 0.01f);

                NativeList <TextUtils.TextLineInfo> lines = new NativeList <TextUtils.TextLineInfo>(Allocator.Temp);

                TextUtils.CalculateLines(ref rect, canvasScale, styleSpaceMultiplier, ref glyphData, ref textBuffer, lines);
                float textBlockHeight = lines.Length * font.LineHeight * canvasScale.y;

                float2 alignedStartPosition = TextUtils.GetAlignedStartPosition(ref rect, ref settings, ref font, textBlockHeight, canvasScale);
                float2 currentCharacter     = alignedStartPosition;

                int lineIdx = 0;

                for (int i = 0; i < textBuffer.Length; i++)
                {
                    if (lineIdx < lines.Length && i == lines[lineIdx].CharacterOffset)
                    {
                        currentCharacter = new float2(TextUtils.GetAlignedLinePosition(ref rect, lines[lineIdx].LineWidth, horizontalAlignment),
                                                      alignedStartPosition.y - font.LineHeight * canvasScale.y * lineIdx);
                        lineIdx++;
                    }

                    var character = textBuffer[i].Value;
                    if (character == (ushort)'\n')
                    {
                        // It's handled in GetLinesOffsets
                        continue;
                    }
                    if (TextUtils.GetGlyph(character, ref glyphData, out FontGlyphData ch))
                    {
                        int startIndex = vertices.Length;

                        float2 uv2 = new float2(ch.Scale, ch.Scale) * math.select(canvasScale, -canvasScale, settings.Bold);

                        float2 vMin = currentCharacter + new float2(ch.Metrics.horizontalBearingX - stylePadding, ch.Metrics.horizontalBearingY - ch.Metrics.height - stylePadding) * canvasScale;
                        float2 vMax = vMin + new float2(ch.Metrics.width + stylePadding * 2.0f, ch.Metrics.height + stylePadding * 2.0f) * canvasScale;

                        float4 uv = new float4(ch.Rect.x - stylePadding, ch.Rect.y - stylePadding, (ch.Rect.x + ch.Rect.width + stylePadding), (ch.Rect.y + ch.Rect.height + stylePadding)) / new float4(font.AtlasSize, font.AtlasSize);

                        RectMaskCut cut     = SpriteUtility.GetRectangleMaskCut(vMin, vMax, uv.xy, uv.zw, ref mask);
                        var         cutSize = cut.Max + cut.Min;
                        if (!(cutSize.x > vMax.x - vMin.x) && !(cutSize.y > vMax.y - vMin.y))
                        {
                            float4 vertexPos = new float4(vMin + cut.Min, vMax - cut.Max);
                            uv.xy = uv.xy + cut.UvMin;
                            uv.zw = uv.zw - cut.UvMax;

                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 2
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 1
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex
                            });

                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 3
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex + 2
                            });
                            triangles.Add(new ControlVertexIndex()
                            {
                                Value = startIndex
                            });

                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.xy, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.xy,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.zy, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.zy,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.zw, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.zw,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                            vertices.Add(new ControlVertexData()
                            {
                                Position  = new float3(vertexPos.xw, 0.0f),
                                Normal    = new float3(0.0f, 0.0f, -1.0f),
                                TexCoord0 = uv.xw,
                                TexCoord1 = uv2,
                                Color     = color
                            });
                        }

                        currentCharacter += (new float2(ch.Metrics.horizontalAdvance * styleSpaceMultiplier, 0.0f) *
                                             canvasScale);
                    }
                }
            }
Beispiel #6
0
        /// <summary>
        /// Creates a 3D text (TextMeshPro) object using the specified parameters.
        /// </summary>
        /// <returns>The 3D text instance</returns>
        public static TextMeshPro CreateWorldText(string text, Transform parent, Vector3 position, Quaternion rotation, int fontSize, Color color,
                                                  HorizontalAlignmentOptions horizontalAlignment, VerticalAlignmentOptions verticalAlignment, int sortingOrder)
        {
            TextMeshPro textMesh = new GameObject("WorldText", typeof(TextMeshPro)).GetComponent <TextMeshPro>();

            textMesh.text                = text;
            textMesh.fontSize            = fontSize;
            textMesh.color               = color;
            textMesh.horizontalAlignment = horizontalAlignment;
            textMesh.verticalAlignment   = verticalAlignment;
            textMesh.sortingOrder        = sortingOrder;

            textMesh.transform.SetParent(parent, false);
            textMesh.transform.localPosition = position;
            textMesh.transform.localRotation = rotation;

            return(textMesh);
        }