diff options
Diffstat (limited to 'src/SearchBar.cpp')
-rw-r--r-- | src/SearchBar.cpp | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index 803eaee..62a0196 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -13,7 +13,7 @@ const float PADDING_HORIZONTAL = 50.0f; const float padding_vertical = 20.0f; namespace QuickMedia { - SearchBar::SearchBar(sf::Font &font, sf::Texture &plugin_logo, const std::string &placeholder) : + SearchBar::SearchBar(sf::Font &font, sf::Texture *plugin_logo, const std::string &placeholder, bool input_masked) : onTextUpdateCallback(nullptr), onTextSubmitCallback(nullptr), onTextBeginTypingCallback(nullptr), @@ -27,7 +27,10 @@ namespace QuickMedia { updated_search(false), updated_autocomplete(false), draw_logo(false), - needs_update(true) + needs_update(true), + input_masked(input_masked), + caret_visible(true), + vertical_pos(0.0f) { text.setFillColor(text_placeholder_color); autocomplete_text.setFillColor(text_placeholder_color); @@ -37,8 +40,8 @@ namespace QuickMedia { shade.setFillColor(sf::Color(0, 85, 119)); //background.setOutlineThickness(1.0f); //background.setOutlineColor(sf::Color(13, 15, 17)); - if(plugin_logo.getNativeHandle() != 0) - plugin_logo_sprite.setTexture(plugin_logo, true); + if(plugin_logo && plugin_logo->getNativeHandle() != 0) + plugin_logo_sprite.setTexture(*plugin_logo, true); } void SearchBar::draw(sf::RenderWindow &window, bool draw_shadow) { @@ -53,13 +56,23 @@ namespace QuickMedia { window.draw(background); // TODO: Render starting from the character after text length window.draw(autocomplete_text); - window.draw(text); - if(show_placeholder || text.getString().isEmpty()) - caret.setPosition(text.getPosition() - sf::Vector2f(2.0f, 0.0f)); - else - caret.setPosition(text.findCharacterPos(text.getString().getSize())); + if(input_masked && !show_placeholder) { + std::string masked_str(text.getString().getSize(), '*'); + sf::Text masked_text(std::move(masked_str), *text.getFont(), text.getCharacterSize()); + masked_text.setPosition(text.getPosition()); + window.draw(masked_text); + caret.setPosition(masked_text.findCharacterPos(masked_text.getString().getSize())); + } else { + window.draw(text); + if(show_placeholder || text.getString().isEmpty()) + caret.setPosition(text.getPosition() - sf::Vector2f(2.0f, 0.0f)); + else + caret.setPosition(text.findCharacterPos(text.getString().getSize())); + } - window.draw(caret); + if(caret_visible) + window.draw(caret); + if(draw_logo) window.draw(plugin_logo_sprite); } @@ -104,19 +117,20 @@ namespace QuickMedia { 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(25.0f, padding_vertical); + plugin_logo_sprite.setPosition(25.0f, padding_vertical + vertical_pos); offset_x = 25.0f + new_size.x + 25.0f; } const float width = std::floor(window_size.x - offset_x - padding_horizontal); background.setSize(sf::Vector2f(width, rect_height)); shade.setSize(sf::Vector2f(window_size.x, padding_vertical + rect_height + padding_vertical)); - caret.setSize(sf::Vector2f(2.0f, text.getLocalBounds().height + 12.0f)); + caret.setSize(sf::Vector2f(2.0f, text.getCharacterSize() + 8.0f)); background_shadow.setSize(sf::Vector2f(window_size.x, 5.0f)); - background.setPosition(offset_x, padding_vertical); - background_shadow.setPosition(0.0f, std::floor(shade.getSize().y)); - sf::Vector2f font_position(std::floor(offset_x + background_margin_horizontal), std::floor(padding_vertical + background_margin_vertical)); + background.setPosition(offset_x, padding_vertical + 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_vertical + background_margin_vertical + vertical_pos)); autocomplete_text.setPosition(font_position); text.setPosition(font_position); } @@ -221,6 +235,13 @@ 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; + needs_update = true; + } + } + void SearchBar::clear_autocomplete_if_text_not_substring() { const sf::String &text_str = text.getString(); const sf::String &autocomplete_str = autocomplete_text.getString(); @@ -258,4 +279,10 @@ namespace QuickMedia { float SearchBar::getBottomWithoutShadow() const { return shade.getSize().y; } + + std::string SearchBar::get_text() const { + if(show_placeholder) + return ""; + return text.getString(); + } }
\ No newline at end of file |