💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "splash-state.hpp"
  20. #include "game.hpp"
  21. #include "sandbox-state.hpp"
  22. #include "ui/ui.hpp"
  23. SplashState::SplashState(Game* game):
  24. GameState(game)
  25. {}
  26. SplashState::~SplashState()
  27. {}
  28. void SplashState::enter()
  29. {
  30. AnimationChannel<float>* channel;
  31. // Construct fade-in animation clip
  32. fadeInClip.setInterpolator(lerp<float>);
  33. channel = fadeInClip.addChannel(0);
  34. channel->insertKeyframe(0.0f, 0.0f);
  35. channel->insertKeyframe(0.5f, 0.0f);
  36. channel->insertKeyframe(1.25f, 1.0f);
  37. channel->insertKeyframe(5.25f, 1.0f);
  38. // Construct fade-out animation clip
  39. fadeOutClip.setInterpolator(lerp<float>);
  40. channel = fadeOutClip.addChannel(0);
  41. channel->insertKeyframe(0.0f, 1.0f);
  42. channel->insertKeyframe(0.75f, 0.0f);
  43. channel->insertKeyframe(1.25f, 0.0f);
  44. // Setup animate callback to change splash screen opacity
  45. fadeAnimation.setAnimateCallback
  46. (
  47. [this](std::size_t id, float opacity)
  48. {
  49. this->game->splashImage->setTintColor(Vector4(1.0f, 1.0f, 1.0f, opacity));
  50. }
  51. );
  52. // Setup end callback to fade-out after fading in
  53. fadeAnimation.setEndCallback
  54. (
  55. [this]()
  56. {
  57. // Change clip to fade-out
  58. this->fadeAnimation.setClip(&fadeOutClip);
  59. this->fadeAnimation.setTimeFrame(this->fadeOutClip.getTimeFrame());
  60. this->fadeAnimation.rewind();
  61. this->fadeAnimation.play();
  62. // Setup end callback to change to title state after fading out
  63. this->fadeAnimation.setEndCallback
  64. (
  65. [this]()
  66. {
  67. //this->game->changeState(this->game->titleState);
  68. this->game->changeState(this->game->sandboxState);
  69. }
  70. );
  71. }
  72. );
  73. // Setup fade animation to fade-in
  74. fadeAnimation.setSpeed(1.0f);
  75. fadeAnimation.setLoop(false);
  76. fadeAnimation.setClip(&fadeInClip);
  77. fadeAnimation.setTimeFrame(fadeInClip.getTimeFrame());
  78. // Play the fade animation
  79. fadeAnimation.play();
  80. // Add fade animation to the animator
  81. game->getAnimator()->addAnimation(&fadeAnimation);
  82. // Subscribe this state to input events
  83. game->getEventDispatcher()->subscribe<KeyPressedEvent>(this);
  84. game->getEventDispatcher()->subscribe<MouseButtonPressedEvent>(this);
  85. game->getEventDispatcher()->subscribe<GamepadButtonPressedEvent>(this);
  86. // Make splash screen visible
  87. game->splashBackgroundImage->setVisible(true);
  88. game->splashImage->setVisible(true);
  89. game->splashImage->setTintColor(Vector4(1.0f, 1.0f, 1.0f, 0.0f));
  90. game->splashBackgroundImage->setTintColor(Vector4(0.0f, 0.0f, 0.0f, 1.0f));
  91. game->splashImage->resetTweens();
  92. game->splashBackgroundImage->resetTweens();
  93. game->uiRootElement->update();
  94. // Hide mouse
  95. game->mouse->setVisible(false);
  96. }
  97. void SplashState::execute()
  98. {}
  99. void SplashState::exit()
  100. {
  101. // Remove fade animation from the animator
  102. game->getAnimator()->removeAnimation(&fadeAnimation);
  103. // Unsubscribe this state from input events
  104. game->getEventDispatcher()->unsubscribe<KeyPressedEvent>(this);
  105. game->getEventDispatcher()->unsubscribe<MouseButtonPressedEvent>(this);
  106. game->getEventDispatcher()->unsubscribe<GamepadButtonPressedEvent>(this);
  107. // Make splash screen invisible
  108. game->splashBackgroundImage->setVisible(false);
  109. game->splashImage->setVisible(false);
  110. }
  111. void SplashState::handleEvent(const KeyPressedEvent& event)
  112. {
  113. skip();
  114. }
  115. void SplashState::handleEvent(const MouseButtonPressedEvent& event)
  116. {
  117. skip();
  118. }
  119. void SplashState::handleEvent(const GamepadButtonPressedEvent& event)
  120. {
  121. skip();
  122. }
  123. void SplashState::skip()
  124. {
  125. game->splashImage->setVisible(false);
  126. game->changeState(game->sandboxState);
  127. }