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

153 lines
4.4 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/controls-menu.hpp"
  20. #include "game/states/keyboard-config-menu.hpp"
  21. #include "game/states/gamepad-config-menu.hpp"
  22. #include "game/states/options-menu.hpp"
  23. #include "application.hpp"
  24. #include "scene/text.hpp"
  25. #include "debug/logger.hpp"
  26. #include "game/menu.hpp"
  27. namespace game {
  28. namespace state {
  29. namespace controls_menu {
  30. void enter(game::context* ctx)
  31. {
  32. // Construct menu item texts
  33. scene::text* keyboard_text = new scene::text();
  34. scene::text* gamepad_text = new scene::text();
  35. scene::text* back_text = new scene::text();
  36. // Build list of menu item texts
  37. ctx->menu_item_texts.push_back({keyboard_text, nullptr});
  38. ctx->menu_item_texts.push_back({gamepad_text, nullptr});
  39. ctx->menu_item_texts.push_back({back_text, nullptr});
  40. // Set content of menu item texts
  41. keyboard_text->set_content((*ctx->strings)["controls_menu_keyboard"]);
  42. gamepad_text->set_content((*ctx->strings)["controls_menu_gamepad"]);
  43. back_text->set_content((*ctx->strings)["back"]);
  44. // Init menu item index
  45. game::menu::init_menu_item_index(ctx, "controls");
  46. game::menu::update_text_color(ctx);
  47. game::menu::update_text_font(ctx);
  48. game::menu::align_text(ctx);
  49. game::menu::update_text_tweens(ctx);
  50. game::menu::add_text_to_ui(ctx);
  51. game::menu::setup_animations(ctx);
  52. // Construct menu item callbacks
  53. auto select_keyboard_callback = [ctx]()
  54. {
  55. // Disable controls
  56. game::menu::clear_controls(ctx);
  57. game::menu::fade_out
  58. (
  59. ctx,
  60. [ctx]()
  61. {
  62. application::state next_state;
  63. next_state.name = "keyboard_config_menu";
  64. next_state.enter = std::bind(game::state::keyboard_config_menu::enter, ctx);
  65. next_state.exit = std::bind(game::state::keyboard_config_menu::exit, ctx);
  66. ctx->app->queue_state(next_state);
  67. }
  68. );
  69. };
  70. auto select_gamepad_callback = [ctx]()
  71. {
  72. // Disable controls
  73. game::menu::clear_controls(ctx);
  74. game::menu::fade_out
  75. (
  76. ctx,
  77. [ctx]()
  78. {
  79. application::state next_state;
  80. next_state.name = "gamepad_config_menu";
  81. next_state.enter = std::bind(game::state::gamepad_config_menu::enter, ctx);
  82. next_state.exit = std::bind(game::state::gamepad_config_menu::exit, ctx);
  83. ctx->app->queue_state(next_state);
  84. }
  85. );
  86. };
  87. auto select_back_callback = [ctx]()
  88. {
  89. // Disable controls
  90. game::menu::clear_controls(ctx);
  91. game::menu::fade_out
  92. (
  93. ctx,
  94. [ctx]()
  95. {
  96. application::state next_state;
  97. next_state.name = "options_menu";
  98. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  99. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  100. ctx->app->queue_state(next_state);
  101. }
  102. );
  103. };
  104. // Build list of menu select callbacks
  105. ctx->menu_select_callbacks.push_back(select_keyboard_callback);
  106. ctx->menu_select_callbacks.push_back(select_gamepad_callback);
  107. ctx->menu_select_callbacks.push_back(select_back_callback);
  108. // Build list of menu left callbacks
  109. ctx->menu_left_callbacks.push_back(nullptr);
  110. ctx->menu_left_callbacks.push_back(nullptr);
  111. ctx->menu_left_callbacks.push_back(nullptr);
  112. // Build list of menu right callbacks
  113. ctx->menu_right_callbacks.push_back(nullptr);
  114. ctx->menu_right_callbacks.push_back(nullptr);
  115. ctx->menu_right_callbacks.push_back(nullptr);
  116. // Set menu back callback
  117. ctx->menu_back_callback = select_back_callback;
  118. // Queue menu control setup
  119. ctx->function_queue.push(std::bind(game::menu::setup_controls, ctx));
  120. // Fade in menu
  121. game::menu::fade_in(ctx, nullptr);
  122. }
  123. void exit(game::context* ctx)
  124. {
  125. // Destruct menu
  126. game::menu::clear_controls(ctx);
  127. game::menu::clear_callbacks(ctx);
  128. game::menu::delete_animations(ctx);
  129. game::menu::remove_text_from_ui(ctx);
  130. game::menu::delete_text(ctx);
  131. }
  132. } // namespace controls_menu
  133. } // namespace state
  134. } // namespace game