Saturday, March 29, 2008

New Menu Code

Download source code for this project

Okay, It's been busy at home lately. I've been moving it. But I've been trying to find a little time every day to work on my XNA stuff, and I've got a much more complete menu system now.

I've made some changes:

- The menu now shows a default "Return" choice at the bottom of each list. This can be turned off by setting the "showBackChoice" boolean.
- The menu now has two types of choices, Normal and LeftRight. Normal choice will show their child choices as a separate menu when they are executed. LeftRight choices will show their child choices as a list to the left of the parent choice when they are SELECTED.

I've also made the sample code show a bit more info in the console as you are viewing the menu. The sample code initializes the menu as follows:

mainMenu.AddChoice("Choice 1");

MenuChoice choice = mainMenu.AddChoice("LR Choice");

choice.AddLeftRightChoices(new string[] { "one", "two", "three" });

choice = mainMenu.AddChoice("Sub Menu");

choice.AddChoice("Sub choice 1");

choice.AddChoice("Sub choice 2");

mainMenu.ChoiceExecuted += new Menu.ChoiceExecutedHandler(ChoiceExecuted);

mainMenu.ChoiceSelected += new Menu.ChoiceSelectedHandler(ChoiceSelected);

mainMenu.ChoiceDeselected += new Menu.ChoiceDeselectedHandler(ChoiceDeselected);


So there you go. Pretty simple. If I make any more changes, I'll be sure to post them. Possible changes could be:

- using a proper spriteFont to display the text (so we could use something besides Times new Roman).
- making background images actually work.
- something I overlooked....

I've realized as I've been working on this that is might have been easier to just implement it using a full on tree, rather than a bunch of sneaky lists.

4 comments:

Clay said...

Excellent work Luke!!! I was looking for something like this for a long time.. Can you tell me how can I change the color of my Menu text, something like using some function like color.blue; i know i can make changes to the font library, but I will have to make changes whenever i want to make chagnes to the font.
Can you give more options to change the color in the next version Please..
thank you ..
Keep up the great work..

Luke Rymarz said...

Hi irwizeel,

It's good to hear you found a use for this code :).

Changing the font color is actually very easy. Simply set the textColor and selectColor properties of the menu class to whichever colors you want. here's a code snippet:

mainMenu = new GameMenu.Menu(this);
mainMenu.textColor = Color.Pink;
mainMenu.selectColor = Color.Orange;

I noticed there was a bug with the "return" choice. It was always white text instead of the custom color. I made a quick fix for it and updated the link in the main post, so be sure to download the latest code when you try this out.

Kristian F. Erikson said...

Hi Luke

Thanks for putting your code up and making it available :)
We've adapted your menu code and are now using it for our university project 'Robot Soccer Visualization'. If you're interested have a look past:
http://www.erikson.dk/blog/?cat=18

Thanks again
Kristian

Anonymous said...
This comment has been removed by a blog administrator.