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

#include "Text.hpp"
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include "../external/RoundedRectangleShape.hpp"
#include <json/value.h>
#include <thread>
#include <future>

namespace sf {
    class RenderWindow;
}

namespace QuickMedia {
    class Program;

    class BodyItem {
    public:
        BodyItem(std::string _title);
        BodyItem(const BodyItem &other);

        void set_title(std::string new_title) {
            if(title.empty() && new_title.empty())
                return;
            title = std::move(new_title);
            dirty = true;
        }

        void append_title(std::string str) {
            title += str;
            dirty = true;
        }

        void set_description(std::string new_description) {
            if(description.empty() && new_description.empty())
                return;
            description = std::move(new_description);
            dirty_description = true;
        }

        void append_description(std::string str) {
            description += str;
            dirty_description = true;
        }

        void set_author(std::string new_author) {
            if(author.empty() && new_author.empty())
                return;
            author = std::move(new_author);
            dirty_author = true;
        }

        const std::string& get_title() const { return title; }
        const std::string& get_description() const { return description; }
        const std::string& get_author() const { return author; }

        // TODO: Use a list of strings instead, not all plugins need all of these fields
        std::string url;
        std::string thumbnail_url;
        std::string attached_content_url; // TODO: Remove and use |url| instead
        bool visible;
        bool dirty;
        bool dirty_description;
        bool dirty_author;
        bool thumbnail_is_local;
        std::unique_ptr<Text> title_text;
        std::unique_ptr<Text> description_text;
        std::unique_ptr<Text> author_text;
        // Used by image boards for example. The elements are indices to other body items
        std::vector<size_t> replies;
        std::string post_number;
        sf::Color title_color;
        sf::Color author_color;
    private:
        std::string title;
        std::string description;
        std::string author;
    };

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

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

        // Select previous page, ignoring invisible items. Returns true if the item was changed. This can be used to check if the top was hit when wrap_around is set to false
        bool select_previous_page();
        // Select next page, ignoring invisible items. Returns true if the item was changed. This can be used to check if the bottom was hit when wrap_around is set to false
        bool select_next_page();

        // Select previous item, ignoring invisible items. Returns true if the item was changed. This can be used to check if the top was hit when wrap_around is set to false
        bool select_previous_item();

        // Select next item, ignoring invisible items. Returns true if the item was changed. This can be used to check if the bottom was hit when wrap_around is set to false
        bool select_next_item();
        void set_selected_item(int item);
        
        void select_first_item();
        void select_last_item();
        void reset_selected();
        void clear_items();
        void prepend_items(BodyItems new_items);
        void append_items(BodyItems new_items);
        void clear_thumbnails();

        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;

        int get_selected_item() const { return selected_item; }

        void set_page_scroll(float scroll) { page_scroll = scroll; }
        float get_page_scroll() const { return page_scroll; }
        bool is_last_item_fully_visible() const { return last_item_fully_visible; }

        sf::Font *font;
        sf::Font *bold_font;
        sf::Font *cjk_font;
        sf::Text progress_text;
        sf::Text replies_text;
        BodyItems items;
        bool draw_thumbnails;
        bool wrap_around;
        // Set to {0, 0} to disable resizing
        sf::Vector2i thumbnail_resize_target_size;
        sf::Vector2f thumbnail_fallback_size;
        sf::Color line_seperator_color;
    private:
        void draw_item(sf::RenderWindow &window, BodyItem *item, const sf::Vector2f &pos, const sf::Vector2f &size, const float item_height, const int item_index, const Json::Value &content_progress);
        float get_item_height(BodyItem *item);
    private:
        enum class LoadingState {
            NOT_LOADED,
            LOADING,
            FINISHED_LOADING,
            APPLIED_TO_TEXTURE
        };

        struct ThumbnailData {
            bool referenced = false;
            LoadingState loading_state = LoadingState::NOT_LOADED;
            sf::Texture texture;
            std::unique_ptr<sf::Image> image; // Set in another thread, and then reset after loading it into |texture|
        };
        Program *program;
        void load_thumbnail_from_url(const std::string &url, bool local, sf::Vector2i thumbnail_resize_target_size, std::shared_ptr<ThumbnailData> thumbnail_data);
        std::unordered_map<std::string, std::shared_ptr<ThumbnailData>> item_thumbnail_textures;
        bool loading_thumbnail;
        int selected_item;
        int prev_selected_item;
        float page_scroll;
        // TODO: Use a loading gif or something similar instead, to properly indicate the image is loading. Which could also have text that says "failed to load image" when image loading failed.
        sf::RectangleShape image_fallback;
        sf::RectangleShape item_background_shadow;
        sf::RoundedRectangleShape item_background;
        sf::Sprite image;
        std::future<void> load_thumbnail_future;
        int num_visible_items;
        bool last_item_fully_visible;
    };
}