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

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