aboutsummaryrefslogtreecommitdiff
path: root/src/Body.cpp
blob: 02970727b951f5a6b688a142c0cc85b5468de2cf (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "../include/Body.hpp"
#include "../include/QuickMedia.hpp"
#include "../plugins/Plugin.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/OpenGL.hpp>
#include <assert.h>
#include <cmath>

const sf::Color front_color(43, 45, 47);
const sf::Color back_color(33, 35, 37);

namespace QuickMedia {
    Body::Body(Program *program, sf::Font *font, sf::Font *bold_font) :
        program(program),
        font(font),
        bold_font(bold_font),
        progress_text("", *font, 14),
        author_text("", *bold_font, 14),
        replies_text("", *font, 14),
        selected_item(0),
        draw_thumbnails(false),
        loading_thumbnail(false)
    {
        progress_text.setFillColor(sf::Color::White);
        author_text.setFillColor(sf::Color::White);
        replies_text.setFillColor(sf::Color(129, 162, 190));
    }

    void Body::select_previous_item() {
        if(items.empty())
            return;

        int num_items = (int)items.size();
        for(int i = 0; i < num_items; ++i) {
            --selected_item;
            if(selected_item < 0)
                selected_item = num_items - 1;
            if(items[selected_item]->visible)
                return;
        }
    }

    void Body::select_next_item() {
        if(items.empty())
            return;

        int num_items = (int)items.size();
        for(int i = 0; i < num_items; ++i) {
            ++selected_item;
            if(selected_item == num_items)
                selected_item = 0;
            if(items[selected_item]->visible)
                return;
        }
    }

    void Body::select_first_item() {
        selected_item = 0;
        clamp_selection();
    }

    void Body::reset_selected() {
        for(size_t i = 0; i < items.size(); ++i) {
            if(items[i]->visible) {
                selected_item = i;
                return;
            }
        }
        selected_item = 0;
    }

    void Body::clear_items() {
        items.clear();
        selected_item = 0;
    }

    void Body::clear_thumbnails() {
        item_thumbnail_textures.clear();
    }

    BodyItem* Body::get_selected() const {
        if(items.empty() || !items[selected_item]->visible)
            return nullptr;
        return items[selected_item].get();
    }

    void Body::clamp_selection() {
        int num_items = (int)items.size();
        if(items.empty())
            return;

        if(selected_item < 0)
            selected_item = 0;
        else if(selected_item >= num_items)
            selected_item = num_items - 1;

        for(int i = selected_item; i >= 0; --i) {
            if(items[i]->visible) {
                selected_item = i;
                return;
            }
        }

        for(int i = selected_item; i < num_items; ++i) {
            if(items[i]->visible) {
                selected_item = i;
                return;
            }
        }
    }

    std::shared_ptr<sf::Texture> Body::load_thumbnail_from_url(const std::string &url) {
        auto result = std::make_shared<sf::Texture>();
        result->setSmooth(true);
        assert(!loading_thumbnail);
        loading_thumbnail = true;
        thumbnail_load_thread = std::thread([this, result, url]() {
            std::string texture_data;
            if(download_to_string_cache(url, texture_data, {}, program->get_current_plugin()->use_tor, true) == DownloadResult::OK) {
                if(result->loadFromMemory(texture_data.data(), texture_data.size())) {
                    //result->generateMipmap();
                }
            }
            loading_thumbnail = false;
        });
        thumbnail_load_thread.detach();
        return result;
    }

    void Body::draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size) {
        Json::Value empty_object(Json::objectValue);
        draw(window, pos, size, empty_object);
    }

    // TODO: Use a render target for the whole body so all images can be put into one.
    // TODO: Load thumbnails with more than one thread.
    // TODO: Show chapters (rows) that have been read differently to make it easier to see what hasn't been read yet.
    void Body::draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, const Json::Value &content_progress) {
        sf::Vector2f scissor_pos = pos;

        const float image_max_height = 100.0f;
        const float spacing_y = 15.0f;
        const float padding_x = 10.0f;
        const float image_padding_x = 5.0f;
        const float padding_y = 5.0f;
        const float start_y = pos.y;

        sf::RectangleShape image_fallback(sf::Vector2f(50, image_max_height));
        image_fallback.setFillColor(sf::Color::White);

        sf::Sprite image;

        sf::RectangleShape item_background;
        item_background.setFillColor(front_color);
        //item_background.setOutlineThickness(1.0f);
        //item_background.setOutlineColor(sf::Color(13, 15, 17));
        sf::RectangleShape item_background_shadow;
        item_background_shadow.setFillColor(sf::Color(23, 25, 27));

        sf::RectangleShape selected_border;
        selected_border.setFillColor(sf::Color(0, 85, 119));

        int num_items = items.size();
        if(num_items == 0)
            return;

        for(auto &thumbnail_it : item_thumbnail_textures) {
            thumbnail_it.second.referenced = false;
        }

        for(auto &body_item : items) {
            if(body_item->dirty) {
                body_item->dirty = false;
                if(body_item->title_text)
                    body_item->title_text->setString(body_item->title);
                else
                    body_item->title_text = std::make_unique<Text>(body_item->title, font, 14, size.x - 50 - image_padding_x * 2.0f);
                //body_item->title_text->updateGeometry(); // TODO: Call this to make getHeight work on first frame (called below)
            }
        }

        // Find the starting row that can be drawn to make selected row visible as well
        int first_visible_item = selected_item;
        assert(first_visible_item >= 0 && first_visible_item < (int)items.size());
        float visible_height = 0.0f;
        for(; first_visible_item >= 0; --first_visible_item) {
            auto &item = items[first_visible_item];
            if(item->visible) {
                float item_height = item->title_text->getHeight();
                if(!item->author.empty()) {
                    item_height += author_text.getCharacterSize() + 2.0f;
                }
                if(draw_thumbnails && !item->thumbnail_url.empty()) {
                    auto &item_thumbnail = item_thumbnail_textures[item->thumbnail_url];
                    item_thumbnail.referenced = false;
                    float image_height = image_max_height;
                    if(item_thumbnail.texture && item_thumbnail.texture->getNativeHandle() != 0) {
                        auto image_size = item_thumbnail.texture->getSize();
                        image_height = std::min(image_max_height, (float)image_size.y);
                    }
                    item_height = std::max(item_height, image_height);
                }
                item_height += spacing_y + padding_y * 2.0f;
                visible_height += item_height;
                if(visible_height >= size.y) {
                    --first_visible_item;
                    //pos.y += (size.y - (visible_height - item_height));
                    pos.y -= (visible_height - size.y);
                    break;
                }
            }
        }

        sf::Vector2u window_size = window.getSize();
        glEnable(GL_SCISSOR_TEST);
        glScissor(scissor_pos.x, (int)window_size.y - (int)scissor_pos.y - (int)size.y, size.x, size.y);

        for(int i = first_visible_item + 1; i < num_items; ++i) {
            const auto &item = items[i];

            if(pos.y >= start_y + size.y)
                break;

            if(!item->visible)
                continue;

            // TODO: Instead of generating a new hash everytime to access textures, cache the hash of the thumbnail url
            // Intentionally create the item with the key item->thumbnail_url if it doesn't exist
            item_thumbnail_textures[item->thumbnail_url].referenced = true;
            auto &item_thumbnail = item_thumbnail_textures[item->thumbnail_url];

            float item_height = item->title_text->getHeight();
            if(!item->author.empty()) {
                item_height += author_text.getCharacterSize() + 2.0f;
            }
            if(draw_thumbnails && !item->thumbnail_url.empty()) {
                float image_height = image_max_height;
                if(item_thumbnail.texture && item_thumbnail.texture->getNativeHandle() != 0) {
                    auto image_size = item_thumbnail.texture->getSize();
                    image_height = std::min(image_max_height, (float)image_size.y);
                }
                item_height = std::max(item_height, image_height);
            }
            item_height += (padding_y * 2.0f);

            if(draw_thumbnails) {
                if(!item->thumbnail_url.empty() && !loading_thumbnail && !item_thumbnail.texture) {
                    item_thumbnail.texture = load_thumbnail_from_url(item->thumbnail_url);
                }
            }

            sf::Vector2f item_pos = pos;
            if(i == selected_item) {
                //selected_border.setPosition(pos);
                //selected_border.setSize(sf::Vector2f(selected_border_width, item_height));
                //window.draw(selected_border);
                //item_pos.x += selected_border_width;
                item_background.setFillColor(sf::Color(0, 85, 119));
            } else {
                item_background.setFillColor(front_color);
            }

            item_pos.x = std::floor(item_pos.x);
            item_pos.y = std::floor(item_pos.y);

            item_background_shadow.setPosition(item_pos + sf::Vector2f(size.x, 0.0f) + sf::Vector2f(0.0, 5.0f));
            item_background_shadow.setSize(sf::Vector2f(5.0f, item_height));
            window.draw(item_background_shadow);

            item_background_shadow.setPosition(item_pos + sf::Vector2f(0.0f, item_height) + sf::Vector2f(5.0, 0.0f));
            item_background_shadow.setSize(sf::Vector2f(size.x - 5.0f, 5.0f));
            window.draw(item_background_shadow);

            item_background.setPosition(item_pos);
            item_background.setSize(sf::Vector2f(size.x, item_height));
            window.draw(item_background);

            float text_offset_x = padding_x;
            if(draw_thumbnails) {
                // TODO: Verify if this is safe. The thumbnail is being modified in another thread
                // and it might not be fully finished before the native handle is set?
                if(item_thumbnail.texture && item_thumbnail.texture->getNativeHandle() != 0) {
                    image.setTexture(*item_thumbnail.texture, true);
                    auto image_size = image.getTexture()->getSize();
                    auto height_ratio = std::min(image_max_height, (float)image_size.y) / image_size.y;
                    auto scale = image.getScale();
                    auto image_scale_ratio = scale.x / scale.y;
                    const float width_ratio = height_ratio * image_scale_ratio;
                    image.setScale(width_ratio, height_ratio);
                    image.setPosition(item_pos + sf::Vector2f(image_padding_x, padding_y));
                    window.draw(image);
                    text_offset_x += image_padding_x + width_ratio * image_size.x;
                } else if(!item->thumbnail_url.empty()) {
                    image_fallback.setPosition(item_pos + sf::Vector2f(image_padding_x, padding_y));
                    window.draw(image_fallback);
                    text_offset_x += image_padding_x + image_fallback.getSize().x;
                }
            }

            if(!item->author.empty()) {
                author_text.setString(item->author);
                author_text.setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y));
                window.draw(author_text);

                sf::Vector2f replies_text_pos = author_text.getPosition();
                replies_text_pos.x += author_text.getLocalBounds().width + 5.0f;
                for(size_t reply_index : item->replies) {
                    BodyItem *reply_item = items[reply_index].get();
                    replies_text.setString(">>" + reply_item->post_number);
                    replies_text.setPosition(replies_text_pos);
                    window.draw(replies_text);
                    replies_text_pos.x += replies_text.getLocalBounds().width + 5.0f;
                }

                item_pos.y += author_text.getCharacterSize() + 2.0f;
            }
            //title_text.setString(item->title);
            //title_text.setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y));
            //window.draw(title_text);
            item->title_text->setString(item->title);
            item->title_text->setPosition(std::floor(item_pos.x + text_offset_x), std::floor(item_pos.y + padding_y - 4.0f));
            item->title_text->setMaxWidth(size.x - text_offset_x - image_padding_x * 2.0f);
            item->title_text->draw(window);

            // TODO: Do the same for non-manga content
            const Json::Value &item_progress = content_progress[item->title];
            if(item_progress.isObject()) {
                const Json::Value &current_json = item_progress["current"];
                const Json::Value &total_json = item_progress["total"];
                if(current_json.isNumeric() && total_json.isNumeric()) {
                    progress_text.setString(std::string("Page: ") + std::to_string(current_json.asInt()) + "/" + std::to_string(total_json.asInt()));
                    auto bounds = progress_text.getLocalBounds();
                    progress_text.setPosition(std::floor(item_pos.x + size.x - bounds.width - padding_x), std::floor(item_pos.y + padding_y));
                    window.draw(progress_text);
                }
            }

            pos.y += item_height + spacing_y;
        }
        glDisable(GL_SCISSOR_TEST);

        for(auto it = item_thumbnail_textures.begin(); it != item_thumbnail_textures.end();) {
            if(!it->second.referenced) 
                it = item_thumbnail_textures.erase(it);
            else
                ++it;
        }
    }

    //static
    bool Body::string_find_case_insensitive(const std::string &str, const std::string &substr) {
        auto it = std::search(str.begin(), str.end(), substr.begin(), substr.end(),
            [](char c1, char c2) {
                return std::toupper(c1) == std::toupper(c2);
            });
        return it != str.end();
    }

    void Body::filter_search_fuzzy(const std::string &text) {
        if(text.empty()) {
            for(auto &item : items) {
                item->visible = true;
            }
            return;
        }

        for(auto &item : items) {
            item->visible = string_find_case_insensitive(item->title, text);
        }
    }

    bool Body::no_items_visible() const {
        for(auto &item : items) {
            if(item->visible)
                return false;
        }
        return true;
    }
}