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

#include "RoundedRectangle.hpp"
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Clock.hpp>
#include <functional>

namespace sf {
    class Font;
    class RenderWindow;
    class Event;
    class Shader;
}

namespace QuickMedia {
    using TextUpdateCallback = std::function<void(const std::string &text)>;
    using TextSubmitCallback = std::function<void(const std::string &text)>;
    using TextBeginTypingCallback = std::function<void()>;
    using AutocompleteRequestCallback = std::function<void(const std::string &text)>;

    class SearchBar {
    public:
        SearchBar(sf::Texture *plugin_logo, sf::Shader *rounded_rectangle_shader, const std::string &placeholder, bool input_masked = false);
        void draw(sf::RenderWindow &window, bool draw_shadow = true);
        void on_event(sf::Event &event);
        void update();
        void onWindowResize(const sf::Vector2f &window_size);
        void clear();
        void append_text(const std::string &text_to_add);
        bool is_cursor_at_start_of_line() const;
        void set_to_autocomplete();
        void set_autocomplete_text(const std::string &text);
        void set_vertical_position(float vertical_pos);

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

        std::string get_text() const;

        TextUpdateCallback onTextUpdateCallback;
        TextSubmitCallback onTextSubmitCallback;
        TextBeginTypingCallback onTextBeginTypingCallback;
        AutocompleteRequestCallback onAutocompleteRequestCallback;
        int text_autosearch_delay;
        int autocomplete_search_delay;
        bool caret_visible;
    private:
        void onTextEntered(sf::Uint32 codepoint);
        void clear_autocomplete_if_text_not_substring();
        void clear_autocomplete_if_last_char_not_substr();
    private:
        sf::Text text;
        sf::Text autocomplete_text;
        RoundedRectangle background;
        sf::RectangleShape background_shadow;
        sf::RectangleShape shade;
        sf::RectangleShape caret;
        sf::Sprite plugin_logo_sprite;
        std::string placeholder_str;
        bool show_placeholder;
        bool updated_search;
        bool updated_autocomplete;
        bool draw_logo;
        bool needs_update;
        bool input_masked;
        bool typing;
        bool backspace_pressed;
        bool mouse_left_inside;
        float vertical_pos;
        sf::Clock time_since_search_update;
        sf::Vector2u prev_window_size;
    };
}