InstalledOptions.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "InstalledOptions.hpp"
  2. #include "InstalledItem.hpp"
  3. #include "AssetManager.hpp"
  4. #include "DownloadQueue.hpp"
  5. #include "Notification.hpp"
  6. #include "InstalledList.hpp"
  7. #include "States/StateIdentifiers.hpp"
  8. #include "States/DialogState.hpp"
  9. #include <cpp3ds/System/I18n.hpp>
  10. namespace FreeShop {
  11. InstalledOptions::InstalledOptions()
  12. : m_installedItem(nullptr)
  13. , m_browseState(nullptr)
  14. {
  15. m_textGame.setString(_("Game"));
  16. m_textGame.setFillColor(cpp3ds::Color(100, 100, 100));
  17. m_textGame.setCharacterSize(10);
  18. m_textGame.setPosition(20.f, 0.f);
  19. m_textGame.useSystemFont();
  20. m_textUpdates = m_textGame;
  21. m_textUpdates.setString(_("Updates"));
  22. m_textUpdates.move(m_textGame.getLocalBounds().width + 40.f, 0.f);
  23. m_textDLC = m_textUpdates;
  24. m_textDLC.setString(_("DLC"));
  25. m_textDLC.move(m_textUpdates.getLocalBounds().width + 40.f, 0.f);
  26. m_textIconGame.setFont(AssetManager<cpp3ds::Font>::get("fonts/fontawesome.ttf"));
  27. m_textIconGame.setString(L"\uf1f8");
  28. m_textIconGame.setFillColor(cpp3ds::Color(50, 100, 50));
  29. m_textIconGame.setCharacterSize(18);
  30. m_textIconGame.setPosition(m_textGame.getPosition().x + m_textGame.getLocalBounds().width + 5.f, -5.f);
  31. m_textIconUpdates = m_textIconGame;
  32. m_textIconUpdates.setPosition(m_textUpdates.getPosition().x + m_textUpdates.getLocalBounds().width + 5.f, -5.f);
  33. m_textIconDLC = m_textIconGame;
  34. m_textIconDLC.setPosition(m_textDLC.getPosition().x + m_textDLC.getLocalBounds().width + 5.f, -5.f);
  35. }
  36. void InstalledOptions::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  37. {
  38. states.transform *= getTransform();
  39. target.draw(m_textGame, states);
  40. target.draw(m_textIconGame, states);
  41. if (m_updatesAvailable)
  42. {
  43. target.draw(m_textUpdates, states);
  44. target.draw(m_textIconUpdates, states);
  45. }
  46. if (m_dlcAvailable)
  47. {
  48. target.draw(m_textDLC, states);
  49. target.draw(m_textIconDLC, states);
  50. }
  51. }
  52. #define SET_ALPHA(obj, alpha) \
  53. color = obj.getFillColor(); \
  54. color.a = alpha; \
  55. obj.setFillColor(color);
  56. void InstalledOptions::setValues(int tweenType, float *newValues)
  57. {
  58. switch (tweenType) {
  59. case ALPHA: {
  60. cpp3ds::Color color;
  61. auto alpha = static_cast<cpp3ds::Uint8>(newValues[0]);
  62. SET_ALPHA(m_textGame, alpha);
  63. SET_ALPHA(m_textUpdates, alpha);
  64. SET_ALPHA(m_textDLC, alpha);
  65. SET_ALPHA(m_textIconGame, alpha);
  66. SET_ALPHA(m_textIconUpdates, alpha);
  67. SET_ALPHA(m_textIconDLC, alpha);
  68. break;
  69. }
  70. default:
  71. TweenTransformable::setValues(tweenType, newValues);
  72. }
  73. }
  74. int InstalledOptions::getValues(int tweenType, float *returnValues)
  75. {
  76. switch (tweenType) {
  77. case ALPHA:
  78. returnValues[0] = m_textGame.getFillColor().a;
  79. return 1;
  80. default:
  81. return TweenTransformable::getValues(tweenType, returnValues);
  82. }
  83. }
  84. void InstalledOptions::processTouchEvent(const cpp3ds::Event &event)
  85. {
  86. cpp3ds::String appTitle = m_installedItem->getAppItem()->getTitle();
  87. cpp3ds::Vector2f touchPos(event.touch.x - getPosition().x, event.touch.y - getPosition().y);
  88. if (m_textIconGame.getGlobalBounds().contains(touchPos))
  89. {
  90. cpp3ds::Uint64 titleId = m_installedItem->getTitleId();
  91. if (m_browseState)
  92. m_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  93. auto event = reinterpret_cast<DialogState::Event*>(data);
  94. if (event->type == DialogState::GetText)
  95. {
  96. auto str = reinterpret_cast<cpp3ds::String*>(event->data);
  97. *str = _("You are deleting this game,\nincluding all save data:\n\n");
  98. str->insert(str->getSize(), appTitle);
  99. return true;
  100. }
  101. else if (event->type == DialogState::Response)
  102. {
  103. bool *accepted = reinterpret_cast<bool*>(event->data);
  104. if (*accepted)
  105. {
  106. #ifdef _3DS
  107. FS_MediaType mediaType = ((titleId >> 32) & 0x8010) != 0 ? MEDIATYPE_NAND : MEDIATYPE_SD;
  108. AM_DeleteTitle(m_mediaType, titleId);
  109. #endif
  110. m_installedItem->getAppItem()->setInstalled(false);
  111. appTitle.insert(0, _("Deleted game: "));
  112. Notification::spawn(appTitle);
  113. InstalledList::getInstance().refresh();
  114. }
  115. return true;
  116. }
  117. return false;
  118. });
  119. }
  120. else if (m_textIconUpdates.getGlobalBounds().contains(touchPos))
  121. {
  122. if (m_updatesInstalled)
  123. {
  124. if (m_browseState)
  125. m_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  126. auto event = reinterpret_cast<DialogState::Event*>(data);
  127. if (event->type == DialogState::GetText)
  128. {
  129. auto str = reinterpret_cast<cpp3ds::String*>(event->data);
  130. *str = _("You are deleting updates for\nthis title:\n\n");
  131. str->insert(str->getSize(), appTitle);
  132. return true;
  133. }
  134. else if (event->type == DialogState::Response)
  135. {
  136. bool *accepted = reinterpret_cast<bool*>(event->data);
  137. if (*accepted)
  138. {
  139. for (auto &id : m_installedItem->getAppItem()->getUpdates()) {
  140. #ifdef _3DS
  141. AM_DeleteTitle(m_mediaType, id);
  142. #endif
  143. m_installedItem->setUpdateStatus(id, false);
  144. }
  145. m_updatesInstalled = false;
  146. appTitle.insert(0, _("Deleted update: "));
  147. Notification::spawn(appTitle);
  148. update();
  149. }
  150. return true;
  151. }
  152. return false;
  153. });
  154. }
  155. else
  156. {
  157. for (auto &id : m_installedItem->getAppItem()->getUpdates())
  158. if (!m_installedItem->getUpdateStatus(id))
  159. DownloadQueue::getInstance().addDownload(m_installedItem->getAppItem(), id, [=](bool succeeded){
  160. if (!succeeded) {
  161. m_updatesAvailable = true;
  162. update();
  163. }
  164. });
  165. appTitle.insert(0, _("Queued update: "));
  166. m_updatesAvailable = false;
  167. Notification::spawn(appTitle);
  168. update();
  169. }
  170. }
  171. else if (m_textIconDLC.getGlobalBounds().contains(touchPos))
  172. {
  173. if (m_dlcInstalled)
  174. {
  175. if (m_browseState)
  176. m_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  177. auto event = reinterpret_cast<DialogState::Event*>(data);
  178. if (event->type == DialogState::GetText)
  179. {
  180. auto str = reinterpret_cast<cpp3ds::String*>(event->data);
  181. *str = _("You are deleting DLC for\nthis title:\n\n");
  182. str->insert(str->getSize(), appTitle);
  183. return true;
  184. }
  185. else if (event->type == DialogState::Response)
  186. {
  187. bool *accepted = reinterpret_cast<bool*>(event->data);
  188. if (*accepted)
  189. {
  190. for (auto &id : m_installedItem->getAppItem()->getDLC()) {
  191. #ifdef _3DS
  192. AM_DeleteTitle(m_mediaType, id);
  193. #endif
  194. m_installedItem->setDLCStatus(id, false);
  195. }
  196. m_dlcInstalled = false;
  197. appTitle.insert(0, _("Deleted DLC: "));
  198. Notification::spawn(appTitle);
  199. update();
  200. }
  201. return true;
  202. }
  203. return false;
  204. });
  205. }
  206. else
  207. {
  208. for (auto &id : m_installedItem->getAppItem()->getDLC())
  209. if (!m_installedItem->getDLCStatus(id))
  210. DownloadQueue::getInstance().addDownload(m_installedItem->getAppItem(), id, [=](bool succeeded){
  211. if (!succeeded) {
  212. m_dlcAvailable = true;
  213. update();
  214. }
  215. });
  216. appTitle.insert(0, _("Queued DLC: "));
  217. m_dlcAvailable = false;
  218. Notification::spawn(appTitle);
  219. update();
  220. }
  221. }
  222. }
  223. void InstalledOptions::setInstalledItem(InstalledItem *installedItem)
  224. {
  225. m_installedItem = installedItem;
  226. m_updatesAvailable = installedItem->getUpdates().size() > 0;
  227. m_dlcAvailable = installedItem->getDLC().size() > 0;
  228. for (auto &id : m_installedItem->getAppItem()->getUpdates())
  229. if (DownloadQueue::getInstance().isDownloading(id))
  230. m_updatesAvailable = false;
  231. for (auto &id : m_installedItem->getAppItem()->getDLC())
  232. if (DownloadQueue::getInstance().isDownloading(id))
  233. m_dlcAvailable = false;
  234. update();
  235. #ifdef _3DS
  236. m_mediaType = ((installedItem->getTitleId() >> 32) & 0x8010) != 0 ? MEDIATYPE_NAND : MEDIATYPE_SD;
  237. #endif
  238. }
  239. InstalledItem *InstalledOptions::getInstalledItem() const
  240. {
  241. return m_installedItem;
  242. }
  243. void InstalledOptions::update()
  244. {
  245. m_updatesInstalled = true;
  246. for (auto &update : m_installedItem->getUpdates())
  247. if (!update.second)
  248. {
  249. m_updatesInstalled = false;
  250. break;
  251. }
  252. m_dlcInstalled = true;
  253. for (auto &dlc : m_installedItem->getDLC())
  254. if (!dlc.second)
  255. {
  256. m_dlcInstalled = false;
  257. break;
  258. }
  259. m_textIconUpdates.setString(m_updatesInstalled ? L"\uf1f8" : L"\uf019");
  260. m_textIconDLC.setString(m_dlcInstalled ? L"\uf1f8" : L"\uf019");
  261. }
  262. void InstalledOptions::setBrowseState(BrowseState *state)
  263. {
  264. m_browseState = state;
  265. }
  266. } // namespace FreeShop