aboutsummaryrefslogtreecommitdiff
path: root/include/SearchBar.hpp
blob: 0cffff52e94cbf002d8c6f56670886e888444d72 (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
#pragma once

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <functional>

namespace QuickMedia {
    using TextUpdateCallback = std::function<void(const sf::String &text)>;
    // Return true to consume the search (clear the search field)
    using TextSubmitCallback = std::function<bool(const sf::String &text)>;
    using TextBeginTypingCallback = std::function<void()>;
    using AutocompleteRequestCallback = std::function<std::string(const sf::String &text)>;

    class SearchBar {
    public:
        SearchBar(sf::Font &font, sf::Texture &plugin_logo, const std::string &placeholder);
        void draw(sf::RenderWindow &window, bool draw_shadow = true);
        void update();
        void onWindowResize(const sf::Vector2f &window_size);
        void onTextEntered(sf::Uint32 codepoint);
        void clear();
        void append_text(const std::string &text_to_add);
        bool is_cursor_at_start_of_line() const;
        void set_to_autocomplete();

        float getBottom() const;
        float getBottomWithoutShadow() const;

        TextUpdateCallback onTextUpdateCallback;
        TextSubmitCallback onTextSubmitCallback;
        TextBeginTypingCallback onTextBeginTypingCallback;
        AutocompleteRequestCallback onAutocompleteRequestCallback;
        int text_autosearch_delay;
        int autocomplete_search_delay;
    private:
        void clear_autocomplete_if_text_not_substring();
        void clear_autocomplete_if_last_char_not_substr();
    private:
        sf::Text text;
        sf::Text autocomplete_text;
        sf::RectangleShape background;
        sf::RectangleShape background_shadow;
        sf::RectangleShape shade;
        sf::Sprite plugin_logo_sprite;
        std::string placeholder_str;
        bool show_placeholder;
        bool updated_search;
        bool updated_autocomplete;
        bool draw_logo;
        bool needs_update;
        sf::Clock time_since_search_update;
    };
}