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

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