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

222 lines
8.5 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/states/title.hpp"
  20. #include "game/states/main-menu.hpp"
  21. #include "animation/screen-transition.hpp"
  22. #include "animation/ease.hpp"
  23. #include "animation/animation.hpp"
  24. #include "animation/animator.hpp"
  25. #include "application.hpp"
  26. #include "scene/text.hpp"
  27. #include "configuration.hpp"
  28. #include "render/passes/clear-pass.hpp"
  29. namespace game {
  30. namespace state {
  31. namespace title {
  32. void enter(game::context* ctx)
  33. {
  34. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  35. // Construct title text
  36. ctx->title_text = new scene::text();
  37. ctx->title_text->set_material(&ctx->title_font_material);
  38. ctx->title_text->set_font(&ctx->title_font);
  39. ctx->title_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
  40. ctx->title_text->set_content((*ctx->strings)["title_antkeeper"]);
  41. // Construct "Press any key" text
  42. ctx->title_press_any_key_text = new scene::text();
  43. ctx->title_press_any_key_text->set_material(&ctx->menu_font_material);
  44. ctx->title_press_any_key_text->set_font(&ctx->menu_font);
  45. ctx->title_press_any_key_text->set_color({1.0f, 1.0f, 1.0f, 0.0f});
  46. ctx->title_press_any_key_text->set_content((*ctx->strings)["title_press_any_key"]);
  47. int window_height = std::get<1>(ctx->app->get_viewport_dimensions());
  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 + (window_height / 3) / 2), 0.0f});
  53. // Align "Press any key" text
  54. const auto& any_key_aabb = static_cast<const geom::aabb<float>&>(ctx->title_press_any_key_text->get_local_bounds());
  55. float any_key_w = any_key_aabb.max_point.x - any_key_aabb.min_point.x;
  56. float any_key_h = any_key_aabb.max_point.y - any_key_aabb.min_point.y;
  57. ctx->title_press_any_key_text->set_translation({std::round(-any_key_w * 0.5f), std::round(-any_key_h * 0.5f - (window_height / 3) / 2), 0.0f});
  58. // Load animation timing configuration
  59. double title_fade_in_duration = 0.0;
  60. double title_fade_out_duration = 0.0;
  61. double title_press_any_key_duration = 0.0;
  62. double title_press_any_key_delay = 0.0;
  63. if (ctx->config->contains("title_fade_in_duration"))
  64. title_fade_in_duration = (*ctx->config)["title_fade_in_duration"].get<double>();
  65. if (ctx->config->contains("title_fade_out_duration"))
  66. title_fade_out_duration = (*ctx->config)["title_fade_out_duration"].get<double>();
  67. if (ctx->config->contains("title_press_any_key_duration"))
  68. title_press_any_key_duration = (*ctx->config)["title_press_any_key_duration"].get<double>();
  69. if (ctx->config->contains("title_press_any_key_delay"))
  70. title_press_any_key_delay = (*ctx->config)["title_press_any_key_delay"].get<double>();
  71. auto set_title_opacity = [ctx](int channel, const float& opacity)
  72. {
  73. ctx->title_text->set_color({1.0f, 1.0f, 1.0f, opacity});
  74. };
  75. // Build title fade in animation
  76. ctx->title_fade_in_animation = new animation<float>();
  77. animation_channel<float>* title_fade_in_opacity_channel = ctx->title_fade_in_animation->add_channel(0);
  78. ctx->title_fade_in_animation->set_interpolator(ease<float>::in_quad);
  79. title_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  80. title_fade_in_opacity_channel->insert_keyframe({title_fade_in_duration, 1.0f});
  81. title_fade_in_opacity_channel->insert_keyframe({title_fade_in_duration + title_press_any_key_delay, 1.0f});
  82. ctx->title_fade_in_animation->set_frame_callback(set_title_opacity);
  83. // Trigger "Press any key" animation after title fade in animation ends
  84. ctx->title_fade_in_animation->set_end_callback
  85. (
  86. [ctx]()
  87. {
  88. ctx->title_press_any_key_animation->play();
  89. }
  90. );
  91. // Build title fade out animation
  92. ctx->title_fade_out_animation = new animation<float>();
  93. animation_channel<float>* title_fade_out_opacity_channel = ctx->title_fade_out_animation->add_channel(0);
  94. ctx->title_fade_out_animation->set_interpolator(ease<float>::out_quad);
  95. title_fade_out_opacity_channel->insert_keyframe({0.0, 1.0f});
  96. title_fade_out_opacity_channel->insert_keyframe({title_fade_out_duration, 0.0f});
  97. ctx->title_fade_out_animation->set_frame_callback(set_title_opacity);
  98. // Trigger a state change when the title fade out animation ends
  99. ctx->title_fade_out_animation->set_end_callback
  100. (
  101. [ctx]()
  102. {
  103. application::state next_state;
  104. next_state.name = "main_menu";
  105. next_state.enter = std::bind(game::state::main_menu::enter, ctx, 0);
  106. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  107. ctx->app->queue_state(next_state);
  108. }
  109. );
  110. // Build "Press any key" animation
  111. ctx->title_press_any_key_animation = new animation<float>();
  112. ctx->title_press_any_key_animation->loop(true);
  113. animation_channel<float>* title_press_any_key_opacity_channel = ctx->title_press_any_key_animation->add_channel(0);
  114. ctx->title_press_any_key_animation->set_interpolator(math::lerp<float, double>);
  115. title_press_any_key_opacity_channel->insert_keyframe({0.0, 0.0f});
  116. title_press_any_key_opacity_channel->insert_keyframe({title_press_any_key_duration * 0.5, 1.0f});
  117. title_press_any_key_opacity_channel->insert_keyframe({title_press_any_key_duration, 0.0f});
  118. ctx->title_press_any_key_animation->set_frame_callback
  119. (
  120. [ctx](int channel, const float& opacity)
  121. {
  122. ctx->title_press_any_key_text->set_color({1.0f, 1.0f, 1.0f, 0.5f * ease<float>::out_cubic(0.0f, 1.0f, opacity)});
  123. }
  124. );
  125. // Add title fade animations to animator
  126. ctx->animator->add_animation(ctx->title_fade_in_animation);
  127. ctx->animator->add_animation(ctx->title_fade_out_animation);
  128. ctx->animator->add_animation(ctx->title_press_any_key_animation);
  129. // Start title fade in animation
  130. ctx->title_fade_in_animation->play();
  131. // Set up title skipper
  132. ctx->input_listener->set_callback
  133. (
  134. [ctx](const event_base& event)
  135. {
  136. auto id = event.get_event_type_id();
  137. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  138. {
  139. /*
  140. if (ctx->title_fade_in_animation->is_stopped())
  141. {
  142. ctx->title_fade_out_animation->play();
  143. ctx->input_listener->set_enabled(false);
  144. }
  145. */
  146. if (ctx->title_text->get_color()[3] > 0.0f)
  147. {
  148. ctx->input_listener->set_enabled(false);
  149. // Black out screen
  150. ctx->rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  151. ctx->rasterizer->clear_framebuffer(true, false, false);
  152. ctx->app->swap_buffers();
  153. // Change state
  154. application::state next_state;
  155. next_state.name = "main_menu";
  156. next_state.enter = std::bind(game::state::main_menu::enter, ctx, 0);
  157. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  158. ctx->app->change_state(next_state);
  159. }
  160. }
  161. }
  162. );
  163. ctx->input_listener->set_enabled(true);
  164. ctx->ui_scene->add_object(ctx->title_text);
  165. ctx->title_text->update_tweens();
  166. ctx->ui_scene->add_object(ctx->title_press_any_key_text);
  167. ctx->title_press_any_key_text->update_tweens();
  168. }
  169. void exit(game::context* ctx)
  170. {
  171. // Remove title text
  172. ctx->ui_scene->remove_object(ctx->title_text);
  173. ctx->ui_scene->remove_object(ctx->title_press_any_key_text);
  174. // Disable title skipper
  175. ctx->input_listener->set_enabled(false);
  176. ctx->input_listener->set_callback(nullptr);
  177. // Destruct title animations
  178. ctx->animator->remove_animation(ctx->title_fade_in_animation);
  179. ctx->animator->remove_animation(ctx->title_fade_out_animation);
  180. ctx->animator->remove_animation(ctx->title_press_any_key_animation);
  181. delete ctx->title_fade_in_animation;
  182. delete ctx->title_fade_out_animation;
  183. delete ctx->title_press_any_key_animation;
  184. ctx->title_fade_in_animation = nullptr;
  185. ctx->title_fade_out_animation = nullptr;
  186. ctx->title_press_any_key_animation = nullptr;
  187. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  188. }
  189. } // namespace title
  190. } // namespace state
  191. } // namespace game