diff options
Diffstat (limited to 'src/Tabs.cpp')
-rw-r--r-- | src/Tabs.cpp | 155 |
1 files changed, 74 insertions, 81 deletions
diff --git a/src/Tabs.cpp b/src/Tabs.cpp index 4605f2c..db25ffb 100644 --- a/src/Tabs.cpp +++ b/src/Tabs.cpp @@ -4,8 +4,10 @@ #include "../include/Utils.hpp" #include "../include/Config.hpp" #include "../include/Theme.hpp" -#include <SFML/Window/Event.hpp> -#include <SFML/Graphics/RenderWindow.hpp> +#include "../include/Scale.hpp" +#include <mglpp/window/Event.hpp> +#include <mglpp/window/Window.hpp> +#include <mglpp/graphics/Texture.hpp> #include <cmath> namespace QuickMedia { @@ -24,15 +26,16 @@ namespace QuickMedia { return tab_height + std::floor(10.0f * get_config().scale); } - Tabs::Tabs(sf::Shader *rounded_rectangle_shader, sf::Color shade_color) : background(sf::Vector2f(1.0f, 1.0f), 10.0f * get_config().scale, get_theme().selected_color, rounded_rectangle_shader), shade_color(shade_color) { - shade.setFillColor(shade_color); + 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(sf::Shader *rounded_rectangle_shader) : Tabs(rounded_rectangle_shader, get_theme().shade_color) {} + Tabs::Tabs(mgl::Shader *rounded_rectangle_shader) : Tabs(rounded_rectangle_shader, get_theme().shade_color) {} - int Tabs::add_tab(const std::string &title, Body *body) { + int Tabs::add_tab(std::string title, Body *body) { assert(body); - tabs.push_back({ sf::Text(title, *FontLoader::get_font(FontLoader::FontType::LATIN), tab_text_size), title, body} ); + tabs.push_back({ mgl::Text(std::move(title), *FontLoader::get_font(FontLoader::FontType::LATIN, tab_text_size)), body} ); return tabs.size() - 1; } @@ -62,13 +65,13 @@ namespace QuickMedia { on_change_tab(prev_tab, selected_tab); } - void Tabs::on_event(sf::Event &event) { - if(event.type == sf::Event::KeyPressed && event.key.control && !tabs.empty()) { - bool move_left = (event.key.code == sf::Keyboard::Left || (event.key.alt && event.key.code == sf::Keyboard::H)); - move_left |= (event.key.code == sf::Keyboard::Tab && event.key.shift); + 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 == sf::Keyboard::Right || (event.key.alt && event.key.code == sf::Keyboard::L)); - move_right |= (event.key.code == sf::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) @@ -76,37 +79,33 @@ namespace QuickMedia { } else if(move_right) { if(selected_tab < (int)tabs.size() - 1) move_selected_tab(selected_tab + 1); - } else if(event.key.code >= sf::Keyboard::Num1 && event.key.code <= sf::Keyboard::Num9) { - const int tab_target = event.key.code - sf::Keyboard::Num1; + } 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 sf::View create_scissor_view(sf::Vector2f pos, sf::Vector2f size, const sf::Vector2f window_size) { - sf::View view(sf::FloatRect(0.0f, 0.0f, size.x, size.y)); - view.setViewport(sf::FloatRect( - pos.x / (float)window_size.x, pos.y / (float)window_size.y, - size.x / (float)window_size.x, size.y / (float)window_size.y)); + 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(sf::RenderWindow &window, sf::Vector2f pos, float width) { + void Tabs::draw(mgl::Window &window, mgl::vec2f pos, float width) { if(width - tab_margin_x < 0.0f || tabs.empty()) return; - auto window_size = window.getSize(); 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 + 5.0f*get_config().scale)*0.5f); + const float tab_text_y = std::floor(pos.y + tab_height*0.5f - (tab_text_size + 13.0f*get_config().scale)*0.5f); tab_background_width = std::floor(width_per_tab - tab_margin_x*2.0f); - background.set_size(sf::Vector2f(tab_background_width, tab_height)); + background.set_size(mgl::vec2f(tab_background_width, tab_height)); - if(shade_color != sf::Color::Transparent) { - shade.setSize(sf::Vector2f(width, get_shade_height())); - shade.setPosition(std::floor(pos.x), std::floor(pos.y)); + 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); } @@ -127,7 +126,7 @@ namespace QuickMedia { bool tabs_cutoff_right = false; const auto start_pos = pos; - const sf::View prev_view = window.getView(); + const mgl::View prev_view = window.get_view(); pos.x += scroll_fixed; for(size_t i = 0; i < tabs.size(); ++i) { @@ -142,83 +141,77 @@ namespace QuickMedia { } if((int)index == selected_tab) { - background.set_position(sf::Vector2f(background_pos_x, std::floor(pos.y))); + background.set_position(mgl::vec2f(background_pos_x, std::floor(pos.y))); background.draw(window); } - sf::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.getLocalBounds().width*0.5f); + 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.setView(create_scissor_view({ text_pos_x, tab_text_y }, { tab_background_width, tab_height }, { (float)window_size.x, (float)window_size.y })); + window.set_view(create_scissor_view({ text_pos_x, tab_text_y }, { tab_background_width, tab_height })); window.draw(tab_text); - window.setView(prev_view); + window.set_view(prev_view); } - float lw = std::floor(25.0f * get_config().scale); - float lh = background.get_size().y; - - float line_offset_y = std::floor(lw * 0.35f); + const float lw = std::floor(25.0f * get_config().scale); + const float lh = background.get_size().y; if(tabs_cutoff_left) { - sf::Vertex gradient_points[4]; - gradient_points[0].position = sf::Vector2f(start_pos.x + tab_margin_x, start_pos.y); - gradient_points[1].position = sf::Vector2f(start_pos.x + tab_margin_x + lw, start_pos.y); - gradient_points[2].position = sf::Vector2f(start_pos.x + tab_margin_x + lw, start_pos.y + lh); - gradient_points[3].position = sf::Vector2f(start_pos.x + tab_margin_x, start_pos.y + lh); + 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 = sf::Color(shade_color.r, shade_color.g, shade_color.b, 10); - gradient_points[2].color = sf::Color(shade_color.r, shade_color.g, shade_color.b, 10); + 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, sf::Quads); - - sf::RectangleShape line(vec2f_floor(10.0f * get_config().scale, 2.0f * get_config().scale)); - line.setFillColor(get_theme().arrow_color); - line.setOrigin(line.getSize().x * 0.5f, line.getSize().y * 0.5f); - - line.rotate(-45.0f); - line.setPosition(std::floor(start_pos.x + line.getLocalBounds().width), std::floor(pos.y + background.get_size().y * 0.5f - lh * 0.5f + line_offset_y)); - window.draw(line); - - line.rotate(-90.0f); - line.setPosition(std::floor(start_pos.x + line.getLocalBounds().width), std::floor(pos.y + background.get_size().y * 0.5f - lh * 0.5f + line_offset_y + std::floor(7.0f * get_config().scale))); - window.draw(line); + 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) { - sf::Vertex gradient_points[4]; - gradient_points[0].position = sf::Vector2f(start_pos.x + width - lw - tab_margin_x, start_pos.y); - gradient_points[1].position = sf::Vector2f(start_pos.x + width, start_pos.y); - gradient_points[2].position = sf::Vector2f(start_pos.x + width, start_pos.y + lh); - gradient_points[3].position = sf::Vector2f(start_pos.x + width - lw - tab_margin_x, start_pos.y + lh); + 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 = sf::Color(shade_color.r, shade_color.g, shade_color.b, 10); + 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 = sf::Color(shade_color.r, shade_color.g, shade_color.b, 10); - - window.draw(gradient_points, 4, sf::Quads); - - sf::RectangleShape line(vec2f_floor(10.0f * get_config().scale, 2.0f * get_config().scale)); - line.setFillColor(get_theme().arrow_color); - line.setOrigin(line.getSize().x * 0.5f, line.getSize().y * 0.5f); - - line.rotate(45.0f); - line.setPosition(std::floor(start_pos.x + width - lw*0.75f + line.getLocalBounds().width), std::floor(pos.y + background.get_size().y * 0.5f - lh * 0.5f + line_offset_y)); - window.draw(line); - - line.rotate(-90.0f); - line.setPosition(std::floor(start_pos.x + width - lw*0.75f + line.getLocalBounds().width), std::floor(pos.y + background.get_size().y * 0.5f - lh * 0.5f + line_offset_y + std::floor(7.0f * get_config().scale))); - window.draw(line); + 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].label_utf8) return; - tabs[index].text.setString(sf::String::fromUtf8(text.begin(), text.end())); - tabs[index].label_utf8 = 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) { |