IconSet.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "Theme.hpp"
  7. #include <sstream>
  8. #include <TweenEngine/Tween.h>
  9. namespace FreeShop
  10. {
  11. IconSet::IconSet()
  12. : m_selectedIndex(-1)
  13. , m_padding(6.f)
  14. {
  15. }
  16. IconSet::~IconSet()
  17. {
  18. }
  19. void IconSet::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  20. {
  21. states.transform *= getTransform();
  22. for (auto& icon : m_icons)
  23. {
  24. target.draw(icon, states);
  25. }
  26. }
  27. bool IconSet::processEvent(const cpp3ds::Event &event)
  28. {
  29. if (event.type == cpp3ds::Event::TouchBegan)
  30. {
  31. cpp3ds::FloatRect bounds;
  32. cpp3ds::Vector2f point(event.touch.x, event.touch.y);
  33. point -= getPosition();
  34. int i = 0;
  35. for (auto& icon : m_icons)
  36. {
  37. bounds = icon.getGlobalBounds();
  38. bounds.top -= m_padding / 2.f;
  39. bounds.left -= m_padding / 2.f;
  40. bounds.width += m_padding;
  41. bounds.height += m_padding;
  42. if (bounds.contains(point))
  43. {
  44. setSelectedIndex(i);
  45. }
  46. i++;
  47. }
  48. }
  49. return true;
  50. }
  51. void IconSet::update(float delta)
  52. {
  53. m_tweenManager.update(delta);
  54. }
  55. void IconSet::addIcon(const cpp3ds::String &string)
  56. {
  57. util3ds::TweenText text;
  58. text.setString(string);
  59. text.setCharacterSize(20);
  60. if (Theme::isTextThemed)
  61. text.setFillColor(Theme::iconSetColor);
  62. else
  63. text.setFillColor(cpp3ds::Color(100, 100, 100));
  64. text.setOutlineColor(cpp3ds::Color::White);
  65. text.setOutlineThickness(2.f);
  66. text.setScale(0.8f, 0.8f);
  67. text.setFont(AssetManager<cpp3ds::Font>::get("fonts/fontawesome.ttf"));
  68. cpp3ds::FloatRect textRect = text.getLocalBounds();
  69. text.setOrigin(textRect.left + textRect.width / 2.f, textRect.top + textRect.height / 2.f);
  70. m_icons.push_back(text);
  71. resize();
  72. }
  73. void IconSet::setSelectedIndex(int index)
  74. {
  75. if (index == m_selectedIndex)
  76. return;
  77. if (index < 0 || index >= m_icons.size())
  78. index = -1;
  79. for (auto& icon : m_icons)
  80. {
  81. TweenEngine::Tween::to(icon, SCALE_XY, 0.3f)
  82. .target(0.8f, 0.8f)
  83. .start(m_tweenManager);
  84. if (Theme::isTextThemed) {
  85. TweenEngine::Tween::to(icon, util3ds::TweenText::FILL_COLOR_RGB, 0.2f)
  86. .target(Theme::iconSetColor.r, Theme::iconSetColor.g, Theme::iconSetColor.b)
  87. .start(m_tweenManager);
  88. TweenEngine::Tween::to(icon, util3ds::TweenText::OUTLINE_COLOR_RGB, 0.2f)
  89. .target(Theme::iconSetColor.r, Theme::iconSetColor.g, Theme::iconSetColor.b)
  90. .start(m_tweenManager);
  91. TweenEngine::Tween::to(icon, util3ds::TweenText::OUTLINE_COLOR_ALPHA, 0.2f)
  92. .target(0.f)
  93. .start(m_tweenManager);
  94. } else {
  95. TweenEngine::Tween::to(icon, util3ds::TweenText::FILL_COLOR_RGB, 0.2f)
  96. .target(100.f, 100.f, 100.f)
  97. .start(m_tweenManager);
  98. TweenEngine::Tween::to(icon, util3ds::TweenText::OUTLINE_COLOR_RGB, 0.2f)
  99. .target(255.f, 255.f, 255.f)
  100. .start(m_tweenManager);
  101. }
  102. }
  103. TweenEngine::Tween::to(m_icons[index], SCALE_XY, 0.3f)
  104. .target(1.f, 1.f)
  105. .start(m_tweenManager);
  106. if (Theme::isTextThemed) {
  107. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::FILL_COLOR_RGB, 0.2f)
  108. .target(Theme::iconSetColorActive.r, Theme::iconSetColorActive.g, Theme::iconSetColorActive.b)
  109. .start(m_tweenManager);
  110. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::OUTLINE_COLOR_RGB, 0.2f)
  111. .target(Theme::iconSetColorActive.r, Theme::iconSetColorActive.g, Theme::iconSetColorActive.b)
  112. .start(m_tweenManager);
  113. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::OUTLINE_COLOR_ALPHA, 0.2f)
  114. .target(128.f)
  115. .start(m_tweenManager);
  116. } else {
  117. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::FILL_COLOR_RGB, 0.2f)
  118. .target(0.f, 0.f, 0.f)
  119. .start(m_tweenManager);
  120. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::OUTLINE_COLOR_RGB, 0.2f)
  121. .target(200.f, 200.f, 200.f)
  122. .start(m_tweenManager);
  123. }
  124. m_selectedIndex = index;
  125. }
  126. int IconSet::getSelectedIndex() const
  127. {
  128. return m_selectedIndex;
  129. }
  130. void IconSet::resize()
  131. {
  132. float posX = 0;
  133. for (auto& icon : m_icons)
  134. {
  135. icon.setPosition(posX, 0);
  136. posX += icon.getLocalBounds().width + m_padding;
  137. }
  138. }
  139. void IconSet::resetIcon(int index, bool isAnimated)
  140. {
  141. TweenEngine::Tween::to(m_icons[index], SCALE_XY, isAnimated ? 0.3f : 0.f)
  142. .target(0.8f, 0.8f)
  143. .start(m_tweenManager);
  144. if (Theme::isTextThemed) {
  145. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::FILL_COLOR_RGB, isAnimated ? 0.2f : 0.f)
  146. .target(Theme::iconSetColor.r, Theme::iconSetColor.g, Theme::iconSetColor.b)
  147. .start(m_tweenManager);
  148. } else {
  149. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::FILL_COLOR_RGB, isAnimated ? 0.2f : 0.f)
  150. .target(100.f, 100.f, 100.f)
  151. .start(m_tweenManager);
  152. }
  153. TweenEngine::Tween::to(m_icons[index], util3ds::TweenText::OUTLINE_COLOR_RGB, isAnimated ? 0.2f : 0.f)
  154. .target(255.f, 255.f, 255.f)
  155. .start(m_tweenManager);
  156. }
  157. } // namespace FreeShop