aboutsummaryrefslogtreecommitdiff
path: root/src/gui/ComboBox.cpp
blob: dd683e163aa5c13195461f91b81740c1679331f5 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "../../include/gui/ComboBox.hpp"
#include "../../include/gui/Utils.hpp"
#include "../../include/Theme.hpp"
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/graphics/Font.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <assert.h>

namespace gsr {
    static const float padding_top = 10.0f;
    static const float padding_bottom = 10.0f;
    static const float padding_left = 10.0f;
    static const float padding_right = 10.0f;

    ComboBox::ComboBox(mgl::Font *font) : font(font) {
        assert(font);
    }

    bool ComboBox::on_event(mgl::Event &event, mgl::Window&) {
        if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) {
            const mgl::vec2f mouse_pos = { (float)event.mouse_button.x, (float)event.mouse_button.y };
            const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom);

            if(show_dropdown && !items.empty()) {
                mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top);
                pos.y += items[selected_item].text.get_bounds().size.y + padding_top + padding_bottom;

                for(size_t i = 0; i < items.size(); ++i) {
                    Item &item = items[i];
                    const mgl::FloatRect text_bounds = item.text.get_bounds();
                    if(mgl::FloatRect(pos - mgl::vec2f(padding_left, padding_top), item_size).contains(mouse_pos)) {
                        selected_item = i;
                        show_dropdown = false;
                        return false;
                    }
                    pos.y += text_bounds.size.y + padding_top + padding_bottom;
                }
            }

            if(mgl::FloatRect(position, item_size).contains(mouse_pos)) {
                show_dropdown = !show_dropdown;
                move_to_top = true;
            } else {
                show_dropdown = false;
            }
        }
        return true;
    }

    void ComboBox::draw(mgl::Window &window) {
        update_if_dirty();

        if(items.empty())
            return;

        const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom);
        const mgl::vec2i mouse_pos = window.get_mouse_position();
        bool inside = false;

        mgl::Rectangle background(position, mgl::vec2f(max_size.x, item_size.y));
        if(show_dropdown) {
            background.set_size(max_size);
            background.set_color(mgl::Color(0, 0, 0));
        } else {
            background.set_color(mgl::Color(0, 0, 0, 220));
        }
        window.draw(background);

        mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top);

        Item &item = items[selected_item];
        item.text.set_position(pos.floor());
        if(show_dropdown) {
            const int border_size = 3;
            const mgl::Color border_color = gsr::get_theme().tint_color;
            draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size, border_color, border_size);
        }
        window.draw(item.text);
        pos.y += item.text.get_bounds().size.y + padding_top + padding_bottom;

        for(size_t i = 0; i < items.size(); ++i) {
            Item &item = items[i];
            item.text.set_position(pos.floor());
            const mgl::FloatRect text_bounds = item.text.get_bounds();

            if(show_dropdown) {
                if(!inside) {
                    inside = mgl::FloatRect(text_bounds.position - mgl::vec2f(padding_left, padding_top), item_size).contains({ (float)mouse_pos.x, (float)mouse_pos.y });
                    if(inside) {
                        mgl::Rectangle item_background(text_bounds.position - mgl::vec2f(padding_left, padding_top), item_size);
                        item_background.set_color(gsr::get_theme().tint_color);
                        window.draw(item_background);
                    } else {
                        /*const int border_size = 3;
                        const mgl::Color border_color(150, 150, 150);
                        draw_rectangle_outline(window, text_bounds.position, item_size, border_color, border_size);*/
                    }
                }
                window.draw(item.text);
            }

            pos.y += text_bounds.size.y + padding_top + padding_bottom;
        }
    }

    void ComboBox::add_item(const std::string &text, const std::string &id) {
        items.push_back({mgl::Text(text, *font), id});
        dirty = true;
    }

    void ComboBox::update_if_dirty() {
        if(!dirty)
            return;

        max_size = { 0.0f, font->get_character_size() + padding_top + padding_bottom };
        for(Item &item : items) {
            const mgl::vec2f bounds = item.text.get_bounds().size;
            max_size.x = std::max(max_size.x, bounds.x + padding_left + padding_right);
            max_size.y += bounds.y + padding_top + padding_bottom;
        }
        dirty = false;
    }

    mgl::vec2f ComboBox::get_size() {
        update_if_dirty();
        return { max_size.x, font->get_character_size() + padding_top + padding_bottom };
    }
}