IconSet.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <cpp3ds/System/Err.hpp>
  2. #include <iostream>
  3. #include <cpp3ds/System/I18n.hpp>
  4. #include "AssetManager.hpp"
  5. #include "IconSet.hpp"
  6. #include <sstream>
  7. #include <TweenEngine/Tween.h>
  8. namespace FreeShop
  9. {
  10. IconSet::IconSet()
  11. : m_selectedIndex(-1)
  12. , m_padding(6.f)
  13. {
  14. }
  15. IconSet::~IconSet()
  16. {
  17. }
  18. void IconSet::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  19. {
  20. states.transform *= getTransform();
  21. for (auto& icon : m_icons)
  22. {
  23. target.draw(icon, states);
  24. }
  25. }
  26. bool IconSet::processEvent(const cpp3ds::Event &event)
  27. {
  28. if (event.type == cpp3ds::Event::TouchBegan)
  29. {
  30. cpp3ds::FloatRect bounds;
  31. cpp3ds::Vector2f point(event.touch.x, event.touch.y);
  32. point -= getPosition();
  33. int i = 0;
  34. for (auto& icon : m_icons)
  35. {
  36. bounds = icon.getGlobalBounds();
  37. bounds.top -= m_padding / 2.f;
  38. bounds.left -= m_padding / 2.f;
  39. bounds.width += m_padding;
  40. bounds.height += m_padding;
  41. if (bounds.contains(point))
  42. {
  43. setSelectedIndex(i);
  44. }
  45. i++;
  46. }
  47. }
  48. return true;
  49. }
  50. void IconSet::update(float delta)
  51. {
  52. m_tweenManager.update(delta);
  53. }
  54. void IconSet::addIcon(const cpp3ds::String &string)
  55. {
  56. util3ds::TweenText text;
  57. text.setString(string);
  58. text.setCharacterSize(20);
  59. text.setFillColor(cpp3ds::Color(100, 100, 100));
  60. text.setOutlineColor(cpp3ds::Color::White);
  61. text.setOutlineThickness(2.f);
  62. text.setScale(0.8f, 0.8f);
  63. text.setFont(AssetManager<cpp3ds::Font>::get("fonts/fontawesome.ttf"));
  64. cpp3ds::FloatRect textRect = text.getLocalBounds();
  65. text.setOrigin(textRect.left + textRect.width / 2.f, textRect.top + textRect.height / 2.f);
  66. m_icons.push_back(text);
  67. resize();
  68. }
  69. void IconSet::setSelectedIndex(int index)
  70. {
  71. if (index == m_selectedIndex)
  72. return;
  73. if (index < 0 || index >= m_icons.size())
  74. index = -1;
  75. for (auto& icon : m_icons)
  76. {
  77. TweenEngine::Tween::to(icon, SCALE_XY, 0.3f)
  78. .target(0.8f, 0.8f)
  79. .start(m_tweenManager);
  80. TweenEngine::Tween::to(icon, util3ds::TweenText::FILL_COLOR_RGB, 0.2f)
  81. .target(100.f, 100.f, 100.f)
  82. .start(m_tweenManager);
  83. TweenEngine::Tween::to(icon, util3ds::TweenText::OUTLINE_COLOR_RGB, 0.2f)
  84. .target(255.f, 255.f, 255.f)
  85. .start(m_tweenManager);
  86. }
  87. TweenEngine::Tween::to(m_icons[index], SCALE_XY, 0.3f)
  88. .target(1.f, 1.f)
  89. .start(m_tweenManager);
  90. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::FILL_COLOR_RGB, 0.2f)
  91. .target(0.f, 0.f, 0.f)
  92. .start(m_tweenManager);
  93. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::OUTLINE_COLOR_RGB, 0.3f)
  94. .target(200.f, 200.f, 200.f)
  95. .start(m_tweenManager);
  96. m_selectedIndex = index;
  97. }
  98. int IconSet::getSelectedIndex() const
  99. {
  100. return m_selectedIndex;
  101. }
  102. void IconSet::resize()
  103. {
  104. float posX = 0;
  105. for (auto& icon : m_icons)
  106. {
  107. icon.setPosition(posX, 0);
  108. posX += icon.getLocalBounds().width + m_padding;
  109. }
  110. }
  111. } // namespace FreeShop