#include "menu.h"

Menu::Menu(int MenustartY)
{
    startY = MenustartY;
    selection = 0;
    selector.change("-");
}

void Menu::add(std::string newText)
{
    elements.push_back(Text(newText));
}

void Menu::change(int i, std::string newText)
{
    elements[i].change(newText);
}

int Menu::input()
{
    #ifndef PSP
        if (event.type == SDL_KEYDOWN)
        {
            switch (event.key.keysym.sym)
            {
                case SDLK_w:     --selection; break;
                case SDLK_s:     ++selection; break;
                case SDLK_SPACE: return selection;
            }
        }
    #else
        if (event.type == SDL_JOYBUTTONDOWN)
        {
            switch (event.jbutton.button)
            {
                case UP:    --selection; break;
                case DOWN:  ++selection; break;
                case CROSS: return selection;
            }
        }
    #endif

    if (selection < 0)
        selection = elements.size() - 1;
    else if (selection > elements.size() - 1)
        selection = 0;

    return -1;
}

void Menu::draw() const
{
    for (int i = 0; i < elements.size(); ++i)
    {
        elements[i].draw(SCREENWIDTH / 2, startY + 2 * elements[i].getH() * i, true, false);
    }
    selector.draw(SCREENWIDTH / 2 - elements[selection].getW() / 2 - 5, startY + 2 * elements[selection].getH() * selection, true, false);
    selector.draw(SCREENWIDTH / 2 + elements[selection].getW() / 2 + 5, startY + 2 * elements[selection].getH() * selection, true, false);
}
