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:
This structure requires
MenuName as the name you want your menu to be identified by,
PositionY are the top left
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:
- Load a menu
- Add an
- Optionally set what your current menu is
- Get input from the menu
- 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:
string Files = Directory.GetFiles(Content.RootDirectory + "\\Menus\\");
foreach (string FileName in Files)
Menu AMenu = new 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;
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() 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!
- 28th August, 2010: Initial post