InstalledOptions.cpp 8.1 KB

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