DialogState.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include <cpp3ds/System/I18n.hpp>
  2. #include <TweenEngine/Tween.h>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include "DialogState.hpp"
  6. #include "../AssetManager.hpp"
  7. #include "../Theme.hpp"
  8. #include "SleepState.hpp"
  9. #include "BrowseState.hpp"
  10. namespace FreeShop {
  11. DialogState::DialogState(StateStack &stack, Context &context, StateCallback callback)
  12. : State(stack, context, callback)
  13. , m_isClosing(false)
  14. , m_finishedFadeIn(false)
  15. , m_isOkButtonTouched(false)
  16. , m_isCancelButtonTouched(false)
  17. {
  18. m_overlay.setSize(cpp3ds::Vector2f(400.f, 240.f));
  19. m_overlay.setFillColor(cpp3ds::Color(0, 0, 0, 0));
  20. if (!Theme::isButtonRadius9Themed) {
  21. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get("images/button-radius.9.png");
  22. m_background.setTexture(&texture);
  23. } else {
  24. cpp3ds::Texture &texture = AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/button-radius.9.png");
  25. m_background.setTexture(&texture);
  26. }
  27. m_background.setSize(cpp3ds::Vector2f(280.f, 200.f));
  28. m_background.setPosition(20.f, 20.f);
  29. if (Theme::isTextThemed)
  30. m_background.setColor(cpp3ds::Color(Theme::dialogBackground.r, Theme::dialogBackground.g, Theme::dialogBackground.b, 128));
  31. else
  32. m_background.setColor(cpp3ds::Color(255, 255, 255, 128));
  33. m_message.setCharacterSize(12);
  34. m_message.setFillColor(cpp3ds::Color(66, 66, 66, 0));
  35. m_message.useSystemFont();
  36. m_message.setPosition(25.f, 60.f);
  37. m_title.setPosition(160.f, 40.f);
  38. m_title.setFillColor(cpp3ds::Color::Transparent);
  39. m_title.setCharacterSize(16);
  40. m_title.useSystemFont();
  41. m_buttonOkBackground.setSize(cpp3ds::Vector2f(110.f, 25.f));
  42. if (Theme::isTextThemed)
  43. m_buttonOkBackground.setOutlineColor(cpp3ds::Color(Theme::dialogButton.r, Theme::dialogButton.g, Theme::dialogButton.b, 0));
  44. else
  45. m_buttonOkBackground.setOutlineColor(cpp3ds::Color(158, 158, 158, 0));
  46. m_buttonOkBackground.setOutlineThickness(1.f);
  47. m_buttonOkBackground.setPosition(165.f, 180.f);
  48. m_buttonOkBackground.setFillColor(cpp3ds::Color(m_buttonOkBackground.getOutlineColor().r - 20, m_buttonOkBackground.getOutlineColor().g - 20, m_buttonOkBackground.getOutlineColor().b - 20, 0));
  49. m_buttonCancelBackground.setSize(cpp3ds::Vector2f(110.f, 25.f));
  50. if (Theme::isTextThemed)
  51. m_buttonCancelBackground.setOutlineColor(cpp3ds::Color(Theme::dialogButton.r, Theme::dialogButton.g, Theme::dialogButton.b, 0));
  52. else
  53. m_buttonCancelBackground.setOutlineColor(cpp3ds::Color(158, 158, 158, 0));
  54. m_buttonCancelBackground.setOutlineThickness(1.f);
  55. m_buttonCancelBackground.setPosition(40.f, 180.f);
  56. m_buttonCancelBackground.setFillColor(cpp3ds::Color(m_buttonCancelBackground.getOutlineColor().r - 20, m_buttonCancelBackground.getOutlineColor().g - 20, m_buttonCancelBackground.getOutlineColor().b - 20, 0));
  57. m_buttonOkText.setString(_("\uE000 Ok"));
  58. m_buttonOkText.setCharacterSize(14);
  59. m_buttonOkText.setFillColor(cpp3ds::Color(3, 169, 244, 0));
  60. if (Theme::isTextThemed)
  61. m_buttonOkText.setFillColor(cpp3ds::Color(Theme::dialogButtonText.r, Theme::dialogButtonText.g, Theme::dialogButtonText.b, 0));
  62. else
  63. m_buttonOkText.setFillColor(cpp3ds::Color(3, 169, 244, 0));
  64. m_buttonOkText.useSystemFont();
  65. m_buttonOkText.setPosition(m_buttonOkBackground.getPosition().x + m_buttonOkBackground.getGlobalBounds().width / 2, m_buttonOkBackground.getPosition().y + m_buttonOkBackground.getGlobalBounds().height / 2);
  66. m_buttonOkText.setOrigin(m_buttonOkText.getGlobalBounds().width / 2, m_buttonOkText.getGlobalBounds().height / 1.6f);
  67. m_buttonCancelText = m_buttonOkText;
  68. m_buttonCancelText.setString(_("\uE001 Cancel"));
  69. m_buttonCancelText.setCharacterSize(12);
  70. m_buttonCancelText.setPosition(m_buttonCancelBackground.getPosition().x + m_buttonCancelBackground.getGlobalBounds().width / 2, m_buttonCancelBackground.getPosition().y + m_buttonCancelBackground.getGlobalBounds().height / 2);
  71. m_buttonCancelText.setOrigin(m_buttonCancelText.getGlobalBounds().width / 2, m_buttonCancelText.getGlobalBounds().height / 1.5f);
  72. m_bottomView.setCenter(cpp3ds::Vector2f(160.f, 120.f));
  73. m_bottomView.setSize(cpp3ds::Vector2f(320.f * 0.9f, 240.f * 0.9f));
  74. cpp3ds::String tmp;
  75. Event event = {GetText, &tmp};
  76. if (runCallback(&event)) {
  77. auto message = reinterpret_cast<cpp3ds::String*>(event.data);
  78. m_message.setString(*message);
  79. } else
  80. m_message.setString(_("Are you sure you want to continue?"));
  81. // Calculate word-wrapping
  82. int startPos = 0;
  83. int lastSpace = 0;
  84. auto s = m_message.getString().toUtf8();
  85. cpp3ds::Text tmpText = m_message;
  86. for (int i = 0; i < s.size(); ++i)
  87. {
  88. if (s[i] == ' ')
  89. lastSpace = i;
  90. tmpText.setString(cpp3ds::String::fromUtf8(s.begin() + startPos, s.begin() + i));
  91. if (tmpText.getLocalBounds().width > 260)
  92. {
  93. if (lastSpace != 0)
  94. {
  95. s[lastSpace] = '\n';
  96. i = startPos = lastSpace + 1;
  97. lastSpace = 0;
  98. }
  99. else
  100. {
  101. s.insert(s.begin() + i, '\n');
  102. startPos = ++i;
  103. }
  104. }
  105. }
  106. m_message.setString(cpp3ds::String::fromUtf8(s.begin(), s.end()));
  107. event = {GetTitle, &tmp};
  108. if (runCallback(&event)) {
  109. auto message = reinterpret_cast<cpp3ds::String*>(event.data);
  110. m_title.setString(*message);
  111. } else
  112. m_title.setString(_("Dialog"));
  113. // Shorten the dialog title if it's out of the screen
  114. int maxSize = 270;
  115. if (m_title.getLocalBounds().width > maxSize) {
  116. cpp3ds::Text tmpText = m_title;
  117. tmpText.setString("");
  118. auto s = m_title.getString().toUtf8();
  119. for (int i = 0; i <= s.size(); ++i) {
  120. tmpText.setString(cpp3ds::String::fromUtf8(s.begin(), s.begin() + i));
  121. if (tmpText.getLocalBounds().width > maxSize) {
  122. cpp3ds::String titleTxt = tmpText.getString();
  123. titleTxt.erase(titleTxt.getSize() - 3, 3);
  124. titleTxt.insert(titleTxt.getSize(), "...");
  125. m_title.setString(titleTxt);
  126. break;
  127. }
  128. }
  129. }
  130. m_title.setOrigin(std::round(m_title.getLocalBounds().width / 2), std::round(m_title.getLocalBounds().height / 2));
  131. m_scrollbar.setSize(cpp3ds::Vector2u(2, 158));
  132. m_scrollbar.setScrollAreaSize(cpp3ds::Vector2u(320, 109));
  133. m_scrollbar.setDragRect(cpp3ds::IntRect(0, 0, 320, 240));
  134. m_scrollbar.setColor(cpp3ds::Color(0, 0, 0, 40));
  135. m_scrollbar.setPosition(292.f, 60.f);
  136. m_scrollbar.setAutoHide(false);
  137. m_scrollbar.attachObject(this);
  138. m_scrollSize = cpp3ds::Vector2f(320.f, m_message.getLocalBounds().top + m_message.getLocalBounds().height);
  139. #define TWEEN_IN(obj) \
  140. TweenEngine::Tween::to(obj, obj.FILL_COLOR_ALPHA, 0.2f).target(255.f).start(m_tweenManager); \
  141. TweenEngine::Tween::to(obj, obj.OUTLINE_COLOR_ALPHA, 0.2f).target(128.f).start(m_tweenManager);
  142. TweenEngine::Tween::to(m_overlay, m_overlay.FILL_COLOR_ALPHA, 0.2f).target(150.f).start(m_tweenManager);
  143. TweenEngine::Tween::to(m_background, m_background.COLOR_ALPHA, 0.2f).target(255.f).setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {
  144. m_finishedFadeIn = true;
  145. }).start(m_tweenManager);
  146. TweenEngine::Tween::to(m_buttonOkBackground, m_buttonOkBackground.OUTLINE_COLOR_ALPHA, 0.2f).target(128.f).start(m_tweenManager);
  147. TweenEngine::Tween::to(m_buttonCancelBackground, m_buttonCancelBackground.OUTLINE_COLOR_ALPHA, 0.2f).target(128.f).start(m_tweenManager);
  148. TWEEN_IN(m_message);
  149. TWEEN_IN(m_buttonOkText);
  150. TWEEN_IN(m_buttonCancelText);
  151. TWEEN_IN(m_title);
  152. TweenEngine::Tween::to(m_bottomView, m_bottomView.SIZE_XY, 0.2f).target(320.f, 240.f).start(m_tweenManager);
  153. }
  154. void DialogState::renderTopScreen(cpp3ds::Window &window)
  155. {
  156. window.draw(m_overlay);
  157. }
  158. void DialogState::renderBottomScreen(cpp3ds::Window &window)
  159. {
  160. cpp3ds::UintRect scissor(0, 40 + m_background.getPosition().y, 320, (240.f / m_bottomView.getSize().y) * 119.f);
  161. window.draw(m_overlay);
  162. window.setView(m_bottomView);
  163. window.draw(m_background);
  164. window.draw(m_message, scissor);
  165. window.draw(m_buttonOkBackground);
  166. window.draw(m_buttonOkText);
  167. window.draw(m_buttonCancelBackground);
  168. window.draw(m_buttonCancelText);
  169. window.draw(m_title);
  170. if (m_finishedFadeIn && !m_isClosing)
  171. window.draw(m_scrollbar);
  172. window.setView(window.getDefaultView());
  173. }
  174. bool DialogState::update(float delta)
  175. {
  176. m_tweenManager.update(delta);
  177. m_scrollbar.update(delta);
  178. return false;
  179. }
  180. bool DialogState::processEvent(const cpp3ds::Event &event)
  181. {
  182. BrowseState::clockDownloadInactivity.restart();
  183. SleepState::clock.restart();
  184. if (m_isClosing)
  185. return false;
  186. m_scrollbar.processEvent(event);
  187. bool triggerResponse = false;
  188. bool accepted = false;
  189. if (event.type == cpp3ds::Event::TouchEnded)
  190. {
  191. if (m_buttonOkBackground.getGlobalBounds().contains(event.touch.x, event.touch.y) && m_isOkButtonTouched)
  192. triggerResponse = accepted = true;
  193. else if (m_buttonCancelBackground.getGlobalBounds().contains(event.touch.x, event.touch.y) && m_isCancelButtonTouched)
  194. triggerResponse = true;
  195. }
  196. else if (event.type == cpp3ds::Event::TouchMoved)
  197. {
  198. if (!m_buttonOkBackground.getGlobalBounds().contains(event.touch.x, event.touch.y) && m_isOkButtonTouched) {
  199. TweenEngine::Tween::to(m_buttonOkBackground, m_buttonOkBackground.FILL_COLOR_ALPHA, 0.2f).target(0.f).start(m_tweenManager);
  200. m_isOkButtonTouched = false;
  201. }
  202. if (!m_buttonCancelBackground.getGlobalBounds().contains(event.touch.x, event.touch.y) && m_isCancelButtonTouched) {
  203. TweenEngine::Tween::to(m_buttonCancelBackground, m_buttonCancelBackground.FILL_COLOR_ALPHA, 0.2f).target(0.f).start(m_tweenManager);
  204. m_isCancelButtonTouched = false;
  205. }
  206. }
  207. else if (event.type == cpp3ds::Event::TouchBegan)
  208. {
  209. if (m_buttonOkBackground.getGlobalBounds().contains(event.touch.x, event.touch.y)) {
  210. TweenEngine::Tween::to(m_buttonOkBackground, m_buttonOkBackground.FILL_COLOR_ALPHA, 0.2f).target(255.f).start(m_tweenManager);
  211. m_isOkButtonTouched = true;
  212. } else if (m_buttonCancelBackground.getGlobalBounds().contains(event.touch.x, event.touch.y)) {
  213. TweenEngine::Tween::to(m_buttonCancelBackground, m_buttonCancelBackground.FILL_COLOR_ALPHA, 0.2f).target(255.f).start(m_tweenManager);
  214. m_isCancelButtonTouched = true;
  215. }
  216. }
  217. else if (event.type == cpp3ds::Event::KeyReleased)
  218. {
  219. if (event.key.code == cpp3ds::Keyboard::A)
  220. triggerResponse = accepted = true;
  221. else if (event.key.code == cpp3ds::Keyboard::B)
  222. triggerResponse = true;
  223. }
  224. #define TWEEN_OUT(obj) \
  225. TweenEngine::Tween::to(obj, obj.FILL_COLOR_ALPHA, 0.2f).target(0.f).start(m_tweenManager); \
  226. TweenEngine::Tween::to(obj, obj.OUTLINE_COLOR_ALPHA, 0.2f).target(0.f).start(m_tweenManager);
  227. if (triggerResponse)
  228. {
  229. Event evt = {Response, &accepted};
  230. if (runCallback(&evt))
  231. {
  232. m_isClosing = true;
  233. m_tweenManager.killAll();
  234. // Highlight selected button
  235. if (accepted)
  236. TweenEngine::Tween::to(m_buttonOkBackground, m_buttonOkBackground.FILL_COLOR_RGB, 0.1f).target(m_buttonOkBackground.getOutlineColor().r, m_buttonOkBackground.getOutlineColor().g, m_buttonOkBackground.getOutlineColor().b).start(m_tweenManager);
  237. else
  238. TweenEngine::Tween::to(m_buttonCancelBackground, m_buttonCancelBackground.FILL_COLOR_RGB, 0.1f).target(m_buttonCancelBackground.getOutlineColor().r, m_buttonCancelBackground.getOutlineColor().g, m_buttonCancelBackground.getOutlineColor().b).start(m_tweenManager);
  239. TweenEngine::Tween::to(m_background, m_background.COLOR_ALPHA, 0.2f).target(0.f).start(m_tweenManager);
  240. TWEEN_OUT(m_message);
  241. TWEEN_OUT(m_buttonOkBackground);
  242. TWEEN_OUT(m_buttonCancelBackground);
  243. TWEEN_OUT(m_buttonOkText);
  244. TWEEN_OUT(m_buttonCancelText);
  245. TWEEN_OUT(m_title);
  246. TweenEngine::Tween::to(m_bottomView, m_bottomView.SIZE_XY, 0.2f).target(320.f * 1.2f, 240.f * 1.2f).start(m_tweenManager);
  247. TweenEngine::Tween::to(m_overlay, m_overlay.FILL_COLOR_ALPHA, 0.3f).target(0.f)
  248. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween* source) {
  249. requestStackPop();
  250. })
  251. .delay(0.2f).start(m_tweenManager);
  252. }
  253. }
  254. return false;
  255. }
  256. void DialogState::setScroll(float position)
  257. {
  258. m_scrollPos = position;
  259. m_message.setPosition(25.f, std::round(60.f + position));
  260. }
  261. float DialogState::getScroll()
  262. {
  263. return m_scrollPos;
  264. }
  265. const cpp3ds::Vector2f &DialogState::getScrollSize()
  266. {
  267. return m_scrollSize;
  268. }
  269. } // namespace FreeShop