AppItem.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #define FTS_FUZZY_MATCH_IMPLEMENTATION
  2. #include "AppItem.hpp"
  3. #include <cpp3ds/System/Err.hpp>
  4. #include <iostream>
  5. #include <cpp3ds/System/I18n.hpp>
  6. #include <cpp3ds/System/FileSystem.hpp>
  7. #include <cpp3ds/System/FileInputStream.hpp>
  8. #include "../AssetManager.hpp"
  9. #include "../AppItem.hpp"
  10. #include "../Util.hpp"
  11. #include "../Theme.hpp"
  12. #include "../fts_fuzzy_match.h"
  13. #include "../States/BrowseState.hpp"
  14. #include <sstream>
  15. namespace {
  16. }
  17. namespace FreeShop
  18. {
  19. namespace GUI
  20. {
  21. AppItem::AppItem()
  22. : m_appItem(nullptr)
  23. , m_infoVisible(true)
  24. , m_filteredOut(false)
  25. , m_visible(true)
  26. , m_matchScore(-1)
  27. {
  28. deselect();
  29. m_icon.setSize(cpp3ds::Vector2f(48.f, 48.f));
  30. m_icon.setPosition(4.f, 4.f);
  31. m_icon.setFillColor(cpp3ds::Color(180,180,180));
  32. m_icon.setOutlineColor(cpp3ds::Color(0, 0, 0, 50));
  33. m_progressBar.setFillColor(cpp3ds::Color(0, 0, 0, 50));
  34. m_titleText.setCharacterSize(12);
  35. m_titleText.setPosition(57.f, 3.f);
  36. if (Theme::isTextThemed)
  37. m_titleText.setFillColor(Theme::primaryTextColor);
  38. else
  39. m_titleText.setFillColor(cpp3ds::Color(50,50,50));
  40. m_titleText.useSystemFont();
  41. m_filesizeText.setCharacterSize(10);
  42. m_filesizeText.setPosition(56.f, 38.f);
  43. if (Theme::isTextThemed)
  44. m_filesizeText.setFillColor(Theme::secondaryTextColor);
  45. else
  46. m_filesizeText.setFillColor(cpp3ds::Color(150,150,150));
  47. m_filesizeText.useSystemFont();
  48. setSize(194.f, 56.f);
  49. }
  50. AppItem::~AppItem()
  51. {
  52. }
  53. void AppItem::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  54. {
  55. states.transform *= getTransform();
  56. cpp3ds::FloatRect rect(0, 0, 400, 240);
  57. cpp3ds::FloatRect rect2 = states.transform.transformRect(rect);
  58. if (rect.intersects(rect2)) {
  59. if (m_infoVisible) {
  60. target.draw(m_background, states);
  61. target.draw(m_icon, states);
  62. target.draw(m_titleText, states);
  63. target.draw(m_filesizeText, states);
  64. for (const auto& flag : m_regionFlags)
  65. target.draw(flag, states);
  66. } else {
  67. target.draw(m_icon, states);
  68. }
  69. }
  70. }
  71. void AppItem::setSize(float width, float height)
  72. {
  73. m_size.x = width;
  74. m_size.y = height;
  75. m_background.setSize(width, height);
  76. }
  77. const cpp3ds::Vector2f& AppItem::getSize() const
  78. {
  79. return m_size;
  80. }
  81. void AppItem::setTitle(const cpp3ds::String &title)
  82. {
  83. cpp3ds::Text tmpText = m_titleText;
  84. auto s = title.toUtf8();
  85. int lineCount = 1;
  86. int startPos = 0;
  87. int lastSpace = 0;
  88. for (int i = 0; i < s.size(); ++i)
  89. {
  90. if (s[i] == ' ')
  91. lastSpace = i;
  92. tmpText.setString(cpp3ds::String::fromUtf8(s.begin() + startPos, s.begin() + i));
  93. if (tmpText.getLocalBounds().width > 126)
  94. {
  95. if (lineCount < 2 && lastSpace != 0)
  96. {
  97. s[lastSpace] = '\n';
  98. i = startPos = lastSpace + 1;
  99. lastSpace = 0;
  100. lineCount++;
  101. }
  102. else
  103. {
  104. s.erase(s.begin() + i, s.end());
  105. break;
  106. }
  107. }
  108. }
  109. m_titleText.setString(cpp3ds::String::fromUtf8(s.begin(), s.end()));
  110. }
  111. void AppItem::setAppItem(std::shared_ptr<::FreeShop::AppItem> appItem)
  112. {
  113. m_appItem = appItem;
  114. if (appItem)
  115. {
  116. setTitle(appItem->getTitle());
  117. setFilesize(appItem->getFilesize());
  118. if (appItem->getIconIndex() >= 0)
  119. {
  120. cpp3ds::IntRect rect;
  121. m_icon.setTexture(appItem->getIcon(rect));
  122. m_icon.setTextureRect(rect);
  123. m_icon.setFillColor(cpp3ds::Color::White);
  124. }
  125. // Region flag sprites
  126. m_regionFlags.clear();
  127. for (int i = 0; i < 7; ++i)
  128. {
  129. auto region = static_cast<FreeShop::AppItem::Region>(1 << i);
  130. if (region & appItem->getRegions())
  131. addRegionFlag(region);
  132. }
  133. float posX = 55.f;
  134. for (auto& flag : m_regionFlags)
  135. {
  136. flag.setColor(cpp3ds::Color(255, 255, 255, 150));
  137. flag.setScale(0.7f, 0.7f);
  138. flag.setPosition(posX, 37.f);
  139. posX += 21.f;
  140. }
  141. }
  142. }
  143. std::shared_ptr<::FreeShop::AppItem> AppItem::getAppItem() const
  144. {
  145. return m_appItem;
  146. }
  147. void AppItem::select()
  148. {
  149. if (!Theme::isItemBGSelected9Themed) {
  150. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get("images/itembg-selected.9.png");
  151. m_background.setTexture(&texture);
  152. } else {
  153. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/itembg-selected.9.png");
  154. m_background.setTexture(&texture);
  155. }
  156. m_background.setColor(cpp3ds::Color(255, 255, 255, 200));
  157. m_icon.setOutlineThickness(2.f);
  158. }
  159. void AppItem::deselect()
  160. {
  161. if (!Theme::isItemBG9Themed) {
  162. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get("images/itembg.9.png");
  163. m_background.setTexture(&texture);
  164. } else {
  165. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/itembg.9.png");
  166. m_background.setTexture(&texture);
  167. }
  168. m_background.setColor(cpp3ds::Color(255, 255, 255, 40));
  169. m_icon.setOutlineThickness(0.f);
  170. }
  171. bool AppItem::isVisible() const
  172. {
  173. return m_visible;
  174. }
  175. void AppItem::setVisible(bool visible)
  176. {
  177. m_visible = visible;
  178. }
  179. bool AppItem::isFilteredOut() const
  180. {
  181. return m_filteredOut;
  182. }
  183. void AppItem::setFilteredOut(bool filteredOut)
  184. {
  185. m_filteredOut = filteredOut;
  186. }
  187. void AppItem::setInfoVisible(bool visible)
  188. {
  189. m_infoVisible = visible;
  190. }
  191. bool AppItem::isInfoVisible() const
  192. {
  193. return m_infoVisible;
  194. }
  195. #define SET_ALPHA(getFunc, setFunc, maxValue) \
  196. color = getFunc(); \
  197. color.a = std::max(std::min(newValues[0] * maxValue / 255.f, 255.f), 0.f); \
  198. setFunc(color);
  199. void AppItem::setValues(int tweenType, float *newValues)
  200. {
  201. switch (tweenType) {
  202. case INFO_ALPHA: {
  203. cpp3ds::Color color;
  204. SET_ALPHA(m_background.getColor, m_background.setColor, 40.f);
  205. SET_ALPHA(m_titleText.getFillColor, m_titleText.setFillColor, 255.f);
  206. SET_ALPHA(m_filesizeText.getFillColor, m_filesizeText.setFillColor, 255.f);
  207. for (auto& flag : m_regionFlags) {
  208. SET_ALPHA(flag.getColor, flag.setColor, 150.f);
  209. }
  210. break;
  211. }
  212. case BACKGROUND_ALPHA: {
  213. cpp3ds::Color color;
  214. SET_ALPHA(m_background.getColor, m_background.setColor, 255.f);
  215. break;
  216. }
  217. default:
  218. TweenTransformable::setValues(tweenType, newValues);
  219. }
  220. }
  221. int AppItem::getValues(int tweenType, float *returnValues)
  222. {
  223. switch (tweenType) {
  224. case BACKGROUND_ALPHA:
  225. returnValues[0] = m_background.getColor().a;
  226. return 1;
  227. case INFO_ALPHA:
  228. returnValues[0] = m_titleText.getFillColor().a;
  229. return 1;
  230. default:
  231. return TweenTransformable::getValues(tweenType, returnValues);
  232. }
  233. }
  234. void AppItem::setMatchTerm(const std::string &string)
  235. {
  236. if (string.empty() || !m_appItem) {
  237. m_matchScore = 0;
  238. return;
  239. }
  240. if (g_browseState->getJapKeyboard()) {
  241. std::size_t found = m_appItem->getNormalizedTitle().find(string);
  242. if (found == std::string::npos)
  243. m_matchScore = -99;
  244. else
  245. m_matchScore = found;
  246. } else if (g_browseState->getTIDKeyboard()) {
  247. if (!fts::fuzzy_match(string.c_str(), m_appItem->getTitleIdStr().c_str(), m_matchScore))
  248. m_matchScore = -99;
  249. } else {
  250. if (!fts::fuzzy_match(string.c_str(), m_appItem->getNormalizedTitle().c_str(), m_matchScore))
  251. m_matchScore = -99;
  252. }
  253. }
  254. int AppItem::getMatchScore() const
  255. {
  256. return m_matchScore;
  257. }
  258. void AppItem::setFilesize(cpp3ds::Uint64 filesize)
  259. {
  260. if (filesize > 1024 * 1024 * 1024)
  261. m_filesizeText.setString(_("%.1f GB", static_cast<float>(filesize) / 1024.f / 1024.f / 1024.f));
  262. else if (filesize > 1024 * 1024)
  263. m_filesizeText.setString(_("%.1f MB", static_cast<float>(filesize) / 1024.f / 1024.f));
  264. else
  265. m_filesizeText.setString(_("%d KB", filesize / 1024));
  266. m_filesizeText.setPosition(189.f - m_filesizeText.getLocalBounds().width, 38.f);
  267. }
  268. void AppItem::addRegionFlag(FreeShop::AppItem::Region region)
  269. {
  270. cpp3ds::Sprite flag;
  271. bool isRegionRecognized = true;
  272. if (!Theme::isFlagsThemed) {
  273. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get("images/flags.png");
  274. texture.setSmooth(true);
  275. flag.setTexture(texture);
  276. } else {
  277. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/flags.png");
  278. texture.setSmooth(true);
  279. flag.setTexture(texture);
  280. }
  281. if (region == FreeShop::AppItem::Europe)
  282. flag.setTextureRect(cpp3ds::IntRect(0, 0, 30, 20));
  283. else if (region == FreeShop::AppItem::Japan)
  284. flag.setTextureRect(cpp3ds::IntRect(30, 0, 30, 20));
  285. else if (region == FreeShop::AppItem::USA)
  286. flag.setTextureRect(cpp3ds::IntRect(0, 20, 30, 20));
  287. else if (region == FreeShop::AppItem::Korea)
  288. flag.setTextureRect(cpp3ds::IntRect(30, 20, 30, 20));
  289. else
  290. isRegionRecognized = false;
  291. if (isRegionRecognized)
  292. m_regionFlags.push_back(flag);
  293. }
  294. } // namespace GUI
  295. } // namespace FreeShop