aboutsummaryrefslogtreecommitdiff
path: root/src/Tabs.cpp
blob: 3d0dc765ab65acdd6a38afd4acf879403c6e6dc8 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "../include/Tabs.hpp"
#include "../include/Body.hpp"
#include "../include/ResourceLoader.hpp"
#include "../include/Utils.hpp"
#include "../include/Config.hpp"
#include "../include/Theme.hpp"
#include "../include/Scale.hpp"
#include <mglpp/window/Event.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/graphics/Texture.hpp>
#include <cmath>

namespace QuickMedia {
    static const float tab_text_size = std::floor(get_config().tab.font_size * get_config().scale * get_config().font_scale);
    static const float tab_height = tab_text_size + std::floor(10.0f * get_config().scale);
    static const float tab_min_width = 250.0f;
    static const float tab_margin_x = 10.0f;

    // static
    float Tabs::get_height() {
        return tab_height;
    }

    // static
    float Tabs::get_shade_height() {
        return tab_height + std::floor(10.0f * get_config().scale);
    }

    Tabs::Tabs(mgl::Shader *rounded_rectangle_shader, mgl::Color shade_color) : background(mgl::vec2f(1.0f, 1.0f), 10.0f * get_config().scale, get_theme().selected_color, rounded_rectangle_shader), shade_color(shade_color) {
        shade.set_color(shade_color);
        arrow_sprite.set_texture(TextureLoader::get_texture("images/arrow.png"));
    }

    Tabs::Tabs(mgl::Shader *rounded_rectangle_shader) : Tabs(rounded_rectangle_shader, get_theme().shade_color) {}

    int Tabs::add_tab(std::string title, Body *body) {
        assert(body);
        tabs.push_back({ mgl::Text(std::move(title), *FontLoader::get_font(FontLoader::FontType::LATIN, tab_text_size)), body} );
        return tabs.size() - 1;
    }

    void Tabs::move_selected_tab(int new_tab) {
        const int prev_tab = selected_tab;
        const int tab_diff = new_tab - prev_tab;

        if(tab_diff > 0) {
            while(selected_tab < new_tab) {
                ++selected_tab;
                const float scroll_fixed = scroll + (tab_offset * width_per_tab);
                if(scroll_fixed + tab_index_to_x_offset(selected_tab) + tab_background_width > container_width - tab_margin_x)
                    --tab_offset;
            }
        } else if(tab_diff < 0) {
            while(selected_tab > new_tab) {
                --selected_tab;
                const float scroll_fixed = scroll + (tab_offset * width_per_tab);
                if(scroll_fixed + tab_index_to_x_offset(selected_tab) + tab_background_width < tab_margin_x)
                    ++tab_offset;
            }
        } else {
            return;
        }

        if(on_change_tab)
            on_change_tab(prev_tab, selected_tab);
    }

    void Tabs::on_event(mgl::Event &event) {
        if(event.type == mgl::Event::KeyPressed && event.key.control && !tabs.empty()) {
            bool move_left = (event.key.code == mgl::Keyboard::Left || (event.key.alt && event.key.code == mgl::Keyboard::H));
            move_left |= (event.key.code == mgl::Keyboard::Tab && event.key.shift);

            bool move_right = (event.key.code == mgl::Keyboard::Right || (event.key.alt && event.key.code == mgl::Keyboard::L));
            move_right |= (event.key.code == mgl::Keyboard::Tab && !event.key.shift);

            if(move_left) {
                if(selected_tab > 0)
                    move_selected_tab(selected_tab - 1);
            } else if(move_right) {
                if(selected_tab < (int)tabs.size() - 1)
                    move_selected_tab(selected_tab + 1);
            } else if(event.key.code >= mgl::Keyboard::Num1 && event.key.code <= mgl::Keyboard::Num9) {
                const int tab_target = event.key.code - mgl::Keyboard::Num1;
                if(tab_target < (int)tabs.size())
                    move_selected_tab(tab_target);
            }
        }
    }

    static mgl::View create_scissor_view(mgl::vec2f pos, mgl::vec2f size) {
        mgl::View view = { mgl::vec2i(pos.x, pos.y), mgl::vec2i(size.x, size.y) };
        return view;
    }

    void Tabs::draw(mgl::Window &window, mgl::vec2f pos, float width) {
        if(width - tab_margin_x < 0.0f || tabs.empty()) return;

        container_width = width;

        const int num_visible_tabs = std::min((int)tabs.size(), std::max(1, (int)(width / tab_min_width)));
        width_per_tab = std::floor(width / num_visible_tabs);
        const float tab_text_y = std::floor(pos.y + tab_height*0.5f - tab_text_size);
        tab_background_width = std::floor(width_per_tab - tab_margin_x*2.0f);
        background.set_size(mgl::vec2f(tab_background_width, tab_height));

        if(shade_color != mgl::Color(0, 0, 0, 0)) {
            shade.set_size(mgl::vec2f(width, get_shade_height()));
            shade.set_position(mgl::vec2f(std::floor(pos.x), std::floor(pos.y)));
            window.draw(shade);
        }

        float scroll_fixed = scroll + (tab_offset * width_per_tab);

        float overflow_last = (scroll_fixed + tab_index_to_x_offset(tabs.size() - 1) + tab_background_width) - (width - tab_margin_x);
        if(overflow_last < 0.0f)
            scroll_fixed -= overflow_last;

        float overflow = (scroll_fixed + tab_index_to_x_offset(selected_tab) + tab_background_width) - (width - tab_margin_x);
        float underflow = scroll_fixed + tab_index_to_x_offset(selected_tab);
        if(overflow > 0.0f)
            scroll_fixed -= overflow;
        else if(underflow < 0.0f)
            scroll_fixed -= underflow;

        bool tabs_cutoff_left = false;
        bool tabs_cutoff_right = false;
        const auto start_pos = pos;

        const mgl::View prev_view = window.get_view();

        pos.x += scroll_fixed;
        for(size_t i = 0; i < tabs.size(); ++i) {
            const int index = i;
            const float background_pos_x = std::floor(pos.x + tab_index_to_x_offset(i));
            if(background_pos_x - start_pos.x >= width - tab_margin_x) {
                tabs_cutoff_right = true;
                break;
            } else if(background_pos_x + tab_background_width - start_pos.x <= tab_margin_x) {
                tabs_cutoff_left = true;
                continue;
            }

            if((int)index == selected_tab) {
                background.set_position(mgl::vec2f(background_pos_x, std::floor(pos.y)));
                background.draw(window);
            }

            mgl::Text &tab_text = tabs[index].text;
            float text_pos_x = std::floor(pos.x + i*width_per_tab + width_per_tab*0.5f - tab_text.get_bounds().size.x*0.5f);
            text_pos_x = std::max(text_pos_x, background_pos_x);

            window.set_view(create_scissor_view({ text_pos_x, tab_text_y }, { tab_background_width, tab_height }));
            window.draw(tab_text);
            window.set_view(prev_view);
        }

        const float lw = std::floor(25.0f * get_config().scale);
        const float lh = background.get_size().y;

        if(tabs_cutoff_left) {
            mgl::Vertex gradient_points[4];
            gradient_points[0].position = mgl::vec2f(start_pos.x + tab_margin_x,       start_pos.y);
            gradient_points[1].position = mgl::vec2f(start_pos.x + tab_margin_x + lw,  start_pos.y);
            gradient_points[2].position = mgl::vec2f(start_pos.x + tab_margin_x + lw,  start_pos.y + lh);
            gradient_points[3].position = mgl::vec2f(start_pos.x + tab_margin_x,       start_pos.y + lh);

            gradient_points[0].color = shade_color;
            gradient_points[1].color = mgl::Color(shade_color.r, shade_color.g, shade_color.b, 10);
            gradient_points[2].color = mgl::Color(shade_color.r, shade_color.g, shade_color.b, 10);
            gradient_points[3].color = shade_color;

            window.draw(gradient_points, 4, mgl::PrimitiveType::Quads);

            mgl::vec2i arrow_sprite_width = arrow_sprite.get_texture()->get_size();
            arrow_sprite_width.y = arrow_sprite_width.x;
            const mgl::vec2f arrow_scale_ratio = get_ratio(arrow_sprite_width, clamp_to_size_y(arrow_sprite_width, (int)lh - 8));
            arrow_sprite.set_scale(arrow_scale_ratio);
            arrow_sprite.set_origin(arrow_sprite.get_texture()->get_size().to_vec2f() * 0.5f);
            arrow_sprite.set_position(start_pos + mgl::vec2f((int)(arrow_sprite.get_texture()->get_size().x * 0.5f * arrow_scale_ratio.x) + 5, (int)(lh * 0.5f)));
            arrow_sprite.set_rotation(-90.0f);
            arrow_sprite.set_color(get_theme().arrow_color);
            window.draw(arrow_sprite);
        }

        if(tabs_cutoff_right) {
            mgl::Vertex gradient_points[4];
            gradient_points[0].position = mgl::vec2f(start_pos.x + width - lw - tab_margin_x,    start_pos.y);
            gradient_points[1].position = mgl::vec2f(start_pos.x + width,                        start_pos.y);
            gradient_points[2].position = mgl::vec2f(start_pos.x + width,                        start_pos.y + lh);
            gradient_points[3].position = mgl::vec2f(start_pos.x + width - lw - tab_margin_x,    start_pos.y + lh);

            gradient_points[0].color = mgl::Color(shade_color.r, shade_color.g, shade_color.b, 10);
            gradient_points[1].color = shade_color;
            gradient_points[2].color = shade_color;
            gradient_points[3].color = mgl::Color(shade_color.r, shade_color.g, shade_color.b, 10);

            window.draw(gradient_points, 4, mgl::PrimitiveType::Quads);

            mgl::vec2i arrow_sprite_width = arrow_sprite.get_texture()->get_size();
            arrow_sprite_width.y = arrow_sprite_width.x;
            const mgl::vec2f arrow_scale_ratio = get_ratio(arrow_sprite_width, clamp_to_size_y(arrow_sprite_width, (int)lh - 8));
            arrow_sprite.set_scale(arrow_scale_ratio);
            arrow_sprite.set_origin(arrow_sprite.get_texture()->get_size().to_vec2f() * 0.5f);
            arrow_sprite.set_position(start_pos + mgl::vec2f(width - (int)(arrow_sprite.get_texture()->get_size().x * 0.5f * arrow_scale_ratio.x) - 5, (int)(lh * 0.5f)));
            arrow_sprite.set_rotation(90.0f);
            arrow_sprite.set_color(get_theme().arrow_color);
            window.draw(arrow_sprite);
        }
    }

    void Tabs::set_text(int index, const std::string &text) {
        if(index < 0 || index >= (int)tabs.size() || text == tabs[index].text.get_string())
            return;
        tabs[index].text.set_string(text);
    }

    void Tabs::set_selected(int index) {
        if(index >= 0 && index < (int)tabs.size())
            move_selected_tab(index);
    }

    int Tabs::get_selected() const {
        return selected_tab;
    }

    float Tabs::tab_index_to_x_offset(int index) {
        return std::floor(index*width_per_tab + tab_margin_x);
    }
}