Exemple #1
0
 private void DrawTooltip()
 {
     if (!string.IsNullOrWhiteSpace(Tooltip))
     {
         SpriteReference ui_tooltip        = SpriteLoader.Instance.AddSprite("content/ui_box");
         TextParameters  tooltipParameters = new TextParameters().SetColor(Color.White, Color.Black);
         int             tooltipWidth      = FontUtil.GetStringWidth(Tooltip, tooltipParameters);
         int             screenWidth       = Viewport.Width - 8 - InputState.MouseX + 4;
         bool            invert            = false;
         if (tooltipWidth > screenWidth)
         {
             screenWidth = Viewport.Width - screenWidth;
             invert      = true;
         }
         tooltipParameters = new TextParameters().SetColor(Color.White, Color.Black).SetConstraints(screenWidth, int.MaxValue);
         tooltipWidth      = FontUtil.GetStringWidth(Tooltip, tooltipParameters);
         int tooltipHeight = FontUtil.GetStringHeight(Tooltip, tooltipParameters);
         int tooltipX      = InputState.MouseX + 4;
         int tooltipY      = Math.Max(0, InputState.MouseY - 4 - tooltipHeight);
         if (invert)
         {
             tooltipX -= tooltipWidth;
         }
         DrawUI(ui_tooltip, new Rectangle(tooltipX, tooltipY, tooltipWidth, tooltipHeight), Color.White);
         DrawText(Tooltip, new Vector2(tooltipX, tooltipY), Alignment.Left, tooltipParameters);
     }
 }
Exemple #2
0
 public static int GetStringHeight(string str, TextParameters parameters)
 {
     if (str != CachedString)
     {
         SetupString(str, Vector2.Zero, Alignment.Center, parameters, null);
     }
     return(CachedHeight);
 }
Exemple #3
0
 public void DrawText(string str, Vector2 drawpos, Alignment alignment, TextParameters parameters)
 {
     Game.DrawText(str, drawpos, alignment, parameters);
 }
Exemple #4
0
        public static void SetupString(string str, Vector2 drawpos, Alignment alignment, TextParameters parameters, Game game)
        {
            bool cache = false;

            if (str != CachedString || parameters.NoCache)
            {
                cache = true;
            }

            if (cache)
            {
                CachedString     = str;
                CachedParameters = parameters;

                CachedWidth  = 0;
                CachedHeight = 0;
            }

            parameters = CachedParameters.Copy();

            int getWidth(object token)
            {
                if (token is FormatToken format)
                {
                    if (format == FormatToken.Space)
                    {
                        return(4);
                    }
                    return(0);
                }
                if (token is string s)
                {
                    return(GetWordLength(s, parameters));
                }
                if (token is FormatCode code)
                {
                    return(code.Width);
                }
                return(0);
            }

            int width    = 0;
            int maxwidth = parameters.MaxWidth ?? int.MaxValue;

            int y = 0;
            int offset;

            if (cache)
            {
                CachedTokens = Tokenize(str);
            }
            var           tokens = new Stack <object>(CachedTokens.Reverse <object>());
            List <object> line   = new List <object>();

            if (cache)
            {
                CachedHeight = CharHeight;
            }

            TextParameters lineParameters = parameters.Copy();

            int index = 0;

            while (tokens.Count > 0)
            {
                var token      = tokens.Pop();
                int tokenWidth = getWidth(token);
                if (tokenWidth > maxwidth)
                {
                    if (token is string s && s.Length > 1 && maxwidth - width > 0)
                    {
                        var split = SplitWord(ref s, parameters.Copy(), maxwidth - width);
                        tokens.Push(s);
                        token      = split;
                        tokenWidth = getWidth(token);
                    }
                    else
                    {
                        break;
                    }
                }

                bool isNewLine = false;

                if (token is FormatCode code)
                {
                    parameters.Format(code);
                }
                else if (token is FormatToken format)
                {
                    parameters.Format(format);

                    if (format == FormatToken.Newline)
                    {
                        isNewLine = true;
                    }
                }

                if (width + tokenWidth > maxwidth || isNewLine)
                {
                    offset = (parameters.MaxWidth ?? 0) - width;
                    switch (alignment)
                    {
                    case (Alignment.Left):
                        offset = 0;
                        break;

                    case (Alignment.Center):
                        offset /= 2;
                        break;
                    }
                    if (game != null)
                    {
                        DrawLine(line, ref index, lineParameters, drawpos + new Vector2(offset, y), game);
                    }
                    line.Clear();
                    //Newline
                    y += parameters.LineSeperator;
                    if (cache)
                    {
                        CachedWidth   = Math.Max(CachedWidth, width);
                        CachedHeight += parameters.LineSeperator;
                    }
                    width = 0;
                    if (!isNewLine)
                    {
                        tokens.Push(token);
                    }
                    continue;
                }

                width += tokenWidth;
                line.Add(token);
            }
Exemple #5
0
 public int GetStringHeight(string str, TextParameters parameters)
 {
     return(FontUtil.GetStringHeight(str, parameters));
 }