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

169 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/states/controls-menu-state.hpp"
  20. #include "game/states/keyboard-config-menu-state.hpp"
  21. #include "game/states/gamepad-config-menu-state.hpp"
  22. #include "game/states/options-menu-state.hpp"
  23. #include "game/controls.hpp"
  24. #include <engine/scene/text.hpp>
  25. #include <engine/debug/log.hpp>
  26. #include "game/menu.hpp"
  27. #include "game/strings.hpp"
  28. #include <engine/utility/hash/fnv1a.hpp>
  29. using namespace hash::literals;
  30. controls_menu_state::controls_menu_state(::game& ctx):
  31. game_state(ctx)
  32. {
  33. debug::log_trace("Entering controls menu state...");
  34. // Construct menu item texts
  35. keyboard_text = std::make_unique<scene::text>();
  36. gamepad_text = std::make_unique<scene::text>();
  37. back_text = std::make_unique<scene::text>();
  38. // Build list of menu item texts
  39. ctx.menu_item_texts.push_back({keyboard_text.get(), nullptr});
  40. ctx.menu_item_texts.push_back({gamepad_text.get(), nullptr});
  41. ctx.menu_item_texts.push_back({back_text.get(), nullptr});
  42. // Set content of menu item texts
  43. keyboard_text->set_content(get_string(ctx, "controls_menu_keyboard"));
  44. gamepad_text->set_content(get_string(ctx, "controls_menu_gamepad"));
  45. back_text->set_content(get_string(ctx, "back"));
  46. // Init menu item index
  47. ::menu::init_menu_item_index(ctx, "controls");
  48. ::menu::update_text_color(ctx);
  49. ::menu::update_text_font(ctx);
  50. ::menu::align_text(ctx, true);
  51. ::menu::add_text_to_ui(ctx);
  52. ::menu::setup_animations(ctx);
  53. // Construct menu item callbacks
  54. auto select_keyboard_callback = [&ctx]()
  55. {
  56. // Disable menu controls
  57. ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
  58. ::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(std::make_unique<keyboard_config_menu_state>(ctx));
  70. }
  71. );
  72. }
  73. );
  74. };
  75. auto select_gamepad_callback = [&ctx]()
  76. {
  77. // Disable menu controls
  78. ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
  79. ::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(std::make_unique<gamepad_config_menu_state>(ctx));
  91. }
  92. );
  93. }
  94. );
  95. };
  96. auto select_back_callback = [&ctx]()
  97. {
  98. // Disable menu controls
  99. ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
  100. ::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(std::make_unique<options_menu_state>(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(::enable_menu_controls, std::ref(ctx)));
  133. // Fade in menu
  134. ::menu::fade_in(ctx, nullptr);
  135. debug::log_trace("Entered controls menu state");
  136. }
  137. controls_menu_state::~controls_menu_state()
  138. {
  139. debug::log_trace("Exiting options menu state...");
  140. // Destruct menu
  141. ::disable_menu_controls(ctx);
  142. ::menu::clear_callbacks(ctx);
  143. ::menu::delete_animations(ctx);
  144. ::menu::remove_text_from_ui(ctx);
  145. ::menu::delete_text(ctx);
  146. debug::log_trace("Exited controls menu state");
  147. }