## Recommended Posts

Hi, I was wondering how you guys would approach this problem. Say I wanted to write a circular menu like so, assuming this menu had 6 items:
Menu Item 5    [Menu Item 6]    Menu Item 1
And when I press the right arrow:
Menu Item 6    [Menu Item 1]    Menu Item 2
And again:
Menu Item 1    [Menu Item 2]    Menu Item 3
And so on. The items would smoothly slide over to the left, and emerging items on the right side would fade in; items leaving on the left side would fade out. How would you approach this?

##### Share on other sites
You have an array of six items, and an index into that array to represent the selected item. Pressing left/right decrements/increments the index, but you wrap the value if it is less than zero, or greater than the length of the array.

##### Share on other sites
Circular array ?
const unsigned short MAX_OPTIONS = 3;Menu menu[MAX_OPTIONS] = { Menu("opt1"), Menu("opt2"), Menu("opt3") };unsigned int currentMenuIndex = 0 ;if(RightKeyPressed){  //wrap around  currentMenuIndex = ++currentMenuIndex % MAX_OPTIONS;  display(menu,currentMenuIndex);}

##### Share on other sites
I remember doing this just for fun like 5years ago. I can't guarantee good programming methods, but here you go:

##### Share on other sites
Thanks for the replies, but I actually had a very similar method in mind. I'm curious, however, to how other people would approach the graphical aspect.

##### Share on other sites
Scratch that; I've run into a problem. What if I wanted to do the following:
menuItems.push_back("Item1");menuItems.push_back("Item2");menuItems.push_back("Item3");menuItems[index + 7]; // Or some other arbitrary number

and automatically convert that into a valid element?

Then, menuItems[index + 11] would be "Item3" and menuItems[index + 21] would be "Item1" and so on?

##### Share on other sites
Well you could always overload the [] operator to modulus the argument with the vector's size, but I can't imagine why you would ever want to do that.

Just perform the modulus in the calling code. It's a lot cleaner.

##### Share on other sites
In case you haven't used modulus before, you write it as menuItems[(index + X) % menuItems.size()]. The % operator will give you the remainder from division, so if you write 10 % 3 for example, 3 will be subtracted from 10 until the remaining result is less than 3, so the answer will be 1.

##### Share on other sites
Thanks fellas. [smile]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628332
• Total Posts
2982120

• 22
• 9
• 9
• 13
• 11