1

Im working in xna and my problem is the following. I got a text where the first occurence of " ________ " should be highlightet for the user in some way. This could be done by making the fontsize of this part bigger, highlighting or some other way if anyone got a great idea.

public void DrawStringWithStyle(SpriteBatch batch, SpriteFont thisFont, Vector2 pos, string thisText, SpriteFont BoldFont)
    {
        string[] paragraphs = Regex.Split(thisText, @"\\(c[a-fA-F0-9]{6})|\\(b)|\\(o)|\\(l)");
        SpriteFont CurrentFont = font;
        float tempPosX = pos.X;

        for (int i = 0; i < paragraphs.Length; i++)
        {
            batch.DrawString(CurrentFont, paragraphs[i], new Vector2(tempPosX, pos.Y), Color.Black);

            if (i + 1 < paragraphs.Length)
            {
                tempPosX += CurrentFont.MeasureString(paragraphs[i]).X;

                i++;

                switch (char.ToLower(paragraphs[i][0]))
                {
                    case 'o': CurrentFont = font; break;
                    case 'b': CurrentFont = BoldFont; break;
                    case 'l':
                        paragraphs[i+1] = paragraphs[i+1].Insert(0, Environment.NewLine);
                        tempPosX = pos.X;
                        break;
                }
            }
        }
    }

So i got 2 new problems as you might be able to tell. One of them is that 2 commands musnt come in a row because then it will screw up bigtime, need to be able to check if the next is a command or if its a normal paragraph somehow. The other problem needs a solution quite like that because my (l) command only works if the following paragraph isnt a command. Any idea on how to fix my 2 problems?

Skami
  • 1,506
  • 1
  • 18
  • 29
Drakthal
  • 193
  • 3
  • 14
  • sorry, my english is not enough to understand you. Why a row can not contain two commands? and why do you insert a newline in the command l? I think is easy do something like tempPos.X = pos.X; tempPos.Y+= CurrentFont.VerticalLineSpace; – Blau May 06 '12 at 16:31
  • Yes found out myself after some time that if 2 commands comes right after each other a paragraph consisting of "" will be created between them. Didnt know that and that was the problem, so fixed it. But thanks anyhow – Drakthal May 06 '12 at 19:13
  • it is the same if the command is at start, but why is that a problem? I look for that... that way the code is easier... if the spritebatch.drawstring gives any problem check if the paragraph has lenth>0 to draw it... – Blau May 06 '12 at 20:45

1 Answers1

2

Split the text that has different styles... and draw each part with its style.

You can use a \c to change color: "My \cFF5566favaourite \cFFFFFFgame is \c444444warcraft 3", or \b to use bold font...

public static void DrawStringWithStyle( this SpriteBatch batch, SpriteFont font, Vector2 pos, string text, Color color, SpriteFont BoldFont=null )
{
    string[] paragraphs = Regex.Split( text, @"\\(c[a-fA-F0-9]{6})|\\(b)|\\(n)" );
    Color CurrentColor = color;
    SpriteFont CurrentFont = font;

    for ( int i=0; i< paragraphs.Length; i++ )
    {
        batch.DrawString( CurrentFont, paragraphs[i], pos, CurrentColor );

        if ( i+1<paragraphs.Length )
        {
            pos.X += CurrentFont.MeasureString( paragraphs[i] ).X;
            i++;

            switch (char.ToLower(paragraphs[i][0]))
            {
                case 'c':
                    CurrentColor.R = byte.Parse( paragraphs[i].Substring( 1, 2 ) );
                    CurrentColor.G = byte.Parse( paragraphs[i].Substring( 3, 2 ) );
                    CurrentColor.B = byte.Parse( paragraphs[i].Substring( 5, 2 ) );
                    break;
                case 'n': CurrentFont = font; break;
                case 'b': CurrentFont = BoldFont; break;
            }
        }
    }
}
Blau
  • 5,742
  • 1
  • 18
  • 27
  • This seems like an awesome idea Blau, but you should elaborate! I'm not sure that user1322838 will be able to code this from a scratch, so it would be nice if you could put it in code, or at the very least give ideas on how you would go about implementing it. – neeKo May 05 '12 at 19:27
  • 1
    Ok... I'll do... but maybe he wants do it by himself... :) – Blau May 05 '12 at 19:52
  • Hey ive added some more to the question since ive encountered a problem and i cant really find out how to fix it. – Drakthal May 06 '12 at 15:38