InstalledItem.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <cpp3ds/System/I18n.hpp>
  2. #include "InstalledItem.hpp"
  3. #include "AssetManager.hpp"
  4. #include "AppList.hpp"
  5. #include "TitleKeys.hpp"
  6. #include "Theme.hpp"
  7. namespace FreeShop {
  8. InstalledItem::InstalledItem(cpp3ds::Uint64 titleId)
  9. : m_titleId(titleId)
  10. , m_updateInstallCount(0)
  11. , m_dlcInstallCount(0)
  12. {
  13. TitleKeys::TitleType titleType = static_cast<TitleKeys::TitleType>(titleId >> 32);
  14. bool found = false;
  15. if (titleType == TitleKeys::SystemApplication || titleType == TitleKeys::SystemApplet)
  16. {
  17. m_appItem = std::make_shared<AppItem>();
  18. m_appItem->loadFromSystemTitleId(titleId);
  19. }
  20. else
  21. {
  22. for (auto& appItem : AppList::getInstance().getList())
  23. {
  24. m_appItem = appItem->getAppItem();
  25. if (titleId == m_appItem->getTitleId())
  26. {
  27. found = true;
  28. break;
  29. }
  30. }
  31. if (!found)
  32. {
  33. throw 0;
  34. }
  35. }
  36. m_appItem->setInstalled(true);
  37. if (Theme::isInstalledItemBG9Themed)
  38. m_background.setTexture(&AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/installed_item_bg.9.png"));
  39. else
  40. m_background.setTexture(&AssetManager<cpp3ds::Texture>::get("images/installed_item_bg.9.png"));
  41. m_textTitle.setString(m_appItem->getTitle());
  42. m_textTitle.setCharacterSize(11);
  43. m_textTitle.setPosition(m_background.getPadding().left, m_background.getPadding().top);
  44. if (Theme::isTextThemed)
  45. m_textTitle.setFillColor(Theme::primaryTextColor);
  46. else
  47. m_textTitle.setFillColor(cpp3ds::Color::Black);
  48. m_textTitle.useSystemFont();
  49. if (titleType == TitleKeys::SystemApplication || titleType == TitleKeys::SystemApplet)
  50. m_textSize.setString(_(" - System Title"));
  51. else if (m_appItem->getFilesize() > 1024 * 1024 * 1024)
  52. m_textSize.setString(_(" - %.1f GB", static_cast<float>(m_appItem->getFilesize()) / 1024.f / 1024.f / 1024.f));
  53. else if (m_appItem->getFilesize() > 1024 * 1024)
  54. m_textSize.setString(_(" - %.1f MB", static_cast<float>(m_appItem->getFilesize()) / 1024.f / 1024.f));
  55. else
  56. m_textSize.setString(_(" - %d KB", m_appItem->getFilesize() / 1024));
  57. m_textSize.setCharacterSize(11);
  58. m_textSize.setPosition(m_textTitle.getLocalBounds().width + 4, m_background.getPadding().top);
  59. if (Theme::isTextThemed)
  60. m_textSize.setFillColor(Theme::secondaryTextColor);
  61. else
  62. m_textSize.setFillColor(cpp3ds::Color(150, 150, 150));
  63. m_textSize.useSystemFont();
  64. m_textWarningUpdate.setFont(AssetManager<cpp3ds::Font>::get("fonts/fontawesome.ttf"));
  65. m_textWarningUpdate.setString(L"\uf071");
  66. m_textWarningUpdate.setCharacterSize(14);
  67. m_textWarningUpdate.setFillColor(cpp3ds::Color(50, 50, 50));
  68. m_textWarningUpdate.setOutlineColor(cpp3ds::Color(0, 200, 0));
  69. m_textWarningUpdate.setOutlineThickness(1.f);
  70. m_textWarningUpdate.setOrigin(0, m_textWarningUpdate.getLocalBounds().top + m_textWarningUpdate.getLocalBounds().height / 2.f);
  71. m_textWarningDLC = m_textWarningUpdate;
  72. m_textWarningDLC.setOutlineColor(cpp3ds::Color(255, 255, 0, 255));
  73. setHeight(16.f);
  74. }
  75. void InstalledItem::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  76. {
  77. states.transform *= getTransform();
  78. target.draw(m_background, states);
  79. target.draw(m_textTitle, states);
  80. target.draw(m_textSize, states);
  81. if (m_updates.size() - m_updateInstallCount > 0)
  82. target.draw(m_textWarningUpdate, states);
  83. if (m_dlc.size() - m_dlcInstallCount > 0)
  84. target.draw(m_textWarningDLC, states);
  85. }
  86. cpp3ds::Uint64 InstalledItem::getTitleId() const
  87. {
  88. return m_titleId;
  89. }
  90. void InstalledItem::setUpdateStatus(cpp3ds::Uint64 titleId, bool installed)
  91. {
  92. m_updates[titleId] = installed;
  93. m_updateInstallCount = 0;
  94. for (auto& update : m_updates)
  95. if (update.second)
  96. m_updateInstallCount++;
  97. }
  98. void InstalledItem::setDLCStatus(cpp3ds::Uint64 titleId, bool installed)
  99. {
  100. m_dlc[titleId] = installed;
  101. m_dlcInstallCount = 0;
  102. for (auto& dlc : m_dlc)
  103. if (dlc.second)
  104. m_dlcInstallCount++;
  105. }
  106. std::shared_ptr<AppItem> InstalledItem::getAppItem() const
  107. {
  108. return m_appItem;
  109. }
  110. void InstalledItem::processEvent(const cpp3ds::Event &event)
  111. {
  112. }
  113. void InstalledItem::setValues(int tweenType, float *newValues)
  114. {
  115. switch (tweenType) {
  116. case HEIGHT:
  117. setHeight(newValues[0]);
  118. break;
  119. default:
  120. TweenTransformable::setValues(tweenType, newValues);
  121. }
  122. }
  123. int InstalledItem::getValues(int tweenType, float *returnValues)
  124. {
  125. switch (tweenType) {
  126. case HEIGHT:
  127. returnValues[0] = getHeight();
  128. return 1;
  129. default:
  130. return TweenTransformable::getValues(tweenType, returnValues);
  131. }
  132. }
  133. void InstalledItem::setHeight(float height)
  134. {
  135. m_height = height;
  136. m_background.setContentSize(320.f + m_background.getPadding().width - m_background.getTexture()->getSize().x + 2.f, height);
  137. float paddingRight = m_background.getSize().x - m_background.getContentSize().x - m_background.getPadding().left;
  138. float paddingBottom = m_background.getSize().y - m_background.getContentSize().y - m_background.getPadding().top;
  139. m_textWarningUpdate.setPosition(m_background.getSize().x - paddingRight - m_textWarningUpdate.getLocalBounds().width - 7.f,
  140. m_background.getPadding().top + height - height * 0.4f - 3.f);
  141. m_textWarningDLC.setPosition(m_textWarningUpdate.getPosition());
  142. m_textWarningDLC.move(-20.f, 0);
  143. }
  144. float InstalledItem::getHeight() const
  145. {
  146. return m_height;
  147. }
  148. void InstalledItem::updateGameTitle()
  149. {
  150. int numberOfDLC = 0;
  151. int totalDLC = TitleKeys::getRelated(m_appItem->getTitleId(), TitleKeys::DLC).size();
  152. for (auto &dlc : getDLC())
  153. if (dlc.second)
  154. numberOfDLC++;
  155. int numberOfUpdates = 0;
  156. int totalUpdates = TitleKeys::getRelated(m_appItem->getTitleId(), TitleKeys::Update).size();
  157. for (auto &update : getUpdates())
  158. if (update.second)
  159. numberOfUpdates++;
  160. int maxSize = 310;
  161. if ((totalDLC - numberOfDLC) > 0)
  162. maxSize = m_textWarningDLC.getPosition().x - 5;
  163. else if ((totalUpdates - numberOfUpdates) > 0)
  164. maxSize = m_textWarningUpdate.getPosition().x - 5;
  165. // Shorten the app name if it's out of the screen
  166. if (m_textTitle.getLocalBounds().width + m_textSize.getLocalBounds().width > maxSize) {
  167. cpp3ds::Text tmpText;
  168. tmpText.setCharacterSize(11);
  169. tmpText.useSystemFont();
  170. auto s = m_textTitle.getString().toUtf8();
  171. for (int i = 0; i <= s.size(); ++i) {
  172. tmpText.setString(cpp3ds::String::fromUtf8(s.begin(), s.begin() + i));
  173. if (tmpText.getLocalBounds().width + m_textSize.getLocalBounds().width > maxSize) {
  174. cpp3ds::String titleTxt = tmpText.getString();
  175. titleTxt.erase(titleTxt.getSize() - 3, 3);
  176. titleTxt.insert(titleTxt.getSize(), "...");
  177. m_textTitle.setString(titleTxt);
  178. m_textSize.setPosition(m_textTitle.getLocalBounds().width + 4, m_background.getPadding().top);
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. } // namespace FreeShop