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

176 lines
4.6 KiB

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