From 2b294258bbc89f1b49554468022a035782e49074 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 5 Aug 2019 20:15:47 +0200 Subject: Show current image / number of images for manga --- plugins/Manganelo.hpp | 4 +++- src/QuickMedia.cpp | 43 +++++++++++++++++++++++++++++++++++++------ src/plugins/Manganelo.cpp | 31 ++++++++++++++++++++++--------- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/plugins/Manganelo.hpp b/plugins/Manganelo.hpp index 5e9382e..0f6f421 100644 --- a/plugins/Manganelo.hpp +++ b/plugins/Manganelo.hpp @@ -8,8 +8,10 @@ namespace QuickMedia { SearchResult search(const std::string &url, std::vector> &result_items, Page &next_page) override; SuggestionResult update_search_suggestions(const std::string &text, std::vector> &result_items) override; ImageResult get_image_by_index(const std::string &url, int index, std::string &image_data); + ImageResult get_number_of_images(const std::string &url, int &num_images); private: - ImageResult get_image_urls_for_chapter(const std::string &url, std::vector &urls); + // Caches url. If the same url is requested multiple times then the cache is used + ImageResult get_image_urls_for_chapter(const std::string &url); private: std::string last_chapter_url; std::vector last_chapter_image_urls; diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 4a0a75d..5f65fdd 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -503,14 +503,14 @@ namespace QuickMedia { image_texture.setSmooth(true); image.setTexture(image_texture, true); } else { - error_message.setString(std::string("Failed to load image for page ") + std::to_string(image_index)); + error_message.setString(std::string("Failed to load image for page ") + std::to_string(image_index + 1)); } } else if(image_result == ImageResult::END) { - // TODO: Better error message, with chapter name + // TODO: Improve this message error_message.setString("End of chapter"); } else { // TODO: Convert ImageResult error to a string and show to user - error_message.setString(std::string("Network error, failed to get image for page ") + std::to_string(image_index)); + error_message.setString(std::string("Network error, failed to get image for page ") + std::to_string(image_index + 1)); } image_data.resize(0); @@ -518,8 +518,20 @@ namespace QuickMedia { bool resized = true; sf::Event event; - // TODO: Show current page / number of pages. - // TODO: Show to user if a certain page is missing (by checking page name (number) and checking if some is skipped) + int num_images = 0; + image_plugin->get_number_of_images(images_url, num_images); + + sf::Text chapter_text(std::string("Page ") + std::to_string(image_index + 1) + "/" + std::to_string(num_images), font, 18); + if(image_index == num_images) + chapter_text.setString("end"); + chapter_text.setFillColor(sf::Color::White); + sf::RectangleShape chapter_text_background; + chapter_text_background.setFillColor(sf::Color(0, 0, 0, 150)); + + sf::Clock window_last_focused; + bool focused = window.hasFocus(); + + // TODO: Show to user if a certain page is missing (by checking page name (number) and checking if some are skipped) while (current_page == Page::IMAGES) { while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { @@ -537,13 +549,20 @@ namespace QuickMedia { return; } } else if(event.key.code == sf::Keyboard::Down) { - if(!error) { + if(image_index < num_images) { ++image_index; return; } } else if(event.key.code == sf::Keyboard::Escape) { current_page = Page::EPISODE_LIST; } + } else if(event.type == sf::Event::MouseEntered) { + window_last_focused.restart(); + focused = true; + } else if(event.type == sf::Event::MouseLeft) { + focused = false; + } else if(event.type == sf::Event::MouseMoved && focused) { + window_last_focused.restart(); } } @@ -568,6 +587,18 @@ namespace QuickMedia { } else { window.draw(image); } + if(window_last_focused.getElapsedTime().asMilliseconds() < 4000) { + float font_height = chapter_text.getCharacterSize() + 8.0f; + float background_height = font_height + 20.0f; + + chapter_text_background.setSize(sf::Vector2f(window_size.x, background_height)); + chapter_text_background.setPosition(0.0f, window_size.y - background_height); + window.draw(chapter_text_background); + + auto text_bounds = chapter_text.getLocalBounds(); + chapter_text.setPosition(window_size.x * 0.5f - text_bounds.width * 0.5f, window_size.y - background_height * 0.5f - text_bounds.height * 0.5f); + window.draw(chapter_text); + } window.display(); } } diff --git a/src/plugins/Manganelo.cpp b/src/plugins/Manganelo.cpp index d69aab1..6dd4ec7 100644 --- a/src/plugins/Manganelo.cpp +++ b/src/plugins/Manganelo.cpp @@ -95,13 +95,9 @@ namespace QuickMedia { } ImageResult Manganelo::get_image_by_index(const std::string &url, int index, std::string &image_data) { - if(url != last_chapter_url) { - last_chapter_image_urls.clear(); - ImageResult image_result = get_image_urls_for_chapter(url, last_chapter_image_urls); - if(image_result != ImageResult::OK) - return image_result; - last_chapter_url = url; - } + ImageResult image_result = get_image_urls_for_chapter(url); + if(image_result != ImageResult::OK) + return image_result; int num_images = last_chapter_image_urls.size(); if(index < 0 || index >= num_images) @@ -120,7 +116,22 @@ namespace QuickMedia { } } - ImageResult Manganelo::get_image_urls_for_chapter(const std::string &url, std::vector &urls) { + ImageResult Manganelo::get_number_of_images(const std::string &url, int &num_images) { + num_images = 0; + ImageResult image_result = get_image_urls_for_chapter(url); + if(image_result != ImageResult::OK) + return image_result; + + num_images = last_chapter_image_urls.size(); + return ImageResult::OK; + } + + ImageResult Manganelo::get_image_urls_for_chapter(const std::string &url) { + if(url == last_chapter_url) + return ImageResult::OK; + + last_chapter_image_urls.clear(); + std::string website_data; if(download_to_string(url, website_data) != DownloadResult::OK) return ImageResult::NET_ERR; @@ -136,10 +147,12 @@ namespace QuickMedia { const char *src = quickmedia_html_node_get_attribute_value(node, "src"); if(src) urls->push_back(src); - }, &urls); + }, &last_chapter_image_urls); cleanup: quickmedia_html_search_deinit(&html_search); + if(result == 0) + last_chapter_url = url; return result == 0 ? ImageResult::OK : ImageResult::ERR; } } \ No newline at end of file -- cgit v1.2.3