DownloadQueue.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef FREESHOP_DOWNLOADQUEUE_HPP
  2. #define FREESHOP_DOWNLOADQUEUE_HPP
  3. #include <bits/unique_ptr.h>
  4. #include <cpp3ds/Window/Event.hpp>
  5. #include <TweenEngine/TweenManager.h>
  6. #include <cpp3ds/Audio/Sound.hpp>
  7. #include <cpp3ds/Audio/SoundBuffer.hpp>
  8. #include "Download.hpp"
  9. #include "GUI/AppItem.hpp"
  10. #include "Installer.hpp"
  11. #include "GUI/Scrollable.hpp"
  12. namespace FreeShop {
  13. typedef std::function<void(bool)> DownloadCompleteCallback;
  14. struct DownloadItem {
  15. DownloadItem(std::shared_ptr<AppItem> appItem, Download *download, Installer *installer);
  16. ~DownloadItem();
  17. std::shared_ptr<AppItem> appItem;
  18. Download *download;
  19. Installer *installer;
  20. };
  21. class DownloadQueue : public cpp3ds::Drawable, public Scrollable, public util3ds::TweenTransformable<cpp3ds::Transformable> {
  22. public:
  23. ~DownloadQueue();
  24. static DownloadQueue &getInstance();
  25. void addDownload(std::shared_ptr<AppItem> app, cpp3ds::Uint64 titleId = 0, DownloadCompleteCallback callback = nullptr, int contentIndex = -1, float progress = 0.f);
  26. void restartDownload(Download *download);
  27. bool isDownloading(std::shared_ptr<AppItem> app);
  28. bool isDownloading(cpp3ds::Uint64 titleId);
  29. void suspend();
  30. void resume();
  31. void save();
  32. void sendTop(Download* download);
  33. size_t getCount();
  34. size_t getActiveCount();
  35. void update(float delta);
  36. bool processEvent(const cpp3ds::Event& event);
  37. virtual void setScroll(float position);
  38. virtual float getScroll();
  39. virtual const cpp3ds::Vector2f &getScrollSize();
  40. protected:
  41. DownloadQueue();
  42. void load();
  43. void realign();
  44. virtual void draw(cpp3ds::RenderTarget& target, cpp3ds::RenderStates states) const;
  45. // Go through list make sure only top item is downloading and rest are suspended if necessary
  46. void refresh();
  47. private:
  48. std::vector<std::unique_ptr<DownloadItem>> m_downloads;
  49. TweenEngine::TweenManager m_tweenManager;
  50. cpp3ds::SoundBuffer m_soundBufferFinish;
  51. cpp3ds::Sound m_soundFinish;
  52. float m_scrollPos;
  53. cpp3ds::Vector2f m_size;
  54. cpp3ds::Thread m_threadRefresh;
  55. cpp3ds::Mutex m_mutexRefresh;
  56. cpp3ds::Clock m_clockRefresh;
  57. volatile bool m_refreshEnd;
  58. };
  59. } // namespace FreeShop
  60. #endif // FREESHOP_DOWNLOADQUEUE_HPP