InstalledOptions.cpp 8.2 KB

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