Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XNA Menus

0.00/5 (No votes)
28 Aug 2010 1  
How to do menus easily using XNA. Easy to use classes that display menus.

Introduction

This code allows you to display a menu with unlimited options and results very easily.

Using the Code

There are two classes and an XML file that are key to each new menu you want. The two classes are Menu.cs and MenuItem.cs. To use the classes, you must first create a menu XML file. To create a menu, you must create a folder in the Content part of your game project called Menus. Within that, create an XML file with the following structure:

<?xml version="1.0" encoding="utf-8" ?>

<Menu>
	<MenuName>Main Menu</MenuName>
	<MenuItem>
		<MenuItemText>Option 1</MenuItemText>
		<MenuItemEvent>ShowMessageBox</MenuItemEvent>
		<EventParams>Option 1</EventParams>
	</MenuItem>
	<PositionX>10</PositionX>
	<PositionY>10</PositionY>
	<SelectedItemNum>0</SelectedItemNum>
</Menu>

This structure requires MenuName as the name you want your menu to be identified by, PositionX and PositionY are the top left x and y coordinates of your menu when it is displayed. You can have as many MenuItem sets as you like. Within a MenuItem must be MenuItemText, the text that is displayed to the user, MenuItemEvent, the name of the method to call when the user selects that item and optionally you can have EventParams, used as extra parameters if you have a method that needs them.

To use the menu, you need to do the above and 5 more things to your code:

  1. Load a menu
  2. Add an EventHandler
  3. Optionally set what your current menu is
  4. Get input from the menu
  5. Draw the menu

To load a menu, you have to get a list of XML files that are in your Menus folder, then create a new menu and load it using the selected file name. My code works on the basis that you can have multiple menus so loads all available in LoadContent() and has a method for selecting one of them. The code for loading looks like the following:

//Get a list of the xml files
string[] Files = Directory.GetFiles(Content.RootDirectory + "\\Menus\\");
//Loop through each one
foreach (string FileName in Files)
{
	//Create a new menu
	Menu AMenu = new Menu();
	//Tell it load the file
	AMenu.Load(FileName);
	//Add it to the list
	Menus.Add(AMenu);
	//If it is the Main Menu set it as the current menu
	if (AMenu.MenuTitle == "Main Menu")
	{
		CurrentMenu = AMenu;
	}
	foreach (MenuItem AnItem in AMenu.MenuItems)
	{
		AnItem.OnConfirmedEvent += new EventHandler(AnItem_OnConfirmedEvent);
	}
}

My code gets a list of files, loops through them creating a new menu and loading it. It also checks if the menu it has just loaded is the Main Menu and then sets it as the CurrentMenu being displayed. To use a menu, you must also add an event handler to all the menu items' OnConfirmedEvents. Whilst adding them, you can distinguish between what methods you set the events to call, but I chose to set them all to the same method. This method is as follows:

private void AnItem_OnConfirmedEvent(object sender, EventArgs e)
{
	MenuItem TheItem = (MenuItem)sender;
        switch (TheItem.EventName)
        {
            case "ShowMessageBox":
                System.Windows.Forms.MessageBox.Show(TheItem.EventParams[0]);
                break;
            case "Quit":
                this.Exit();
                break;
            default:
                break;
        }
}

It gets what MenuItem called it, then looks at the method name and if it's equal to ShowMessageBox, then it shows a message box with the first event parameter in it. My menu also has GetInput() and Draw() methods. GetInput() is used in update when you wish to allow the user to select a menu option.

That's it! Not much to it really but it can be surprisingly difficult to do for yourself. It took me a while to get this working.

Points of Interest

I had fun working out a sensible XML file standard that would encompass everything I wanted to do and thus be flexible, it's surprisingly difficult!

History

  • 28th August, 2010: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here