aboutsummaryrefslogtreecommitdiff
path: root/include/Body.hpp
blob: 86b6984ed06757c3fc52cf01e83b2b1c32800019 (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
#pragma once

#include "Text.hpp"
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <json/value.h>
#include <thread>

namespace QuickMedia {
    class Program;

    class BodyItem {
    public:
        BodyItem(std::string _title): visible(true), dirty(true) {
            set_title(std::move(_title));
        }

        BodyItem(const BodyItem &other) {
            title = other.title;
            url = other.url;
            thumbnail_url = other.thumbnail_url;
            attached_content_url = other.attached_content_url;
            author = other.author;
            visible = other.visible;
            dirty = other.dirty;
            if(other.title_text)
                title_text = std::make_unique<Text>(*other.title_text);
            else
                title_text = nullptr;
            replies = other.replies;
            post_number = other.post_number;
        }

        void set_title(std::string new_title) {
            title = std::move(new_title);
            dirty = true;
        }

        std::string title;
        std::string url;
        std::string thumbnail_url;
        std::string attached_content_url;
        std::string author;
        bool visible;
        bool dirty;
        std::unique_ptr<Text> title_text;
        // Used by image boards for example. The elements are indices to other body items
        std::vector<size_t> replies;
        std::string post_number;
    };

    using BodyItems = std::vector<std::unique_ptr<BodyItem>>;

    class Body {
    public:
        Body(Program *program, sf::Font &font, sf::Font &bold_font);

        // Select previous item, ignoring invisible items
        void select_previous_item();

        // Select next item, ignoring invisible items
        void select_next_item();
        
        void select_first_item();
        void reset_selected();
        void clear_items();

        BodyItem* get_selected() const;

        void clamp_selection();
        void draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size);
        void draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, const Json::Value &content_progress);
        static bool string_find_case_insensitive(const std::string &str, const std::string &substr);

        // TODO: Make this actually fuzzy... Right now it's just a case insensitive string find.
        // This would require reordering the body.
        // TODO: Highlight the part of the text that matches the search.
        // TODO: Ignore dot, whitespace and special characters
        void filter_search_fuzzy(const std::string &text);

        bool no_items_visible() const;

        sf::Text title_text;
        sf::Text progress_text;
        sf::Text author_text;
        sf::Text replies_text;
        int selected_item;
        BodyItems items;
        std::thread thumbnail_load_thread;
        bool draw_thumbnails;
    private:
        struct ThumbnailData {
            bool referenced;
            std::shared_ptr<sf::Texture> texture;
        };
        Program *program;
        std::shared_ptr<sf::Texture> load_thumbnail_from_url(const std::string &url);
        std::unordered_map<std::string, ThumbnailData> item_thumbnail_textures;
        bool loading_thumbnail;
    };
}