Пример #1
0
        /// <summary>
        /// Get the Buffer group where the sprite will be added, these compononents will be draw at the same time
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="sampler"></param>
        /// <returns></returns>
        public SpriteDrawInfo GetSpriteDrawInfo(SpriteTexture texture, SamplerState sampler, int drawGroupId, float textureRotation = 0)
        {
            SpriteDrawInfo spriteDrawInfo;
            int            textureHashCode = SpriteDrawInfo.ComputeHashCode(texture, sampler, drawGroupId, textureRotation);

            if (_spritesByTexture.TryGetValue(textureHashCode, out spriteDrawInfo) == false)
            {
                //The sprite group for this texture is not existing => Create it !
                if (textureRotation != 0f)
                {
                    Matrix textureRotationMatrix = Matrix.Translation(-0.5f, -0.5f, 0) * Matrix.RotationYawPitchRoll(0f, 0f, textureRotation) * Matrix.Translation(0.5f, 0.5f, 0);
                    spriteDrawInfo = new SpriteDrawInfo(texture, sampler, textureRotationMatrix);
                }
                else
                {
                    spriteDrawInfo = new SpriteDrawInfo(texture, sampler);
                }
                _spritesByTexture.Add(textureHashCode, spriteDrawInfo);
            }

            return(spriteDrawInfo);
        }
Пример #2
0
        public void DrawText(SpriteFont spriteFont, string text, ref Vector2 position, ref ByteColor color, float maxWidth = -1, int withCarret = -1, TextFontPosition textFontPosition = TextFontPosition.RelativeToFontUp, int drawGroupId = 0)
        {
            SpriteFont.WordInfo[] infos;
            Vector2 textPosition = position;

            if (textFontPosition == TextFontPosition.RelativeToFontBottom)
            {
                textPosition.Y -= spriteFont.CharHeight;                                                            //remove the char. height
            }
            bool isFirstChar = true;
            int  length      = text.Length;

            if (length == 0 && withCarret == -1)
            {
                return;
            }

            if (withCarret > -1)
            {
                _descCarret = spriteFont.CharDescriptors['|'];
            }
            withCarret--;
            _spriteBuffer.AutoDepth -= 0.001f;
            int numCharsToDraw = length;

            //Get the SpriteDrawInfo group for this Font
            SpriteDrawInfo spritesDrawFont = _spriteBuffer.GetSpriteDrawInfo(spriteFont.SpriteTexture, _spriteSamplerClamp, drawGroupId);

            //If Carret at starup position
            if (withCarret == -1)
            {
                spritesDrawFont.AddSprite(ref textPosition, ref _descCarret, true, 0, ref color, _spriteBuffer.AutoDepth); //Add Carret at startUp text Position
            }
            if (maxWidth == -1)
            {
                infos = new[] { new SpriteFont.WordInfo {
                                    IndexStart = 0, Length = numCharsToDraw
                                } };
            }
            else
            {
                spriteFont.MeasureStringWords(text, maxWidth, out infos);
            }

            //For each Words
            char  previousChar     = (char)0;
            float currentLineWidth = 0;

            foreach (SpriteFont.WordInfo info in infos)
            {
                // Check if we need to start a new line
                if (info.Width == -1 ||
                    currentLineWidth + info.Width > maxWidth && info.Width < maxWidth)
                {
                    InsertNewLine(ref textPosition, ref currentLineWidth, spriteFont, position.X);
                    //continue;
                }

                //For Each character in the word
                for (int i = info.IndexStart; i < info.IndexStart + info.Length; i++)
                {
                    char character = text[i];

                    if (character == '\r')
                    {
                        continue;
                    }

                    //Managing Space
                    if (character == ' ')
                    {
                        textPosition.X += spriteFont.SpaceWidth;
                    }
                    else
                    {
                        //Managing New Line
                        if (character == '\n')
                        {
                            InsertNewLine(ref textPosition, ref currentLineWidth, spriteFont, position.X);
                        }
                        else
                        {
                            //Apply Kerning
                            if (!isFirstChar && spriteFont.WithKerning)
                            {
                                int kerningAmount = spriteFont.GetKerning(previousChar, character);
                                textPosition.X += kerningAmount;
                            }

                            //All other characters goes here
                            RectangleF desc = spriteFont.CharDescriptors[character];
                            spritesDrawFont.AddSprite(ref textPosition, ref desc, true, 0, ref color, _spriteBuffer.AutoDepth); //Add Carret at startUp text Position

                            textPosition.X += desc.Width;
                            previousChar    = character;
                            isFirstChar     = false;
                        }
                    }

                    //Display carret ??
                    if (i == withCarret)
                    {
                        spritesDrawFont.AddSprite(ref textPosition, ref _descCarret, true, 0, ref color, _spriteBuffer.AutoDepth);                  //Add Carret at startUp text Position
                    }
                }

                //If in word max mode, then add a space after the word
                if (maxWidth > -1)
                {
                    //Display carret ??
                    if (info.Width > -1)
                    {
                        textPosition.X += spriteFont.SpaceWidth;
                    }
                    currentLineWidth += info.Width + spriteFont.SpaceWidth;
                }
            }

            if (length > numCharsToDraw)
            {
                DrawText(spriteFont, text.Substring(numCharsToDraw - 1), ref textPosition, ref color, maxWidth, withCarret);
            }
        }