💿🐜 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.

207 lines
5.9 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
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 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 "title-state.hpp"
  20. #include "../application.hpp"
  21. #include "../camera-controller.hpp"
  22. #include "../ui/menu.hpp"
  23. #include <iostream>
  24. #include <SDL2/SDL.h>
  25. TitleState::TitleState(Application* application):
  26. ApplicationState(application)
  27. {}
  28. TitleState::~TitleState()
  29. {}
  30. void TitleState::enter()
  31. {
  32. application->backgroundLayer->addObject(&application->bgCamera);
  33. application->backgroundLayer->addObject(&application->bgBatch);
  34. application->inputManager->addWindowObserver(this);
  35. windowResized(application->resolution.x, application->resolution.y);
  36. application->camera.setPerspective(
  37. glm::radians(25.0f),
  38. application->resolution.x / application->resolution.y,
  39. 0.1f,
  40. 1000.0f);
  41. // Setup camera controller
  42. application->surfaceCam->setCamera(&application->camera);
  43. application->surfaceCam->setFocalPoint(Vector3(0.0f));
  44. application->surfaceCam->setFocalDistance(50.0f);
  45. application->surfaceCam->setElevation(glm::radians(-30.0f));
  46. application->surfaceCam->setAzimuth(glm::radians(180.0f));
  47. application->surfaceCam->setTargetFocalPoint(application->surfaceCam->getFocalPoint());
  48. application->surfaceCam->setTargetFocalDistance(application->surfaceCam->getFocalDistance());
  49. application->surfaceCam->setTargetElevation(application->surfaceCam->getElevation());
  50. application->surfaceCam->setTargetAzimuth(application->surfaceCam->getAzimuth());
  51. application->surfaceCam->update(0.0f);
  52. // Dim background
  53. application->darkenImage->setVisible(true);
  54. // Show title
  55. application->titleImage->setVisible(true);
  56. application->titleImage->setTintColor(Vector4(1.0f));
  57. // Open main menu
  58. application->openMenu(application->mainMenu);
  59. // Position options menu
  60. application->optionsMenu->getUIContainer()->setAnchor(Vector2(0.5f, 0.8f));
  61. application->controlsMenu->getUIContainer()->setAnchor(Vector2(0.5f, 0.8f));
  62. application->levelsMenu->getUIContainer()->setAnchor(Vector2(0.5f, 0.8f));
  63. // Setup fade-in
  64. application->blackoutImage->setVisible(true);
  65. application->fadeInTween->start();
  66. }
  67. void TitleState::execute()
  68. {
  69. // Navigate menu
  70. if (application->activeMenu != nullptr)
  71. {
  72. MenuItem* selectedItem = application->activeMenu->getSelectedItem();
  73. if (application->menuDown.isTriggered() && !application->menuDown.wasTriggered())
  74. {
  75. if (selectedItem != nullptr)
  76. {
  77. if (selectedItem->getItemIndex() < application->activeMenu->getItemCount() - 1)
  78. {
  79. application->selectMenuItem(selectedItem->getItemIndex() + 1);
  80. }
  81. else
  82. {
  83. application->selectMenuItem(0);
  84. }
  85. }
  86. else
  87. {
  88. application->selectMenuItem(0);
  89. }
  90. }
  91. else if (application->menuUp.isTriggered() && !application->menuUp.wasTriggered())
  92. {
  93. if (selectedItem != nullptr)
  94. {
  95. if (selectedItem->getItemIndex() > 0)
  96. {
  97. application->selectMenuItem(selectedItem->getItemIndex() - 1);
  98. }
  99. else
  100. {
  101. application->selectMenuItem(application->activeMenu->getItemCount() - 1);
  102. }
  103. }
  104. else
  105. {
  106. application->selectMenuItem(application->activeMenu->getItemCount() - 1);
  107. }
  108. }
  109. if (application->menuLeft.isTriggered() && !application->menuLeft.wasTriggered())
  110. {
  111. application->decrementMenuItem();
  112. }
  113. else if (application->menuRight.isTriggered() && !application->menuRight.wasTriggered())
  114. {
  115. application->incrementMenuItem();
  116. }
  117. if (application->menuSelect.isTriggered() && !application->menuSelect.wasTriggered())
  118. {
  119. application->activateMenuItem();
  120. }
  121. }
  122. if (application->escape.isTriggered())
  123. {
  124. application->close(EXIT_SUCCESS);
  125. }
  126. // Set selector icon position
  127. /*
  128. float lineHeight = application->menuFont->getMetrics().getHeight();
  129. const UIContainer* container = application->menuContainers[application->currentMenuIndex];
  130. application->menuSelectorLabel->setTranslation(
  131. Vector2(container->getPosition().x - application->menuSelectorLabel->getDimensions().x * 1.5f,
  132. container->getPosition().y + lineHeight * 0.5f - application->menuSelectorLabel->getDimensions().y * 0.5f + lineHeight * application->selectedMenuItemIndex));
  133. */
  134. }
  135. void TitleState::exit()
  136. {
  137. // Remove input observers
  138. application->inputManager->removeWindowObserver(this);
  139. // Hide UI
  140. application->titleImage->setVisible(false);
  141. application->anyKeyLabel->setVisible(false);
  142. application->darkenImage->setVisible(false);
  143. // Remove clear scene
  144. application->backgroundLayer->removeObject(&application->bgCamera);
  145. application->backgroundLayer->removeObject(&application->bgBatch);
  146. }
  147. void TitleState::windowClosed()
  148. {
  149. application->close(EXIT_SUCCESS);
  150. }
  151. void TitleState::windowResized(int width, int height)
  152. {
  153. /*
  154. // Update application dimensions
  155. application->width = width;
  156. application->height = height;
  157. if (application->fullscreen)
  158. {
  159. application->fullscreenWidth = width;
  160. application->fullscreenHeight = height;
  161. }
  162. else
  163. {
  164. application->windowedWidth = width;
  165. application->windowedHeight = height;
  166. }
  167. // Setup default render target
  168. application->defaultRenderTarget.width = application->width;
  169. application->defaultRenderTarget.height = application->height;
  170. // Resize UI
  171. application->resizeUI();
  172. // 3D camera
  173. application->camera.setPerspective(
  174. glm::radians(25.0f),
  175. (float)application->width / (float)application->height,
  176. 0.1f,
  177. 1000.0f);
  178. */
  179. }