Click here to Skip to main content
15,867,756 members
Articles / Game Development

XNA Menus

Rate me:
Please Sign up or sign in to vote.
4.48/5 (7 votes)
28 Aug 2010CPOL2 min read 38.3K   1.1K   15   3
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
<?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:

C#
//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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralXNA Menus Pin
Member 766814413-Feb-11 8:23
Member 766814413-Feb-11 8:23 
NewsXNA Notification Box Pin
Ed Nutting12-Nov-10 1:43
Ed Nutting12-Nov-10 1:43 
GeneralMy vote of 3 Pin
tec-goblin31-Aug-10 2:19
tec-goblin31-Aug-10 2:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.