Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: a better way to select options from menu?  (Read 3302 times)

0 Members and 1 Guest are viewing this topic.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
a better way to select options from menu?
« on: August 30, 2014, 06:34:49 pm »
hi,
i'd like some suggestions on how to better manage the cursor position on a menu.
for now, i have a enum that holds all options, like
enum CursorPosition {NEW_GAME, LOAD_GAME, OPTIONS, EXIT);
when the user press DownArrow, the CursorPosition goes from NEW_GAME to LOAD_GAME. if he press it again, the cursor goes to OPTIONS, etc. as its not possible to directly iterate trough enums, i use static_cast. but i feel it's too messy.
here is an example.

#include <SFML/Graphics.hpp>
enum Cursor{FIRST, SECOND, THIRD, FOURTH};

int main(){
    sf::RenderWindow window(sf::VideoMode(600, 480), "SFML window");
    const size_t num_options = 3;
    sf::RectangleShape rect[num_options];
    for (size_t n=0; n<num_options; n++){
        rect[n].setPosition(20, n*100+20);
        rect[n].setSize(sf::Vector2f(560, 80));
    }
    Cursor cursor = FIRST;
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }

            //////////////////////////////////////////////
            //Here is where I handle the cursor movement//
            else if (event.type == sf::Event::KeyPressed){
                int c = static_cast<int>(cursor);
                switch(event.key.code){
                    case sf::Keyboard::Up:{
                        c--;
                        if (c<0){c=0;}
                        break;
                    }
                    case sf::Keyboard::Down:{
                        c++;
                        if (c>=num_options){c=num_options-1;}
                        break;
                    }
                    default:{
                        break;
                    }
                }
            cursor = static_cast<Cursor>(c);
            }
        }
        window.clear(sf::Color::White);
        for (size_t n=0; n<num_options; n++){
            rect[n].setFillColor(sf::Color::Green);
            if (cursor == static_cast<Cursor>(n)){
                rect[n].setFillColor(sf::Color::Red);
            }
            window.draw(rect[n]);
        }
        window.display();
    }
    return 0;
}

is that a good approach? or is there some better way to make the cursor move on a menu? how do people usually do that?
sorry for this being more a design-related question  :P

thanks in advance!
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: a better way to select options from menu?
« Reply #1 on: August 30, 2014, 07:11:00 pm »
I don't know how SFGUI and other GUI toolkits handle this (unless you count the proprietary one I use at work), and there's a good chance that "use one of them" is the correct answer if you want to handle all these little details properly, but assuming you want to do this yourself...

Personally, I would always have some kind of Button class, and there would be a container somewhere of all the Buttons currently shown on screen, and some variable indicating which if any of them was currently "selected" by arrow key navigation.  On each arrow key event, I would simply iterate over the container and calculate each button's distance and direction from the currently "selected" button, and examine those values to select the optimal button to move to.

This may sound slow since it iterates over all the buttons on every keyboard event, but keep in mind this is only once per event, not once per frame.  And you shouldn't have a prohibitively large number of buttons on a menu screen anyway.  More importantly, this approach would make it easy to add/remove/reposition those buttons later (not to mention adding/removing additional menus) without also having to change your keyboard navigation logic.  So that's why I would do it this way.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: a better way to select options from menu?
« Reply #2 on: August 31, 2014, 08:28:50 pm »
good idea. this way thing will be more clear. i'll create a Button class, an array to hold it's objects in each menu, and iterate trough them.
thanks for the hint :)
Visit my game site (and hopefully help funding it? )
Website | IndieDB