InstalledOptions.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "Theme.hpp"
  8. #include "States/StateIdentifiers.hpp"
  9. #include "States/DialogState.hpp"
  10. #include <cpp3ds/System/I18n.hpp>
  11. namespace FreeShop {
  12. InstalledOptions::InstalledOptions()
  13. : m_installedItem(nullptr)
  14. , m_threadUninstallGame(&InstalledOptions::uninstallGame, this)
  15. , m_threadUninstallUpdate(&InstalledOptions::uninstallUpdate, this)
  16. , m_threadUninstallDLC(&InstalledOptions::uninstallDLC, this)
  17. {
  18. m_textGame.setString(_("Game"));
  19. if (Theme::isTextThemed)
  20. m_textGame.setFillColor(Theme::secondaryTextColor);
  21. else
  22. m_textGame.setFillColor(cpp3ds::Color(100, 100, 100));
  23. m_textGame.setCharacterSize(10);
  24. m_textGame.setPosition(20.f, 0.f);
  25. m_textGame.useSystemFont();
  26. m_textUpdates = m_textGame;
  27. m_textUpdates.setString(_("Updates"));
  28. m_textUpdates.move(m_textGame.getLocalBounds().width + 40.f, 0.f);
  29. m_textDLC = m_textUpdates;
  30. m_textDLC.setString(_("DLC"));
  31. m_textDLC.move(m_textUpdates.getLocalBounds().width + 40.f, 0.f);
  32. m_textIconGame.setFont(AssetManager<cpp3ds::Font>::get("fonts/fontawesome.ttf"));
  33. m_textIconGame.setString(L"\uf1f8");
  34. m_textIconGame.setFillColor(cpp3ds::Color(50, 100, 50));
  35. m_textIconGame.setCharacterSize(18);
  36. m_textIconGame.setPosition(m_textGame.getPosition().x + m_textGame.getLocalBounds().width + 5.f, -5.f);
  37. m_textIconUpdates = m_textIconGame;
  38. m_textIconUpdates.setPosition(m_textUpdates.getPosition().x + m_textUpdates.getLocalBounds().width + 5.f, -5.f);
  39. m_textIconDLC = m_textIconGame;
  40. m_textIconDLC.setPosition(m_textDLC.getPosition().x + m_textDLC.getLocalBounds().width + 5.f, -5.f);
  41. }
  42. void InstalledOptions::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  43. {
  44. states.transform *= getTransform();
  45. if (m_titleType != TitleKeys::SystemApplet && m_titleType != TitleKeys::SystemApplication)
  46. {
  47. target.draw(m_textGame, states);
  48. target.draw(m_textIconGame, states);
  49. }
  50. if (m_updatesAvailable)
  51. {
  52. target.draw(m_textUpdates, states);
  53. target.draw(m_textIconUpdates, states);
  54. }
  55. if (m_dlcAvailable)
  56. {
  57. target.draw(m_textDLC, states);
  58. target.draw(m_textIconDLC, states);
  59. }
  60. }
  61. #define SET_ALPHA(obj, alpha) \
  62. color = obj.getFillColor(); \
  63. color.a = alpha; \
  64. obj.setFillColor(color);
  65. void InstalledOptions::setValues(int tweenType, float *newValues)
  66. {
  67. switch (tweenType) {
  68. case ALPHA: {
  69. cpp3ds::Color color;
  70. auto alpha = static_cast<cpp3ds::Uint8>(newValues[0]);
  71. SET_ALPHA(m_textGame, alpha);
  72. SET_ALPHA(m_textUpdates, alpha);
  73. SET_ALPHA(m_textDLC, alpha);
  74. SET_ALPHA(m_textIconGame, alpha);
  75. SET_ALPHA(m_textIconUpdates, alpha);
  76. SET_ALPHA(m_textIconDLC, alpha);
  77. break;
  78. }
  79. default:
  80. TweenTransformable::setValues(tweenType, newValues);
  81. }
  82. }
  83. int InstalledOptions::getValues(int tweenType, float *returnValues)
  84. {
  85. switch (tweenType) {
  86. case ALPHA:
  87. returnValues[0] = m_textGame.getFillColor().a;
  88. return 1;
  89. default:
  90. return TweenTransformable::getValues(tweenType, returnValues);
  91. }
  92. }
  93. void InstalledOptions::processTouchEvent(const cpp3ds::Event &event)
  94. {
  95. cpp3ds::String appTitle = m_installedItem->getAppItem()->getTitle();
  96. cpp3ds::Vector2f touchPos(event.touch.x - getPosition().x, event.touch.y - getPosition().y);
  97. if (m_textIconGame.getGlobalBounds().contains(touchPos))
  98. {
  99. cpp3ds::Uint64 titleId = m_installedItem->getTitleId();
  100. if (m_titleType != TitleKeys::SystemApplet && m_titleType != TitleKeys::SystemApplication)
  101. {
  102. g_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  103. auto event = reinterpret_cast<DialogState::Event *>(data);
  104. if (event->type == DialogState::GetText) {
  105. auto str = reinterpret_cast<cpp3ds::String *>(event->data);
  106. *str = _("You are deleting this game, including all save data:\n\n%s",
  107. appTitle.toAnsiString().c_str());
  108. return true;
  109. }
  110. else if (event->type == DialogState::GetTitle) {
  111. auto str = reinterpret_cast<cpp3ds::String *>(event->data);
  112. *str = _("Uninstall a game");
  113. return true;
  114. }
  115. else if (event->type == DialogState::Response) {
  116. bool *accepted = reinterpret_cast<bool *>(event->data);
  117. if (*accepted) {
  118. m_threadUninstallGame.launch();
  119. }
  120. return true;
  121. }
  122. return false;
  123. });
  124. }
  125. }
  126. else if (m_textIconUpdates.getGlobalBounds().contains(touchPos))
  127. {
  128. if (m_updatesInstalled)
  129. {
  130. g_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  131. auto event = reinterpret_cast<DialogState::Event*>(data);
  132. if (event->type == DialogState::GetText)
  133. {
  134. auto str = reinterpret_cast<cpp3ds::String*>(event->data);
  135. *str = _("You are deleting updates for this title:\n\n%s", appTitle.toAnsiString().c_str());
  136. return true;
  137. }
  138. else if (event->type == DialogState::GetTitle) {
  139. auto str = reinterpret_cast<cpp3ds::String *>(event->data);
  140. *str = _("Uninstall an update");
  141. return true;
  142. }
  143. else if (event->type == DialogState::Response)
  144. {
  145. bool *accepted = reinterpret_cast<bool*>(event->data);
  146. if (*accepted)
  147. {
  148. m_threadUninstallUpdate.launch();
  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. m_updatesAvailable = false;
  166. Notification::spawn(_("Queued update: %s", appTitle.toAnsiString().c_str()));
  167. update();
  168. }
  169. }
  170. else if (m_textIconDLC.getGlobalBounds().contains(touchPos))
  171. {
  172. if (m_dlcInstalled)
  173. {
  174. g_browseState->requestStackPush(States::Dialog, false, [=](void *data) mutable {
  175. auto event = reinterpret_cast<DialogState::Event*>(data);
  176. if (event->type == DialogState::GetText)
  177. {
  178. auto str = reinterpret_cast<cpp3ds::String*>(event->data);
  179. *str = _("You are deleting DLC for this title:\n\n%s", appTitle.toAnsiString().c_str());
  180. return true;
  181. }
  182. else if (event->type == DialogState::GetTitle) {
  183. auto str = reinterpret_cast<cpp3ds::String *>(event->data);
  184. *str = _("Uninstall a DLC");
  185. return true;
  186. }
  187. else if (event->type == DialogState::Response)
  188. {
  189. bool *accepted = reinterpret_cast<bool*>(event->data);
  190. if (*accepted)
  191. {
  192. m_threadUninstallDLC.launch();
  193. }
  194. return true;
  195. }
  196. return false;
  197. });
  198. }
  199. else
  200. {
  201. for (auto &id : m_installedItem->getAppItem()->getDLC())
  202. if (!m_installedItem->getDLCStatus(id))
  203. DownloadQueue::getInstance().addDownload(m_installedItem->getAppItem(), id, [=](bool succeeded){
  204. if (!succeeded) {
  205. m_dlcAvailable = true;
  206. update();
  207. }
  208. });
  209. m_dlcAvailable = false;
  210. Notification::spawn(_("Queued DLC: %s", appTitle.toAnsiString().c_str()));
  211. update();
  212. }
  213. }
  214. }
  215. void InstalledOptions::setInstalledItem(InstalledItem *installedItem)
  216. {
  217. m_installedItem = installedItem;
  218. m_updatesAvailable = installedItem->getUpdates().size() > 0;
  219. m_dlcAvailable = installedItem->getDLC().size() > 0;
  220. for (auto &id : m_installedItem->getAppItem()->getUpdates())
  221. if (DownloadQueue::getInstance().isDownloading(id))
  222. m_updatesAvailable = false;
  223. for (auto &id : m_installedItem->getAppItem()->getDLC())
  224. if (DownloadQueue::getInstance().isDownloading(id))
  225. m_dlcAvailable = false;
  226. update();
  227. m_titleType = static_cast<TitleKeys::TitleType>(installedItem->getTitleId() >> 32);
  228. #ifdef _3DS
  229. m_mediaType = (m_titleType == TitleKeys::DSiWare) ? MEDIATYPE_NAND : MEDIATYPE_SD;
  230. #endif
  231. }
  232. InstalledItem *InstalledOptions::getInstalledItem() const
  233. {
  234. return m_installedItem;
  235. }
  236. void InstalledOptions::update()
  237. {
  238. m_updatesInstalled = true;
  239. for (auto &update : m_installedItem->getUpdates())
  240. if (!update.second)
  241. {
  242. m_updatesInstalled = false;
  243. break;
  244. }
  245. m_dlcInstalled = true;
  246. for (auto &dlc : m_installedItem->getDLC())
  247. if (!dlc.second)
  248. {
  249. m_dlcInstalled = false;
  250. break;
  251. }
  252. m_textIconUpdates.setString(m_updatesInstalled ? L"\uf1f8" : L"\uf019");
  253. m_textIconDLC.setString(m_dlcInstalled ? L"\uf1f8" : L"\uf019");
  254. m_installedItem->updateGameTitle();
  255. }
  256. void InstalledOptions::uninstallGame()
  257. {
  258. InstalledList::getInstance().setDrawList(false);
  259. g_browseState->blockControls(true);
  260. g_browseState->requestStackPush(States::Loading);
  261. cpp3ds::String appTitle = m_installedItem->getAppItem()->getTitle();
  262. cpp3ds::Uint64 titleId = m_installedItem->getTitleId();
  263. #ifdef _3DS
  264. AM_DeleteTitle(m_mediaType, titleId);
  265. #endif
  266. m_installedItem->getAppItem()->setInstalled(false);
  267. Notification::spawn(_("Deleted: %s", appTitle.toAnsiString().c_str()));
  268. g_browseState->requestStackPop();
  269. InstalledList::getInstance().setDrawList(true);
  270. g_browseState->blockControls(false);
  271. InstalledList::getInstance().refresh();
  272. }
  273. void InstalledOptions::uninstallUpdate()
  274. {
  275. InstalledList::getInstance().setDrawList(false);
  276. g_browseState->blockControls(true);
  277. g_browseState->requestStackPush(States::Loading);
  278. cpp3ds::String appTitle = m_installedItem->getAppItem()->getTitle();
  279. for (auto &id : m_installedItem->getAppItem()->getUpdates()) {
  280. #ifdef _3DS
  281. AM_DeleteTitle(m_mediaType, id);
  282. #endif
  283. m_installedItem->setUpdateStatus(id, false);
  284. }
  285. m_updatesInstalled = false;
  286. Notification::spawn(_("Deleted update: %s", appTitle.toAnsiString().c_str()));
  287. g_browseState->requestStackPop();
  288. InstalledList::getInstance().setDrawList(true);
  289. g_browseState->blockControls(false);
  290. update();
  291. }
  292. void InstalledOptions::uninstallDLC()
  293. {
  294. InstalledList::getInstance().setDrawList(false);
  295. g_browseState->blockControls(true);
  296. g_browseState->requestStackPush(States::Loading);
  297. cpp3ds::String appTitle = m_installedItem->getAppItem()->getTitle();
  298. for (auto &id : m_installedItem->getAppItem()->getDLC()) {
  299. #ifdef _3DS
  300. AM_DeleteTitle(m_mediaType, id);
  301. #endif
  302. m_installedItem->setDLCStatus(id, false);
  303. }
  304. m_dlcInstalled = false;
  305. Notification::spawn(_("Deleted DLC: %s", appTitle.toAnsiString().c_str()));
  306. g_browseState->requestStackPop();
  307. InstalledList::getInstance().setDrawList(true);
  308. g_browseState->blockControls(false);
  309. update();
  310. }
  311. } // namespace FreeShop