aboutsummaryrefslogtreecommitdiff
path: root/src/SearchBar.cpp
blob: a621d02e100fdad49558df194dc55aa7146f452a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "../include/SearchBar.hpp"
#include "../include/Scale.hpp"
#include <cmath>
#include <assert.h>

const sf::Color text_placeholder_color(255, 255, 255, 100);
const sf::Color front_color(43, 45, 47);
const float background_margin_horizontal = 8.0f;
const float background_margin_vertical = 4.0f;
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) :
        onTextUpdateCallback(nullptr),
        onTextSubmitCallback(nullptr),
        onTextBeginTypingCallback(nullptr),
        onAutocompleteRequestCallback(nullptr),
        text_autosearch_delay(0),
        autocomplete_search_delay(0),
        text(placeholder, font, 18), 
        autocomplete_text("", font, 18),
        placeholder_str(placeholder),
        show_placeholder(true),
        updated_search(false),
        updated_autocomplete(false),
        draw_logo(false),
        needs_update(false)
    {
        text.setFillColor(text_placeholder_color);
        autocomplete_text.setFillColor(text_placeholder_color);
        background.setFillColor(front_color);
        background_shadow.setFillColor(sf::Color(23, 25, 27));
        //background_shadow.setPosition(background.getPosition() + sf::Vector2f(5.0f, 5.0f));
        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);
    }

    void SearchBar::draw(sf::RenderWindow &window, bool draw_shadow) {
        if(needs_update) {
            needs_update = false;
            sf::Vector2u window_size = window.getSize();
            onWindowResize(sf::Vector2f(window_size.x, window_size.y));
        }
        if(draw_shadow)
            window.draw(background_shadow);
        window.draw(shade);
        window.draw(background);
        // TODO: Render starting from the character after text length
        window.draw(autocomplete_text);
        window.draw(text);
        if(draw_logo)
            window.draw(plugin_logo_sprite);
    }

    void SearchBar::update() {
        sf::Int32 elapsed_time = time_since_search_update.getElapsedTime().asMilliseconds();
        if(updated_search && elapsed_time >= text_autosearch_delay) {
            updated_search = false;
            sf::String str = text.getString();
            if(show_placeholder)
                str.clear();
            if(onTextUpdateCallback)
                onTextUpdateCallback(str);
        } else if(updated_autocomplete && elapsed_time >= autocomplete_search_delay) {
            updated_autocomplete = false;
            if(!show_placeholder && onAutocompleteRequestCallback)
                onAutocompleteRequestCallback(text.getString());
        }
    }

    void SearchBar::onWindowResize(const sf::Vector2f &window_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;
            draw_logo = false;
        }

        float font_height = text.getLocalBounds().height + 12.0f;
        float rect_height = std::floor(font_height + background_margin_vertical * 2.0f);

        float offset_x = padding_horizontal;
        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(25.0f, padding_vertical);
            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));
        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));
        autocomplete_text.setPosition(font_position);
        text.setPosition(font_position);
    }

    void SearchBar::onTextEntered(sf::Uint32 codepoint) {
        if(codepoint == 8 && !show_placeholder) { // Backspace
            sf::String str = text.getString();
            if(str.getSize() > 0) {
                // TODO: When it's possible to move the cursor, then check at cursor position instead of end of the string
                if(str[str.getSize() - 1] == '\n')
                    needs_update = true;
                str.erase(str.getSize() - 1, 1);
                text.setString(str);
                if(str.getSize() == 0) {
                    show_placeholder = true;
                    text.setString(placeholder_str);
                    text.setFillColor(text_placeholder_color);
                    autocomplete_text.setString("");
                } else {
                    clear_autocomplete_if_text_not_substring();
                }
                if(!updated_search && onTextBeginTypingCallback)
                    onTextBeginTypingCallback();
                updated_search = true;
                updated_autocomplete = true;
                time_since_search_update.restart();
            }
        } else if(codepoint == 13) { // Return
            bool clear_search = true;
            if(onTextSubmitCallback)
                clear_search = onTextSubmitCallback(show_placeholder ? "" : text.getString());

            if(clear_search)
                clear();
        } else if(codepoint > 31) { // Non-control character
            if(show_placeholder) {
                show_placeholder = false;
                text.setString("");
                text.setFillColor(sf::Color::White);
            }
            sf::String str = text.getString();
            str += codepoint;
            text.setString(str);
            clear_autocomplete_if_last_char_not_substr();
            if(!updated_search && onTextBeginTypingCallback)
                onTextBeginTypingCallback();
            updated_search = true;
            updated_autocomplete = true;
            time_since_search_update.restart();
        } else if(codepoint == '\n')
            needs_update = true;
    }

    void SearchBar::clear() {
        if(show_placeholder)
            return;
        show_placeholder = true;
        text.setString(placeholder_str);
        text.setFillColor(text_placeholder_color);
        autocomplete_text.setString("");
        needs_update = true;
        updated_search = false;
        updated_autocomplete = false;
    }

    void SearchBar::append_text(const std::string &text_to_add) {
        if(show_placeholder) {
            show_placeholder = false;
            text.setString("");
            text.setFillColor(sf::Color::White);
        }
        sf::String str = text.getString();
        str += text_to_add;
        text.setString(str);
        clear_autocomplete_if_text_not_substring();
        if(!updated_search && onTextBeginTypingCallback)
            onTextBeginTypingCallback();
        updated_search = true;
        updated_autocomplete = true;
        time_since_search_update.restart();
        needs_update = true;
    }

    bool SearchBar::is_cursor_at_start_of_line() const {
        // TODO: When it's possible to move the cursor, then check at the cursor position instead of end of the string
        const sf::String &str = text.getString();
        return show_placeholder || str.getSize() == 0 || str[str.getSize() - 1] == '\n';
    }

    void SearchBar::set_to_autocomplete() {
        const sf::String &autocomplete_str = autocomplete_text.getString();
        if(!autocomplete_str.isEmpty()) {
            if(show_placeholder) {
                show_placeholder = false;
                text.setString("");
                text.setFillColor(sf::Color::White);
            }
            text.setString(autocomplete_str);
            if(!updated_search && onTextBeginTypingCallback)
                onTextBeginTypingCallback();
            updated_search = true;
            updated_autocomplete = true;
            time_since_search_update.restart();
            needs_update = true;
        }
    }

    void SearchBar::set_autocomplete_text(const std::string &text) {
        autocomplete_text.setString(text);
    }

    void SearchBar::clear_autocomplete_if_text_not_substring() {
        const sf::String &text_str = text.getString();
        const sf::String &autocomplete_str = autocomplete_text.getString();
        if(text_str.getSize() > autocomplete_str.getSize()) {
            autocomplete_text.setString("");
            return;
        }

        for(size_t i = 0; i < autocomplete_str.getSize(); ++i) {
            if(text_str[i] != autocomplete_str[i]) {
                autocomplete_text.setString("");
                return;
            }
        }
    }

    void SearchBar::clear_autocomplete_if_last_char_not_substr() {
        const sf::String &text_str = text.getString();
        const sf::String &autocomplete_str = autocomplete_text.getString();
        if(text_str.isEmpty() || text_str.getSize() > autocomplete_str.getSize()) {
            autocomplete_text.setString("");
            return;
        }

        if(autocomplete_str[text_str.getSize() - 1] != text_str[text_str.getSize() - 1]) {
            autocomplete_text.setString("");
            return;
        }
    }

    float SearchBar::getBottom() const {
        return shade.getSize().y + background_shadow.getSize().y;
    }

    float SearchBar::getBottomWithoutShadow() const {
        return shade.getSize().y;
    }
}