LapList.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "LapList.hpp"
  2. #include "Clock.hpp"
  3. #include "AssetManager.hpp"
  4. #include "Notification.hpp"
  5. #include "Util.hpp"
  6. #include <cpp3ds/System/I18n.hpp>
  7. #include <cpp3ds/System/FileSystem.hpp>
  8. #include <TweenEngine/Tween.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11. namespace Clock {
  12. LapList::LapList()
  13. : m_scrollPos(0.f)
  14. {
  15. m_textEmptyList.setString("The lap list is empty.\nPress the X button to add one.");
  16. m_textEmptyList.setCharacterSize(10);
  17. m_textEmptyList.useSystemFont();
  18. m_textEmptyList.setPosition(240.f, 120.f);
  19. m_textEmptyList.setFillColor(cpp3ds::Color(66, 66, 66));
  20. m_textEmptyList.setOrigin(m_textEmptyList.getLocalBounds().width / 2, m_textEmptyList.getLocalBounds().height / 2);
  21. }
  22. LapList::~LapList()
  23. {
  24. }
  25. void LapList::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  26. {
  27. states.transform *= getTransform();
  28. states.scissor = cpp3ds::UintRect(0, 35, 320, 205);
  29. target.draw(m_textEmptyList, states);
  30. for (auto& item : m_lapItems) {
  31. float posY = item->getPosition().y;
  32. if (posY > -10.f && posY < 240.f)
  33. target.draw(*item, states);
  34. }
  35. }
  36. void LapList::update(float delta)
  37. {
  38. m_tweenManager.update(delta);
  39. }
  40. void LapList::processEvent(const cpp3ds::Event& event)
  41. {
  42. }
  43. void LapList::setScroll(float position)
  44. {
  45. m_scrollPos = std::round(position);
  46. repositionItems(true);
  47. }
  48. float LapList::getScroll()
  49. {
  50. return m_scrollPos;
  51. }
  52. void LapList::repositionItems(bool noAnim)
  53. {
  54. float posY = 36.f + m_scrollPos;
  55. for (auto& item : m_lapItems)
  56. {
  57. item->setPosition(160.f, item->getPosition().y);
  58. TweenEngine::Tween::to(*item, util3ds::TweenSprite::POSITION_Y, noAnim ? 0.f : 0.2f)
  59. .target(posY)
  60. .start(m_tweenManager);
  61. posY += item->getHeight();
  62. }
  63. m_size.y = posY - 6.f - m_scrollPos;
  64. updateScrollSize();
  65. }
  66. const cpp3ds::Vector2f &LapList::getScrollSize()
  67. {
  68. return m_size;
  69. }
  70. void LapList::addLap(cpp3ds::Time actualTime)
  71. {
  72. if (m_lapItems.empty())
  73. setEmptyListTextState(false);
  74. cpp3ds::Time oldTime;
  75. if (count() > 0)
  76. oldTime = m_lapItems.front().get()->getTime();
  77. std::shared_ptr<LapItem> item(new LapItem(m_lapItems.size() + 1, actualTime, oldTime));
  78. m_lapItems.insert(m_lapItems.begin(), std::move(item));
  79. repositionItems(false);
  80. }
  81. void LapList::clearList()
  82. {
  83. m_lapItems.clear();
  84. repositionItems(true);
  85. setEmptyListTextState(true);
  86. }
  87. size_t LapList::count() const
  88. {
  89. return m_lapItems.size();
  90. }
  91. void LapList::setEmptyListTextState(bool state)
  92. {
  93. if (!state) {
  94. TweenEngine::Tween::to(m_textEmptyList, m_textEmptyList.SCALE_XY, 0.2f)
  95. .target(.8f, .8f)
  96. .start(m_tweenManager);
  97. TweenEngine::Tween::to(m_textEmptyList, m_textEmptyList.FILL_COLOR_ALPHA, 0.2f)
  98. .target(0)
  99. .start(m_tweenManager);
  100. } else {
  101. TweenEngine::Tween::to(m_textEmptyList, m_textEmptyList.SCALE_XY, 0.2f)
  102. .target(1.f, 1.f)
  103. .start(m_tweenManager);
  104. TweenEngine::Tween::to(m_textEmptyList, m_textEmptyList.FILL_COLOR_ALPHA, 0.2f)
  105. .target(255)
  106. .start(m_tweenManager);
  107. }
  108. }
  109. } // namespace Clock