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

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