Click here to Skip to main content
Click here to Skip to main content

XNA Notification Box

, 28 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A notification box you can put in your game to update your user as to what's happening (as seen in a lot of shooting games)

Introduction

It is surprisingly difficult to find code on the internet that will let you easily display a notification box where, when and what size you want. Well, I've now produced a class that will (hopefully) solve all these problems.

Using the Code

The class is simple and to use it, you need only change a few small things. These are:

  1. FontLocation - The location of the spriteFont used to draw the text
  2. DisplayedText - Simply do 'DisplayedText = ' to add a line of text to the box

My code is written so that the notification box is displayed for a set length of time, then fades out till it is transparent. It is displayed again when more text is added. The code automatically splits the text into lines so none spills over the edge of the specified rectangle. When you do 'DisplayedText = ', my code adds it to the start of the current text, splits the text into lines, then displays it. The hardest bit of the code was splitting the text up into lines as otherwise there would be one endless string of text that spilled outside the notification box. The code I used was in part from the MSDN website but modified to allow for more flexibility and so it did what I actually wanted. The final code looks like this:

private string parseText(string SomeText)
{
	//Create a string to return that is empty
	String returnString = String.Empty;
	//Create a list of lines that are already in the text 
         //so that they remain separated
	String[] ExistingLines = SomeText.Split("\n".ToCharArray());
	//Create a list to contain the new lines of text.
	List<String> Lines = new List<String>();
	//For every existing line, check its length and split it up if it's too long.
	foreach (String ALine in ExistingLines)
	{
		//Current line that is being split up
		String line = String.Empty;
		//Lines that this existing line has been split up into
		List<String> CLines = new List<String>();
		//Words in this existing line
		String[] wordArray = ALine.Split(' ');
		//For each word, check if it will fit on the current line, 
		//if not create a new line and add the old one to the CLines object.
		foreach (String word in wordArray)
		{
			//Check to see if word will fit on current line.
			if (TheFont.MeasureString(line + word + " ").Length() > 
							SurroundingBox.Width)
			{
				//If not, add the line to CLines with a new line 
				character on the end to make sure that the text is 
				split when it is drawn.
				CLines.Add(line + "\n");
				//Reset the current line to blank.
				line = String.Empty;
			}
			//Add the word to the current line.
			line = line + word + ' ';
		}
		//Add the current line to Clines as it won't already have been added.
		CLines.Add(line + "\n");
		//For every line this existing line has been split up into, 
						add it to the final set of lines.
		foreach (string TheLine in CLines)
		{
			Lines.Add(TheLine);
		}
	}
	//Remove the first line until the number of lines is less than the maximum.
	while(Lines.Count > MaxLines)
	{
		Lines.RemoveAt(0);
	}
	//Add the final set of lines to the return string.
	for (int i = 0; i < Lines.Count; i++)
	{
		returnString += Lines[i];
	}
	return returnString;
}

As you can see, it looks at the text it has been passed, splits it up into existing lines, then looks at those to see if any are too long. If they are, the code splits them up and re-adds the new lines in place of the original line. Finally, it concatenates all those lines into one string and returns that string.

And that's it! Apart from calling the draw, update and resize functions at the relevant points, all you need to do is give the box some text to display!

Points of Interest

I found it both interesting and annoying that although the spriteBatch.DrawString function recognises the escape character \n and draws a new line, no function for drawing a string inside a box is provided.

History

  • 28th August, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ed Nutting
Student
United Kingdom United Kingdom
Hi,
 
I am Edward Nutting, born March 1996 and I am currently an Electrical and Electronic Engineering undergraduate at the University of Bristol, UK. I've been self-teaching programming since I was eight years old and have dabbled in many different areas from web development (where I had my own advertising startup) to low level operating system stuff (my current project Fling OS).
 
The articles and code on this profile are some of my early work (from when I was aged about 13 to 16) but despite my young age, it has been well received by CodeProject users. My more recent work has been for companies or on projects hosted on GitHub, CodePlex or BitBucket.
 
I keep an eye on many things techy and am open to any and all ideas, so please feel free to contact me if you think I can help! Smile | :)
 
Cheers,
Ed Nutting
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 28 Aug 2010
Article Copyright 2010 by Ed Nutting
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid