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

181 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/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. // Construct menu item callbacks
  59. auto next_language_callback = [ctx]()
  60. {
  61. // Increment language index
  62. ++ctx->language_index;
  63. if (ctx->language_index >= ctx->language_count)
  64. ctx->language_index = 0;
  65. // Find corresponding language code and strings
  66. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  67. ctx->strings = &ctx->string_table_map[ctx->language_code];
  68. // Update language in config
  69. (*ctx->config)["language"] = ctx->language_code;
  70. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  71. // Reload fonts
  72. ctx->logger->push_task("Reloading fonts");
  73. try
  74. {
  75. game::load_fonts(ctx);
  76. }
  77. catch (...)
  78. {
  79. ctx->logger->pop_task(EXIT_FAILURE);
  80. }
  81. ctx->logger->pop_task(EXIT_SUCCESS);
  82. game::menu::update_text_font(ctx);
  83. update_text_content(ctx);
  84. game::menu::refresh_text(ctx);
  85. game::menu::align_text(ctx);
  86. game::menu::update_text_tweens(ctx);
  87. };
  88. auto previous_language_callback = [ctx]()
  89. {
  90. // Increment language index
  91. --ctx->language_index;
  92. if (ctx->language_index < 0)
  93. ctx->language_index = ctx->language_count - 1;
  94. // Find corresponding language code and strings
  95. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  96. ctx->strings = &ctx->string_table_map[ctx->language_code];
  97. // Update language in config
  98. (*ctx->config)["language"] = ctx->language_code;
  99. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  100. // Reload fonts
  101. ctx->logger->push_task("Reloading fonts");
  102. try
  103. {
  104. game::load_fonts(ctx);
  105. }
  106. catch (...)
  107. {
  108. ctx->logger->pop_task(EXIT_FAILURE);
  109. }
  110. ctx->logger->pop_task(EXIT_SUCCESS);
  111. game::menu::update_text_font(ctx);
  112. update_text_content(ctx);
  113. game::menu::refresh_text(ctx);
  114. game::menu::align_text(ctx);
  115. game::menu::update_text_tweens(ctx);
  116. };
  117. auto select_back_callback = [ctx]()
  118. {
  119. application::state next_state;
  120. next_state.name = "options_menu";
  121. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  122. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  123. ctx->app->change_state(next_state);
  124. };
  125. // Build list of menu select callbacks
  126. ctx->menu_select_callbacks.push_back(next_language_callback);
  127. ctx->menu_select_callbacks.push_back(select_back_callback);
  128. // Build list of menu left callbacks
  129. ctx->menu_left_callbacks.push_back(previous_language_callback);
  130. ctx->menu_left_callbacks.push_back(nullptr);
  131. // Build list of menu right callbacks
  132. ctx->menu_right_callbacks.push_back(next_language_callback);
  133. ctx->menu_right_callbacks.push_back(nullptr);
  134. // Set menu back callback
  135. ctx->menu_back_callback = select_back_callback;
  136. // Schedule menu control setup
  137. timeline* timeline = ctx->timeline;
  138. float t = timeline->get_position();
  139. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  140. }
  141. void exit(game::context* ctx)
  142. {
  143. // Destruct menu
  144. game::menu::clear_controls(ctx);
  145. game::menu::clear_callbacks(ctx);
  146. game::menu::remove_text_from_ui(ctx);
  147. game::menu::delete_text(ctx);
  148. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  149. }
  150. } // namespace language_menu
  151. } // namespace state
  152. } // namespace game