Esempio n. 1
0
        public SimpleText(IContext context, Font font, int maxLength, Color color)
        {
            Device device = context.DirectX.Device;

            Font      = font;
            MaxLength = maxLength;
            Color     = color;
            _shader   = context.Shaders.Get <FontShader>();
            _icons    = new List <TexturedRectangle>();
            // The index buffer is static and do not change when the text changes
            uint[] indices = new uint[maxLength * 6]; // 6 indices per character

            for (uint i = 0; i < maxLength; i++)
            {
                indices[i * 6]     = i * 4;
                indices[i * 6 + 1] = i * 4 + 1;
                indices[i * 6 + 2] = i * 4 + 2;
                indices[i * 6 + 3] = i * 4;
                indices[i * 6 + 4] = i * 4 + 3;
                indices[i * 6 + 5] = i * 4 + 1;
            }

            _indexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices);

            // The vertex buffer is initialized empty
            _vertices = new VertexDefinition.PositionTextureColor[maxLength * 4]; // 4 vertices per character

            var vertexBufferDesc = new BufferDescription
            {
                Usage               = ResourceUsage.Dynamic,
                SizeInBytes         = Utilities.SizeOf <VertexDefinition.PositionTextureColor>() * maxLength * 4,
                BindFlags           = BindFlags.VertexBuffer,
                CpuAccessFlags      = CpuAccessFlags.Write,
                OptionFlags         = ResourceOptionFlags.None,
                StructureByteStride = 0
            };

            // Create the vertex buffer.
            _vertexBuffer = Buffer.Create(device, _vertices, vertexBufferDesc);
        }
Esempio n. 2
0
        public Vector2I UpdateVertexArray(string text, ref VertexDefinition.PositionTextureColor[] vertices, ref Buffer vertexBuffer, Color defaultColor, List <TexturedRectangle> icons, int positionX = 0, int positionY = 0)
        {
            icons.Clear();
            Color color     = defaultColor;
            int   width     = 0;
            int   maxWidth  = 0;
            int   height    = Characters.First().Value.height;
            int   maxHeight = height;

            for (int i = 0; i < text.Length; i++)
            {
                char letter = text[i];
                if (letter == '\n')
                {
                    maxWidth   = Math.Max(maxWidth, width);
                    width      = 0;
                    positionX  = 0;
                    positionY += height;
                    maxHeight += height;
                    continue;
                }
                if (letter == '[')
                {
                    if (text[i + 1] == '[')
                    {
                        continue;
                    }
                    string token = text.Substring(i + 1, text.IndexOf(']', i + 1) - (i + 1));
                    if (!ColorParser.TryParse(token, out color))
                    {
                        if (token == "-")
                        {
                            color = defaultColor;
                        }
                        else if (token == "gold")
                        {
                            icons.Add(new TexturedRectangle(_context,
                                                            _context.TextureManager.Create("gold.png", "Data/UI/Icons/"), new Vector2I(height, height)));
                            positionX += height + 1;
                            width     += height + 1;
                        }
                        else
                        {
                            throw new InvalidOperationException("Unexpected token : " + token);
                        }
                    }
                    i = text.IndexOf(']', i + 1);
                    continue;
                }
                Character c             = Characters[letter];
                Vector4   colorAsVector = color.ToVector4();
                vertices[i * 4] = new VertexDefinition.PositionTextureColor {
                    position = new Vector3(positionX, positionY, 0.0f), texture = new Vector2(c.uLeft, c.vTop), color = colorAsVector
                };                                                                                                                                                                                 //Top left
                vertices[i * 4 + 1] = new VertexDefinition.PositionTextureColor {
                    position = new Vector3(positionX + c.width, positionY + c.height, 0.0f), texture = new Vector2(c.uRight, c.vBottom), color = colorAsVector
                };                                                                                                                                                                                                              //Right bottom
                vertices[i * 4 + 2] = new VertexDefinition.PositionTextureColor {
                    position = new Vector3(positionX, positionY + c.height, 0.0f), texture = new Vector2(c.uLeft, c.vBottom), color = colorAsVector
                };                                                                                                                                                                                                   //Left bottom
                vertices[i * 4 + 3] = new VertexDefinition.PositionTextureColor {
                    position = new Vector3(positionX + c.width, positionY, 0.0f), texture = new Vector2(c.uRight, c.vTop), color = colorAsVector
                };                                                                                                                                                                                                //Top right

                positionX += c.width + 1;
                width     += c.width + 1;
            }
            DataStream mappedResource;

            _context.DirectX.Device.ImmediateContext.MapSubresource(vertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None,
                                                                    out mappedResource);
            mappedResource.WriteRange(vertices);
            _context.DirectX.Device.ImmediateContext.UnmapSubresource(vertexBuffer, 0);
            return(new Vector2I(Math.Max(maxWidth, width), maxHeight));
        }
Esempio n. 3
0
        private void CreateVertexBuffer(Device device)
        {
            _vertices = new VertexDefinition.PositionTextureColor[_numberOfLetters * 4];
            int   letterIndex = 0;
            Color color       = BaseColor;
            int   lineHeight  = Font.Characters.First().Value.height;
            float spaceSize   = SpaceSize;

            float positionY = Padding.Top;

            if (VerticalAlignment == VerticalAlignment.Bottom)
            {
                positionY = Size.Y - (lineHeight * Lines.Count + LineSpacing * (Lines.Count - 1)) - Padding.Bottom;
            }
            if (VerticalAlignment == VerticalAlignment.Middle)
            {
                positionY = Padding.Top + (float)(Size.Y - Padding.Bottom - Padding.Top - (lineHeight * Lines.Count + LineSpacing * (Lines.Count - 1))) / 2;
            }

            for (int i = 0; i < Lines.Count; i++)
            {
                TextLine line = Lines[i];
                line.Width -= SpaceSize + 1; //Remove last space and pixel padding after last character
                float positionX = Padding.Left;
                if (HorizontalAlignment == HorizontalAlignment.Right)
                {
                    positionX = Size.X - line.Width - Padding.Right;
                }
                if (HorizontalAlignment == HorizontalAlignment.Center)
                {
                    positionX = Padding.Left + (float)(Size.X - Padding.Left - Padding.Right - line.Width) / 2;
                }
                if (HorizontalAlignment == HorizontalAlignment.Justify && line.WordWrapped)
                {
                    spaceSize = SpaceSize + (float)(Size.X - Padding.Left - Padding.Right - line.Width) / (line.WordCount - 1);
                }
                for (int j = 0; j < line.Words.Count; j++)
                {
                    string word = line.Words[j];
                    for (int k = 0; k < word.Length; k++)
                    {
                        char letter = word[k];
                        if (letter == '[')
                        {
                            if (word[k + 1] != '[')
                            {
                                string token = word.Substring(k + 1, word.IndexOf(']', k + 1) - (k + 1));
                                if (token == "-")
                                {
                                    color = BaseColor;
                                }
                                else if (!ColorParser.TryParse(token, out color))
                                {
                                    throw new InvalidOperationException("Unexpected token : " + token);
                                }
                                k = word.IndexOf(']', k + 1);
                                continue;
                            }
                            k++;
                        }
                        Vector4        colorAsVector = color.ToVector4();
                        Font.Character c             = Font.Characters[letter];
                        _vertices[letterIndex * 4] = new VertexDefinition.PositionTextureColor {
                            position = new Vector3(positionX, positionY, 0.0f), texture = new Vector2(c.uLeft, c.vTop), color = colorAsVector
                        };                                                                                                                                                                                            //Top left
                        _vertices[letterIndex * 4 + 1] = new VertexDefinition.PositionTextureColor {
                            position = new Vector3(positionX + c.width, positionY + c.height, 0.0f), texture = new Vector2(c.uRight, c.vBottom), color = colorAsVector
                        };                                                                                                                                                                                                                         //Right bottom
                        _vertices[letterIndex * 4 + 2] = new VertexDefinition.PositionTextureColor {
                            position = new Vector3(positionX, positionY + c.height, 0.0f), texture = new Vector2(c.uLeft, c.vBottom), color = colorAsVector
                        };                                                                                                                                                                                                              //Left bottom
                        _vertices[letterIndex * 4 + 3] = new VertexDefinition.PositionTextureColor {
                            position = new Vector3(positionX + c.width, positionY, 0.0f), texture = new Vector2(c.uRight, c.vTop), color = colorAsVector
                        };                                                                                                                                                                                                           //Top right
                        positionX += c.width + 1;
                        letterIndex++;
                    }
                    positionX += spaceSize;
                }
                positionY += lineHeight + LineSpacing;
            }
            if (_vertexBuffer != null)
            {
                _vertexBuffer.Dispose();
            }
            _vertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, _vertices);
            UsedSize      = new Vector2I(Lines.Max(l => l.Width) + Padding.Left + Padding.Right,
                                         (lineHeight * Lines.Count + LineSpacing * (Lines.Count - 1)) + Padding.Bottom + Padding.Top);
        }