Sign in to follow this  
Sanctux

Circular menu

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this