AppItem.cpp 6.6 KB

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