Circular menu

Started by
7 comments, last by Sanctux 14 years, 3 months ago
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?
Jack
Advertisement
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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);}
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
I remember doing this just for fun like 5years ago. I can't guarantee good programming methods, but here you go:

http://rapidshare.com/files/325562352/Menu_and_sources.rar.html
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.
Jack
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?
Jack
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.
I trust exceptions about as far as I can throw them.
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.
Thanks fellas. [smile]
Jack

This topic is closed to new replies.

Advertisement