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

284 lines
8.3 KiB

  1. /*
  2. * Copyright (C) 2021 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 "game/state/main-menu.hpp"
  20. #include "game/state/options-menu.hpp"
  21. #include "game/state/extras-menu.hpp"
  22. #include "game/state/forage.hpp"
  23. #include "game/state/nuptial-flight.hpp"
  24. #include "game/menu.hpp"
  25. #include "render/passes/clear-pass.hpp"
  26. #include "resources/resource-manager.hpp"
  27. #include "render/model.hpp"
  28. #include "animation/animation.hpp"
  29. #include "animation/animator.hpp"
  30. #include "animation/screen-transition.hpp"
  31. #include "animation/ease.hpp"
  32. #include "animation/timeline.hpp"
  33. #include "application.hpp"
  34. #include <limits>
  35. namespace game {
  36. namespace state {
  37. main_menu::main_menu(game::context& ctx, bool fade_in):
  38. game::state::base(ctx)
  39. {
  40. ctx.logger->push_task("Entering main menu state");
  41. ctx.ui_clear_pass->set_cleared_buffers(true, true, false);
  42. // Construct title text
  43. ctx.title_text = new scene::text();
  44. ctx.title_text->set_material(&ctx.title_font_material);
  45. ctx.title_text->set_font(&ctx.title_font);
  46. ctx.title_text->set_color({1.0f, 1.0f, 1.0f, 1.0f});
  47. ctx.title_text->set_content((*ctx.strings)["title_antkeeper"]);
  48. // Align title text
  49. const auto& title_aabb = static_cast<const geom::aabb<float>&>(ctx.title_text->get_local_bounds());
  50. float title_w = title_aabb.max_point.x - title_aabb.min_point.x;
  51. float title_h = title_aabb.max_point.y - title_aabb.min_point.y;
  52. ctx.title_text->set_translation({std::round(-title_w * 0.5f), std::round(-title_h * 0.5f + (ctx.app->get_viewport_dimensions().y / 3.0f) / 2.0f), 0.0f});
  53. ctx.title_text->update_tweens();
  54. // Add title text to UI
  55. ctx.ui_scene->add_object(ctx.title_text);
  56. // Construct title fade animation
  57. ctx.title_fade_animation = new animation<float>();
  58. animation_channel<float>* opacity_channel = ctx.title_fade_animation->add_channel(0);
  59. ctx.title_fade_animation->set_frame_callback
  60. (
  61. [&ctx](int channel, const float& opacity)
  62. {
  63. float4 color = ctx.title_text->get_color();
  64. color[3] = opacity;
  65. ctx.title_text->set_color(color);
  66. }
  67. );
  68. ctx.animator->add_animation(ctx.title_fade_animation);
  69. // Construct menu item texts
  70. scene::text* start_text = new scene::text();
  71. scene::text* options_text = new scene::text();
  72. scene::text* extras_text = new scene::text();
  73. scene::text* quit_text = new scene::text();
  74. // Build list of menu item texts
  75. ctx.menu_item_texts.push_back({start_text, nullptr});
  76. ctx.menu_item_texts.push_back({options_text, nullptr});
  77. ctx.menu_item_texts.push_back({extras_text, nullptr});
  78. ctx.menu_item_texts.push_back({quit_text, nullptr});
  79. // Set content of menu item texts
  80. start_text->set_content((*ctx.strings)["main_menu_start"]);
  81. options_text->set_content((*ctx.strings)["main_menu_options"]);
  82. extras_text->set_content((*ctx.strings)["main_menu_extras"]);
  83. quit_text->set_content((*ctx.strings)["main_menu_quit"]);
  84. // Init menu item index
  85. game::menu::init_menu_item_index(ctx, "main");
  86. game::menu::update_text_color(ctx);
  87. game::menu::update_text_font(ctx);
  88. game::menu::align_text(ctx, true, false, (-ctx.app->get_viewport_dimensions().y / 3.0f) / 2.0f);
  89. game::menu::update_text_tweens(ctx);
  90. game::menu::add_text_to_ui(ctx);
  91. game::menu::setup_animations(ctx);
  92. auto select_start_callback = [&ctx]()
  93. {
  94. // Disable controls and menu callbacks
  95. game::menu::clear_controls(ctx);
  96. game::menu::clear_callbacks(ctx);
  97. // Create change state function
  98. auto change_state_nuptial_flight = [&ctx]()
  99. {
  100. // Queue change to nuptial state
  101. ctx.function_queue.push
  102. (
  103. [&ctx]()
  104. {
  105. ctx.state_machine.pop();
  106. ctx.state_machine.emplace(new game::state::nuptial_flight(ctx));
  107. }
  108. );
  109. };
  110. // Set up timing
  111. const float fade_out_duration = 1.0f;
  112. // Schedule state change
  113. timeline* timeline = ctx.timeline;
  114. float t = timeline->get_position();
  115. timeline->add_sequence({{t + fade_out_duration, change_state_nuptial_flight}});
  116. // Start fade out to white
  117. ctx.fade_transition_color->set_value({1, 1, 1});
  118. ctx.fade_transition->transition(fade_out_duration, false, ease<float>::out_cubic, false);
  119. };
  120. auto select_options_callback = [this, &ctx]()
  121. {
  122. game::menu::clear_controls(ctx);
  123. // Fade out title
  124. this->fade_out_title();
  125. // Fade out menu
  126. game::menu::fade_out
  127. (
  128. ctx,
  129. [&ctx]()
  130. {
  131. // Queue change to options menu state
  132. ctx.function_queue.push
  133. (
  134. [&ctx]()
  135. {
  136. ctx.state_machine.pop();
  137. ctx.state_machine.emplace(new game::state::options_menu(ctx));
  138. }
  139. );
  140. }
  141. );
  142. };
  143. auto select_extras_callback = [this, &ctx]()
  144. {
  145. // Disable controls
  146. game::menu::clear_controls(ctx);
  147. // Fade out title
  148. this->fade_out_title();
  149. // Fade out menu
  150. game::menu::fade_out
  151. (
  152. ctx,
  153. [&ctx]()
  154. {
  155. // Queue change to extras menu state
  156. ctx.function_queue.push
  157. (
  158. [&ctx]()
  159. {
  160. ctx.state_machine.pop();
  161. ctx.state_machine.emplace(new game::state::extras_menu(ctx));
  162. }
  163. );
  164. }
  165. );
  166. };
  167. auto select_quit_callback = [&ctx]()
  168. {
  169. ctx.app->close();
  170. };
  171. // Build list of menu select callbacks
  172. ctx.menu_select_callbacks.push_back(select_start_callback);
  173. ctx.menu_select_callbacks.push_back(select_options_callback);
  174. ctx.menu_select_callbacks.push_back(select_extras_callback);
  175. ctx.menu_select_callbacks.push_back(select_quit_callback);
  176. // Build list of menu left callbacks
  177. ctx.menu_left_callbacks.push_back(nullptr);
  178. ctx.menu_left_callbacks.push_back(nullptr);
  179. ctx.menu_left_callbacks.push_back(nullptr);
  180. ctx.menu_left_callbacks.push_back(nullptr);
  181. // Build list of menu right callbacks
  182. ctx.menu_right_callbacks.push_back(nullptr);
  183. ctx.menu_right_callbacks.push_back(nullptr);
  184. ctx.menu_right_callbacks.push_back(nullptr);
  185. ctx.menu_right_callbacks.push_back(nullptr);
  186. // Set menu back callback
  187. ctx.menu_back_callback = select_quit_callback;
  188. // Queue menu control setup
  189. ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
  190. if (fade_in)
  191. {
  192. ctx.fade_transition->transition(0.5f, true, ease<float>::out_cubic);
  193. }
  194. else
  195. {
  196. // Fade in title
  197. ctx.title_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
  198. ctx.title_text->update_tweens();
  199. fade_in_title();
  200. // Fade in menu
  201. game::menu::fade_in(ctx, nullptr);
  202. }
  203. ctx.logger->pop_task(EXIT_SUCCESS);
  204. }
  205. main_menu::~main_menu()
  206. {
  207. ctx.logger->push_task("Exiting main menu state");
  208. // Destruct menu
  209. game::menu::clear_controls(ctx);
  210. game::menu::clear_callbacks(ctx);
  211. game::menu::delete_animations(ctx);
  212. game::menu::remove_text_from_ui(ctx);
  213. game::menu::delete_text(ctx);
  214. // Destruct title animation
  215. ctx.animator->remove_animation(ctx.title_fade_animation);
  216. delete ctx.title_fade_animation;
  217. ctx.title_fade_animation = nullptr;
  218. // Destruct title text
  219. ctx.ui_scene->remove_object(ctx.title_text);
  220. delete ctx.title_text;
  221. ctx.title_text = nullptr;
  222. ctx.logger->pop_task(EXIT_SUCCESS);
  223. }
  224. void main_menu::fade_in_title()
  225. {
  226. ctx.title_fade_animation->set_interpolator(ease<float>::out_cubic);
  227. animation_channel<float>* opacity_channel = ctx.title_fade_animation->get_channel(0);
  228. opacity_channel->remove_keyframes();
  229. opacity_channel->insert_keyframe({0.0, 0.0f});
  230. opacity_channel->insert_keyframe({game::menu::fade_in_duration, 1.0f});
  231. ctx.title_fade_animation->stop();
  232. ctx.title_fade_animation->play();
  233. }
  234. void main_menu::fade_out_title()
  235. {
  236. ctx.title_fade_animation->set_interpolator(ease<float>::out_cubic);
  237. animation_channel<float>* opacity_channel = ctx.title_fade_animation->get_channel(0);
  238. opacity_channel->remove_keyframes();
  239. opacity_channel->insert_keyframe({0.0, 1.0f});
  240. opacity_channel->insert_keyframe({game::menu::fade_out_duration, 0.0f});
  241. ctx.title_fade_animation->stop();
  242. ctx.title_fade_animation->play();
  243. }
  244. } // namespace state
  245. } // namespace game