Example #1
0
        public void DrawText(string str, Vector2 drawpos, Alignment alignment, TextParameters parameters)
        {
            parameters = parameters.Copy();
            int lineoffset = 0;
            int totalindex = 0;

            str = FontUtil.FitString(str, parameters);

            foreach (string line in str.Split('\n'))
            {
                if (lineoffset + parameters.LineSeperator > parameters.MaxHeight)
                {
                    break;
                }
                int textwidth = FontUtil.GetStringWidth(line, parameters);
                int offset    = (parameters.MaxWidth ?? 0) - textwidth;
                switch (alignment)
                {
                case (Alignment.Left):
                    offset = 0;
                    break;

                case (Alignment.Center):
                    offset /= 2;
                    break;
                }
                DrawTextLine(line, drawpos + new Vector2(offset, lineoffset), totalindex, parameters);
                totalindex += line.Length;
                lineoffset += parameters.LineSeperator;
            }
        }
Example #2
0
        public static string FitString(string str, TextParameters parameters)
        {
            parameters = parameters.Copy();
            int    maxwidth  = parameters.MaxWidth ?? int.MaxValue;
            int    lastspot  = 0;
            int    idealspot = 0;
            int    width     = 0;
            string newstr    = "";

            for (int i = 0; i < str.Length; i++)
            {
                char chr       = str[i];
                var  charWidth = GetCharWidth(chr) + parameters.CharSeperator + (parameters.Bold ? 1 : 0);
                if (charWidth > maxwidth)
                {
                    return("");
                }
                width += charWidth;

                switch (chr)
                {
                case (Game.FORMAT_BOLD):
                    parameters.Bold = !parameters.Bold;
                    break;
                }

                if (chr == ' ')
                {
                    idealspot = i + 1;
                }

                if (chr == '\n')
                {
                    width = 0;
                }

                if (width > maxwidth)
                {
                    if (idealspot == lastspot)
                    {
                        idealspot = i;
                    }
                    string substr = str.Substring(lastspot, idealspot - lastspot);
                    newstr  += substr.Trim() + "\n";
                    lastspot = idealspot;
                    i        = idealspot - 1;
                    width    = 0;
                }
            }

            newstr += str.Substring(lastspot, str.Length - lastspot);

            return(newstr);
        }
Example #3
0
        public void DrawMessageHistory(SceneGame scene, MessageHistory history)
        {
            int offsetY = 150;

            foreach (var message in history.Messages.Reverse <Message>())
            {
                var    parameters = new TextParameters().SetColor(Color.White, Color.Black).SetBold(true).SetConstraints(scene.Viewport.Width, 128).SetIcons(scene, message.Icons);
                string fitString  = FontUtil.FitString(message.RenderText(Player), parameters);
                var    height     = FontUtil.GetStringHeight(fitString);
                offsetY -= height;
                if (offsetY + height < 0)
                {
                    break;
                }
                scene.DrawText(fitString, new Vector2(0, offsetY), Alignment.Right, parameters);
            }
        }
Example #4
0
        public static int GetStringWidth(string str, TextParameters parameters)
        {
            parameters = parameters.Copy();
            int n = 0;

            foreach (char chr in str)
            {
                n += GetCharWidth(chr) + parameters.CharSeperator + (parameters.Bold ? 1 : 0);

                switch (chr)
                {
                case (Game.FORMAT_BOLD):
                    parameters.Bold = !parameters.Bold;
                    break;
                }
            }

            return(n);
        }
Example #5
0
 public void DrawText(string str, Vector2 drawpos, Alignment alignment, TextParameters parameters)
 {
     Game.DrawText(str, drawpos, alignment, parameters);
 }
Example #6
0
        private void DrawTextLine(string str, Vector2 drawpos, int totalindex, TextParameters parameters)
        {
            int pos = 0;

            foreach (char chr in str)
            {
                if (totalindex > parameters.DialogIndex)
                {
                    break;
                }
                switch (chr)
                {
                case (FORMAT_BOLD):
                    parameters.Bold = !parameters.Bold;
                    break;

                case (FORMAT_UNDERLINE):
                    parameters.Underline = !parameters.Underline;
                    break;

                case (FORMAT_SUBSCRIPT):
                    parameters.ScriptOffset += 8;
                    break;

                case (FORMAT_SUPERSCRIPT):
                    parameters.ScriptOffset -= 8;
                    break;
                }
                Texture2D tex    = FontSprites[chr / FontUtil.CharsPerPage].Texture;
                int       index  = chr % FontUtil.CharsPerPage;
                int       offset = FontUtil.GetCharOffset(chr);
                int       width  = FontUtil.GetCharWidth(chr);

                var color      = parameters.Color(totalindex);
                var border     = parameters.Border(totalindex);
                var charOffset = parameters.Offset(totalindex);

                if (border.A > 0)
                { //Only draw outline if it's actually non-transparent
                    SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset - 1, parameters.ScriptOffset + 0), FontUtil.GetCharRect(index), border);
                    SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset, parameters.ScriptOffset + 1), FontUtil.GetCharRect(index), border);
                    SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset, parameters.ScriptOffset - 1), FontUtil.GetCharRect(index), border);
                    if (parameters.Bold)
                    {
                        SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset + 2, parameters.ScriptOffset + 0), FontUtil.GetCharRect(index), border);
                        SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset + 1, parameters.ScriptOffset + 1), FontUtil.GetCharRect(index), border);
                        SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset + 1, parameters.ScriptOffset - 1), FontUtil.GetCharRect(index), border);
                    }
                    else
                    {
                        SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset + 1, parameters.ScriptOffset), FontUtil.GetCharRect(index), border);
                    }
                }

                SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset, parameters.ScriptOffset), FontUtil.GetCharRect(index), color);
                if (parameters.Bold)
                {
                    SpriteBatch.Draw(tex, drawpos + charOffset + new Vector2(pos - offset + 1, parameters.ScriptOffset), FontUtil.GetCharRect(index), color);
                }

                if (chr == FORMAT_ICON)
                {
                    parameters.Icons[parameters.IconIndex].Draw(drawpos + charOffset + new Vector2(pos - offset, parameters.ScriptOffset) + new Vector2(8, 8));
                    parameters.IconIndex++;
                }

                pos += width + parameters.CharSeperator + (parameters.Bold ? 1 : 0);
                totalindex++;
            }
        }