Notification.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <TweenEngine/Tween.h>
  2. #include <cmath>
  3. #include "Notification.hpp"
  4. namespace FreeShop {
  5. // Static members
  6. cpp3ds::Texture Notification::m_texture;
  7. float Notification::m_spawnPositionY = 240.f;
  8. std::vector<std::unique_ptr<Notification>> Notification::notifications;
  9. Notification::Notification()
  10. : m_markForDelete(false)
  11. {
  12. if (m_texture.getSize().x == 0)
  13. m_texture.loadFromFile("images/notification.9.png");
  14. setTexture(&m_texture);
  15. getText().setCharacterSize(12);
  16. }
  17. Notification::~Notification()
  18. {
  19. m_tweenManager.killAll();
  20. }
  21. void Notification::update(float delta)
  22. {
  23. for (auto i = notifications.begin(); i != notifications.end();) {
  24. if ((*i)->m_markForDelete) {
  25. float offsetY = (*i)->getSize().y + NOTIFICATION_SPACING;
  26. m_spawnPositionY += offsetY;
  27. for (auto j = i+1; j != notifications.end(); j++) {
  28. auto notification = j->get();
  29. notification->m_destinationY += offsetY;
  30. TweenEngine::Tween::to(*notification, POSITION_Y, 0.3f)
  31. .target(notification->m_destinationY)
  32. .start(notification->m_tweenManager);
  33. }
  34. notifications.erase(i);
  35. } else {
  36. ++i;
  37. }
  38. }
  39. for (auto& notification : notifications)
  40. notification->m_tweenManager.update(delta);
  41. }
  42. void Notification::spawn(cpp3ds::String message)
  43. {
  44. std::unique_ptr<Notification> notification(new Notification());
  45. notification->setString(message);
  46. notification->setPosition(std::round(200.f - notification->getSize().x / 2.f),
  47. std::round(m_spawnPositionY - notification->getSize().y - 4.f));
  48. m_spawnPositionY -= NOTIFICATION_SPACING + notification->getSize().y;
  49. notification->animate();
  50. notifications.emplace_back(std::move(notification));
  51. }
  52. void Notification::animate()
  53. {
  54. setColor(cpp3ds::Color(255, 255, 255, 0));
  55. setTextColor(cpp3ds::Color(60, 60, 60, 0));
  56. m_destinationY = getPosition().y;
  57. move(0, -10.f);
  58. TweenEngine::Tween::to(*this, POSITION_Y, 0.5f).target(m_destinationY).start(m_tweenManager);
  59. TweenEngine::Tween::to(*this, COLOR_ALPHA, 0.5f).target(225.f).start(m_tweenManager);
  60. TweenEngine::Tween::to(*this, TEXTCOLOR_ALPHA, 0.5f).target(255.f).start(m_tweenManager);
  61. // Fade out and mark notification for deletion in update()
  62. TweenEngine::Tween::to(*this, COLOR_ALPHA, 0.5f).target(0.f).delay(NOTIFICATION_DURATION).start(m_tweenManager);
  63. TweenEngine::Tween::to(*this, TEXTCOLOR_ALPHA, 0.5f)
  64. .target(0.f)
  65. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {
  66. m_markForDelete = true;
  67. })
  68. .delay(NOTIFICATION_DURATION).start(m_tweenManager);
  69. }
  70. } // namespace FreeShop