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

213 lines
6.7 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/states/options-menu.hpp"
  20. #include "game/states/main-menu.hpp"
  21. #include "game/states/language-menu.hpp"
  22. #include "animation/ease.hpp"
  23. #include "animation/animation.hpp"
  24. #include "animation/animator.hpp"
  25. #include "application.hpp"
  26. #include "scene/text.hpp"
  27. #include "render/passes/clear-pass.hpp"
  28. namespace game {
  29. namespace state {
  30. namespace options_menu {
  31. void enter(game::context* ctx)
  32. {
  33. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  34. ctx->options_menu_index = 0;
  35. // Construct options menu texts
  36. ctx->options_menu_controls_text = new scene::text();
  37. ctx->options_menu_graphics_text = new scene::text();
  38. ctx->options_menu_sound_text = new scene::text();
  39. ctx->options_menu_language_text = new scene::text();
  40. ctx->options_menu_back_text = new scene::text();
  41. // Build list of options menu texts
  42. ctx->options_menu_texts.push_back(ctx->options_menu_controls_text);
  43. ctx->options_menu_texts.push_back(ctx->options_menu_graphics_text);
  44. ctx->options_menu_texts.push_back(ctx->options_menu_sound_text);
  45. ctx->options_menu_texts.push_back(ctx->options_menu_language_text);
  46. ctx->options_menu_texts.push_back(ctx->options_menu_back_text);
  47. // Construct options menu callbacks
  48. auto menu_back_callback = [ctx]()
  49. {
  50. application::state next_state;
  51. next_state.name = "main_menu";
  52. next_state.enter = std::bind(game::state::main_menu::enter, ctx, 1);
  53. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  54. ctx->app->change_state(next_state);
  55. };
  56. auto change_state_language_menu = [ctx]()
  57. {
  58. application::state next_state;
  59. next_state.name = "language_menu";
  60. next_state.enter = std::bind(game::state::language_menu::enter, ctx);
  61. next_state.exit = std::bind(game::state::language_menu::exit, ctx);
  62. ctx->app->change_state(next_state);
  63. };
  64. // Build list of options menu callbacks
  65. ctx->options_menu_callbacks.push_back(nullptr);
  66. ctx->options_menu_callbacks.push_back(nullptr);
  67. ctx->options_menu_callbacks.push_back(nullptr);
  68. ctx->options_menu_callbacks.push_back(change_state_language_menu);
  69. ctx->options_menu_callbacks.push_back(menu_back_callback);
  70. // Set content of texts
  71. ctx->options_menu_controls_text->set_content((*ctx->strings)["options_menu_controls"]);
  72. ctx->options_menu_graphics_text->set_content((*ctx->strings)["options_menu_graphics"]);
  73. ctx->options_menu_sound_text->set_content((*ctx->strings)["options_menu_sound"]);
  74. ctx->options_menu_language_text->set_content((*ctx->strings)["options_menu_language"]);
  75. ctx->options_menu_back_text->set_content((*ctx->strings)["back"]);
  76. float4 inactive_color = {1.0f, 1.0f, 1.0f, 0.5f};
  77. float4 active_color = {1.0f, 1.0f, 1.0f, 1.0f};
  78. float menu_width = 0.0f;
  79. for (std::size_t i = 0; i < ctx->options_menu_texts.size(); ++i)
  80. {
  81. scene::text* text = ctx->options_menu_texts[i];
  82. // Set text material and font
  83. text->set_material(&ctx->menu_font_material);
  84. text->set_font(&ctx->menu_font);
  85. // Set text color
  86. if (i == ctx->options_menu_index)
  87. text->set_color(active_color);
  88. else
  89. text->set_color(inactive_color);
  90. // Update menu width
  91. const auto& bounds = static_cast<const geom::aabb<float>&>(text->get_local_bounds());
  92. float width = bounds.max_point.x - bounds.min_point.x;
  93. menu_width = std::max<float>(menu_width, width);
  94. // Add text to UI
  95. ctx->ui_scene->add_object(text);
  96. }
  97. // Align texts
  98. float menu_height = ctx->options_menu_texts.size() * ctx->menu_font.get_font_metrics().linespace;
  99. float menu_x = -menu_width * 0.5f;
  100. float menu_y = menu_height * 0.5f - ctx->menu_font.get_font_metrics().linespace;
  101. for (std::size_t i = 0; i < ctx->options_menu_texts.size(); ++i)
  102. {
  103. scene::text* text = ctx->options_menu_texts[i];
  104. float x = menu_x;
  105. float y = menu_y - ctx->menu_font.get_font_metrics().linespace * i;
  106. text->set_translation({std::round(x), std::round(y), 0.0f});
  107. text->update_tweens();
  108. }
  109. ctx->controls["menu_down"]->set_activated_callback
  110. (
  111. [ctx]()
  112. {
  113. ++ctx->options_menu_index;
  114. if (ctx->options_menu_index >= ctx->options_menu_texts.size())
  115. ctx->options_menu_index = 0;
  116. float4 active_color{1.0f, 1.0f, 1.0f, 1.0f};
  117. float4 inactive_color{1.0f, 1.0f, 1.0f, 0.5f};
  118. for (std::size_t i = 0; i < ctx->options_menu_texts.size(); ++i)
  119. {
  120. scene::text* text = ctx->options_menu_texts[i];
  121. if (i == ctx->options_menu_index)
  122. text->set_color(active_color);
  123. else
  124. text->set_color(inactive_color);
  125. }
  126. }
  127. );
  128. ctx->controls["menu_up"]->set_activated_callback
  129. (
  130. [ctx]()
  131. {
  132. --ctx->options_menu_index;
  133. if (ctx->options_menu_index < 0)
  134. ctx->options_menu_index = ctx->options_menu_texts.size() - 1;
  135. float4 active_color{1.0f, 1.0f, 1.0f, 1.0f};
  136. float4 inactive_color{1.0f, 1.0f, 1.0f, 0.5f};
  137. for (std::size_t i = 0; i < ctx->options_menu_texts.size(); ++i)
  138. {
  139. scene::text* text = ctx->options_menu_texts[i];
  140. if (i == ctx->options_menu_index)
  141. text->set_color(active_color);
  142. else
  143. text->set_color(inactive_color);
  144. }
  145. }
  146. );
  147. ctx->controls["menu_select"]->set_activated_callback
  148. (
  149. [ctx]()
  150. {
  151. auto callback = ctx->options_menu_callbacks[ctx->options_menu_index];
  152. if (callback != nullptr)
  153. callback();
  154. }
  155. );
  156. ctx->controls["menu_back"]->set_activated_callback(menu_back_callback);
  157. /*
  158. ctx->controls["menu_back"]->set_activated_callback
  159. (
  160. std::bind(&application::close, ctx->app, 0)
  161. );
  162. */
  163. }
  164. void exit(game::context* ctx)
  165. {
  166. // Clear control callbacks
  167. ctx->controls["menu_down"]->set_activated_callback(nullptr);
  168. ctx->controls["menu_up"]->set_activated_callback(nullptr);
  169. ctx->controls["menu_select"]->set_activated_callback(nullptr);
  170. ctx->controls["menu_back"]->set_activated_callback(nullptr);
  171. // Clear options menu callbacks
  172. ctx->options_menu_callbacks.clear();
  173. // Destruct options menu texts
  174. for (scene::text* text: ctx->options_menu_texts)
  175. {
  176. ctx->ui_scene->remove_object(text);
  177. delete text;
  178. }
  179. ctx->options_menu_texts.clear();
  180. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  181. }
  182. } // namespace options_menu
  183. } // namespace state
  184. } // namespace game