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

155 lines
5.1 KiB

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/save.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 "render/passes/clear-pass.hpp"
  33. namespace game {
  34. namespace state {
  35. namespace options_menu {
  36. void enter(game::context* ctx)
  37. {
  38. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  39. // Construct menu item texts
  40. scene::text* controls_text = new scene::text();
  41. scene::text* graphics_text = new scene::text();
  42. scene::text* sound_text = new scene::text();
  43. scene::text* language_text = new scene::text();
  44. scene::text* back_text = new scene::text();
  45. // Set content of menu item texts
  46. controls_text->set_content((*ctx->strings)["options_menu_controls"]);
  47. graphics_text->set_content((*ctx->strings)["options_menu_graphics"]);
  48. sound_text->set_content((*ctx->strings)["options_menu_sound"]);
  49. language_text->set_content((*ctx->strings)["options_menu_language"]);
  50. back_text->set_content((*ctx->strings)["back"]);
  51. // Build list of menu item texts
  52. ctx->menu_item_texts.push_back({controls_text, nullptr});
  53. ctx->menu_item_texts.push_back({graphics_text, nullptr});
  54. ctx->menu_item_texts.push_back({sound_text, nullptr});
  55. ctx->menu_item_texts.push_back({language_text, nullptr});
  56. ctx->menu_item_texts.push_back({back_text, nullptr});
  57. // Init menu item index
  58. game::menu::init_menu_item_index(ctx, "options");
  59. game::menu::update_text_color(ctx);
  60. game::menu::update_text_font(ctx);
  61. game::menu::align_text(ctx);
  62. game::menu::update_text_tweens(ctx);
  63. game::menu::add_text_to_ui(ctx);
  64. // Construct menu item callbacks
  65. auto select_controls_callback = [ctx]()
  66. {
  67. application::state next_state;
  68. next_state.name = "controls_menu";
  69. next_state.enter = std::bind(game::state::controls_menu::enter, ctx);
  70. next_state.exit = std::bind(game::state::controls_menu::exit, ctx);
  71. ctx->app->change_state(next_state);
  72. };
  73. auto select_graphics_callback = [ctx]()
  74. {
  75. application::state next_state;
  76. next_state.name = "graphics_menu";
  77. next_state.enter = std::bind(game::state::graphics_menu::enter, ctx);
  78. next_state.exit = std::bind(game::state::graphics_menu::exit, ctx);
  79. ctx->app->change_state(next_state);
  80. };
  81. auto select_sound_callback = [ctx]()
  82. {
  83. application::state next_state;
  84. next_state.name = "sound_menu";
  85. next_state.enter = std::bind(game::state::sound_menu::enter, ctx);
  86. next_state.exit = std::bind(game::state::sound_menu::exit, ctx);
  87. ctx->app->change_state(next_state);
  88. };
  89. auto select_language_callback = [ctx]()
  90. {
  91. application::state next_state;
  92. next_state.name = "language_menu";
  93. next_state.enter = std::bind(game::state::language_menu::enter, ctx);
  94. next_state.exit = std::bind(game::state::language_menu::exit, ctx);
  95. ctx->app->change_state(next_state);
  96. };
  97. auto select_back_callback = [ctx]()
  98. {
  99. // Save config
  100. game::save_config(ctx);
  101. // Return to main menu
  102. application::state next_state;
  103. next_state.name = "main_menu";
  104. next_state.enter = std::bind(game::state::main_menu::enter, ctx);
  105. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  106. ctx->app->change_state(next_state);
  107. };
  108. // Build list of menu select callbacks
  109. ctx->menu_select_callbacks.push_back(select_controls_callback);
  110. ctx->menu_select_callbacks.push_back(select_graphics_callback);
  111. ctx->menu_select_callbacks.push_back(select_sound_callback);
  112. ctx->menu_select_callbacks.push_back(select_language_callback);
  113. ctx->menu_select_callbacks.push_back(select_back_callback);
  114. // Build list of menu right callbacks
  115. ctx->menu_right_callbacks.resize(5, nullptr);
  116. // Build list of menu left callbacks
  117. ctx->menu_left_callbacks.resize(5, nullptr);
  118. // Set menu back callback
  119. ctx->menu_back_callback = select_back_callback;
  120. // Setup menu controls
  121. game::menu::setup_controls(ctx);
  122. }
  123. void exit(game::context* ctx)
  124. {
  125. // Destruct menu
  126. game::menu::clear_controls(ctx);
  127. game::menu::clear_callbacks(ctx);
  128. game::menu::remove_text_from_ui(ctx);
  129. game::menu::delete_text(ctx);
  130. // Save config
  131. game::save_config(ctx);
  132. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  133. }
  134. } // namespace options_menu
  135. } // namespace state
  136. } // namespace game