Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.7 kB
2
Indexable
#include <SFML/Graphics.hpp>
#include <array>
#include <string>

class Menu {
public:
    Menu(float width, float height) {
        if (!font.loadFromFile("arial.ttf")) {
            // handle error
        }

        menu[0].setFont(font);
        menu[0].setFillColor(sf::Color::Red);
        menu[0].setString("Start");
        menu[0].setPosition(sf::Vector2f(width / 2, height / (MAX_NUMBER_OF_ITEMS + 1) * 1));

        menu[1].setFont(font);
        menu[1].setFillColor(sf::Color::White);
        menu[1].setString("Tutorial");
        menu[1].setPosition(sf::Vector2f(width / 2, height / (MAX_NUMBER_OF_ITEMS + 1) * 2));

        menu[2].setFont(font);
        menu[2].setFillColor(sf::Color::White);
        menu[2].setString("Quit");
        menu[2].setPosition(sf::Vector2f(width / 2, height / (MAX_NUMBER_OF_ITEMS + 1) * 3));

        selectedItemIndex = 0;
    }

    void draw(sf::RenderWindow &window) {
        for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++) {
            window.draw(menu[i]);
        }
    }

    void MoveUp() {
        if (selectedItemIndex - 1 >= 0) {
            menu[selectedItemIndex].setFillColor(sf::Color::White);
            selectedItemIndex--;
            menu[selectedItemIndex].setFillColor(sf::Color::Red);
        }
    }

    void MoveDown() {
        if (selectedItemIndex + 1 < MAX_NUMBER_OF_ITEMS) {
            menu[selectedItemIndex].setFillColor(sf::Color::White);
            selectedItemIndex++;
            menu[selectedItemIndex].setFillColor(sf::Color::Red);
        }
    }

    int GetPressedItem() {
        return selectedItemIndex;
    }

private:
    int selectedItemIndex;
    sf::Font font;
    sf::Text menu[3];

    static const int MAX_NUMBER_OF_ITEMS = 3;
};
Leave a Comment