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

240 lines
6.1 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/options-menu.hpp"
  20. #include "game/state/main-menu.hpp"
  21. #include "game/state/controls-menu.hpp"
  22. #include "game/state/graphics-menu.hpp"
  23. #include "game/state/sound-menu.hpp"
  24. #include "game/state/language-menu.hpp"
  25. #include "game/state/pause-menu.hpp"
  26. #include "game/menu.hpp"
  27. #include "animation/ease.hpp"
  28. #include "animation/animation.hpp"
  29. #include "animation/animator.hpp"
  30. #include "application.hpp"
  31. #include "scene/text.hpp"
  32. #include "debug/log.hpp"
  33. #include "game/strings.hpp"
  34. #include "utility/hash/fnv1a.hpp"
  35. using namespace hash::literals;
  36. namespace game {
  37. namespace state {
  38. options_menu::options_menu(game::context& ctx):
  39. game::state::base(ctx)
  40. {
  41. debug::log::trace("Entering options menu state...");
  42. // Construct menu item texts
  43. scene::text* controls_text = new scene::text();
  44. scene::text* graphics_text = new scene::text();
  45. scene::text* sound_text = new scene::text();
  46. scene::text* language_text = new scene::text();
  47. scene::text* back_text = new scene::text();
  48. // Set content of menu item texts
  49. controls_text->set_content(get_string(ctx, "options_menu_controls"_fnv1a32));
  50. graphics_text->set_content(get_string(ctx, "options_menu_graphics"_fnv1a32));
  51. sound_text->set_content(get_string(ctx, "options_menu_sound"_fnv1a32));
  52. language_text->set_content(get_string(ctx, "options_menu_language"_fnv1a32));
  53. back_text->set_content(get_string(ctx, "back"_fnv1a32));
  54. // Build list of menu item texts
  55. ctx.menu_item_texts.push_back({controls_text, nullptr});
  56. ctx.menu_item_texts.push_back({graphics_text, nullptr});
  57. ctx.menu_item_texts.push_back({sound_text, nullptr});
  58. ctx.menu_item_texts.push_back({language_text, nullptr});
  59. ctx.menu_item_texts.push_back({back_text, nullptr});
  60. // Init menu item index
  61. game::menu::init_menu_item_index(ctx, "options");
  62. game::menu::update_text_color(ctx);
  63. game::menu::update_text_font(ctx);
  64. game::menu::align_text(ctx, true);
  65. game::menu::update_text_tweens(ctx);
  66. game::menu::add_text_to_ui(ctx);
  67. game::menu::setup_animations(ctx);
  68. // Construct menu item callbacks
  69. auto select_controls_callback = [&ctx]()
  70. {
  71. // Disable controls
  72. game::menu::clear_controls(ctx);
  73. // Return to main menu
  74. game::menu::fade_out
  75. (
  76. ctx,
  77. [&ctx]()
  78. {
  79. // Queue change to controls menu state
  80. ctx.function_queue.push
  81. (
  82. [&ctx]()
  83. {
  84. ctx.state_machine.pop();
  85. ctx.state_machine.emplace(new game::state::controls_menu(ctx));
  86. }
  87. );
  88. }
  89. );
  90. };
  91. auto select_graphics_callback = [&ctx]()
  92. {
  93. // Disable controls
  94. game::menu::clear_controls(ctx);
  95. // Return to main menu
  96. game::menu::fade_out
  97. (
  98. ctx,
  99. [&ctx]()
  100. {
  101. // Queue change to graphics menu state
  102. ctx.function_queue.push
  103. (
  104. [&ctx]()
  105. {
  106. ctx.state_machine.pop();
  107. ctx.state_machine.emplace(new game::state::graphics_menu(ctx));
  108. }
  109. );
  110. }
  111. );
  112. };
  113. auto select_sound_callback = [&ctx]()
  114. {
  115. // Disable controls
  116. game::menu::clear_controls(ctx);
  117. // Return to main menu
  118. game::menu::fade_out
  119. (
  120. ctx,
  121. [&ctx]()
  122. {
  123. // Queue change to sound menu state
  124. ctx.function_queue.push
  125. (
  126. [&ctx]()
  127. {
  128. ctx.state_machine.pop();
  129. ctx.state_machine.emplace(new game::state::sound_menu(ctx));
  130. }
  131. );
  132. }
  133. );
  134. };
  135. auto select_language_callback = [&ctx]()
  136. {
  137. // Disable controls
  138. game::menu::clear_controls(ctx);
  139. // Return to main menu
  140. game::menu::fade_out
  141. (
  142. ctx,
  143. [&ctx]()
  144. {
  145. // Queue change to language menu state
  146. ctx.function_queue.push
  147. (
  148. [&ctx]()
  149. {
  150. ctx.state_machine.pop();
  151. ctx.state_machine.emplace(new game::state::language_menu(ctx));
  152. }
  153. );
  154. }
  155. );
  156. };
  157. auto select_back_callback = [&ctx]()
  158. {
  159. // Disable controls
  160. game::menu::clear_controls(ctx);
  161. // Save config
  162. //game::save::config(ctx);
  163. game::menu::fade_out
  164. (
  165. ctx,
  166. [&ctx]()
  167. {
  168. // Queue change to main menu state
  169. ctx.function_queue.push
  170. (
  171. [&ctx]()
  172. {
  173. ctx.state_machine.pop();
  174. if (ctx.resume_callback)
  175. ctx.state_machine.emplace(new game::state::pause_menu(ctx));
  176. else
  177. ctx.state_machine.emplace(new game::state::main_menu(ctx, false));
  178. }
  179. );
  180. }
  181. );
  182. };
  183. // Build list of menu select callbacks
  184. ctx.menu_select_callbacks.push_back(select_controls_callback);
  185. ctx.menu_select_callbacks.push_back(select_graphics_callback);
  186. ctx.menu_select_callbacks.push_back(select_sound_callback);
  187. ctx.menu_select_callbacks.push_back(select_language_callback);
  188. ctx.menu_select_callbacks.push_back(select_back_callback);
  189. // Build list of menu right callbacks
  190. ctx.menu_right_callbacks.resize(5, nullptr);
  191. // Build list of menu left callbacks
  192. ctx.menu_left_callbacks.resize(5, nullptr);
  193. // Set menu back callback
  194. ctx.menu_back_callback = select_back_callback;
  195. // Queue menu control setup
  196. ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
  197. // Fade in menu
  198. game::menu::fade_in(ctx, nullptr);
  199. debug::log::trace("Entered options menu state");
  200. }
  201. options_menu::~options_menu()
  202. {
  203. debug::log::trace("Exiting options menu state...");
  204. // Destruct menu
  205. game::menu::clear_controls(ctx);
  206. game::menu::clear_callbacks(ctx);
  207. game::menu::delete_animations(ctx);
  208. game::menu::remove_text_from_ui(ctx);
  209. game::menu::delete_text(ctx);
  210. debug::log::trace("Exited options menu state");
  211. }
  212. } // namespace state
  213. } // namespace game