aboutsummaryrefslogtreecommitdiff
path: root/include/gui/DropdownButton.hpp
blob: 306a327aa34ef68d73b6d8ee2642ce505f39f159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once

#include "Widget.hpp"
#include <string>
#include <functional>
#include <vector>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Sprite.hpp>

namespace gsr {
    class DropdownButton : public Widget {
    public:
        DropdownButton(mgl::Font *title_font, mgl::Font *description_font, const char *title, const char *description_activated, const char *description_deactivated, mgl::Texture *icon_texture, mgl::vec2f size);
        DropdownButton(const DropdownButton&) = delete;
        DropdownButton& operator=(const DropdownButton&) = delete;

        bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override;
        void draw(mgl::Window &window, mgl::vec2f offset) override;

        void add_item(const std::string &text, const std::string &id);
        void set_item_label(const std::string &id, const std::string &new_label);

        void set_activated(bool activated);

        mgl::vec2f get_size() override;

        std::function<void(const std::string &id)> on_click;
    private:
        void update_if_dirty();
    private:
        struct Item {
            mgl::Text text;
            std::string id;
        };

        std::vector<Item> items;
        mgl::Font *title_font;
        mgl::Font *description_font;
        mgl::vec2f size;
        bool mouse_inside = false;
        bool show_dropdown = false;
        bool dirty = true;
        mgl::vec2f max_size;
        int mouse_inside_item = -1;

        mgl::Text title;
        mgl::Text description;
        mgl::Sprite icon_sprite;

        std::string description_activated;
        std::string description_deactivated;
        bool activated = false;
    };
}