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

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