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

214 lines
6.1 KiB

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