aboutsummaryrefslogtreecommitdiff
path: root/include/gui/ComboBox.hpp
blob: 1f403715d5b83aead32a185411fcfadcbae5faae (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
#pragma once

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

#include <functional>
#include <string>
#include <vector>

namespace gsr {
    class ComboBox : public Widget {
    public:
        ComboBox(mgl::Font *font);
        ComboBox(const ComboBox&) = delete;
        ComboBox& operator=(const ComboBox&) = 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_selected_item(const std::string &id, bool trigger_event = true, bool trigger_event_even_if_selection_not_changed = true);
        const std::string& get_selected_id() const;

        mgl::vec2f get_size() override;

        std::function<void(const std::string &text, const std::string &id)> on_selection_changed;
    private:
        void draw_selected(mgl::Window &window, mgl::vec2f draw_pos);
        void draw_unselected(mgl::Window &window, mgl::vec2f draw_pos);
        void draw_item_outline(mgl::Window &window, mgl::vec2f pos, mgl::vec2f size);
        void update_if_dirty();
        float get_dropdown_arrow_height() const;
    private:
        struct Item {
            mgl::Text text;
            std::string id;
            mgl::vec2f position;
        };

        mgl::vec2f max_size;
        mgl::Font *font;
        std::vector<Item> items;
        mgl::Sprite dropdown_arrow;
        bool dirty = true;
        bool show_dropdown = false;
        size_t selected_item = 0;
    };
}