diff options
Diffstat (limited to 'src/SearchBar.cpp')
-rw-r--r-- | src/SearchBar.cpp | 64 |
1 files changed, 27 insertions, 37 deletions
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index 059d028..8362864 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -13,9 +13,8 @@ static const sf::Color text_placeholder_color(255, 255, 255, 100); static const sf::Color front_color(55, 60, 68); static const float background_margin_horizontal = 10.0f + std::floor(5.0f * QuickMedia::get_ui_scale()); -static const float PADDING_HORIZONTAL = std::floor(25.0f * QuickMedia::get_ui_scale()); -static const float padding_top = std::floor(10.0f * QuickMedia::get_ui_scale()); -static const float padding_bottom = std::floor(15.0f * QuickMedia::get_ui_scale()); +static const float padding_top_default = std::floor(10.0f * QuickMedia::get_ui_scale()); +static const float padding_bottom_default = std::floor(15.0f * QuickMedia::get_ui_scale()); static const float background_margin_vertical = std::floor(4.0f * QuickMedia::get_ui_scale()); namespace QuickMedia { @@ -40,36 +39,32 @@ namespace QuickMedia { typing(false), backspace_pressed(false), mouse_left_inside(false), - vertical_pos(0.0f) + pos(0.0f, 0.0f), + prev_size(0.0f, 0.0f) { + padding_top = padding_top_default; + padding_bottom = padding_bottom_default; text.setFillColor(text_placeholder_color); autocomplete_text.setFillColor(text_placeholder_color); - //background.setCornersRadius(5); - background_shadow.setFillColor(sf::Color(23, 25, 27)); - //background_shadow.setPosition(background.getPosition() + sf::Vector2f(5.0f, 5.0f)); shade.setFillColor(sf::Color(33, 37, 44)); - //background.setOutlineThickness(1.0f); - //background.setOutlineColor(sf::Color(13, 15, 17)); if(plugin_logo && plugin_logo->getNativeHandle() != 0) plugin_logo_sprite.setTexture(*plugin_logo, true); } - void SearchBar::draw(sf::RenderWindow &window, bool draw_shadow) { - sf::Vector2u window_size = window.getSize(); - if(window_size.x != prev_window_size.x || window_size.y != prev_window_size.y) { + void SearchBar::draw(sf::RenderWindow &window, sf::Vector2f size, bool draw_background) { + if(std::abs(size.x - prev_size.x) > 1.0f || std::abs(size.y - prev_size.y) > 1.0f) { needs_update = true; - prev_window_size = window_size; + prev_size = size; } if(needs_update) { needs_update = false; - onWindowResize(sf::Vector2f(window_size.x, window_size.y)); + onWindowResize(size); } - (void)draw_shadow; - //if(draw_shadow) - // window.draw(background_shadow); - window.draw(shade); + if(draw_background) + window.draw(shade); + background.draw(window); // TODO: Render starting from the character after text length window.draw(autocomplete_text); @@ -154,41 +149,36 @@ namespace QuickMedia { } } - void SearchBar::onWindowResize(const sf::Vector2f &window_size) { + void SearchBar::onWindowResize(const sf::Vector2f &size) { draw_logo = plugin_logo_sprite.getTexture() != nullptr; - float padding_horizontal = PADDING_HORIZONTAL; - if(window_size.x - padding_horizontal * 2.0f < 400.0f) { - padding_horizontal = 0.0f; + if(size.x * 2.0f < 400.0f) draw_logo = false; - } float font_height = text.getCharacterSize() + 7.0f; float rect_height = std::floor(font_height + background_margin_vertical * 2.0f); - float offset_x = padding_horizontal; + float offset_x; if(draw_logo) { float one_line_height = std::floor(text.getCharacterSize() + 8.0f + background_margin_vertical * 2.0f); auto texture_size = plugin_logo_sprite.getTexture()->getSize(); sf::Vector2f texture_size_f(texture_size.x, texture_size.y); sf::Vector2f new_size = wrap_to_size(texture_size_f, sf::Vector2f(200.0f, one_line_height)); plugin_logo_sprite.setScale(get_ratio(texture_size_f, new_size)); - plugin_logo_sprite.setPosition(10.0f, padding_top + vertical_pos + rect_height * 0.5f - plugin_logo_sprite.getTexture()->getSize().y * plugin_logo_sprite.getScale().y * 0.5f); - offset_x = 10.0f + new_size.x + 10.0f; + plugin_logo_sprite.setPosition(pos.x + padding_x, pos.y + padding_top + rect_height * 0.5f - plugin_logo_sprite.getTexture()->getSize().y * plugin_logo_sprite.getScale().y * 0.5f); + offset_x = padding_x + new_size.x + 10.0f; } else { - offset_x = 10.0f; + offset_x = padding_x; } - const float width = std::floor(window_size.x - offset_x - 10.0f); + const float width = std::floor(size.x - offset_x - padding_x); background.set_size(sf::Vector2f(width, rect_height)); - shade.setSize(sf::Vector2f(window_size.x, padding_top + rect_height + padding_bottom)); + shade.setSize(sf::Vector2f(size.x, padding_top + rect_height + padding_bottom)); caret.setSize(sf::Vector2f(std::floor(2.0f * get_ui_scale()), text.getCharacterSize() + std::floor(2.0f * get_ui_scale()))); - background_shadow.setSize(sf::Vector2f(window_size.x, 5.0f)); - background.set_position(sf::Vector2f(offset_x, padding_top + vertical_pos)); - shade.setPosition(0.0f, vertical_pos); - background_shadow.setPosition(0.0f, std::floor(shade.getSize().y + vertical_pos)); - sf::Vector2f font_position(std::floor(offset_x + background_margin_horizontal), std::floor(padding_top + background_margin_vertical + vertical_pos)); + background.set_position(sf::Vector2f(pos.x + offset_x, pos.y + padding_top)); + shade.setPosition(pos); + sf::Vector2f font_position(std::floor(pos.x + offset_x + background_margin_horizontal), std::floor(pos.y + padding_top + background_margin_vertical)); autocomplete_text.setPosition(font_position); text.setPosition(font_position); } @@ -306,9 +296,9 @@ namespace QuickMedia { autocomplete_text.setString(text); } - void SearchBar::set_vertical_position(float vertical_pos) { - if(std::abs(this->vertical_pos - vertical_pos) > 1.0f) { - this->vertical_pos = vertical_pos; + void SearchBar::set_position(sf::Vector2f pos) { + if(std::abs(this->pos.x - pos.x) > 1.0f || std::abs(this->pos.y - pos.y) > 1.0f) { + this->pos = pos; needs_update = true; } } |