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

158 lines
5.3 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 "animation/timeline.hpp"
  31. #include "application.hpp"
  32. #include "scene/text.hpp"
  33. #include "render/passes/clear-pass.hpp"
  34. namespace game {
  35. namespace state {
  36. namespace options_menu {
  37. void enter(game::context* ctx)
  38. {
  39. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  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. // Construct menu item callbacks
  66. auto select_controls_callback = [ctx]()
  67. {
  68. application::state next_state;
  69. next_state.name = "controls_menu";
  70. next_state.enter = std::bind(game::state::controls_menu::enter, ctx);
  71. next_state.exit = std::bind(game::state::controls_menu::exit, ctx);
  72. ctx->app->change_state(next_state);
  73. };
  74. auto select_graphics_callback = [ctx]()
  75. {
  76. application::state next_state;
  77. next_state.name = "graphics_menu";
  78. next_state.enter = std::bind(game::state::graphics_menu::enter, ctx);
  79. next_state.exit = std::bind(game::state::graphics_menu::exit, ctx);
  80. ctx->app->change_state(next_state);
  81. };
  82. auto select_sound_callback = [ctx]()
  83. {
  84. application::state next_state;
  85. next_state.name = "sound_menu";
  86. next_state.enter = std::bind(game::state::sound_menu::enter, ctx);
  87. next_state.exit = std::bind(game::state::sound_menu::exit, ctx);
  88. ctx->app->change_state(next_state);
  89. };
  90. auto select_language_callback = [ctx]()
  91. {
  92. application::state next_state;
  93. next_state.name = "language_menu";
  94. next_state.enter = std::bind(game::state::language_menu::enter, ctx);
  95. next_state.exit = std::bind(game::state::language_menu::exit, ctx);
  96. ctx->app->change_state(next_state);
  97. };
  98. auto select_back_callback = [ctx]()
  99. {
  100. // Save config
  101. game::save_config(ctx);
  102. // Return to main menu
  103. application::state next_state;
  104. next_state.name = "main_menu";
  105. next_state.enter = std::bind(game::state::main_menu::enter, ctx);
  106. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  107. ctx->app->change_state(next_state);
  108. };
  109. // Build list of menu select callbacks
  110. ctx->menu_select_callbacks.push_back(select_controls_callback);
  111. ctx->menu_select_callbacks.push_back(select_graphics_callback);
  112. ctx->menu_select_callbacks.push_back(select_sound_callback);
  113. ctx->menu_select_callbacks.push_back(select_language_callback);
  114. ctx->menu_select_callbacks.push_back(select_back_callback);
  115. // Build list of menu right callbacks
  116. ctx->menu_right_callbacks.resize(5, nullptr);
  117. // Build list of menu left callbacks
  118. ctx->menu_left_callbacks.resize(5, nullptr);
  119. // Set menu back callback
  120. ctx->menu_back_callback = select_back_callback;
  121. // Schedule menu control setup
  122. timeline* timeline = ctx->timeline;
  123. float t = timeline->get_position();
  124. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  125. }
  126. void exit(game::context* ctx)
  127. {
  128. // Destruct menu
  129. game::menu::clear_controls(ctx);
  130. game::menu::clear_callbacks(ctx);
  131. game::menu::remove_text_from_ui(ctx);
  132. game::menu::delete_text(ctx);
  133. // Save config
  134. game::save_config(ctx);
  135. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  136. }
  137. } // namespace options_menu
  138. } // namespace state
  139. } // namespace game