AppList.cpp 14 KB

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