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

#include "Text.hpp"
#include "AsyncImageLoader.hpp"
#include "RoundedRectangle.hpp"
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <json/value.h>
#include <thread>
#include <future>

namespace sf {
    class RenderWindow;
    class Shader;
    class Texture;
    class Event;
}

namespace QuickMedia {
    class Program;

    enum class FetchStatus {
        NONE,
        QUEUED_LOADING,
        LOADING,
        FINISHED_LOADING,
        FAILED_TO_LOAD
    };

    enum class ThumbnailMaskType {
        NONE,
        CIRCLE,
        ROUNDED_RECTANGLE
    };

    enum BodyTheme : int {
        BODY_THEME_MINIMAL,
        BODY_THEME_MODERN_SPACIOUS
    };

    // TODO: Remove and create an Userdata class instead to replace the void* userdata in BodyItem
    class BodyItemExtra {
    public:
        virtual ~BodyItemExtra() = default;
    };

    struct Reaction {
        std::unique_ptr<Text> text;
        void *userdata = nullptr;
    };

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

        static std::shared_ptr<BodyItem> create(std::string title) { return std::make_shared<BodyItem>(std::move(title)); }

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

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

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

        // |new_timestamp| is in milliseconds
        void set_timestamp(time_t new_timestamp) {
            if(new_timestamp == timestamp)
                return;
            timestamp = new_timestamp;
            dirty_timestamp = true;
        }

        void set_title_color(sf::Color new_color) {
            if(new_color == title_color)
                return;
            title_color = new_color;
            dirty = true;
        }

        void set_description_color(sf::Color new_color) {
            if(new_color == description_color)
                return;
            description_color = new_color;
            dirty_description = true;
        }

        void set_author_color(sf::Color new_color) {
            if(new_color == author_color)
                return;
            author_color = new_color;
            dirty_author = true;
        }

        void add_reaction(std::string text, void *userdata);

        // Returns true if reaction is found
        bool remove_reaction_by_userdata(void *userdata) {
            for(auto it = reactions.begin(); it != reactions.end(); ++it) {
                if(it->userdata == userdata) {
                    reactions.erase(it);
                    return true;
                }
            }
            return false;
        }

        const std::string& get_title() const { return title; }
        const std::string& get_description() const { return description; }
        const std::string& get_author() const { return author; }
        // In milliseconds
        time_t get_timestamp() const { return timestamp; }

        sf::Color get_title_color() const { return title_color; }
        sf::Color get_description_color() const { return description_color; }
        sf::Color get_author_color() const { return author_color; }

        // TODO: Use a list of strings instead, not all plugins need all of these fields
        std::string url;
        std::string thumbnail_url;
        bool visible;
        bool dirty;
        bool dirty_description;
        bool dirty_author;
        bool dirty_timestamp;
        // TODO: Remove this and instead if |thumbnail_url| starts with file://, then its a local file
        bool thumbnail_is_local;
        std::unique_ptr<Text> title_text;
        std::unique_ptr<Text> description_text;
        std::unique_ptr<Text> author_text;
        std::unique_ptr<sf::Text> timestamp_text; // TODO: Remove
        // Used by image boards for example. The elements are indices to other body items
        std::vector<size_t> replies_to;
        // Used by image boards for example. The elements are indices to other body items
        std::vector<size_t> replies;
        std::string post_number;
        void *userdata; // Not managed, should be deallocated by whoever sets this
        float loaded_height = 0.0f;
        float loaded_image_height = 0.0f;
        float loaded_content_height = 0.0f;
        FetchStatus embedded_item_status = FetchStatus::NONE;
        // Important! Should refer to a new BodyItem, not one that already exists in the body.
        // TODO: Allow referring to an existing body item. This doesn't work properly at the moment because max width of text and line count calculation getting messed up
        // if an embedded item wraps but not the original body item.
        std::shared_ptr<BodyItem> embedded_item; // Used by matrix for example to display reply message body. Note: only the first level of embedded items is rendered (not recursive, this is done on purpose)
        ThumbnailMaskType thumbnail_mask_type = ThumbnailMaskType::NONE;
        sf::Vector2i thumbnail_size;
        std::vector<Reaction> reactions; // TODO: Move to a different body item type
        std::shared_ptr<BodyItemExtra> extra; // TODO: Remove

        // Internal use only
        int keep_alive_frames = 0;
    private:
        // TODO: Clean up these strings when set in text, and get_title for example should return |title_text.getString()|
        // TODO: Use sf::String instead, removes the need to convert to utf32 every time the text is dirty (for example when resizing window)
        std::string title;
        std::string description;
        std::string author;
        time_t timestamp;
        sf::Color title_color;
        sf::Color author_color;
        sf::Color description_color;
    };

    using BodyItems = std::vector<std::shared_ptr<BodyItem>>;
    using BodyItemRenderCallback = std::function<void(BodyItem *body_item)>;
    // Return true to merge
    using BodyItemMergeHandler = std::function<bool(BodyItem *prev_item, BodyItem *this_item)>;

    enum class AttachSide {
        TOP,
        BOTTOM
    };

    class Body {
    public:
        Body(BodyTheme body_theme, sf::Texture &loading_icon_texture, sf::Shader *rounded_rectangle_shader, sf::Shader *rounded_rectangle_mask_shader);
        ~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 or if the item scrolled. This can be used to check if the top was hit when wrap_around is set to false
        bool select_previous_item(bool scroll_page_if_large_item = false, bool ignore_columns = false);

        // Select next item, ignoring invisible items. Returns true if the item was changed or if the item scrolled. This can be used to check if the bottom was hit when wrap_around is set to false
        bool select_next_item(bool scroll_page_if_large_item = false, bool ignore_columns = false);
        void set_selected_item(int item, bool reset_prev_selected_item = true);
        void reset_prev_selected_item();

        // Returns -1 if item can't be found
        int get_index_by_body_item(BodyItem *body_item);
        
        void select_first_item(bool reset_page_scroll = true);
        void select_last_item();
        void clear_items();
        void prepend_items_reverse(BodyItems new_items);
        void append_items(BodyItems new_items);
        // Returns the inserted position
        size_t insert_item_by_timestamp(std::shared_ptr<BodyItem> body_item);
        // Note: keeps the selected item (moving the selected_item index if necessary)
        void insert_items_by_timestamps(BodyItems new_items);
        void clear_cache();
        void clear_text_cache();

        BodyItem* get_selected() const;
        std::shared_ptr<BodyItem> get_selected_shared();

        void clamp_selection();

        // Returns true if the event was handled
        bool on_event(const sf::RenderWindow &window, const sf::Event &event, bool keyboard_navigation = true);
        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);
        // |size| is the clip size, another outside this will be cut off.
        // Note: this should be called after |draw|, or thumbnails will be messed up. TODO: find a way to solve this issue in a clean way.
        // This happens because of |draw| sets thumbnails as unreferenced at the beginning and cleans them up at the end if they are not drawn in the same function call.
        // TODO: Right now drawing an item that also exists in the body will cause the text to update geometry every frame if the text is wrapping text and the items are drawn at different sizes,
        // because of Text::setMaxWidth
        void draw_item(sf::RenderWindow &window, BodyItem *item, sf::Vector2f pos, sf::Vector2f size, bool include_embedded_item = true, bool is_embedded = false);

        float get_item_height(BodyItem *item, float width, bool load_texture = true, bool include_embedded_item = true, bool merge_with_previous = false, int item_index = -1);

        // TODO: Highlight the part of the text that matches the search.
        void filter_search_fuzzy(const std::string &text);

        bool no_items_visible() const;

        int get_selected_item() const { return selected_item; }
        bool is_selected_item_last_visible_item() const;

        void set_page_scroll(float scroll);
        float get_page_scroll() const { return page_scroll; }
        // This is the item we can see the start of
        bool is_top_cut_off() const { return top_cut_off; }
        // This is the item we can see the end of
        bool is_bottom_cut_off() const { return bottom_cut_off; }
        int get_num_visible_items() const { return num_visible_items; };

        // Call this once after adding new items
        void items_set_dirty(bool force = false);
        
        void apply_search_filter_for_item(BodyItem *body_item);

        bool can_move_left() const { return selected_column > 0; }
        bool can_move_right() const { return selected_column + 1 < num_columns; }

        BodyItems items;
        bool draw_thumbnails;
        // Set to {0, 0} to disable resizing
        sf::Vector2i thumbnail_max_size;
        BodyItemRenderCallback body_item_render_callback;
        BodyItemMergeHandler body_item_merge_handler;
        std::function<void(BodyItem*)> body_item_select_callback;
        sf::Shader *thumbnail_mask_shader;
        AttachSide attach_side = AttachSide::TOP;
        bool title_mark_urls = false;
        bool swiping_enabled = false;
        bool show_drop_shadow = true;
        bool card_view = false;

        std::function<void()> on_top_reached = nullptr;
        std::function<void()> on_bottom_reached = nullptr;
    private:
        void draw_drop_shadow(sf::RenderWindow &window);
        void filter_search_fuzzy_item(const std::string &text, BodyItem *body_item);
        void handle_item_render(const sf::Vector2f pos, const float item_width, const float item_height, int item_index);
        // Returns the the body total drawn height
        float draw_list_view(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, const int prev_num_visible_items, const Json::Value &content_progress);
        void draw_card_view(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, sf::Vector2u window_size, float scissor_y);
        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, bool include_embedded_item = true, bool merge_with_previous = false);
        void update_dirty_state(BodyItem *body_item, float width);
        void clear_body_item_cache(BodyItem *body_item);
        sf::Vector2i get_item_thumbnail_size(BodyItem *item) const;
        // Returns -1 if not found
        int get_previous_visible_item(int start_index);
        // Returns -1 if not found
        int get_next_visible_item(int start_index);
    private:
        enum class DirtyState {
            FALSE,
            TRUE,
            FORCE_TRUE
        };

        enum class TargetSetState {
            NOT_SET,
            SET,
            APPLIED
        };

        enum class StuckDirection {
            TOP,
            NONE,
            BOTTOM
        };

        BodyTheme body_theme;

        int selected_item;
        int prev_selected_item;
        double page_scroll = 0.0;
        float selected_scrolled = 0.0f;
        // 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.
        // TODO: Use rounded rectangle instead
        sf::RectangleShape image_fallback;
        //sf::RectangleShape item_separator;
        sf::Sprite image;
        sf::Sprite loading_icon;

        sf::Text progress_text;
        sf::Text replies_text;
        sf::Text embedded_item_load_text;

        int num_visible_items;
        bool top_cut_off;
        bool bottom_cut_off;
        int first_visible_item = -1;
        int last_visible_item = -1;
        sf::Clock draw_timer;
        sf::Clock frame_timer;
        double elapsed_time_sec = 0.0;
        bool selected_line_top_visible = true;
        bool selected_line_bottom_visible = true;
        bool selected_item_fits_in_body = true;
        bool mouse_left_pressed = false;
        bool mouse_left_clicked = false;
        bool has_scrolled_with_input = false;
        bool click_counts = false;
        int num_columns = 1;
        int selected_column = 0;
        bool card_view_enabled = true;
        StuckDirection stuck_direction = StuckDirection::NONE;
        sf::Vector2f mouse_click_pos;
        sf::Vector2f mouse_release_pos;
        double mouse_press_pixels_moved_abs;
        sf::Vector2f mouse_pos;
        sf::Vector2i prev_mouse_pos_raw;
        sf::Vector2i mouse_pos_raw;
        sf::Vector2f mouse_scroll_accel;
        sf::Vector2f body_pos;
        sf::Vector2f body_size;
        double body_swipe_x = 0.0;
        bool body_swipe_move_right = false;
        bool grabbed_left_side = false;
        float selected_item_height = 0.0f;
        std::shared_ptr<BodyItem> clicked_body_item = nullptr;
        RoundedRectangle item_background;
        RoundedRectangle reaction_background;
        bool render_selected_item_bg = true;
        sf::Vector2f item_background_prev_pos;
        sf::Vector2f item_background_target_pos;
        sf::Vector2f item_background_prev_size;
        sf::Vector2f item_background_target_size;
        float item_background_target_height;
        TargetSetState target_set = TargetSetState::NOT_SET;
        // TODO: Instead of using this, add functions for modifying |items| and apply the filter on those new items
        DirtyState items_dirty = DirtyState::FALSE;
        std::string current_filter;
        bool using_filter = false;
        sf::Shader *rounded_rectangle_mask_shader;
    };
}