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

196 lines
5.6 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/language-menu.hpp"
  20. #include "game/states/options-menu.hpp"
  21. #include "application.hpp"
  22. #include "scene/text.hpp"
  23. #include "render/passes/clear-pass.hpp"
  24. #include "debug/logger.hpp"
  25. #include "game/fonts.hpp"
  26. #include "game/menu.hpp"
  27. #include "animation/timeline.hpp"
  28. namespace game {
  29. namespace state {
  30. namespace language_menu {
  31. static void update_text_content(game::context* ctx)
  32. {
  33. auto [language_name, language_value] = ctx->menu_item_texts[0];
  34. auto [back_name, back_value] = ctx->menu_item_texts[1];
  35. language_name->set_content((*ctx->strings)["language_menu_language"]);
  36. language_value->set_content((*ctx->strings)["language_name"]);
  37. back_name->set_content((*ctx->strings)["back"]);
  38. }
  39. void enter(game::context* ctx)
  40. {
  41. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  42. // Construct menu item texts
  43. scene::text* language_name_text = new scene::text();
  44. scene::text* language_value_text = new scene::text();
  45. scene::text* back_text = new scene::text();
  46. // Build list of menu item texts
  47. ctx->menu_item_texts.push_back({language_name_text, language_value_text});
  48. ctx->menu_item_texts.push_back({back_text, nullptr});
  49. // Set content of menu item texts
  50. update_text_content(ctx);
  51. // Init menu item index
  52. game::menu::init_menu_item_index(ctx, "language");
  53. game::menu::update_text_color(ctx);
  54. game::menu::update_text_font(ctx);
  55. game::menu::align_text(ctx);
  56. game::menu::update_text_tweens(ctx);
  57. game::menu::add_text_to_ui(ctx);
  58. game::menu::setup_animations(ctx);
  59. // Construct menu item callbacks
  60. auto next_language_callback = [ctx]()
  61. {
  62. // Increment language index
  63. ++ctx->language_index;
  64. if (ctx->language_index >= ctx->language_count)
  65. ctx->language_index = 0;
  66. // Find corresponding language code and strings
  67. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  68. ctx->strings = &ctx->string_table_map[ctx->language_code];
  69. // Update language in config
  70. (*ctx->config)["language"] = ctx->language_code;
  71. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  72. // Reload fonts
  73. ctx->logger->push_task("Reloading fonts");
  74. try
  75. {
  76. game::load_fonts(ctx);
  77. }
  78. catch (...)
  79. {
  80. ctx->logger->pop_task(EXIT_FAILURE);
  81. }
  82. ctx->logger->pop_task(EXIT_SUCCESS);
  83. game::menu::update_text_font(ctx);
  84. update_text_content(ctx);
  85. game::menu::refresh_text(ctx);
  86. game::menu::align_text(ctx);
  87. game::menu::update_text_tweens(ctx);
  88. };
  89. auto previous_language_callback = [ctx]()
  90. {
  91. // Increment language index
  92. --ctx->language_index;
  93. if (ctx->language_index < 0)
  94. ctx->language_index = ctx->language_count - 1;
  95. // Find corresponding language code and strings
  96. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  97. ctx->strings = &ctx->string_table_map[ctx->language_code];
  98. // Update language in config
  99. (*ctx->config)["language"] = ctx->language_code;
  100. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  101. // Reload fonts
  102. ctx->logger->push_task("Reloading fonts");
  103. try
  104. {
  105. game::load_fonts(ctx);
  106. }
  107. catch (...)
  108. {
  109. ctx->logger->pop_task(EXIT_FAILURE);
  110. }
  111. ctx->logger->pop_task(EXIT_SUCCESS);
  112. game::menu::update_text_font(ctx);
  113. update_text_content(ctx);
  114. game::menu::refresh_text(ctx);
  115. game::menu::align_text(ctx);
  116. game::menu::update_text_tweens(ctx);
  117. };
  118. auto select_back_callback = [ctx]()
  119. {
  120. // Disable controls
  121. game::menu::clear_controls(ctx);
  122. game::menu::fade_out
  123. (
  124. ctx,
  125. [ctx]()
  126. {
  127. application::state next_state;
  128. next_state.name = "options_menu";
  129. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  130. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  131. ctx->app->queue_state(next_state);
  132. }
  133. );
  134. };
  135. // Build list of menu select callbacks
  136. ctx->menu_select_callbacks.push_back(next_language_callback);
  137. ctx->menu_select_callbacks.push_back(select_back_callback);
  138. // Build list of menu left callbacks
  139. ctx->menu_left_callbacks.push_back(previous_language_callback);
  140. ctx->menu_left_callbacks.push_back(nullptr);
  141. // Build list of menu right callbacks
  142. ctx->menu_right_callbacks.push_back(next_language_callback);
  143. ctx->menu_right_callbacks.push_back(nullptr);
  144. // Set menu back callback
  145. ctx->menu_back_callback = select_back_callback;
  146. // Schedule menu control setup
  147. timeline* timeline = ctx->timeline;
  148. float t = timeline->get_position();
  149. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  150. // Fade in menu
  151. game::menu::fade_in(ctx, nullptr);
  152. }
  153. void exit(game::context* ctx)
  154. {
  155. // Destruct menu
  156. game::menu::clear_controls(ctx);
  157. game::menu::clear_callbacks(ctx);
  158. game::menu::delete_animations(ctx);
  159. game::menu::remove_text_from_ui(ctx);
  160. game::menu::delete_text(ctx);
  161. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  162. }
  163. } // namespace language_menu
  164. } // namespace state
  165. } // namespace game