#pragma once #include "Text.hpp" #include "AsyncImageLoader.hpp" #include #include #include #include "../external/RoundedRectangleShape.hpp" #include #include #include namespace sf { class RenderWindow; class Shader; class Texture; } namespace QuickMedia { class Program; enum class FetchStatus { NONE, LOADING, FINISHED_LOADING, FAILED_TO_LOAD }; enum class ThumbnailMaskType { NONE, CIRCLE }; class BodyItem { public: BodyItem(std::string _title); BodyItem& operator=(BodyItem &other); static std::shared_ptr create(std::string title) { return std::make_shared(std::move(title)); } void set_title(std::string new_title) { if(title.empty() && new_title.empty()) return; title = std::move(new_title); 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 set_author(std::string new_author) { if(author.empty() && new_author.empty()) return; author = std::move(new_author); dirty_author = true; } void set_timestamp(time_t new_timestamp) { if(timestamp == 0 && new_timestamp == 0) return; timestamp = new_timestamp; dirty_timestamp = true; } void set_title_color(sf::Color new_color) { if(new_color != title_color) { title_color = new_color; dirty = true; } } void set_description_color(sf::Color new_color) { if(new_color != description_color) { description_color = new_color; dirty_description = true; } } void set_author_color(sf::Color new_color) { if(new_color != author_color) { author_color = new_color; 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; } 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; std::string attached_content_url; // TODO: Remove and use |url| instead bool visible; bool dirty; bool dirty_description; bool dirty_author; bool dirty_timestamp; bool thumbnail_is_local; std::unique_ptr title_text; std::unique_ptr description_text; std::unique_ptr author_text; std::unique_ptr timestamp_text; // Used by image boards for example. The elements are indices to other body items std::vector replies; std::string post_number; void *userdata; // Not managed, should be deallocated by whoever sets this double last_drawn_time; FetchStatus embedded_item_status = FetchStatus::NONE; std::shared_ptr 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; private: // TODO: Clean up these strings when set in text, and get_title for example should return |title_text.getString()| 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>; using BodyItemRenderCallback = std::function; class Body { public: Body(Program *program, sf::Font *font, sf::Font *bold_font, sf::Font *cjk_font, sf::Texture &loading_icon_texture); // 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, bool reset_prev_selected_item = true); // Returns -1 if item can't be found int get_index_by_body_item(BodyItem *body_item); void select_first_item(); void select_last_item(); void clear_items(); void prepend_items(BodyItems new_items); void append_items(BodyItems new_items); void insert_item_by_timestamp(std::shared_ptr body_item); void insert_items_by_timestamps(BodyItems new_items); void clear_cache(); void clear_text_cache(); void clear_thumbnails(); BodyItem* get_selected() const; std::shared_ptr get_selected_shared(); // Returns null if not visible item BodyItem* get_last_fully_visible_item(); 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); // |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); float get_item_height(BodyItem *item, float width, bool load_texture = true, bool include_embedded_item = true); float get_spacing_y() const; 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); void filter_search_fuzzy_item(const std::string &text, BodyItem *body_item); 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) { 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; sf::Text embedded_item_load_text; BodyItems items; bool draw_thumbnails; bool wrap_around; // Set to {0, 0} to disable resizing sf::Vector2i thumbnail_max_size; sf::Color line_separator_color; BodyItemRenderCallback body_item_render_callback; sf::Shader *thumbnail_mask_shader; 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, bool include_embedded_item = true); 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; private: Program *program; std::unordered_map> item_thumbnail_textures; AsyncImageLoader async_image_loader; 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_separator; sf::RoundedRectangleShape item_background; sf::Sprite image; sf::Sprite loading_icon; int num_visible_items; bool last_item_fully_visible; int last_fully_visible_item; sf::Clock draw_timer; double elapsed_time_sec = 0.0; }; }