AppList.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include <cpp3ds/System/FileSystem.hpp>
  2. #include <cpp3ds/System/FileInputStream.hpp>
  3. #include <functional>
  4. #include <iostream>
  5. #include <rapidjson/document.h>
  6. #include <rapidjson/filereadstream.h>
  7. #include <TweenEngine/Tween.h>
  8. #include <fstream>
  9. #include <cpp3ds/System/Clock.hpp>
  10. #include <cpp3ds/System/Sleep.hpp>
  11. #include "AppList.hpp"
  12. #include "Util.hpp"
  13. #include "TitleKeys.hpp"
  14. namespace FreeShop {
  15. AppList::AppList(std::string jsonFilename)
  16. : m_sortType(Name)
  17. , m_selectedIndex(-1)
  18. , m_collapsed(false)
  19. , m_filterRegions(0)
  20. , m_filterLanguages(0)
  21. , m_sortAscending(true)
  22. {
  23. m_jsonFilename = jsonFilename;
  24. }
  25. AppList::~AppList()
  26. {
  27. for (auto& texture : m_iconTextures)
  28. delete texture;
  29. }
  30. void AppList::refresh()
  31. {
  32. #ifdef EMULATION
  33. bool isNew3DS = true;
  34. #else
  35. bool isNew3DS = false;
  36. APT_CheckNew3DS(&isNew3DS);
  37. #endif
  38. cpp3ds::FileInputStream file;
  39. if (file.open(m_jsonFilename))
  40. {
  41. // Read file to string
  42. int size = file.getSize();
  43. std::string json;
  44. json.resize(size);
  45. file.read(&json[0], size);
  46. // Parse json string
  47. rapidjson::Document doc;
  48. doc.Parse(json.c_str());
  49. int i = 0;
  50. for (rapidjson::Value::ConstMemberIterator iter = doc.MemberBegin(); iter != doc.MemberEnd(); ++iter)
  51. {
  52. std::string id = iter->name.GetString();
  53. cpp3ds::Uint64 titleId = strtoull(id.c_str(), 0, 16);
  54. if (!isNew3DS && ((titleId >> 24) & 0xF) == 0xF)
  55. continue;
  56. if (TitleKeys::get(titleId))
  57. {
  58. auto appItem = std::make_shared<AppItem>();
  59. std::unique_ptr<GUI::AppItem> guiAppItem(new GUI::AppItem());
  60. appItem->loadFromJSON(iter->name.GetString(), iter->value);
  61. guiAppItem->setAppItem(appItem);
  62. // Move offscreen to avoid everything being drawn at once and crashing
  63. guiAppItem->setPosition(500.f, 100.f);
  64. m_appItems.emplace_back(std::move(appItem));
  65. m_guiAppItems.emplace_back(std::move(guiAppItem));
  66. }
  67. }
  68. }
  69. if (m_selectedIndex < 0 && getVisibleCount() > 0)
  70. m_selectedIndex = 0;
  71. sort();
  72. filter();
  73. reposition();
  74. setSelectedIndex(m_selectedIndex);
  75. }
  76. void AppList::setSortType(AppList::SortType sortType, bool ascending)
  77. {
  78. m_sortType = sortType;
  79. m_sortAscending = ascending;
  80. sort();
  81. reposition();
  82. }
  83. void AppList::sort()
  84. {
  85. setSelectedIndex(-1);
  86. std::sort(m_guiAppItems.begin(), m_guiAppItems.end(), [&](const std::unique_ptr<GUI::AppItem>& a, const std::unique_ptr<GUI::AppItem>& b)
  87. {
  88. if (a->isFilteredOut() != b->isFilteredOut())
  89. return !a->isFilteredOut();
  90. if (a->getMatchScore() != b->getMatchScore())
  91. {
  92. return a->getMatchScore() > b->getMatchScore();
  93. }
  94. else
  95. {
  96. if (m_sortAscending)
  97. {
  98. switch(m_sortType) {
  99. case Name: return a->getAppItem()->getNormalizedTitle() < b->getAppItem()->getNormalizedTitle();
  100. case Size: return a->getAppItem()->getFilesize() < b->getAppItem()->getFilesize();
  101. case VoteScore: return a->getAppItem()->getVoteScore() < b->getAppItem()->getVoteScore();
  102. case VoteCount: return a->getAppItem()->getVoteCount() < b->getAppItem()->getVoteCount();
  103. case ReleaseDate: return a->getAppItem()->getReleaseDate() < b->getAppItem()->getReleaseDate();
  104. }
  105. }
  106. else
  107. {
  108. switch(m_sortType) {
  109. case Name: return a->getAppItem()->getNormalizedTitle() > b->getAppItem()->getNormalizedTitle();
  110. case Size: return a->getAppItem()->getFilesize() > b->getAppItem()->getFilesize();
  111. case VoteScore: return a->getAppItem()->getVoteScore() > b->getAppItem()->getVoteScore();
  112. case VoteCount: return a->getAppItem()->getVoteCount() > b->getAppItem()->getVoteCount();
  113. case ReleaseDate: return a->getAppItem()->getReleaseDate() > b->getAppItem()->getReleaseDate();
  114. }
  115. }
  116. }
  117. });
  118. if (!m_guiAppItems.empty())
  119. setSelectedIndex(0);
  120. }
  121. void AppList::filter()
  122. {
  123. // Region filter
  124. // Also resets the filter state when no region filter is set.
  125. if (m_filterRegions)
  126. {
  127. for (const auto& appItemGUI : m_guiAppItems)
  128. appItemGUI->setFilteredOut(!(appItemGUI->getAppItem()->getRegions() & m_filterRegions));
  129. }
  130. else
  131. for (const auto& appItemGUI : m_guiAppItems)
  132. appItemGUI->setFilteredOut(false);
  133. // Language filter
  134. if (m_filterLanguages != 0)
  135. {
  136. for (const auto& appItemGUI : m_guiAppItems)
  137. if (appItemGUI->isVisible())
  138. if (!(appItemGUI->getAppItem()->getLanguages() & m_filterLanguages))
  139. appItemGUI->setFilteredOut(true);
  140. }
  141. // Genre filter
  142. if (!m_filterGenres.empty())
  143. {
  144. for (const auto& appItemGUI : m_guiAppItems)
  145. if (appItemGUI->isVisible())
  146. {
  147. for (const auto& appGenre : appItemGUI->getAppItem()->getGenres())
  148. for (const auto& filterGenre : m_filterGenres)
  149. if (appGenre == filterGenre)
  150. goto matchedGenre;
  151. appItemGUI->setFilteredOut(true);
  152. matchedGenre:;
  153. }
  154. }
  155. // Platform filter
  156. if (!m_filterPlatforms.empty())
  157. {
  158. for (const auto& appItemGUI : m_guiAppItems)
  159. if (appItemGUI->isVisible())
  160. {
  161. for (const auto& filterPlatform : m_filterPlatforms)
  162. if (appItemGUI->getAppItem()->getPlatform() == filterPlatform)
  163. goto matchedPlatform;
  164. appItemGUI->setFilteredOut(true);
  165. matchedPlatform:;
  166. }
  167. }
  168. sort();
  169. reposition();
  170. setPosition(0.f, 0.f);
  171. }
  172. void AppList::reposition()
  173. {
  174. bool segmentFound = false;
  175. float destY = 4.f;
  176. int i = 0;
  177. float newX = m_selectedIndex / 4 * (m_collapsed ? 59.f : 200.f);
  178. for (auto& app : m_guiAppItems)
  179. {
  180. if (!app->isVisible() || app->isFilteredOut())
  181. continue;
  182. float destX = 3.f + (i/4) * (m_collapsed ? 59.f : 200.f);
  183. float itemPosX = app->getPosition().x + getPosition().x;
  184. if ((destX-newX < -200.f || destX-newX > 400.f) && (itemPosX < -200 || itemPosX > 400.f))
  185. {
  186. if (segmentFound == m_collapsed)
  187. TweenEngine::Tween::set(*app, GUI::AppItem::POSITION_XY)
  188. .target(destX, destY)
  189. .delay(0.3f)
  190. .start(m_tweenManager);
  191. else
  192. app->setPosition(destX, destY);
  193. }
  194. else
  195. {
  196. segmentFound = true;
  197. TweenEngine::Tween::to(*app, GUI::AppItem::POSITION_XY, 0.3f)
  198. .target(destX, destY)
  199. .start(m_tweenManager);
  200. }
  201. if (++i % 4 == 0)
  202. destY = 4.f;
  203. else
  204. destY += 59.f;
  205. }
  206. }
  207. void AppList::setSelectedIndex(int index)
  208. {
  209. if (m_selectedIndex >= 0)
  210. m_guiAppItems[m_selectedIndex]->deselect();
  211. m_selectedIndex = index;
  212. if (m_selectedIndex >= 0)
  213. m_guiAppItems[m_selectedIndex]->select();
  214. }
  215. int AppList::getSelectedIndex() const
  216. {
  217. return m_selectedIndex;
  218. }
  219. GUI::AppItem *AppList::getSelected()
  220. {
  221. if (m_selectedIndex < 0 || m_selectedIndex > m_guiAppItems.size()-1)
  222. return nullptr;
  223. return m_guiAppItems[m_selectedIndex].get();
  224. }
  225. void AppList::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  226. {
  227. states.transform *= getTransform();
  228. for (auto& app : m_guiAppItems)
  229. {
  230. if (app->isVisible() && !app->isFilteredOut())
  231. target.draw(*app, states);
  232. }
  233. }
  234. size_t AppList::getCount() const
  235. {
  236. return m_guiAppItems.size();
  237. }
  238. size_t AppList::getVisibleCount() const
  239. {
  240. size_t count = 0;
  241. for (auto& item : m_guiAppItems)
  242. if (item->isVisible() && !item->isFilteredOut())
  243. count++;
  244. else break;
  245. return count;
  246. }
  247. void AppList::setCollapsed(bool collapsed)
  248. {
  249. if (m_collapsed == collapsed)
  250. return;
  251. m_tweenManager.killAll();
  252. float newX = m_selectedIndex / 4 * (collapsed ? 59.f : 200.f);
  253. if (collapsed)
  254. {
  255. for (auto &app : m_guiAppItems)
  256. {
  257. float appPosX = app->getPosition().x + getPosition().x;
  258. if (appPosX >= 0.f && appPosX < 400.f)
  259. TweenEngine::Tween::to(*app, GUI::AppItem::INFO_ALPHA, 0.3f)
  260. .target(0)
  261. .setCallback(TweenEngine::TweenCallback::COMPLETE, [&](TweenEngine::BaseTween *source) {
  262. app->setInfoVisible(false);
  263. })
  264. .start(m_tweenManager);
  265. else {
  266. app->setInfoVisible(false);
  267. TweenEngine::Tween::set(*app, GUI::AppItem::INFO_ALPHA)
  268. .target(0)
  269. .start(m_tweenManager);
  270. }
  271. }
  272. TweenEngine::Tween::to(*this, AppList::POSITION_X, 0.3f)
  273. .target(-newX)
  274. .delay(0.3f)
  275. .setCallback(TweenEngine::TweenCallback::START, [=](TweenEngine::BaseTween *source) {
  276. m_collapsed = collapsed;
  277. reposition();
  278. })
  279. .start(m_tweenManager);
  280. }
  281. else
  282. {
  283. m_collapsed = collapsed;
  284. reposition();
  285. TweenEngine::Tween::to(*this, AppList::POSITION_X, 0.3f)
  286. .target(-newX)
  287. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this, newX](TweenEngine::BaseTween *source) {
  288. for (auto &app : m_guiAppItems) {
  289. float appPosX = app->getPosition().x - newX;
  290. app->setInfoVisible(true);
  291. if (appPosX >= 0.f && appPosX < 400.f)
  292. TweenEngine::Tween::to(*app, GUI::AppItem::INFO_ALPHA, 0.3f)
  293. .target(255.f)
  294. .start(m_tweenManager);
  295. else
  296. TweenEngine::Tween::set(*app, GUI::AppItem::INFO_ALPHA)
  297. .target(255.f)
  298. .start(m_tweenManager);
  299. }
  300. GUI::AppItem *item = getSelected();
  301. if (item)
  302. TweenEngine::Tween::to(*item, GUI::AppItem::BACKGROUND_ALPHA, 0.3f)
  303. .target(255.f)
  304. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween *source) {
  305. setSelectedIndex(m_selectedIndex);
  306. })
  307. .delay(0.3f)
  308. .start(m_tweenManager);
  309. })
  310. .start(m_tweenManager);
  311. }
  312. }
  313. bool AppList::isCollapsed() const
  314. {
  315. return m_collapsed;
  316. }
  317. void AppList::update(float delta)
  318. {
  319. m_tweenManager.update(delta);
  320. }
  321. void AppList::filterBySearch(const std::string &searchTerm, std::vector<util3ds::RichText> &textMatches)
  322. {
  323. m_tweenManager.killAll();
  324. for (auto& item : m_guiAppItems)
  325. {
  326. item->setMatchTerm(searchTerm);
  327. if (item->getMatchScore() > -99)
  328. {
  329. item->setVisible(true);
  330. TweenEngine::Tween::to(*item, SCALE_XY, 0.3)
  331. .target(1.f, 1.f)
  332. .start(m_tweenManager);
  333. }
  334. else{
  335. GUI::AppItem* itemptr = item.get();
  336. TweenEngine::Tween::to(*item, SCALE_XY, 0.3)
  337. .target(0.f, 0.f)
  338. .setCallback(TweenEngine::TweenCallback::COMPLETE, [itemptr](TweenEngine::BaseTween *source) {
  339. itemptr->setVisible(false);
  340. })
  341. .start(m_tweenManager);
  342. }
  343. }
  344. if (m_selectedIndex >= 0)
  345. m_guiAppItems[m_selectedIndex]->deselect();
  346. sort();
  347. reposition();
  348. int i = 0;
  349. for (auto& textMatch : textMatches)
  350. {
  351. textMatch.clear();
  352. if (searchTerm.empty())
  353. continue;
  354. if (i < getVisibleCount())
  355. {
  356. auto item = m_guiAppItems[i].get();
  357. if (item->getMatchScore() > -99)
  358. {
  359. bool matching = false;
  360. const char *str = item->getAppItem()->getNormalizedTitle().c_str();
  361. const char *pattern = searchTerm.c_str();
  362. const char *strLastPos = str;
  363. auto title = item->getAppItem()->getTitle().toUtf8();
  364. auto titleCurPos = title.begin();
  365. auto titleLastPos = title.begin();
  366. textMatch << cpp3ds::Color(150,150,150);
  367. while (*str != '\0') {
  368. if (tolower(*pattern) == tolower(*str)) {
  369. if (!matching) {
  370. matching = true;
  371. if (str != strLastPos) {
  372. textMatch << cpp3ds::String::fromUtf8(titleLastPos, titleCurPos);
  373. titleLastPos = titleCurPos;
  374. strLastPos = str;
  375. }
  376. textMatch << cpp3ds::Color::Black;
  377. }
  378. ++pattern;
  379. } else {
  380. if (matching) {
  381. matching = false;
  382. if (str != strLastPos) {
  383. textMatch << cpp3ds::String::fromUtf8(titleLastPos, titleCurPos);
  384. titleLastPos = titleCurPos;
  385. strLastPos = str;
  386. }
  387. textMatch << cpp3ds::Color(150,150,150);
  388. }
  389. }
  390. ++str;
  391. titleCurPos = cpp3ds::Utf8::next(titleCurPos, title.end());
  392. }
  393. if (str != strLastPos)
  394. textMatch << cpp3ds::String::fromUtf8(titleLastPos, title.end());
  395. }
  396. i++;
  397. }
  398. }
  399. if (m_guiAppItems.size() > 0 && textMatches.size() > 0)
  400. setSelectedIndex(0);
  401. }
  402. std::vector<std::unique_ptr<GUI::AppItem>> &AppList::getList()
  403. {
  404. return m_guiAppItems;
  405. }
  406. AppList &AppList::getInstance()
  407. {
  408. static AppList list(FREESHOP_DIR "/cache/data.json");
  409. return list;
  410. }
  411. void AppList::setFilterGenres(const std::vector<int> &genres)
  412. {
  413. m_filterGenres = genres;
  414. filter();
  415. }
  416. void AppList::setFilterPlatforms(const std::vector<int> &platforms)
  417. {
  418. m_filterPlatforms = platforms;
  419. filter();
  420. }
  421. void AppList::setFilterRegions(int regions)
  422. {
  423. m_filterRegions = regions;
  424. filter();
  425. }
  426. void AppList::setFilterLanguages(int languages)
  427. {
  428. m_filterLanguages = languages;
  429. filter();
  430. }
  431. } // namespace FreeShop