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

XNA Menus

, 28 Aug 2010
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ed Nutting
Founder
United Kingdom United Kingdom
Hi there, so you're reading this? I probably ought to tell you something helpful then...
 
Okay well, I'm 17 from London, UK and I have been programming since I was 8 years old. You'll see from my profile I've dabbled in a lot of different areas and I do know what I'm talking about so as they say "gimme some respect yeah?" Smile | :) I've done GCSE's (got 7A*'s, 2A's and a B in French) and am now studying for my AS-Levels (Math, Further Maths, Physics and Chemistry). My main aim is to go to university to study electronic engineering and then work in robotics (preferably humanoid but most of the field is interesting).
 
Thanks for reading,
Edward Nutting
Follow on   Twitter

Comments and Discussions

 
NewsXNA Notification Box PinmemberEdMan19612-Nov-10 1:43 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140718.1 | Last Updated 28 Aug 2010
Article Copyright 2010 by Ed Nutting
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid