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

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