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

286 lines
7.8 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/save.hpp"
  27. namespace game {
  28. namespace state {
  29. namespace language_menu {
  30. static void update_text_font(game::context* ctx)
  31. {
  32. for (scene::text* text: ctx->language_menu_texts)
  33. {
  34. text->set_material(&ctx->menu_font_material);
  35. text->set_font(&ctx->menu_font);
  36. }
  37. }
  38. static void update_text_color(game::context* ctx)
  39. {
  40. float4 inactive_color = {1.0f, 1.0f, 1.0f, 0.5f};
  41. float4 active_color = {1.0f, 1.0f, 1.0f, 1.0f};
  42. for (std::size_t i = 0; i < ctx->language_menu_texts.size(); ++i)
  43. {
  44. scene::text* text = ctx->language_menu_texts[i];
  45. if (i == ctx->language_menu_index)
  46. text->set_color(active_color);
  47. else
  48. text->set_color(inactive_color);
  49. }
  50. }
  51. static void update_text_content(game::context* ctx)
  52. {
  53. ctx->language_menu_language_text->set_content((*ctx->strings)["language_name"]);
  54. ctx->language_menu_back_text->set_content((*ctx->strings)["back"]);
  55. }
  56. static void refresh_texts(game::context* ctx)
  57. {
  58. for (scene::text* text: ctx->language_menu_texts)
  59. {
  60. text->refresh();
  61. }
  62. }
  63. static void align_texts(game::context* ctx)
  64. {
  65. float menu_width = 0.0f;
  66. for (std::size_t i = 0; i < ctx->language_menu_texts.size(); ++i)
  67. {
  68. scene::text* text = ctx->language_menu_texts[i];
  69. // Update menu width
  70. const auto& bounds = static_cast<const geom::aabb<float>&>(text->get_local_bounds());
  71. float width = bounds.max_point.x - bounds.min_point.x;
  72. menu_width = std::max<float>(menu_width, width);
  73. }
  74. float menu_height = ctx->language_menu_texts.size() * ctx->menu_font.get_font_metrics().linespace;
  75. float menu_x = -menu_width * 0.5f;
  76. float menu_y = menu_height * 0.5f - ctx->menu_font.get_font_metrics().linespace;
  77. for (std::size_t i = 0; i < ctx->language_menu_texts.size(); ++i)
  78. {
  79. scene::text* text = ctx->language_menu_texts[i];
  80. // Align text
  81. const auto& bounds = static_cast<const geom::aabb<float>&>(text->get_local_bounds());
  82. float w = bounds.max_point.x - bounds.min_point.x;
  83. float x = -w * 0.5f;
  84. //float x = menu_x;
  85. float y = menu_y - ctx->menu_font.get_font_metrics().linespace * i;
  86. text->set_translation({std::round(x), std::round(y), 0.0f});
  87. }
  88. }
  89. static void update_text_tweens(game::context* ctx)
  90. {
  91. for (scene::text* text: ctx->language_menu_texts)
  92. {
  93. text->update_tweens();
  94. }
  95. }
  96. void enter(game::context* ctx)
  97. {
  98. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  99. ctx->language_menu_index = 0;
  100. // Construct language menu texts
  101. ctx->language_menu_language_text = new scene::text();
  102. ctx->language_menu_back_text = new scene::text();
  103. // Build list of language menu texts
  104. ctx->language_menu_texts.push_back(ctx->language_menu_language_text);
  105. ctx->language_menu_texts.push_back(ctx->language_menu_back_text);
  106. // Construct language menu callbacks
  107. auto menu_back_callback = [ctx]()
  108. {
  109. application::state next_state;
  110. next_state.name = "options_menu";
  111. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  112. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  113. ctx->app->change_state(next_state);
  114. };
  115. auto next_language_callback = [ctx]()
  116. {
  117. if (ctx->language_menu_index != 0)
  118. return;
  119. // Increment language index
  120. ++ctx->language_index;
  121. if (ctx->language_index >= ctx->language_count)
  122. ctx->language_index = 0;
  123. // Find corresponding language code and strings
  124. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  125. ctx->strings = &ctx->string_table_map[ctx->language_code];
  126. // Update language in config
  127. (*ctx->config)["language"] = ctx->language_code;
  128. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  129. // Reload fonts
  130. ctx->logger->push_task("Reloading fonts");
  131. try
  132. {
  133. game::load_fonts(ctx);
  134. }
  135. catch (...)
  136. {
  137. ctx->logger->pop_task(EXIT_FAILURE);
  138. }
  139. ctx->logger->pop_task(EXIT_SUCCESS);
  140. update_text_font(ctx);
  141. update_text_content(ctx);
  142. refresh_texts(ctx);
  143. align_texts(ctx);
  144. update_text_tweens(ctx);
  145. };
  146. auto previous_language_callback = [ctx]()
  147. {
  148. if (ctx->language_menu_index != 0)
  149. return;
  150. // Increment language index
  151. --ctx->language_index;
  152. if (ctx->language_index < 0)
  153. ctx->language_index = ctx->language_count - 1;
  154. // Find corresponding language code and strings
  155. ctx->language_code = (*ctx->string_table)[0][ctx->language_index + 2];
  156. ctx->strings = &ctx->string_table_map[ctx->language_code];
  157. // Update language in config
  158. (*ctx->config)["language"] = ctx->language_code;
  159. ctx->logger->log("Language changed to \"" + ctx->language_code + "\"");
  160. // Reload fonts
  161. ctx->logger->push_task("Reloading fonts");
  162. try
  163. {
  164. game::load_fonts(ctx);
  165. }
  166. catch (...)
  167. {
  168. ctx->logger->pop_task(EXIT_FAILURE);
  169. }
  170. ctx->logger->pop_task(EXIT_SUCCESS);
  171. update_text_font(ctx);
  172. update_text_content(ctx);
  173. refresh_texts(ctx);
  174. align_texts(ctx);
  175. update_text_tweens(ctx);
  176. };
  177. // Build list of language menu callbacks
  178. ctx->language_menu_callbacks.push_back(next_language_callback);
  179. ctx->language_menu_callbacks.push_back(menu_back_callback);
  180. ctx->controls["menu_down"]->set_activated_callback
  181. (
  182. [ctx]()
  183. {
  184. ++ctx->language_menu_index;
  185. if (ctx->language_menu_index >= ctx->language_menu_texts.size())
  186. ctx->language_menu_index = 0;
  187. update_text_color(ctx);
  188. }
  189. );
  190. ctx->controls["menu_up"]->set_activated_callback
  191. (
  192. [ctx]()
  193. {
  194. --ctx->language_menu_index;
  195. if (ctx->language_menu_index < 0)
  196. ctx->language_menu_index = ctx->language_menu_texts.size() - 1;
  197. update_text_color(ctx);
  198. }
  199. );
  200. ctx->controls["menu_left"]->set_activated_callback(previous_language_callback);
  201. ctx->controls["menu_right"]->set_activated_callback(next_language_callback);
  202. ctx->controls["menu_select"]->set_activated_callback
  203. (
  204. [ctx]()
  205. {
  206. auto callback = ctx->language_menu_callbacks[ctx->language_menu_index];
  207. if (callback != nullptr)
  208. callback();
  209. }
  210. );
  211. ctx->controls["menu_back"]->set_activated_callback(menu_back_callback);
  212. for (scene::text* text: ctx->language_menu_texts)
  213. ctx->ui_scene->add_object(text);
  214. update_text_font(ctx);
  215. update_text_color(ctx);
  216. update_text_content(ctx);
  217. align_texts(ctx);
  218. update_text_tweens(ctx);
  219. }
  220. void exit(game::context* ctx)
  221. {
  222. // Clear control callbacks
  223. ctx->controls["menu_down"]->set_activated_callback(nullptr);
  224. ctx->controls["menu_up"]->set_activated_callback(nullptr);
  225. ctx->controls["menu_left"]->set_activated_callback(nullptr);
  226. ctx->controls["menu_right"]->set_activated_callback(nullptr);
  227. ctx->controls["menu_select"]->set_activated_callback(nullptr);
  228. ctx->controls["menu_back"]->set_activated_callback(nullptr);
  229. // Clear language menu callbacks
  230. ctx->language_menu_callbacks.clear();
  231. // Destruct language menu texts
  232. for (scene::text* text: ctx->language_menu_texts)
  233. {
  234. ctx->ui_scene->remove_object(text);
  235. delete text;
  236. }
  237. ctx->language_menu_texts.clear();
  238. // Save config
  239. game::save_config(ctx);
  240. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  241. }
  242. } // namespace language_menu
  243. } // namespace state
  244. } // namespace game