aboutsummaryrefslogtreecommitdiff
path: root/src/SearchBar.cpp
blob: 1eff8a8a9f369b91982f66d273c7cca386ffd4ca (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "../include/SearchBar.hpp"
#include "../include/Theme.hpp"
#include "../include/Scale.hpp"
#include "../include/ResourceLoader.hpp"
#include "../include/Config.hpp"
#include "../include/Utils.hpp"
#include <mglpp/system/FloatRect.hpp>
#include <mglpp/system/Utf8.hpp>
#include <mglpp/graphics/Texture.hpp>
#include <mglpp/window/Window.hpp>
#include <assert.h>

namespace QuickMedia {
    static float floor(float v) {
        return (int)v;
    }

    static const float background_margin_horizontal = floor(5.0f * get_config().scale + 10.0f * get_config().spacing_scale);
    static const float padding_top_default = floor(10.0f * get_config().scale * get_config().spacing_scale);
    static const float padding_bottom_default = floor(15.0f * get_config().scale * get_config().spacing_scale);
    static const float background_margin_vertical = floor(4.0f * get_config().scale * get_config().spacing_scale);
    static const int character_size = get_config().search.font_size * get_config().scale * get_config().font_scale;
    static const int search_icon_padding_x = 10 * get_config().scale;

    SearchBar::SearchBar(mgl::Texture *plugin_logo, mgl::Shader *rounded_rectangle_shader, const std::string &placeholder, SearchBarType type) :
        onTextUpdateCallback(nullptr),
        onTextSubmitCallback(nullptr),
        onTextBeginTypingCallback(nullptr),
        text_autosearch_delay_ms(50),
        caret_visible(true),
        text("", *FontLoader::get_font(FontLoader::FontType::LATIN, character_size)),
        placeholder_text(placeholder, *FontLoader::get_font(FontLoader::FontType::LATIN, character_size)),
        background(mgl::vec2f(1.0f, 1.0f), 10.0f * get_config().scale, get_theme().selected_color, rounded_rectangle_shader),
        search_icon_sprite(TextureLoader::get_texture("images/search_icon.png")),
        updated_search(false),
        draw_logo(false),
        needs_update(true),
        typing(false),
        backspace_pressed(false),
        mouse_left_inside(false),
        pos(0.0f, 0.0f),
        prev_size(0.0f, 0.0f),
        type(type)
    {
        padding_top = padding_top_default;
        padding_bottom = padding_bottom_default;
        text.set_color(get_theme().text_color);
        placeholder_text.set_color(get_theme().placeholder_text_color);
        shade.set_color(get_theme().shade_color);
        if(plugin_logo && plugin_logo->is_valid())
            plugin_logo_sprite.set_texture(plugin_logo);
        search_icon_sprite.set_color(get_theme().text_color);
    }

    void SearchBar::draw(mgl::Window &window, mgl::vec2f 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_size = size;
        }

        if(needs_update) {
            needs_update = false;
            onWindowResize(size);
        }

        if(draw_background)
            window.draw(shade);

        background.draw(window);

        const int caret_offset_y = character_size * 0.15f;

        const bool show_placeholder = text.get_string().empty();
        if(type == SearchBarType::Password && !show_placeholder) {
            std::string masked_str(text.get_string().size(), '*');
            mgl::Text masked_text(std::move(masked_str), *text.get_font());
            masked_text.set_position(text.get_position());
            window.draw(masked_text);
            caret.set_position(masked_text.get_position() + mgl::vec2f(masked_text.get_bounds().size.x, 0.0f).floor() + mgl::vec2f(0.0f, caret_offset_y));
        } else {
            if(show_placeholder) {
                window.draw(placeholder_text);
                caret.set_position(placeholder_text.get_position() + mgl::vec2f(-caret.get_size().x, caret_offset_y));
            } else {
                window.draw(text);
                caret.set_position(text.get_position() + mgl::vec2f(text.get_bounds().size.x, 0.0f).floor() + mgl::vec2f(0.0f, caret_offset_y));
            }
        }
        
        if(caret_visible && is_editable())
            window.draw(caret);

        if(draw_logo)
            window.draw(plugin_logo_sprite);
        
        if(type == SearchBarType::Search)
            window.draw(search_icon_sprite);
    }

    void SearchBar::on_event(mgl::Window &window, mgl::Event &event) {
        if(is_touch_enabled() && event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) {
            mgl::FloatRect box(background.get_position(), background.get_size());
            if(box.contains(mgl::vec2f(event.mouse_button.x, event.mouse_button.y)))
                mouse_left_inside = true;
            else
                mouse_left_inside = false;
        } else if(is_touch_enabled() && event.type == mgl::Event::MouseButtonReleased && event.mouse_button.button == mgl::Mouse::Left) {
            mgl::FloatRect box(background.get_position(), background.get_size());
            if(mouse_left_inside && box.contains(mgl::vec2f(event.mouse_button.x, event.mouse_button.y)))
                show_virtual_keyboard();
            mouse_left_inside = false;
        }

        if(!editable)
            return;

        if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::Backspace)
            backspace_pressed = true;
        else if(event.type == mgl::Event::KeyReleased && event.key.code == mgl::Keyboard::Backspace)
            backspace_pressed = false;
        if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::V && event.key.control) {
            append_text(window.get_clipboard_string());
        }

        if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::D && event.key.control) {
            if(!text.get_string().empty()) {
                clear();
                updated_search = true;
                time_since_search_update.restart();
            }
            return;
        }

        const bool pressing_ctrl = (event.type == mgl::Event::KeyPressed && event.key.control);
        if(pressing_ctrl && (event.type != mgl::Event::TextEntered || event.text.codepoint != 13)) // Enter
            return;

        if(event.type == mgl::Event::TextEntered && (window.is_key_pressed(mgl::Keyboard::LControl) || window.is_key_pressed(mgl::Keyboard::RControl)) && !window.is_key_pressed(mgl::Keyboard::LAlt))
            return;

        if(event.type == mgl::Event::TextEntered && event.text.codepoint != 8 && event.text.codepoint != 127) { // 8 = backspace, 127 = del
            onTextEntered(event.text);
        } else if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::Backspace) {
            mgl::Event::TextEvent text_event;
            text_event.codepoint = 8;
            text_event.size = 1;
            text_event.str[0] = 8;
            text_event.str[1] = '\0';
            onTextEntered(text_event);
        }
    }

    void SearchBar::update() {
        double elapsed_time_sec = time_since_search_update.get_elapsed_time_seconds();
        double timeout_sec = (double)text_autosearch_delay_ms * 0.001;
        if(backspace_pressed)
            timeout_sec = 0.75;
        if(updated_search && elapsed_time_sec >= timeout_sec) {
            updated_search = false;
            if(onTextUpdateCallback)
                onTextUpdateCallback(text.get_string());
            typing = false;
        }
    }

    void SearchBar::onWindowResize(const mgl::vec2f &size) {
        draw_logo = plugin_logo_sprite.get_texture() != nullptr;
        if(size.x < 400.0f)
            draw_logo = false;

        float font_height = character_size + 7.0f;
        float rect_height = floor(font_height + background_margin_vertical * 2.0f);

        const int x_pad = padding_x * get_config().scale * get_config().spacing_scale;

        float offset_x;
        if(draw_logo) {
            float one_line_height = floor(character_size + 8.0f + background_margin_vertical * 2.0f);
            auto texture_size = plugin_logo_sprite.get_texture()->get_size();
            mgl::vec2f texture_size_f(texture_size.x, texture_size.y);
            mgl::vec2f new_size = wrap_to_size(texture_size_f, mgl::vec2f(200.0f, one_line_height));
            plugin_logo_sprite.set_scale(get_ratio(texture_size_f, new_size));
            plugin_logo_sprite.set_position(vec2f_floor(pos.x + x_pad, pos.y + padding_top + rect_height * 0.5f - plugin_logo_sprite.get_texture()->get_size().y * plugin_logo_sprite.get_scale().y * 0.5f));
            offset_x = x_pad + new_size.x + floor(10.0f * get_config().spacing_scale);
        } else {
            offset_x = x_pad;
        }

        const float width = floor(size.x - offset_x - x_pad);

        background.set_size(mgl::vec2f(width, rect_height));
        shade.set_size(mgl::vec2f(size.x, padding_top + rect_height + padding_bottom));
        caret.set_size(vec2f_floor(2.0f * get_config().scale, character_size + floor(2.0f * get_config().scale)));

        const int search_padding = (type == SearchBarType::Search ? search_icon_padding_x : 0);

        background.set_position(mgl::vec2f(pos.x + offset_x, pos.y + padding_top));
        shade.set_position(pos);
        mgl::vec2f text_position(pos.x + offset_x + background_margin_horizontal + search_padding + search_padding, pos.y + padding_top + rect_height * 0.5f - character_size * 0.7f);
        text.set_position(text_position.floor());
        placeholder_text.set_position(text_position.floor());

        mgl::vec2f texture_size = search_icon_sprite.get_texture()->get_size().to_vec2f();
        mgl::vec2f new_size = wrap_to_size_y(texture_size, character_size);
        search_icon_sprite.set_scale(get_ratio(texture_size, new_size));
        search_icon_sprite.set_position(background.get_position() + mgl::vec2f(search_padding, background.get_size().y * 0.5f - new_size.y * 0.5f).floor());
    }

    void SearchBar::onTextEntered(const mgl::Event::TextEvent &text_event) {
        if(text_event.codepoint == 8) { // Backspace
            std::string str = text.get_string();
            if(str.size() > 0) {
                // TODO: When it's possible to move the cursor, then check at cursor position instead of end of the string
                if(str[str.size() - 1] == '\n')
                    needs_update = true;
                
                const size_t codepoint_start_index = mgl::utf8_get_start_of_codepoint((const unsigned char*)str.c_str(), str.size(), str.size() - 1);
                str.erase(codepoint_start_index);
                text.set_string(std::move(str));
                if(!updated_search) {
                    typing = true;
                    if(onTextBeginTypingCallback)
                        onTextBeginTypingCallback();
                }
                updated_search = true;
                time_since_search_update.restart();
            }
        } else if(text_event.codepoint == 13) { // Return
            backspace_pressed = false;
            if(onTextSubmitCallback)
                onTextSubmitCallback(text.get_string());
        } else if(text_event.codepoint > 31) { // Non-control character
            append_text(std::string(text_event.str, text_event.size));
        } else if(text_event.codepoint == '\n')
            needs_update = true;
    }

    void SearchBar::clear() {
        text.set_string("");
        needs_update = true;
        updated_search = false;
        backspace_pressed = false;
    }

    void SearchBar::set_text(const std::string &text, bool update_search) {
        clear();
        append_text(text);

        if(!update_search) {
            needs_update = false;
            updated_search = false;
        }
    }

    void SearchBar::append_text(const std::string &text_to_add) {
        if(text_to_add.empty())
            return;

        text.append_string(text_to_add);

        if(!updated_search) {
            typing = true;
            if(onTextBeginTypingCallback)
                onTextBeginTypingCallback();
        }

        updated_search = true;
        time_since_search_update.restart();
        backspace_pressed = false;
        if(text_to_add.find('\n') != std::string::npos)
            needs_update = true;
    }

    void SearchBar::set_position(mgl::vec2f 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;
        }
    }

    void SearchBar::set_editable(bool editable) {
        this->editable = editable;
    }

    bool SearchBar::is_editable() const {
        return editable;
    }

    float SearchBar::getBottom() const {
        return getBottomWithoutShadow() + 5.0f;//background_shadow.get_size().y;
    }

    float SearchBar::getBottomWithoutShadow() const {
        float font_height = character_size + 7.0f;
        return floor(font_height + background_margin_vertical * 2.0f + padding_top + padding_bottom);
    }

    const std::string& SearchBar::get_text() const {
        return text.get_string();
    }

    bool SearchBar::is_empty() const {
        return text.get_string().empty();
    }
}