ImageViewer.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #include "Path.hpp"
  3. #include <string>
  4. #include <vector>
  5. #include <mglpp/graphics/Texture.hpp>
  6. #include <mglpp/graphics/Sprite.hpp>
  7. #include <mglpp/graphics/Text.hpp>
  8. #include <mglpp/system/Clock.hpp>
  9. #include <thread>
  10. #include <memory>
  11. namespace mgl {
  12. class Window;
  13. }
  14. namespace QuickMedia {
  15. enum class ImageStatus {
  16. WAITING,
  17. LOADING,
  18. FAILED_TO_LOAD,
  19. LOADED,
  20. APPLIED_TO_TEXTURE
  21. };
  22. struct ImageData {
  23. mgl::Texture texture;
  24. mgl::Sprite sprite;
  25. ImageStatus image_status;
  26. std::unique_ptr<mgl::Image> image;
  27. bool visible_on_screen;
  28. float prev_height = 0.0f;
  29. };
  30. struct PageSize {
  31. mgl::vec2d size;
  32. mgl::vec2d prev_size;
  33. bool loaded;
  34. };
  35. enum class ImageViewerAction {
  36. NONE,
  37. RETURN,
  38. SWITCH_TO_SINGLE_IMAGE_MODE,
  39. PREVIOUS_CHAPTER,
  40. NEXT_CHAPTER
  41. };
  42. class ImageViewer {
  43. public:
  44. ImageViewer(mgl::Window *window, int num_pages, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir, bool *fit_image_to_window);
  45. ~ImageViewer();
  46. ImageViewerAction draw();
  47. // Returns page as 1 indexed
  48. int get_focused_page() const;
  49. int get_num_pages() const { return num_pages; }
  50. private:
  51. void scroll_to_page(int page);
  52. void load_image_async(const Path &path, std::shared_ptr<ImageData> image_data);
  53. bool render_page(mgl::Window &window, int page, double offset_y);
  54. mgl::vec2d get_page_size(int page);
  55. private:
  56. mgl::Window *window;
  57. int current_page;
  58. int num_pages;
  59. std::string content_title;
  60. std::string chapter_title;
  61. Path chapter_cache_dir;
  62. double scroll = 0.0;
  63. double scroll_speed = 0.0;
  64. double min_page_top_dist;
  65. int page_closest_to_top;
  66. int focused_page;
  67. int prev_focused_page = -1;
  68. mgl::Clock frame_timer;
  69. mgl::Text page_text;
  70. std::vector<std::shared_ptr<ImageData>> image_data;
  71. std::vector<PageSize> page_size;
  72. mgl::vec2d window_size;
  73. bool window_size_set = false;
  74. bool middle_mouse_scrolling = false;
  75. double autoscroll_start_y = 0.0;
  76. // TODO: Fix
  77. //sf::Cursor default_cursor;
  78. //sf::Cursor size_vertical_cursor;
  79. bool has_default_cursor;
  80. bool has_size_vertical_cursor;
  81. bool up_pressed = false;
  82. bool down_pressed = false;
  83. std::thread image_loader_thread;
  84. bool loading_image = false;
  85. bool *fit_image_to_window;
  86. mgl::vec2d prev_size_first_page;
  87. mgl::vec2d prev_size_last_page;
  88. bool show_progress_bar = true;
  89. };
  90. }